2019-12-28 15:52:52 +00:00
|
|
|
import { Friend } from "../entities/Friend";
|
2019-12-31 08:54:29 +00:00
|
|
|
import { RequestHandler } from "../entities/RequestHandler";
|
|
|
|
import { FriendsHelper } from "../helpers/FriendsHelper";
|
|
|
|
import { UserHelper } from "../helpers/UserHelper";
|
2019-12-28 15:52:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Friends controller
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
export class FriendsController {
|
|
|
|
|
2019-12-31 08:54:29 +00:00
|
|
|
/**
|
|
|
|
* Get the list of friends of a user
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async GetList(h: RequestHandler) {
|
|
|
|
const returnAllInfo = h.postBool("complete", false);
|
|
|
|
const list = await FriendsHelper.GetList(h.getUserId());
|
|
|
|
|
|
|
|
// TODO : update user activity (if allowed)
|
|
|
|
|
|
|
|
h.send(list.map((f) => this.FriendToAPI(f, returnAllInfo)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get another user friends list
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async GetOtherUserList(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!");
|
|
|
|
|
|
|
|
if(!await UserHelper.IsUserFriendListPublic(userID))
|
|
|
|
h.error(401, "The friends list of the user is not public!");
|
|
|
|
|
|
|
|
const friends = await FriendsHelper.GetList(userID);
|
|
|
|
|
|
|
|
h.send(friends.map((f) => f.friendID));
|
|
|
|
}
|
|
|
|
|
2019-12-31 09:08:04 +00:00
|
|
|
/**
|
|
|
|
* Get single friendship information
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async GetSingleFrienshipInfo(h: RequestHandler) {
|
|
|
|
const friendID = await h.postUserId("friendID");
|
|
|
|
|
|
|
|
const info = await FriendsHelper.GetSingleInfo(h.getUserId(), friendID);
|
|
|
|
|
|
|
|
if(info == null)
|
|
|
|
h.error(404, "Requested frienship not found!");
|
|
|
|
|
|
|
|
h.send(this.FriendToAPI(info, true));
|
|
|
|
}
|
|
|
|
|
2019-12-28 15:52:52 +00:00
|
|
|
/**
|
|
|
|
* Turn a friend object into an API entry
|
|
|
|
*
|
|
|
|
* @param friend Friend object to transform
|
2019-12-31 08:54:29 +00:00
|
|
|
* @param full Set to true to return all information
|
2019-12-28 15:52:52 +00:00
|
|
|
*/
|
2019-12-31 08:54:29 +00:00
|
|
|
public static FriendToAPI(friend: Friend, full: boolean = false) : any {
|
|
|
|
let info: any = {
|
2019-12-28 15:52:52 +00:00
|
|
|
ID_friend: friend.friendID,
|
|
|
|
accepted: friend.accepted,
|
|
|
|
time_last_activity: friend.lastActivityTime
|
|
|
|
}
|
2019-12-31 08:54:29 +00:00
|
|
|
|
|
|
|
if(full) {
|
|
|
|
info.following = friend.following ? 1 : 0
|
|
|
|
info.canPostTexts = friend.canPostTexts
|
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
2019-12-28 15:52:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|