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

106 lines
3.3 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";
import { UserController } from "./UserController";
2020-03-21 17:05:17 +00:00
import { GeneralSettings, User, UserPageStatus } from "../entities/User";
import { removeHTMLNodes, checkURL } from "../utils/StringUtils";
import { VirtualDirectoryController } from "./VirtualDirectoryController";
import { checkVirtualDirectoryAvailability, VirtualDirType } from "../utils/VirtualDirsUtils";
import { AccountHelper } from "../helpers/AccountHelper";
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");
if(!checkVirtualDirectoryAvailability(virtualDirectory, h.getUserId(), VirtualDirType.USER))
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 16:16:30 +00:00
}