diff --git a/src/controllers/ConversationsController.ts b/src/controllers/ConversationsController.ts index 41379e9..cf1723f 100644 --- a/src/controllers/ConversationsController.ts +++ b/src/controllers/ConversationsController.ts @@ -132,7 +132,7 @@ export class ConversationsController { // Check for new conversations if(h.hasPostParameter("newConversations")) { - for(const convID of h.postNumbersSet("newConversations")) { + for(const convID of h.postNumbersSet("newConversations", 0)) { if(!ConversationsHelper.DoesUsersBelongsTo(h.getUserId(), convID)) h.error(401, "You are not allowed to fetch the messages of this conversation ("+convID+")!"); @@ -144,7 +144,37 @@ export class ConversationsController { } } - // TODO : Check for refresh on some conversations + // Check for refresh on some conversations + if(h.hasPostParameter("toRefresh")) { + const toRefresh = h.postJSON("toRefresh"); + + for (const key in toRefresh) { + if (toRefresh.hasOwnProperty(key)) { + const element = toRefresh[key]; + + // Extract conversation ID + if(!key.startsWith("conversation-")) + h.error(400, "Entries of 'toRefresh' should start with 'conversation-' !"); + const convID = Number.parseInt(key.replace("conversation-", "")); + + // Extract last message ID + if(!element.hasOwnProperty("last_message_id")) + h.error(400, "Missing last_message_id for conversation " + convID + "!"); + const lastMessageID = Number.parseInt(element.last_message_id); + + // Check user rights + if(!ConversationsHelper.DoesUsersBelongsTo(h.getUserId(), convID)) + h.error(401, "You are not allowed to fetch the messages of this conversation ("+convID+")!"); + + // Get the messages + list["conversation-" + convID] = (await ConversationsHelper.GetNewMessages(convID, lastMessageID)) + .map(e => this.ConversationMessageToAPI(e)); + + // Mark the user has seen the messages + await ConversationsHelper.MarkUserSeen(convID, h.getUserId()); + } + } + } h.send(list); } diff --git a/src/entities/RequestHandler.ts b/src/entities/RequestHandler.ts index 66769d4..1752a44 100644 --- a/src/entities/RequestHandler.ts +++ b/src/entities/RequestHandler.ts @@ -103,9 +103,10 @@ export class RequestHandler { * Get a list of integeres included in the request * * @param name The name of the post field + * @param minEntries Specify the minimum number of entries required */ - public postNumbersList(name: string) : Array { - const param = this.postString(name); + public postNumbersList(name: string, minEntries : number = 1) : Array { + const param = this.postString(name, minEntries < 1 ? 0 : minEntries, minEntries > 0); let list = []; for (const el of param.split(",")) { @@ -119,6 +120,9 @@ export class RequestHandler { } + if(list.length < minEntries) + this.error(400, "Not enough entries in '" + name + "'!") + return list; } @@ -126,9 +130,29 @@ export class RequestHandler { * Turn a list of string into a Set object * * @param name Name of POST field + * @param minEntries Minimum number of entries to specify */ - public postNumbersSet(name : string) : Set { - return new Set(this.postNumbersList(name)); + public postNumbersSet(name : string, minEntries : number = 1) : Set { + return new Set(this.postNumbersList(name, minEntries)); + } + + /** + * Attempt to decode JSON included in a POST request + * + * @param name Name of POST field + */ + public postJSON(name: string) : any { + const src = this.getPostParam(name); + + if(src == undefined) + this.error(400, "Missing JSON '" + name + "' in the request!"); + + try { + const response = JSON.parse(src); + return response; + } catch(e) { + this.error(500, "'" + name + "' is not a valid JSON !"); + } } /** diff --git a/src/helpers/ConversationsHelper.ts b/src/helpers/ConversationsHelper.ts index 1d19cde..f4f12d8 100644 --- a/src/helpers/ConversationsHelper.ts +++ b/src/helpers/ConversationsHelper.ts @@ -255,6 +255,24 @@ export class ConversationsHelper { }, limit: numberOfMessages, order: "id DESC" + })).map(m => this.DBToConversationMessage(convID, m)).reverse(); + } + + /** + * Get the new messages of a conversation + * + * @param convID Target conversation ID + * @param lastMessageID The ID of the last known message + */ + public static async GetNewMessages(convID: number, lastMessageID: number): Promise> { + return (await DatabaseHelper.Query({ + table: MESSAGES_TABLE, + where: { + conv_id: convID + }, + customWhere: "ID > ?", + customWhereArgs: [lastMessageID.toString()], + order: "id" })).map(m => this.DBToConversationMessage(convID, m)); }