2019-11-23 14:11:33 +00:00
|
|
|
import { RequestHandler } from "../entities/RequestHandler";
|
|
|
|
import { UserHelper } from "../helpers/UserHelper";
|
|
|
|
import { User, UserPageStatus } from "../entities/User";
|
2019-11-23 15:10:51 +00:00
|
|
|
import { AccountImage, AccountImageVisibilityLevel } from "../entities/AccountImage";
|
2019-11-23 14:11:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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!");
|
|
|
|
|
2019-11-23 15:10:51 +00:00
|
|
|
handler.send(this.UserToAPI(user, handler));
|
2019-11-23 14:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get information about multiple users
|
|
|
|
*/
|
|
|
|
public static async GetMultiple(handler : RequestHandler) {
|
2019-11-23 15:23:36 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2019-11-23 14:11:33 +00:00
|
|
|
|
2019-11-23 15:23:36 +00:00
|
|
|
handler.send(list);
|
2019-11-23 14:11:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 12:38:17 +00:00
|
|
|
/**
|
|
|
|
* Get advanced information about a user
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async GetAdvancedInfo(h: RequestHandler) {
|
|
|
|
const userID = await h.postUserId("userID");
|
|
|
|
|
|
|
|
if(!await UserHelper.CanSeeUserPage(h.optionnalUserID, userID))
|
|
|
|
h.error(401, "You are not allowed to access these information!");
|
|
|
|
|
|
|
|
h.send("Go on");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-23 15:10:51 +00:00
|
|
|
private static UserToAPI(user : User, handler: RequestHandler) : Object {
|
2019-11-23 14:11:33 +00:00
|
|
|
return {
|
|
|
|
"userID": user.id,
|
2019-11-30 13:13:22 +00:00
|
|
|
"firstName": user.firstName,
|
|
|
|
"lastName": user.lastName,
|
2019-11-23 14:11:33 +00:00
|
|
|
"publicPage": user.pageStatus == UserPageStatus.PUBLIC,
|
|
|
|
"openPage": user.pageStatus == UserPageStatus.OPEN,
|
|
|
|
"virtualDirectory": user.virtualDirectory,
|
2019-11-23 15:10:51 +00:00
|
|
|
"accountImage": this.GetAccountImageURL(user.accountImage, handler)
|
2019-11-23 14:11:33 +00:00
|
|
|
};
|
|
|
|
}
|
2019-11-23 15:10:51 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-11-23 14:11:33 +00:00
|
|
|
}
|