From 622ea0099e26861b183d83bf3544be8bb80fb810 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 25 May 2020 18:00:29 +0200 Subject: [PATCH] Start account image migration --- src/controllers/SettingsController.ts | 12 +++--- src/controllers/UserController.ts | 23 +++++----- src/entities/User.ts | 60 +++++++++++++++++++++++++-- src/helpers/UserHelper.ts | 3 +- 4 files changed, 75 insertions(+), 23 deletions(-) diff --git a/src/controllers/SettingsController.ts b/src/controllers/SettingsController.ts index 05751e6..5c0d903 100644 --- a/src/controllers/SettingsController.ts +++ b/src/controllers/SettingsController.ts @@ -10,7 +10,7 @@ import { GeneralSettings, UserPageStatus, SupportedLanguages, LangSettings, Secu import { removeHTMLNodes, checkURL, fixEncoding } from "../utils/StringUtils"; import { checkVirtualDirectoryAvailability, VirtualDirType } from "../utils/VirtualDirsUtils"; import { AccountHelper } from "../helpers/AccountHelper"; -import { AccountImageVisibilityLevel } from "../entities/AccountImage"; +import { AccountImageVisibilityLevel } from "../entities/User"; import { AccountImageHelper } from "../helpers/AccountImageHelper"; import { findKey } from "../utils/ArrayUtils"; import { CustomEmojisHelper } from "../helpers/CustomEmojisHelper"; @@ -223,12 +223,12 @@ export class SettingsController { * @param h Request handler */ public static async GetAccountImageSettings(h: RequestHandler) { - const imageInfo = (await UserHelper.GetUserInfo(h.getUserId())).accountImage; + const user = await UserHelper.GetUserInfo(h.getUserId()); h.send({ - has_image: imageInfo.hasImage, - image_url: imageInfo.url, - visibility: ACCOUNT_IMAGE_VISIBLITY_LEVELS[imageInfo.level] + has_image: user.hasAccountImage, + image_url: user.accountImageURL, + visibility: ACCOUNT_IMAGE_VISIBLITY_LEVELS[user.accountImageVisibilityLevel] }); } @@ -270,7 +270,7 @@ export class SettingsController { h.error(400, "Account image visibility level not understood!"); await AccountImageHelper.SetVisibilityLevel( - h.getUserId(), Number(visibilityLevel)); + h.getUserId(), String(visibilityLevel)); h.success(); } diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index 2be7eef..222537d 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -1,7 +1,6 @@ import { RequestHandler } from "../entities/RequestHandler"; import { UserHelper } from "../helpers/UserHelper"; -import { User, UserPageStatus } from "../entities/User"; -import { AccountImage, AccountImageVisibilityLevel } from "../entities/AccountImage"; +import { User, UserPageStatus, AccountImageVisibilityLevel } from "../entities/User"; import { BackgroundImageHelper } from "../helpers/BackgroundImageHelper"; import { LikesHelper, LikesType } from "../helpers/LikesHelper"; import { FriendsHelper } from "../helpers/FriendsHelper"; @@ -78,7 +77,7 @@ export class UserController { "publicPage": user.pageStatus == UserPageStatus.PUBLIC, "openPage": user.pageStatus == UserPageStatus.OPEN, "virtualDirectory": user.hasVirtualDirectory ? user.virtualDirectory : "", - "accountImage": await this.GetAccountImageURL(user.accountImage, h), + "accountImage": await this.GetAccountImageURL(user, h), "customEmojis": (await CustomEmojisHelper.GetListUser(user.id)).map(SettingsController.CustomEmojiToAPI) }; @@ -101,20 +100,20 @@ export class UserController { return info; } - private static async GetAccountImageURL(image : AccountImage, handler: RequestHandler) { + private static async GetAccountImageURL(user : User, handler: RequestHandler) { - if(image.level == AccountImageVisibilityLevel.EVERYONE - || (handler.signedIn && handler.getUserId() == image.userID)) - return image.url; + if(user.accountImageVisibilityLevel == AccountImageVisibilityLevel.EVERYONE + || (handler.signedIn && handler.getUserId() == user.id)) + return user.accountImageURL; if(!handler.signedIn) - return AccountImage.errorURL; + return User.errorAccountImageURL; - if(image.level == AccountImageVisibilityLevel.COMUNIC_USERS - || await FriendsHelper.AreFriend(image.userID, handler.getUserId())) { - return image.url; + if(user.accountImageVisibilityLevel == AccountImageVisibilityLevel.COMUNIC_USERS + || await FriendsHelper.AreFriend(user.id, handler.getUserId())) { + return user.accountImageURL; } - return AccountImage.errorURL; + return User.errorAccountImageURL; } } \ No newline at end of file diff --git a/src/entities/User.ts b/src/entities/User.ts index 1ffeaeb..77b3972 100644 --- a/src/entities/User.ts +++ b/src/entities/User.ts @@ -1,13 +1,23 @@ -import { AccountImage } from "./AccountImage"; - /** * User information * * @author Pierre HUBERT */ +import { pathUserData } from "../utils/UserDataUtils"; + export const SupportedLanguages = ["fr", "en"] +export enum AccountImageVisibilityLevel { + FRIENDS = "friends", + COMUNIC_USERS = "comunic_users", + EVERYONE = "everyone" +} + +const defaultAccountImage = "0Reverse.png"; +const errorAccountImage = "0Red.png"; + + export enum UserPageStatus { PRIVATE, PUBLIC, @@ -21,7 +31,8 @@ export interface UserInfo { timeCreate: number, virtualDirectory: string, pageStatus: UserPageStatus, - accountImage: AccountImage, + accountImagePath: string, + accountImageVisibilityLevel: AccountImageVisibilityLevel, friendsListPublic: boolean, personnalWebsite ?: string, publicNote ?: string, @@ -70,7 +81,6 @@ export class User implements UserBuilder { timeCreate: number; virtualDirectory: string; pageStatus: UserPageStatus; - accountImage: AccountImage; friendsListPublic: boolean; personnalWebsite?: string; publicNote?: string; @@ -78,6 +88,8 @@ export class User implements UserBuilder { allowPostsFromFriends: boolean; allowMails: boolean; lang: string; + accountImagePath: string; + accountImageVisibilityLevel: AccountImageVisibilityLevel; security_question_1?: string; security_answer_1?: string; security_question_2?: string; @@ -93,6 +105,46 @@ export class User implements UserBuilder { } + /** + * Get account image URL + */ + get accountImageURL() : string { + if(this.accountImagePath.length < 1) + return User.pathForAccountImageFile(defaultAccountImage); + + return User.pathForAccountImageFile(this.accountImagePath); + } + + /** + * Get account image sys path + */ + get accountImageSysPath() : string { + if(this.accountImagePath.length < 1) + throw new Error("This user has no account image!"); + + return User.pathForAccountImageFile(this.accountImagePath, true); + } + + /** + * Check out whether the user has an account image + * or if it is the default account image + */ + get hasAccountImage() : boolean { + return this.accountImagePath.length > 0; + } + + /** + * Fallback account image URL + */ + static get errorAccountImageURL() : string { + return this.pathForAccountImageFile(errorAccountImage); + } + + private static pathForAccountImageFile(file : string, sysPath = false) : string { + return pathUserData("avatars/" + file, sysPath); + } + + get isPublic() : boolean { return this.pageStatus != UserPageStatus.PRIVATE; } diff --git a/src/helpers/UserHelper.ts b/src/helpers/UserHelper.ts index 59f00c6..5febbea 100644 --- a/src/helpers/UserHelper.ts +++ b/src/helpers/UserHelper.ts @@ -199,7 +199,8 @@ export class UserHelper { timeCreate: new Date(row.date_creation).getTime()/1000, virtualDirectory: row.sous_repertoire, pageStatus: row.pageouverte == 1 ? UserPageStatus.OPEN : (row.public == 1 ? UserPageStatus.PUBLIC : UserPageStatus.PRIVATE), - accountImage: await AccountImageHelper.Get(row.ID), + accountImagePath: row.account_image_path, + accountImageVisibilityLevel: row.account_image_visibility, friendsListPublic: row.liste_amis_publique == 1, personnalWebsite: row.site_web, publicNote: row.public_note,