mirror of
				https://gitlab.com/comunic/comunicapiv2
				synced 2025-11-04 03:24:04 +00:00 
			
		
		
		
	Can change group logo
This commit is contained in:
		@@ -1,5 +1,5 @@
 | 
			
		||||
import { RequestHandler } from "../entities/RequestHandler";
 | 
			
		||||
import { GroupsHelper } from "../helpers/GroupsHelper";
 | 
			
		||||
import { GroupsHelper, PATH_GROUPS_LOGOS } from "../helpers/GroupsHelper";
 | 
			
		||||
import { GroupsAccessLevel, GroupInfo, GroupVisibilityLevel, GroupPostsCreationLevel, GroupRegistrationLevel } from "../entities/Group";
 | 
			
		||||
import { GroupMembershipLevels } from "../entities/GroupMember";
 | 
			
		||||
import { time } from "../utils/DateUtils";
 | 
			
		||||
@@ -231,6 +231,50 @@ export class GroupsController {
 | 
			
		||||
		h.success("Requested virtual directory seems to be available!");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Upload a new group logo
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param h Request handler
 | 
			
		||||
	 */
 | 
			
		||||
	public static async UploadLogo(h: RequestHandler) {
 | 
			
		||||
		const groupID = await h.postGroupIDWithAccess("id", GroupsAccessLevel.ADMIN_ACCESS);
 | 
			
		||||
 | 
			
		||||
		if(!h.hasFile("logo"))
 | 
			
		||||
			h.error(400, "An error occured while receiving logo !");
 | 
			
		||||
 | 
			
		||||
		// Delete current logo (if any)
 | 
			
		||||
		await GroupsHelper.DeleteLogo(groupID);
 | 
			
		||||
 | 
			
		||||
		// Save the new group logo
 | 
			
		||||
		const targetFilePath = await h.savePostImage("logo", PATH_GROUPS_LOGOS, 500, 500);
 | 
			
		||||
 | 
			
		||||
		// Update the settings of the group
 | 
			
		||||
		const settings = await GroupsHelper.GetInfo(groupID);
 | 
			
		||||
		settings.logo = targetFilePath;
 | 
			
		||||
		await GroupsHelper.SetSettings(settings);
 | 
			
		||||
 | 
			
		||||
		h.send({
 | 
			
		||||
			success: "Group logo has been successfully updated!",
 | 
			
		||||
			url: settings.logoURL
 | 
			
		||||
		});
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Delete the current logo of a group
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param h Request handler
 | 
			
		||||
	 */
 | 
			
		||||
	public static async DeleteLogo(h: RequestHandler) {
 | 
			
		||||
		const groupID = await h.postGroupIDWithAccess("id", GroupsAccessLevel.ADMIN_ACCESS);
 | 
			
		||||
 | 
			
		||||
		await GroupsHelper.DeleteLogo(groupID);
 | 
			
		||||
 | 
			
		||||
		h.send({
 | 
			
		||||
			success: "Group logo has been successfully deleted!",
 | 
			
		||||
			url: (await GroupsHelper.GetInfo(groupID)).logoURL
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Turn a GroupInfo object into a valid API object
 | 
			
		||||
	 * 
 | 
			
		||||
 
 | 
			
		||||
@@ -100,4 +100,8 @@ export const Routes : Route[] = [
 | 
			
		||||
	{path: "/groups/set_settings", cb: (h) => GroupsController.SetSettings(h)},
 | 
			
		||||
 | 
			
		||||
	{path: "/groups/checkVirtualDirectory", cb: (h) => GroupsController.CheckVirtualDirectory(h)},
 | 
			
		||||
 | 
			
		||||
	{path: "/groups/upload_logo", cb: (h) => GroupsController.UploadLogo(h)},
 | 
			
		||||
 | 
			
		||||
	{path: "/groups/delete_logo", cb: (h) => GroupsController.DeleteLogo(h)},
 | 
			
		||||
]
 | 
			
		||||
@@ -96,6 +96,13 @@ export class GroupInfo implements GroupInfoConstructor {
 | 
			
		||||
		return pathUserData(this.logoPath, false);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	get logoSysPath() : string {
 | 
			
		||||
		if(!this.hasLogo)
 | 
			
		||||
			throw Error("The group has no logo!");
 | 
			
		||||
 | 
			
		||||
		return pathUserData(this.logoPath, true);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	get hasDescription() : boolean {
 | 
			
		||||
		return this.description && true;
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ import { GroupMembershipLevels, GroupMember } from "../entities/GroupMember";
 | 
			
		||||
import { NewGroup } from "../entities/NewGroup";
 | 
			
		||||
import { time } from "../utils/DateUtils";
 | 
			
		||||
import { GroupSettings } from "../entities/GroupSettings";
 | 
			
		||||
import { existsSync, unlinkSync } from "fs";
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Groups helper
 | 
			
		||||
@@ -14,6 +15,8 @@ import { GroupSettings } from "../entities/GroupSettings";
 | 
			
		||||
const GROUPS_LIST_TABLE = "comunic_groups";
 | 
			
		||||
const GROUPS_MEMBERS_TABLE = "comunic_groups_members";
 | 
			
		||||
 | 
			
		||||
export const PATH_GROUPS_LOGOS = "groups_logo";
 | 
			
		||||
 | 
			
		||||
export class GroupsHelper {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -116,6 +119,25 @@ export class GroupsHelper {
 | 
			
		||||
		});
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Delete the logo of a group
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param groupID Target group ID
 | 
			
		||||
	 */
 | 
			
		||||
	public static async DeleteLogo(groupID: number) {
 | 
			
		||||
		const currSettings = await this.GetInfo(groupID);
 | 
			
		||||
 | 
			
		||||
		if(!currSettings.hasLogo)
 | 
			
		||||
			return;
 | 
			
		||||
		
 | 
			
		||||
		if(existsSync(currSettings.logoSysPath))
 | 
			
		||||
			unlinkSync(currSettings.logoSysPath);
 | 
			
		||||
		
 | 
			
		||||
		currSettings.logo = "";
 | 
			
		||||
		
 | 
			
		||||
		await this.SetSettings(currSettings);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Get the visibility level of a group
 | 
			
		||||
	 * 
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user