1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 05:19:22 +00:00

Start account image migration

This commit is contained in:
Pierre HUBERT 2020-05-25 18:00:29 +02:00
parent 327418697c
commit 622ea0099e
4 changed files with 75 additions and 23 deletions

View File

@ -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(), <AccountImageVisibilityLevel>Number(visibilityLevel));
h.getUserId(), <AccountImageVisibilityLevel>String(visibilityLevel));
h.success();
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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,