mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2025-02-20 16:02:40 +00:00
88 lines
1.9 KiB
TypeScript
88 lines
1.9 KiB
TypeScript
|
import { Conversation } from "../entities/Conversation";
|
||
|
import { DatabaseHelper } from "./DatabaseHelper";
|
||
|
|
||
|
/**
|
||
|
* Conversations helper
|
||
|
*
|
||
|
* @author Pierre HUBERT
|
||
|
*/
|
||
|
|
||
|
const LIST_TABLE = "comunic_conversations_list";
|
||
|
const USERS_TABLE = "comunic_conversations_users";
|
||
|
const MESSAGES_TABLE = "comunic_conversations_messages";
|
||
|
|
||
|
export class ConversationsHelper {
|
||
|
|
||
|
/**
|
||
|
* Get the list of conversations of the user
|
||
|
*
|
||
|
* @param userID Target user ID
|
||
|
*/
|
||
|
public static async GetListUser(userID: number) : Promise<Array<Conversation>> {
|
||
|
|
||
|
// Fetch the list of conversations
|
||
|
const result = await DatabaseHelper.Query({
|
||
|
fields: [
|
||
|
"*",
|
||
|
"l.id as id",
|
||
|
"l.user_id as owner_id" // The field conflits with user.user_id
|
||
|
],
|
||
|
table: LIST_TABLE + " l",
|
||
|
joins: [
|
||
|
|
||
|
// Joins with conversation members table
|
||
|
{
|
||
|
table: USERS_TABLE + " u",
|
||
|
condition: "l.id = u.conv_id"
|
||
|
}
|
||
|
|
||
|
],
|
||
|
where: {
|
||
|
"u.user_id": userID
|
||
|
},
|
||
|
order: "l.last_active DESC"
|
||
|
});
|
||
|
|
||
|
const list = [];
|
||
|
for (const el of result) {
|
||
|
list.push(await this.DBToConversationInfo(el));
|
||
|
}
|
||
|
return list;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Get the list of members of a conversation
|
||
|
*
|
||
|
* @param convID The ID of the target conversation
|
||
|
*/
|
||
|
private static async GetConversationMembers(convID : number): Promise<Array<number>> {
|
||
|
const result = await DatabaseHelper.Query({
|
||
|
table: USERS_TABLE,
|
||
|
where: {
|
||
|
"conv_id": convID
|
||
|
},
|
||
|
fields: ["user_id"]
|
||
|
});
|
||
|
|
||
|
return result.map((e) => e.user_id);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Turn a database entry into a conversation object
|
||
|
*
|
||
|
* @param row
|
||
|
*/
|
||
|
private static async DBToConversationInfo(row: any) : Promise<Conversation> {
|
||
|
return {
|
||
|
id: row.id,
|
||
|
ownerID: row.owner_id,
|
||
|
name: row.name,
|
||
|
lastActive: row.last_active,
|
||
|
timeCreate: row.time_add,
|
||
|
following: row.following,
|
||
|
sawLastMessage: row.saw_last_message,
|
||
|
members: await this.GetConversationMembers(row.id)
|
||
|
}
|
||
|
}
|
||
|
}
|