diff --git a/src/controllers/ConversationsController.ts b/src/controllers/ConversationsController.ts index f96eaef..2dd1ac6 100644 --- a/src/controllers/ConversationsController.ts +++ b/src/controllers/ConversationsController.ts @@ -76,37 +76,48 @@ export class ConversationsController { /** * Update conversation settings * - * @param handler Request handler + * @param h Request handler */ - public static async UpdateSettings(handler: RequestHandler) : Promise { - const convID = await this.GetPostConversationId("conversationID", handler); + public static async UpdateSettings(h: RequestHandler) : Promise { + const convID = await this.GetPostConversationId("conversationID", h); // Update following state, if required - if(handler.hasPostParameter("following")) { + if(h.hasPostParameter("following")) { await ConversationsHelper.SetFollowing( - handler.getUserId(), + h.getUserId(), convID, - handler.postBool("following") + h.postBool("following") ); } // Change moderator settings - if(handler.hasPostParameter("members") || handler.hasPostParameter("name")) { + if(h.hasPostParameter("members") || h.hasPostParameter("name")) { // Check if user is the moderator of the conversation - if(!await ConversationsHelper.IsUserModerator(handler.getUserId(), convID)) - handler.error(401, "You are not allowed to perform changes on this conversation !"); + if(!await ConversationsHelper.IsUserModerator(h.getUserId(), convID)) + h.error(401, "You are not allowed to perform changes on this conversation !"); // Update conversation name (if required) - if(handler.hasPostParameter("name")) { - const name = handler.postString("name"); + if(h.hasPostParameter("name")) { + const name = h.postString("name"); await ConversationsHelper.SetName(convID, name == "false" ? "" : removeHTMLNodes(name)); } + // Update the list of members of the conversation (if required) + if(h.hasPostParameter("members")) { + const members = h.postNumbersSet("members"); + + // Make sure current user is on the list + if(!members.has(h.getUserId())) + members.add(h.getUserId()); + + await ConversationsHelper.SetMembers(convID, members); + } + } - handler.success("Conversation information successfully updated!"); + h.success("Conversation information successfully updated!"); } /** diff --git a/src/entities/RequestHandler.ts b/src/entities/RequestHandler.ts index 800adb8..66769d4 100644 --- a/src/entities/RequestHandler.ts +++ b/src/entities/RequestHandler.ts @@ -122,6 +122,15 @@ export class RequestHandler { return list; } + /** + * Turn a list of string into a Set object + * + * @param name Name of POST field + */ + public postNumbersSet(name : string) : Set { + return new Set(this.postNumbersList(name)); + } + /** * Get a boolean included in the request * diff --git a/src/helpers/ConversationsHelper.ts b/src/helpers/ConversationsHelper.ts index 1dd670c..043d092 100644 --- a/src/helpers/ConversationsHelper.ts +++ b/src/helpers/ConversationsHelper.ts @@ -61,6 +61,19 @@ export class ConversationsHelper { ); } + /** + * Remove a user from a conversation + * + * @param convID Conversation ID + * @param userID ID of the user to remove + */ + private static async RemoveMember(convID: number, userID: number) { + await DatabaseHelper.DeleteRows(USERS_TABLE, { + conv_id: convID, + user_id: userID + }); + } + /** * Get the list of conversations of the user * @@ -165,6 +178,32 @@ export class ConversationsHelper { }); } + /** + * Set a new list of members for a given conversation + * + * @param convID Target conversation ID + * @param members The new list of members for the conversation + */ + public static async SetMembers(convID: number, newList: Set) { + const currentList = await this.GetConversationMembers(convID); + + // Add new members + for (const member of newList) { + if(currentList.has(member)) + continue; + + await this.AddMember(convID, member, true); + } + + // Remove old members + for(const member of currentList) { + if(newList.has(member)) + continue; + + await this.RemoveMember(convID, member); + } + } + /** * Update following state of the conversation * @@ -206,7 +245,7 @@ export class ConversationsHelper { * * @param convID The ID of the target conversation */ - private static async GetConversationMembers(convID : number): Promise> { + private static async GetConversationMembers(convID : number): Promise> { const result = await DatabaseHelper.Query({ table: USERS_TABLE, where: { @@ -215,7 +254,7 @@ export class ConversationsHelper { fields: ["user_id"] }); - return result.map((e) => e.user_id); + return new Set(result.map((e) => e.user_id)); } /** @@ -232,7 +271,7 @@ export class ConversationsHelper { timeCreate: row.time_add, following: row.following, sawLastMessage: row.saw_last_message == 1, - members: await this.GetConversationMembers(row.id) + members: [...await this.GetConversationMembers(row.id)] } } } \ No newline at end of file