mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2025-06-20 00:25:17 +00:00
Can create conversations
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
import { Conversation } from "../entities/Conversation";
|
||||
import { Conversation, BaseConversation } from "../entities/Conversation";
|
||||
import { DatabaseHelper } from "./DatabaseHelper";
|
||||
import { time } from "../utils/DateUtils";
|
||||
|
||||
/**
|
||||
* Conversations helper
|
||||
@ -13,6 +14,53 @@ const MESSAGES_TABLE = "comunic_conversations_messages";
|
||||
|
||||
export class ConversationsHelper {
|
||||
|
||||
/**
|
||||
* Create a new conversation
|
||||
*
|
||||
* @param conv Information about the conversation to create
|
||||
*/
|
||||
public static async Create(conv : BaseConversation) : Promise<number> {
|
||||
|
||||
// Create the conversation in the main table
|
||||
const convID = await DatabaseHelper.InsertRow(LIST_TABLE, {
|
||||
"user_id": conv.ownerID,
|
||||
"name": conv.name,
|
||||
"last_active": time(),
|
||||
"creation_time": time()
|
||||
});
|
||||
|
||||
// Add the members to the conversation
|
||||
for (const userID of conv.members) {
|
||||
await this.AddMember(
|
||||
convID,
|
||||
userID,
|
||||
conv.ownerID == userID ? conv.following : true);
|
||||
}
|
||||
|
||||
return convID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a member to a conversation
|
||||
*
|
||||
* @param convID Conversation ID
|
||||
* @param userID User ID
|
||||
* @param following Specify whether the user is following
|
||||
* the conversation or not
|
||||
*/
|
||||
private static async AddMember(convID : number, userID: number, following : boolean = true) {
|
||||
await DatabaseHelper.InsertRow(
|
||||
USERS_TABLE,
|
||||
{
|
||||
"conv_id": convID,
|
||||
"user_id": userID,
|
||||
"time_add": time(),
|
||||
"following": following ? 1 : 0,
|
||||
"saw_last_message": 1
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of conversations of the user
|
||||
*
|
||||
|
@ -32,6 +32,20 @@ export class UserHelper {
|
||||
return this.DbToUser(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check out whether a user exists or not
|
||||
*
|
||||
* @param id The ID of the user to check
|
||||
*/
|
||||
public static async Exists(id: number) : Promise<boolean> {
|
||||
return await DatabaseHelper.Count({
|
||||
table: TABLE_NAME,
|
||||
where: {
|
||||
ID: id
|
||||
}
|
||||
}) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for user in the database
|
||||
*
|
||||
|
Reference in New Issue
Block a user