From 601c41697561727aa2d5ea212cd9b0bada1966d8 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 23 Nov 2019 20:53:13 +0100 Subject: [PATCH] Can get information about a single conversation --- src/controllers/ConversationsController.ts | 31 +++++++++++++++++++++ src/controllers/Routes.ts | 3 ++ src/helpers/ConversationsHelper.ts | 32 +++++++++++++++++++++- 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/controllers/ConversationsController.ts b/src/controllers/ConversationsController.ts index c8504c1..0081c5e 100644 --- a/src/controllers/ConversationsController.ts +++ b/src/controllers/ConversationsController.ts @@ -20,6 +20,37 @@ export class ConversationsController { handler.send(list.map(c => this.ConversationToAPI(c))); } + /** + * Get information about a single conversation + * + * @param handler + */ + public static async GetInfoSingle(handler: RequestHandler) { + const conversationID = await this.GetPostConversationId("conversationID", handler); + const conv = await ConversationsHelper.GetSingle(conversationID); + + if(!conv) + throw Error("Could not get information about the conversation (but it should have been found though) !!"); + + handler.send(this.ConversationToAPI(conv)); + } + + /** + * Get and return safely a conversation ID specified in a $_POST Request + * + * @param name The name of the POST field containing the ID of the conversation + * @param handler + */ + private static async GetPostConversationId(name : string, handler: RequestHandler) : Promise { + const convID = handler.postInt(name); + + // Check out whether the user belongs to the conversation or not + if(!await ConversationsHelper.DoesUsersBelongsTo(handler.getUserId(), convID)) + handler.error(401, "You are not allowed to perform queries on this conversation!"); + + return convID; + } + /** * Turn a conversation object into an API entry * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index fd0cc58..2ea7059 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -48,4 +48,7 @@ export const Routes : Route[] = [ // Conversations controller {path: "/conversations/getList", cb: (h) => ConversationsController.GetList(h)}, + + {path: "/conversations/getInfoOne", cb: (h) => ConversationsController.GetInfoSingle(h)}, + {path: "/conversations/getInfosOne", cb: (h) => ConversationsController.GetInfoSingle(h)}, // Legacy ] \ No newline at end of file diff --git a/src/helpers/ConversationsHelper.ts b/src/helpers/ConversationsHelper.ts index c3894cb..abd0a71 100644 --- a/src/helpers/ConversationsHelper.ts +++ b/src/helpers/ConversationsHelper.ts @@ -50,6 +50,36 @@ export class ConversationsHelper { return list; } + /** + * Get information about a single conversation + * + * @param convID The ID of the conversation to get + */ + public static async GetSingle(convID : number) : Promise { + const result = await DatabaseHelper.QueryRow({ + fields: [ + "*", + "l.id as id", + "l.user_id as owner_id", + ], + table: LIST_TABLE + " l", + joins: [ + // Joins with conversation members table + { + table: USERS_TABLE + " u", + condition: "l.id = u.conv_id" + } + ], + where: { + "l.id": convID, + } + }); + + if(!result) + return null; + + return await this.DBToConversationInfo(result); + } /** * Check out whether a user is the member of a conversation or not @@ -97,7 +127,7 @@ export class ConversationsHelper { lastActive: row.last_active, timeCreate: row.time_add, following: row.following, - sawLastMessage: row.saw_last_message, + sawLastMessage: row.saw_last_message == 1, members: await this.GetConversationMembers(row.id) } }