diff --git a/src/controllers/FriendsController.ts b/src/controllers/FriendsController.ts index cf47f65..502d6a0 100644 --- a/src/controllers/FriendsController.ts +++ b/src/controllers/FriendsController.ts @@ -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; } } \ No newline at end of file diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 6c54671..9a06ed6 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -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)}, diff --git a/src/helpers/UserHelper.ts b/src/helpers/UserHelper.ts index 66b6039..517a708 100644 --- a/src/helpers/UserHelper.ts +++ b/src/helpers/UserHelper.ts @@ -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 { + return (await this.GetUserInfo(userID)).friendsListPublic; + } + /** * Check out whether a user can create a post on another user * page