mirror of
				https://gitlab.com/comunic/comunicapiv2
				synced 2025-11-04 11:34:04 +00:00 
			
		
		
		
	Can update the list of members of a conversation.
This commit is contained in:
		@@ -76,37 +76,48 @@ export class ConversationsController {
 | 
				
			|||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Update conversation settings
 | 
						 * Update conversation settings
 | 
				
			||||||
	 * 
 | 
						 * 
 | 
				
			||||||
	 * @param handler Request handler
 | 
						 * @param h Request handler
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public static async UpdateSettings(handler: RequestHandler) : Promise<void> {
 | 
						public static async UpdateSettings(h: RequestHandler) : Promise<void> {
 | 
				
			||||||
		const convID = await this.GetPostConversationId("conversationID", handler);
 | 
							const convID = await this.GetPostConversationId("conversationID", h);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Update following state, if required
 | 
							// Update following state, if required
 | 
				
			||||||
		if(handler.hasPostParameter("following")) {
 | 
							if(h.hasPostParameter("following")) {
 | 
				
			||||||
			await ConversationsHelper.SetFollowing(
 | 
								await ConversationsHelper.SetFollowing(
 | 
				
			||||||
				handler.getUserId(),
 | 
									h.getUserId(),
 | 
				
			||||||
				convID,
 | 
									convID,
 | 
				
			||||||
				handler.postBool("following")
 | 
									h.postBool("following")
 | 
				
			||||||
			);
 | 
								);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
		// Change moderator settings
 | 
							// 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
 | 
								// Check if user is the moderator of the conversation
 | 
				
			||||||
			if(!await ConversationsHelper.IsUserModerator(handler.getUserId(), convID))
 | 
								if(!await ConversationsHelper.IsUserModerator(h.getUserId(), convID))
 | 
				
			||||||
				handler.error(401, "You are not allowed to perform changes on this conversation !");
 | 
									h.error(401, "You are not allowed to perform changes on this conversation !");
 | 
				
			||||||
			
 | 
								
 | 
				
			||||||
			
 | 
								
 | 
				
			||||||
			// Update conversation name (if required)
 | 
								// Update conversation name (if required)
 | 
				
			||||||
			if(handler.hasPostParameter("name")) {
 | 
								if(h.hasPostParameter("name")) {
 | 
				
			||||||
				const name = handler.postString("name");
 | 
									const name = h.postString("name");
 | 
				
			||||||
				await ConversationsHelper.SetName(convID, name == "false" ? "" : removeHTMLNodes(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!");
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -122,6 +122,15 @@ export class RequestHandler {
 | 
				
			|||||||
		return list;
 | 
							return list;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Turn a list of string into a Set object
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 * @param name Name of POST field
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public postNumbersSet(name : string) : Set<number> {
 | 
				
			||||||
 | 
							return new Set(this.postNumbersList(name));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Get a boolean included in the request
 | 
						 * Get a boolean included in the request
 | 
				
			||||||
	 * 
 | 
						 * 
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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
 | 
						 * 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<number>) {
 | 
				
			||||||
 | 
							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
 | 
						 * Update following state of the conversation
 | 
				
			||||||
	 * 
 | 
						 * 
 | 
				
			||||||
@@ -206,7 +245,7 @@ export class ConversationsHelper {
 | 
				
			|||||||
	 * 
 | 
						 * 
 | 
				
			||||||
	 * @param convID The ID of the target conversation
 | 
						 * @param convID The ID of the target conversation
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	private static async GetConversationMembers(convID : number): Promise<Array<number>> {
 | 
						private static async GetConversationMembers(convID : number): Promise<Set<number>> {
 | 
				
			||||||
		const result = await DatabaseHelper.Query({
 | 
							const result = await DatabaseHelper.Query({
 | 
				
			||||||
			table: USERS_TABLE,
 | 
								table: USERS_TABLE,
 | 
				
			||||||
			where: {
 | 
								where: {
 | 
				
			||||||
@@ -215,7 +254,7 @@ export class ConversationsHelper {
 | 
				
			|||||||
			fields: ["user_id"]
 | 
								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,
 | 
								timeCreate: row.time_add,
 | 
				
			||||||
			following: row.following,
 | 
								following: row.following,
 | 
				
			||||||
			sawLastMessage: row.saw_last_message == 1,
 | 
								sawLastMessage: row.saw_last_message == 1,
 | 
				
			||||||
			members: await this.GetConversationMembers(row.id)
 | 
								members: [...await this.GetConversationMembers(row.id)]
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user