mirror of
				https://gitlab.com/comunic/comunicapiv2
				synced 2025-11-04 11:34:04 +00:00 
			
		
		
		
	Can update group settings
This commit is contained in:
		@@ -4,6 +4,10 @@ import { GroupsAccessLevel, GroupInfo, GroupVisibilityLevel, GroupPostsCreationL
 | 
			
		||||
import { GroupMembershipLevels } from "../entities/GroupMember";
 | 
			
		||||
import { time } from "../utils/DateUtils";
 | 
			
		||||
import { LikesHelper, LikesType } from "../helpers/LikesHelper";
 | 
			
		||||
import { GroupSettings } from "../entities/GroupSettings";
 | 
			
		||||
import { removeHTMLNodes, checkURL } from "../utils/StringUtils";
 | 
			
		||||
import { findKey } from "../utils/ArrayUtils";
 | 
			
		||||
import { checkVirtualDirectoryAvailability, VirtualDirType } from "../utils/VirtualDirsUtils";
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Groups API controller
 | 
			
		||||
@@ -14,7 +18,7 @@ import { LikesHelper, LikesType } from "../helpers/LikesHelper";
 | 
			
		||||
/**
 | 
			
		||||
 * API groups registration levels
 | 
			
		||||
 */
 | 
			
		||||
const GROUPS_REGISTRATION_LEVELS = [];
 | 
			
		||||
const GROUPS_REGISTRATION_LEVELS = {};
 | 
			
		||||
GROUPS_REGISTRATION_LEVELS[GroupRegistrationLevel.OPEN_REGISTRATION] = "open";
 | 
			
		||||
GROUPS_REGISTRATION_LEVELS[GroupRegistrationLevel.MODERATED_REGISTRATION] = "moderated";
 | 
			
		||||
GROUPS_REGISTRATION_LEVELS[GroupRegistrationLevel.CLOSED_REGISTRATION] = "closed";
 | 
			
		||||
@@ -22,7 +26,7 @@ GROUPS_REGISTRATION_LEVELS[GroupRegistrationLevel.CLOSED_REGISTRATION] = "closed
 | 
			
		||||
/**
 | 
			
		||||
 * API groups membership levels
 | 
			
		||||
 */
 | 
			
		||||
const GROUPS_MEMBERSHIP_LEVELS = [];
 | 
			
		||||
const GROUPS_MEMBERSHIP_LEVELS = {};
 | 
			
		||||
GROUPS_MEMBERSHIP_LEVELS[GroupMembershipLevels.ADMINISTRATOR] = "administrator";
 | 
			
		||||
GROUPS_MEMBERSHIP_LEVELS[GroupMembershipLevels.MODERATOR] = "moderator";
 | 
			
		||||
GROUPS_MEMBERSHIP_LEVELS[GroupMembershipLevels.MEMBER] = "member";
 | 
			
		||||
@@ -34,7 +38,7 @@ GROUPS_MEMBERSHIP_LEVELS[GroupMembershipLevels.VISITOR] = "visitor";
 | 
			
		||||
/**
 | 
			
		||||
 * API groups visibility levels
 | 
			
		||||
 */
 | 
			
		||||
const GROUPS_VISIBILITY_LEVELS = [];
 | 
			
		||||
const GROUPS_VISIBILITY_LEVELS = {};
 | 
			
		||||
GROUPS_VISIBILITY_LEVELS[GroupVisibilityLevel.OPEN_GROUP] = "open";
 | 
			
		||||
GROUPS_VISIBILITY_LEVELS[GroupVisibilityLevel.PRIVATE_GROUP] = "private";
 | 
			
		||||
GROUPS_VISIBILITY_LEVELS[GroupVisibilityLevel.SECRETE_GROUP] = "secrete";
 | 
			
		||||
@@ -144,6 +148,74 @@ export class GroupsController {
 | 
			
		||||
		h.send(await this.GroupInfoToAPI(group, h, true));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Set (update) group settings
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param h Request handler
 | 
			
		||||
	 */
 | 
			
		||||
	public static async SetSettings(h: RequestHandler) {
 | 
			
		||||
		const groupID = await h.postGroupIDWithAccess("id", GroupsAccessLevel.ADMIN_ACCESS);
 | 
			
		||||
 | 
			
		||||
		// Check group visibility
 | 
			
		||||
		const visibilityKey = findKey(GROUPS_VISIBILITY_LEVELS, h.postString("visibility", 3));
 | 
			
		||||
		if(visibilityKey == null)
 | 
			
		||||
			h.error(400, "Group visibility level not recognized!");
 | 
			
		||||
 | 
			
		||||
		// Check group registration level
 | 
			
		||||
		const registrationKey = findKey(GROUPS_REGISTRATION_LEVELS, h.postString("registration_level", 3));
 | 
			
		||||
		if(registrationKey == null)
 | 
			
		||||
			h.error(400, "Group registration level not recognized!");
 | 
			
		||||
 | 
			
		||||
		// Check post creation level
 | 
			
		||||
		const postLevelKey = findKey(GROUPS_POSTS_LEVELS, h.postString("posts_level", 3));
 | 
			
		||||
		if(postLevelKey == null)
 | 
			
		||||
			h.error(400, "Group post creation level not recognized!");
 | 
			
		||||
		
 | 
			
		||||
 | 
			
		||||
		// Check URL
 | 
			
		||||
		const url = h.postString("url", 0);
 | 
			
		||||
		if(url.length > 0 && ! checkURL(url))
 | 
			
		||||
			h.error(401, "Invalid group URL!");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		// Check virtual directory
 | 
			
		||||
		let virtualDirectory = "";
 | 
			
		||||
		if(h.hasPostString("virtual_directory", 1)) {
 | 
			
		||||
			virtualDirectory = h.postVirtualDirectory("virtual_directory");
 | 
			
		||||
 | 
			
		||||
			// Check out whether virtual directory is available or not
 | 
			
		||||
			if(!await checkVirtualDirectoryAvailability(virtualDirectory, groupID, VirtualDirType.GROUP))
 | 
			
		||||
				h.error(401, "Requested virtual directory is not available!");
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		const settings = new GroupSettings({
 | 
			
		||||
			// Basic information
 | 
			
		||||
			id: groupID,
 | 
			
		||||
			name: removeHTMLNodes(h.postString("name", 3)),
 | 
			
		||||
			visiblity: <GroupVisibilityLevel>Number(visibilityKey),
 | 
			
		||||
			registrationLevel: <GroupRegistrationLevel>Number(registrationKey),
 | 
			
		||||
			postsCreationLevel: <GroupPostsCreationLevel>Number(postLevelKey),
 | 
			
		||||
			
 | 
			
		||||
			// Useless info
 | 
			
		||||
			membersCount: -1,
 | 
			
		||||
			timeCreate: -1,
 | 
			
		||||
 | 
			
		||||
			// Optionnal
 | 
			
		||||
			description: removeHTMLNodes(h.postString("description", 0)),
 | 
			
		||||
			
 | 
			
		||||
			// Optionnal
 | 
			
		||||
			url: url,
 | 
			
		||||
 | 
			
		||||
			// Optionnal
 | 
			
		||||
			virtualDirectory: virtualDirectory, 
 | 
			
		||||
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		await GroupsHelper.SetSettings(settings);
 | 
			
		||||
 | 
			
		||||
		h.success("Group settings have been successfully updated!");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Turn a GroupInfo object into a valid API object
 | 
			
		||||
	 * 
 | 
			
		||||
 
 | 
			
		||||
@@ -96,4 +96,6 @@ export const Routes : Route[] = [
 | 
			
		||||
	{path: "/groups/get_advanced_info", cb: (h) => GroupsController.GetAdvancedInfo(h), needLogin: false},
 | 
			
		||||
 | 
			
		||||
	{path: "/groups/get_settings", cb: (h) => GroupsController.GetSettings(h)},
 | 
			
		||||
 | 
			
		||||
	{path: "/groups/set_settings", cb: (h) => GroupsController.SetSettings(h)},
 | 
			
		||||
]
 | 
			
		||||
		Reference in New Issue
	
	Block a user