1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-12-26 13:38:52 +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> {
const convID = await h.postConversationId("conversationID");
const isUserModerator = await ConversationsHelper.IsUserModerator(h.getUserId(), convID);
// Update following state, if required
if(h.hasPostParameter("following")) {
@ -94,31 +95,35 @@ export class ConversationsController {
);
}
// Update members list
if(h.hasPostParameter("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
if(!members.has(h.getUserId()))
members.add(h.getUserId());
await ConversationsHelper.SetMembers(convID, members, isUserModerator);
}
// Change moderator settings
if(h.hasPostParameter("members") || h.hasPostParameter("name") || h.hasPostParameter("canEveryoneAddMembers")) {
if(h.hasPostParameter("name") || h.hasPostParameter("canEveryoneAddMembers")) {
// Check if user is the moderator of the conversation
if(!await ConversationsHelper.IsUserModerator(h.getUserId(), convID))
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 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);
}
// Update "canEveryoneAddMembers" parameter
if(h.hasPostParameter("canEveryoneAddMembers")) {
await ConversationsHelper.SetCanEveryoneAddMembers(convID, h.postBool("canEveryoneAddMembers"));

View File

@ -187,8 +187,9 @@ export class ConversationsHelper {
*
* @param convID Target conversation ID
* @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);
// Add new members
@ -200,12 +201,13 @@ export class ConversationsHelper {
}
// Remove old members
for(const member of currentList) {
if(newList.has(member))
continue;
await this.RemoveMember(convID, member);
}
if(canDelete)
for(const member of currentList) {
if(newList.has(member))
continue;
await this.RemoveMember(convID, member);
}
}
/**
@ -281,6 +283,27 @@ export class ConversationsHelper {
})) > 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
*