1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 05:19:22 +00:00

Can update the list of members of a conversation.

This commit is contained in:
Pierre HUBERT 2019-11-30 14:38:49 +01:00
parent 47e9d46b4d
commit 862d8e0210
3 changed files with 74 additions and 15 deletions

View File

@ -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<void> {
const convID = await this.GetPostConversationId("conversationID", handler);
public static async UpdateSettings(h: RequestHandler) : Promise<void> {
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!");
}
/**

View File

@ -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<number> {
return new Set(this.postNumbersList(name));
}
/**
* Get a boolean included in the request
*

View File

@ -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)]
}
}
}