From 46f6e15b02da7a9c1748cab64782be0ffdbc6395 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 30 Nov 2019 15:17:47 +0100 Subject: [PATCH] Can open new conversations --- src/controllers/ConversationsController.ts | 43 ++++++++++++++++++++++ src/controllers/Routes.ts | 3 ++ src/entities/ConversationMessage.ts | 42 +++++++++++++++++++++ src/helpers/ConversationsHelper.ts | 36 ++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 src/entities/ConversationMessage.ts diff --git a/src/controllers/ConversationsController.ts b/src/controllers/ConversationsController.ts index dcc6d7a..8412bb6 100644 --- a/src/controllers/ConversationsController.ts +++ b/src/controllers/ConversationsController.ts @@ -3,6 +3,7 @@ import { ConversationsHelper } from "../helpers/ConversationsHelper"; import { Conversation, BaseConversation } from "../entities/Conversation"; import { UserHelper } from "../helpers/UserHelper"; import { removeHTMLNodes } from "../utils/StringUtils"; +import { ConversationMessage } from "../entities/ConversationMessage"; /** * Conversations controller @@ -120,6 +121,33 @@ export class ConversationsController { h.success("Conversation information successfully updated!"); } + /** + * Refresh current user conversations + * + * @param h Request handler + */ + public static async RefreshList(h: RequestHandler) { + + const list = {}; + + // Check for new conversations + if(h.hasPostParameter("newConversations")) { + for(const convID of h.postNumbersSet("newConversations")) { + if(!ConversationsHelper.DoesUsersBelongsTo(h.getUserId(), convID)) + h.error(401, "You are not allowed to fetch the messages of this conversation ("+convID+")!"); + + list["conversation-" + convID] = (await ConversationsHelper.GetLastMessages(convID, 10)) + .map(e => this.ConversationMessageToAPI(e)); + + // TODO : mark the user has seen the messages + } + } + + // TODO : Check for refresh on some conversations + + h.send(list); + } + /** * Get and return safely a conversation ID specified in a $_POST Request * @@ -152,4 +180,19 @@ export class ConversationsController { members: [...c.members] }; } + + /** + * Turn a conversation message into an API object + * + * @param c Information about the conversation + */ + private static ConversationMessageToAPI(c: ConversationMessage) : any { + return { + ID: c.id, + ID_user: c.userID, + time_insert: c.timeSent, + message: c.message, + image_path: c.hasImage ? c.imageURL : null + }; + } } \ No newline at end of file diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 0a58014..a5af6f8 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -57,6 +57,9 @@ export const Routes : Route[] = [ {path: "/conversations/updateSettings", cb: (h) => ConversationsController.UpdateSettings(h)}, + {path: "/conversations/refresh", cb: (h) => ConversationsController.RefreshList(h)}, + + // Search controller {path: "/search/user", cb: (h) => SearchController.SearchUser(h)}, {path: "/user/search", cb: (h) => SearchController.SearchUser(h)}, // Legacy diff --git a/src/entities/ConversationMessage.ts b/src/entities/ConversationMessage.ts new file mode 100644 index 0000000..f357df0 --- /dev/null +++ b/src/entities/ConversationMessage.ts @@ -0,0 +1,42 @@ +import { pathUserData } from "../utils/UserDataUtils"; + +/** + * Single conversation message + * + * @author Pierre HUBERT + */ + +export interface ConversationMessageInfo { + id: number, + convID: number, + userID: number, + timeSent: number, + imagePath: string, + message: string +} + +export class ConversationMessage implements ConversationMessageInfo { + public id: number; + public convID: number; + public userID: number; + public timeSent: number; + public imagePath: string; + public message: string; + + constructor(info: ConversationMessageInfo) { + this.id = info.id; + this.convID = info.convID; + this.userID = info.userID; + this.timeSent = info.timeSent; + this.imagePath = info.imagePath; + this.message = info.message; + } + + get hasImage() : boolean { + return this.imagePath.length > 1; + } + + get imageURL() : string { + return pathUserData(this.imagePath); + } +} \ No newline at end of file diff --git a/src/helpers/ConversationsHelper.ts b/src/helpers/ConversationsHelper.ts index 1f1dcd1..266b056 100644 --- a/src/helpers/ConversationsHelper.ts +++ b/src/helpers/ConversationsHelper.ts @@ -1,6 +1,7 @@ import { Conversation, BaseConversation } from "../entities/Conversation"; import { DatabaseHelper } from "./DatabaseHelper"; import { time } from "../utils/DateUtils"; +import { ConversationMessage } from "../entities/ConversationMessage"; /** * Conversations helper @@ -240,6 +241,23 @@ export class ConversationsHelper { }) == 1; } + /** + * Get the last messages of a conversation + * + * @param convID Target conversation ID + * @param numberOfMessages The maximum number of messages to return + */ + public static async GetLastMessages(convID: number, numberOfMessages: number) : Promise> { + return (await DatabaseHelper.Query({ + table: MESSAGES_TABLE, + where: { + conv_id: convID + }, + limit: numberOfMessages, + order: "id DESC" + })).map(m => this.DBToConversationMessage(convID, m)); + } + /** * Get the list of members of a conversation * @@ -274,4 +292,22 @@ export class ConversationsHelper { members: await this.GetConversationMembers(row.id) } } + + /** + * Turn a database entry into a conversation message + * + * @param convID The ID of the conversation the message belongs to + * @param row Row to convert + * @return Generated conversation message + */ + private static DBToConversationMessage(convID: number, row: any) : ConversationMessage { + return new ConversationMessage({ + id: row.id, + convID: convID, + userID: row.user_id, + timeSent: row.time_insert, + imagePath: row.image_path ? row.image_path : "", + message: row.message ? row.message : "" + }); + } } \ No newline at end of file