1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-25 23:09:22 +00:00

Allow non-moderator of conversation to add members

This commit is contained in:
Pierre HUBERT 2020-04-25 19:03:55 +02:00
parent 5d604bc576
commit 0c93ae95b4
2 changed files with 49 additions and 21 deletions

View File

@ -84,6 +84,7 @@ export class ConversationsController {
*/ */
public static async UpdateSettings(h: RequestHandler) : Promise<void> { public static async UpdateSettings(h: RequestHandler) : Promise<void> {
const convID = await h.postConversationId("conversationID"); const convID = await h.postConversationId("conversationID");
const isUserModerator = await ConversationsHelper.IsUserModerator(h.getUserId(), convID);
// Update following state, if required // Update following state, if required
if(h.hasPostParameter("following")) { if(h.hasPostParameter("following")) {
@ -94,29 +95,33 @@ export class ConversationsController {
); );
} }
// Change moderator settings // Update members list
if(h.hasPostParameter("members") || h.hasPostParameter("name") || h.hasPostParameter("canEveryoneAddMembers")) {
// Check if user is the moderator of the 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(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")) { if(h.hasPostParameter("members")) {
const members = h.postNumbersSet("members"); const members = h.postNumbersSet("members");
const canEveryoneAddMembers = await ConversationsHelper.CanEveryoneAddMembers(convID);
if(!isUserModerator && !canEveryoneAddMembers)
h.error(401, "You can not update the list of members of this conversation!");
// Make sure current user is on the list // Make sure current user is on the list
if(!members.has(h.getUserId())) if(!members.has(h.getUserId()))
members.add(h.getUserId()); members.add(h.getUserId());
await ConversationsHelper.SetMembers(convID, members); await ConversationsHelper.SetMembers(convID, members, isUserModerator);
}
// Change moderator settings
if(h.hasPostParameter("name") || h.hasPostParameter("canEveryoneAddMembers")) {
// Check if user is the moderator of the conversation
if(!isUserModerator)
h.error(401, "You are not allowed to perform changes on this conversation !");
// Update conversation name (if required)
if(h.hasPostParameter("name")) {
const name = h.postString("name");
await ConversationsHelper.SetName(convID, name == "false" ? "" : removeHTMLNodes(name));
} }
// Update "canEveryoneAddMembers" parameter // Update "canEveryoneAddMembers" parameter

View File

@ -187,8 +187,9 @@ export class ConversationsHelper {
* *
* @param convID Target conversation ID * @param convID Target conversation ID
* @param members The new list of members for the conversation * @param members The new list of members for the conversation
* @param canDelete Set to true to perform memberships deletion
*/ */
public static async SetMembers(convID: number, newList: Set<number>) { public static async SetMembers(convID: number, newList: Set<number>, canDelete: boolean) {
const currentList = await this.GetConversationMembers(convID); const currentList = await this.GetConversationMembers(convID);
// Add new members // Add new members
@ -200,6 +201,7 @@ export class ConversationsHelper {
} }
// Remove old members // Remove old members
if(canDelete)
for(const member of currentList) { for(const member of currentList) {
if(newList.has(member)) if(newList.has(member))
continue; continue;
@ -281,6 +283,27 @@ export class ConversationsHelper {
})) > 0; })) > 0;
} }
/**
* Check out whether all the members of the conversation can
* add members to it or not
*
* @param convID Target conversation ID
*/
public static async CanEveryoneAddMembers(convID: number) : Promise<boolean> {
const info = await DatabaseHelper.QueryRow({
table: LIST_TABLE,
where: {
id: convID
},
fields: ["can_everyone_add_members"]
});
if(info == null)
throw new Error("Could not find conversation!");
return info["can_everyone_add_members"] == 1;
}
/** /**
* Get the last messages of a conversation * Get the last messages of a conversation
* *