import { RequestHandler } from "../entities/RequestHandler"; import { UserHelper } from "../helpers/UserHelper"; import { User, UserPageStatus } from "../entities/User"; import { AccountImage, AccountImageVisibilityLevel } from "../entities/AccountImage"; import { fixEncoding } from "../utils/StringUtils"; /** * User information controller * * @author Pierre HUBERT */ export class UserController { /** * Get information about a single user */ public static async GetSingle(handler : RequestHandler) { const userID = handler.postInt("userID"); const user = await UserHelper.GetUserInfo(userID); if(!user) handler.error(404, "Could not get user data!"); handler.send(this.UserToAPI(user, handler)); } /** * Get information about multiple users */ public static async GetMultiple(handler : RequestHandler) { let list = {}; const IDs = handler.postNumbersList("usersID"); for (const id of IDs) { if(list.hasOwnProperty(id)) continue; const user = await UserHelper.GetUserInfo(id); if(!user) handler.error(404, "One user was not found!"); list[id] = this.UserToAPI(user, handler); } handler.send(list); } private static UserToAPI(user : User, handler: RequestHandler) : Object { return { "userID": user.id, "firstName": fixEncoding(user.firstName), "lastName": fixEncoding(user.lastName), "publicPage": user.pageStatus == UserPageStatus.PUBLIC, "openPage": user.pageStatus == UserPageStatus.OPEN, "virtualDirectory": user.virtualDirectory, "accountImage": this.GetAccountImageURL(user.accountImage, handler) }; } private static GetAccountImageURL(image : AccountImage, handler: RequestHandler) { if(image.level == AccountImageVisibilityLevel.EVERYONE || (handler.signedIn && handler.getUserId() == image.userID)) return image.url; if(image.level == AccountImageVisibilityLevel.COMUNIC_USERS) { if(handler.signedIn) return image.url; else return AccountImage.errorURL; } // TODO : implement frienship support console.error("ERR: Can not check friends for now (for account image)!"); return AccountImage.errorURL; } }