diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 8819f3f..ab1ce7b 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -103,6 +103,8 @@ export const Routes : Route[] = [ {path: "/settings/delete_account_image", cb: (h) => SettingsController.DeleteAccountImage(h)}, + {path: "/settings/set_account_image_visibility", cb: (h) => SettingsController.SetAccountImageVisibility(h)}, + // Friends controller {path: "/friends/getList", cb: (h) => FriendsController.GetList(h)}, diff --git a/src/controllers/SettingsController.ts b/src/controllers/SettingsController.ts index 0cc3421..9d0c4f9 100644 --- a/src/controllers/SettingsController.ts +++ b/src/controllers/SettingsController.ts @@ -12,6 +12,7 @@ import { checkVirtualDirectoryAvailability, VirtualDirType } from "../utils/Virt import { AccountHelper } from "../helpers/AccountHelper"; import { AccountImageVisibilityLevel } from "../entities/AccountImage"; import { AccountImageHelper } from "../helpers/AccountImageHelper"; +import { findKey } from "../utils/ArrayUtils"; /** * API account image visibility levels @@ -242,4 +243,21 @@ export class SettingsController { await AccountImageHelper.Delete(h.getUserId()); h.success(); } + + /** + * Change account image visibility level + * + * @param h Request handler + */ + public static async SetAccountImageVisibility(h: RequestHandler) { + + const visibilityLevel = findKey(ACCOUNT_IMAGE_VISIBLITY_LEVELS, h.postString("visibility")); + if(visibilityLevel == null) + h.error(400, "Account image visibility level not understood!"); + + await AccountImageHelper.SetVisibilityLevel( + h.getUserId(), Number(visibilityLevel)); + + h.success(); + } } \ No newline at end of file diff --git a/src/helpers/AccountImageHelper.ts b/src/helpers/AccountImageHelper.ts index a53196e..b6e04fa 100644 --- a/src/helpers/AccountImageHelper.ts +++ b/src/helpers/AccountImageHelper.ts @@ -55,6 +55,29 @@ export class AccountImageHelper { } } + /** + * Change account image visiblity level + * + * @param userID Target user ID + * @param level New level for account image + */ + public static async SetVisibilityLevel(userID: number, level: AccountImageVisibilityLevel) { + const file = this.GetPathVisibilityFile(userID); + + // If the visiblity is set to everyone, we do not + // need to have a visibility file + if(level == AccountImageVisibilityLevel.EVERYONE) { + + if(existsSync(file)) + unlinkSync(file); + + return; + } + + else + writeFileSync(file, level); + } + /** * Get the content of the file associated to the user account image, if any *