From d6eed99a1a103fdbfbbe36d3112b5ff317040e91 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 23 Nov 2019 18:41:13 +0100 Subject: [PATCH] Can get the list of conversations --- src/controllers/ConversationsController.ts | 39 ++++++++++ src/controllers/Routes.ts | 5 ++ src/entities/Conversation.ts | 16 ++++ src/helpers/ConversationsHelper.ts | 88 ++++++++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 src/controllers/ConversationsController.ts create mode 100644 src/entities/Conversation.ts create mode 100644 src/helpers/ConversationsHelper.ts diff --git a/src/controllers/ConversationsController.ts b/src/controllers/ConversationsController.ts new file mode 100644 index 0000000..c8504c1 --- /dev/null +++ b/src/controllers/ConversationsController.ts @@ -0,0 +1,39 @@ +import { RequestHandler } from "../entities/RequestHandler"; +import { ConversationsHelper } from "../helpers/ConversationsHelper"; +import { Conversation } from "../entities/Conversation"; + +/** + * Conversations controller + * + * @author Pierre HUBERT + */ + +export class ConversationsController { + + /** + * Get the list of conversations of the user + * + * @param handler + */ + public static async GetList(handler: RequestHandler) { + const list = await ConversationsHelper.GetListUser(handler.getUserId()); + handler.send(list.map(c => this.ConversationToAPI(c))); + } + + /** + * Turn a conversation object into an API entry + * + * @param c + */ + private static ConversationToAPI(c : Conversation) : any { + return { + ID: c.id, + ID_owner: c.ownerID, + last_active: c.lastActive, + name: c.name.length > 0 ? c.name : false, + following: c.following ? 1 : 0, + saw_last_message: c.sawLastMessage ? 1 : 0, + members: c.members + }; + } +} \ No newline at end of file diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index dff5fbd..fd0cc58 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -2,6 +2,7 @@ import { WelcomeController } from "./WelcomeController"; import { RequestHandler } from "../entities/RequestHandler"; import { AccountController } from "./AccountController"; import { UserController } from "./UserController"; +import { ConversationsController } from "./ConversationsController"; /** * Controllers routes @@ -43,4 +44,8 @@ export const Routes : Route[] = [ {path: "/user/getInfoMultiple", cb: (h) => UserController.GetMultiple(h), needLogin: false}, {path: "/user/getInfosMultiple", cb: (h) => UserController.GetMultiple(h), needLogin: false}, // Legacy + + + // Conversations controller + {path: "/conversations/getList", cb: (h) => ConversationsController.GetList(h)}, ] \ No newline at end of file diff --git a/src/entities/Conversation.ts b/src/entities/Conversation.ts new file mode 100644 index 0000000..3dd3b15 --- /dev/null +++ b/src/entities/Conversation.ts @@ -0,0 +1,16 @@ +/** + * Single conversation information + * + * @author Pierre HUBERT + */ + +export interface Conversation { + id: number, + ownerID: number, + name: string, + lastActive: number, + timeCreate: number, + following: boolean, + sawLastMessage: boolean, + members: Array +} \ No newline at end of file diff --git a/src/helpers/ConversationsHelper.ts b/src/helpers/ConversationsHelper.ts new file mode 100644 index 0000000..65791b3 --- /dev/null +++ b/src/helpers/ConversationsHelper.ts @@ -0,0 +1,88 @@ +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> { + + // 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> { + 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 { + 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) + } + } +} \ No newline at end of file