1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 13:29:22 +00:00

Can get the list of friends of any user

This commit is contained in:
Pierre HUBERT 2019-12-31 09:54:29 +01:00
parent aac5d02a4f
commit e9bd11e61f
3 changed files with 63 additions and 2 deletions

View File

@ -1,4 +1,7 @@
import { Friend } from "../entities/Friend";
import { RequestHandler } from "../entities/RequestHandler";
import { FriendsHelper } from "../helpers/FriendsHelper";
import { UserHelper } from "../helpers/UserHelper";
/**
* Friends controller
@ -8,17 +11,58 @@ import { Friend } from "../entities/Friend";
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));
}
/**
* Turn a friend object into an API entry
*
* @param friend Friend object to transform
* @param full Set to true to return all information
*/
public static FriendToAPI(friend: Friend) : any {
return {
public static FriendToAPI(friend: Friend, full: boolean = false) : any {
let info: any = {
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;
}
}

View File

@ -9,6 +9,7 @@ import { NotificationsController } from "./NotificationsController";
import { VirtualDirectoryController } from "./VirtualDirectoryController";
import { WebAppControllers } from "./WebAppController";
import { CallsController } from "./CallsController";
import { FriendsController } from "./FriendsController";
/**
* Controllers routes
@ -73,6 +74,12 @@ export const Routes : Route[] = [
{path: "/user/getAdvancedUserInfos", cb: (h) => UserController.GetAdvancedInfo(h), needLogin: false}, // Legacy
// Friends controller
{path: "/friends/getList", cb: (h) => FriendsController.GetList(h)},
{path: "/friends/get_user_list", cb: (h) => FriendsController.GetOtherUserList(h), needLogin: false},
// Conversations controller
{path: "/conversations/create", cb: (h) => ConversationsController.CreateConversation(h)},

View File

@ -137,6 +137,16 @@ export class UserHelper {
return (await this.GetUserInfo(userID)).allowPostsFromFriends;
}
/**
* Convenience function to check whether a friend list is
* public or not
*
* @param userID Target user ID
*/
public static async IsUserFriendListPublic(userID: number) : Promise<boolean> {
return (await this.GetUserInfo(userID)).friendsListPublic;
}
/**
* Check out whether a user can create a post on another user
* page