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-31 09:33:38 +00:00
|
|
|
/**
|
|
|
|
* Get a friendship status
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async GetStatus(h: RequestHandler) {
|
|
|
|
const friendID = await h.postUserId("friendID");
|
|
|
|
|
|
|
|
let response = {
|
|
|
|
are_friend: false,
|
|
|
|
sent_request: false,
|
|
|
|
received_request: false,
|
|
|
|
following: false
|
|
|
|
}
|
|
|
|
|
|
|
|
response.are_friend = await FriendsHelper.AreFriend(h.getUserId(), friendID);
|
|
|
|
|
|
|
|
if(!response.are_friend) {
|
|
|
|
response.sent_request = await FriendsHelper.SentRequest(h.getUserId(), friendID);
|
|
|
|
response.received_request = await FriendsHelper.SentRequest(friendID, h.getUserId());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
response.following = await FriendsHelper.IsFollowing(h.getUserId(), friendID);
|
|
|
|
|
|
|
|
|
|
|
|
h.send(response);
|
|
|
|
}
|
|
|
|
|
2019-12-31 09:47:55 +00:00
|
|
|
/**
|
|
|
|
* Send a friendship request
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async SendRequest(h: RequestHandler) {
|
|
|
|
const friendID = await h.postUserId("friendID");
|
|
|
|
|
|
|
|
if(friendID == h.getUserId())
|
|
|
|
h.error(401, "You can not become a friend of yourself!");
|
|
|
|
|
|
|
|
if(await FriendsHelper.AreFriend(h.getUserId(), friendID))
|
|
|
|
h.error(401, "You are already friend with this personn!");
|
|
|
|
|
|
|
|
// Check if a request is already in progress
|
|
|
|
if(await FriendsHelper.SentRequest(h.getUserId(), friendID)
|
|
|
|
|| await FriendsHelper.SentRequest(friendID, h.getUserId()))
|
|
|
|
h.error(401, "A friendship request is already in progress!");
|
|
|
|
|
|
|
|
// Send the request
|
|
|
|
await FriendsHelper.SendRequest(h.getUserId(), friendID);
|
|
|
|
|
|
|
|
// TODO : create the notification
|
|
|
|
|
|
|
|
h.success("Send (create) the friendship request");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|