mirror of
				https://gitlab.com/comunic/comunicapiv2
				synced 2025-11-04 03:24:04 +00:00 
			
		
		
		
	Can update the list of members of a conversation.
This commit is contained in:
		@@ -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<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
 | 
			
		||||
	 * 
 | 
			
		||||
@@ -206,7 +245,7 @@ export class ConversationsHelper {
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @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({
 | 
			
		||||
			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)]
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user