1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-27 07:49:22 +00:00
comunicapiv2/src/controllers/FriendsController.ts

84 lines
2.0 KiB
TypeScript
Raw Normal View History

2019-12-28 15:52:52 +00:00
import { Friend } from "../entities/Friend";
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 {
/**
* 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));
}
/**
* 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
* @param full Set to true to return all information
2019-12-28 15:52:52 +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
}
if(full) {
info.following = friend.following ? 1 : 0
info.canPostTexts = friend.canPostTexts
}
return info;
2019-12-28 15:52:52 +00:00
}
}