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

205 lines
5.2 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-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");
}
2020-01-01 10:52:34 +00:00
/**
* Cancel (remove) a friendship request
*
* @param h Request handler
*/
public static async CancelRequest(h: RequestHandler) {
const friendID = await h.postUserId("friendID");
if(!await FriendsHelper.SentRequest(h.getUserId(), friendID))
h.error(401, "No friendship request was sent to this user!");
await FriendsHelper.RemoveRequest(h.getUserId(), friendID);
// TODO : delete any related notification
h.success("Friendship request removed!");
}
2020-01-01 11:07:26 +00:00
/**
* Respond to a friendship request
*
* @param h Request handler
*/
public static async RespondRequest(h: RequestHandler) {
const friendID = await h.postUserId("friendID");
const accept = h.postBool("accept");
if(!await FriendsHelper.SentRequest(friendID, h.getUserId()))
h.error(401, "No friendship request was sent by this user!");
await FriendsHelper.RespondRequest(h.getUserId(), friendID, accept);
// TODO : create notification
h.success("Response to the friendship request successfully saved!");
}
2020-01-01 11:17:24 +00:00
/**
* Remove a friend from the list
*
* @param h Request handler
*/
public static async RemoveFriend(h: RequestHandler) {
const friendID = await h.postFriendId("friendID");
2020-01-01 11:17:24 +00:00
await FriendsHelper.RemoveFriendship(h.getUserId(), friendID);
// TODO : delete any related notification
h.success("The friend was removed from the list!");
}
/**
* Update following status of a friendship
*
* @param h Request handler
*/
public static async SetFollowing(h: RequestHandler) {
const friendID = await h.postFriendId("friendID");
const follow = h.postBool("follow");
await FriendsHelper.SetFollowing(h.getUserId(), friendID, follow);
h.success("Following status upated!");
}
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
}
}