1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-27 15:59:22 +00:00
comunicapiv2/src/controllers/SettingsController.ts

218 lines
6.2 KiB
TypeScript
Raw Normal View History

2020-03-21 16:16:30 +00:00
/**
* Settings controller
*
* @author Pierre HUBERT
*/
import { RequestHandler } from "../entities/RequestHandler";
import { UserHelper } from "../helpers/UserHelper";
2020-03-22 13:18:59 +00:00
import { GeneralSettings, UserPageStatus, SupportedLanguages, LangSettings, SecuritySettings } from "../entities/User";
2020-03-21 18:02:54 +00:00
import { removeHTMLNodes, checkURL, fixEncoding } from "../utils/StringUtils";
2020-03-21 17:05:17 +00:00
import { checkVirtualDirectoryAvailability, VirtualDirType } from "../utils/VirtualDirsUtils";
import { AccountHelper } from "../helpers/AccountHelper";
2020-03-22 13:36:44 +00:00
import { AccountImageVisibilityLevel } from "../entities/AccountImage";
/**
* API account image visibility levels
*/
const ACCOUNT_IMAGE_VISIBLITY_LEVELS = {};
ACCOUNT_IMAGE_VISIBLITY_LEVELS[AccountImageVisibilityLevel.EVERYONE] = "open";
ACCOUNT_IMAGE_VISIBLITY_LEVELS[AccountImageVisibilityLevel.COMUNIC_USERS] = "public";
ACCOUNT_IMAGE_VISIBLITY_LEVELS[AccountImageVisibilityLevel.FRIENDS] = "friends";
2020-03-21 16:16:30 +00:00
export class SettingsController {
/**
* Get general account settings
*
* @param h Request handler
*/
public static async GetGeneral(h: RequestHandler) {
const userInfo = await UserHelper.GetUserInfo(h.getUserId());
h.send({
id: userInfo.id,
email: userInfo.email,
firstName: userInfo.firstName,
lastName: userInfo.lastName,
is_public: userInfo.isPublic,
is_open: userInfo.isOpen,
allow_comments: !userInfo.blockComments,
allow_posts_from_friends: userInfo.allowPostsFromFriends,
allow_comunic_mails: userInfo.allowMails,
public_friends_list: userInfo.friendsListPublic,
virtual_directory: userInfo.virtualDirectory,
personnal_website: userInfo.personnalWebsite,
publicNote: userInfo.publicNote,
})
}
2020-03-21 17:05:17 +00:00
/**
* Update (set) general account settings
*
* @param h Request handler
*/
public static async SetGeneral(h: RequestHandler) {
// Determine page status
const pageStatus = h.postBool("isPublic") ? (
h.postBool("isOpen") ? UserPageStatus.OPEN : UserPageStatus.PUBLIC
) : UserPageStatus.PRIVATE;
// Check personnal website
const personnalWebsite = h.postString("personnalWebsite", 0);
if(personnalWebsite.length > 0 && !checkURL(personnalWebsite))
h.error(401, "Invalid personnal website supplied!");
// Check virtual directory
let virtualDirectory = h.postString("virtualDirectory", 0);
if(virtualDirectory.length > 0) {
virtualDirectory = h.postVirtualDirectory("virtualDirectory");
2020-03-21 17:38:47 +00:00
if(!await checkVirtualDirectoryAvailability(virtualDirectory, h.getUserId(), VirtualDirType.USER))
2020-03-21 17:05:17 +00:00
h.error(401, "The specified virtual directory is not available!");
}
// Construct new settings object
const newSettings : GeneralSettings = {
id: h.getUserId(),
firstName: removeHTMLNodes(h.postString("firstName", 3)),
lastName: removeHTMLNodes(h.postString("lastName", 3)),
pageStatus: pageStatus,
blockComments: !h.postBool("allowComments"),
allowPostsFromFriends: h.postBool("allowPostsFromFriends"),
friendsListPublic: h.postBool("publicFriendsList"),
personnalWebsite: personnalWebsite,
virtualDirectory: virtualDirectory,
allowMails: h.postBool("allow_comunic_mails"),
publicNote: removeHTMLNodes(h.postString("publicNote", 0))
};
await AccountHelper.SetGeneral(newSettings);
h.success();
}
/**
* Check the availablity of a virtual directory for a user
*
* @param h Request handler
*/
public static async CheckDirectoryAvailability(h: RequestHandler) {
const directory = h.postVirtualDirectory("directory");
if(!await checkVirtualDirectoryAvailability(directory, h.getUserId(), VirtualDirType.USER))
h.error(401, "The specified directory is unavailable!");
h.success("The directory is available!");
}
2020-03-21 17:45:45 +00:00
/**
* Get language settings
*
* @param h Request handler
*/
public static async GetLanguage(h: RequestHandler) {
const userInfo = await UserHelper.GetUserInfo(h.getUserId());
h.send({
lang: userInfo.lang
});
}
2020-03-21 17:54:30 +00:00
/**
* Update (set) language settings
*
* @param h Request handler
*/
public static async SetLanguage(h: RequestHandler) {
const lang = h.postString("lang", 2);
if(!SupportedLanguages.includes(lang))
h.error(401, "Language not supported!");
const newSettings: LangSettings = {
id: h.getUserId(),
lang: lang
}
await AccountHelper.SetLanguageSettings(newSettings);
h.success();
}
2020-03-21 18:00:08 +00:00
/**
* Get security settings
*
* @param h Request handler
*/
public static async GetSecurity(h: RequestHandler) {
await h.needUserPostPassword("password");
const userInfo = await UserHelper.GetUserInfo(h.getUserId());
h.send({
id: userInfo.id,
2020-03-22 13:18:59 +00:00
security_question_1: userInfo.hasSecurityQuestion1 ? userInfo.security_question_1 : "",
security_answer_1: userInfo.hasSecurityAnswer1 ? userInfo.security_answer_1 : "",
security_question_2: userInfo.hasSecurityQuestion2 ? userInfo.security_question_2 : "",
security_answer_2: userInfo.hasSecurityAnswer2 ? userInfo.security_answer_2 : "",
2020-03-21 18:00:08 +00:00
});
}
2020-03-22 13:18:59 +00:00
/**
* Set security settings
*
* @param h Request handler
*/
public static async SetSecurity(h: RequestHandler) {
await h.needUserPostPassword("password");
const newSettings: SecuritySettings = {
id: h.getUserId(),
security_question_1: removeHTMLNodes(h.postString("security_question_1", 0)),
security_answer_1: removeHTMLNodes(h.postString("security_answer_1", 0)),
security_question_2: removeHTMLNodes(h.postString("security_question_2", 0)),
security_answer_2: removeHTMLNodes(h.postString("security_answer_2", 0))
}
await AccountHelper.SetSecuritySettings(newSettings);
h.success();
}
2020-03-22 13:26:11 +00:00
/**
* Update user password
*
* @param h Request handler
*/
public static async UpdatePassword(h: RequestHandler) {
await h.needUserPostPassword("oldPassword");
const newPassword = h.postString("newPassword");
await AccountHelper.ChangePassword(h.getUserId(), newPassword);
h.success();
}
2020-03-22 13:36:44 +00:00
/**
* Get account image settings
*
* @param h Request handler
*/
public static async GetAccountImageSettings(h: RequestHandler) {
const imageInfo = (await UserHelper.GetUserInfo(h.getUserId())).accountImage;
h.send({
has_image: imageInfo.hasImage,
image_url: imageInfo.url,
visibility: ACCOUNT_IMAGE_VISIBLITY_LEVELS[imageInfo.level]
});
}
2020-03-21 16:16:30 +00:00
}