From 65d07f0f4a6f0a3843ae816514c27c1dcdca4d42 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 28 Dec 2019 16:52:52 +0100 Subject: [PATCH] Can get all the membership of a user --- src/controllers/FriendsController.ts | 24 +++++++++++++++++ src/controllers/Routes.ts | 6 +++++ src/controllers/WebAppController.ts | 36 +++++++++++++++++++++++++ src/entities/UserMembershipEntry.ts | 17 ++++++++++++ src/helpers/GroupsHelper.ts | 10 +++++++ src/helpers/WebappHelper.ts | 40 ++++++++++++++++++++++++++++ 6 files changed, 133 insertions(+) create mode 100644 src/controllers/FriendsController.ts create mode 100644 src/controllers/WebAppController.ts create mode 100644 src/entities/UserMembershipEntry.ts create mode 100644 src/helpers/WebappHelper.ts diff --git a/src/controllers/FriendsController.ts b/src/controllers/FriendsController.ts new file mode 100644 index 0000000..cf47f65 --- /dev/null +++ b/src/controllers/FriendsController.ts @@ -0,0 +1,24 @@ +import { Friend } from "../entities/Friend"; + +/** + * Friends controller + * + * @author Pierre HUBERT + */ + +export class FriendsController { + + /** + * Turn a friend object into an API entry + * + * @param friend Friend object to transform + */ + public static FriendToAPI(friend: Friend) : any { + return { + ID_friend: friend.friendID, + accepted: friend.accepted, + time_last_activity: friend.lastActivityTime + } + } + +} \ No newline at end of file diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 88dbd42..7408cab 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -7,6 +7,7 @@ import { SearchController } from "./SearchController"; import { GroupsController } from "./GroupsController"; import { NotificationsController } from "./NotificationsController"; import { VirtualDirectoryController } from "./VirtualDirectoryController"; +import { WebAppControllers } from "./WebAppController"; /** * Controllers routes @@ -152,4 +153,9 @@ export const Routes : Route[] = [ {path: "/virtualDirectory/find", cb: (h) => VirtualDirectoryController.Find(h)}, + + + // Web app controller + {path: "/webApp/getMemberships", cb: (h) => WebAppControllers.GetMemberships(h)}, + ] \ No newline at end of file diff --git a/src/controllers/WebAppController.ts b/src/controllers/WebAppController.ts new file mode 100644 index 0000000..83d7805 --- /dev/null +++ b/src/controllers/WebAppController.ts @@ -0,0 +1,36 @@ +import { RequestHandler } from "../entities/RequestHandler"; +import { WebappHelper } from "../helpers/WebappHelper"; +import { UserMembershipType } from "../entities/UserMembershipEntry"; +import { FriendsController } from "./FriendsController"; +import { Friend } from "../entities/Friend"; + + +/** + * Web application controllers + * + * @author Pierre HUBERT + */ +export class WebAppControllers { + + /** + * Get the memberships of the user + * + * @param h Request handler + */ + public static async GetMemberships(h: RequestHandler) { + + const list = await WebappHelper.GetUserMemberships(h.getUserId()); + + h.send(list.map((l) => l.type == UserMembershipType.FRIEND ? { + // In case of friend + type: "friend", + friend: FriendsController.FriendToAPI(l.el) + } : { + // In case of group + type: "group", + id: l.el, + last_activity: l.lastActivity + })); + } + +} \ No newline at end of file diff --git a/src/entities/UserMembershipEntry.ts b/src/entities/UserMembershipEntry.ts new file mode 100644 index 0000000..d82d511 --- /dev/null +++ b/src/entities/UserMembershipEntry.ts @@ -0,0 +1,17 @@ +import { Friend } from "./Friend"; + +/** + * Get information about a membership + * + * @author Pierre HUBERT + */ + +export enum UserMembershipType { GROUP, FRIEND } + +export class UserMembershipEntry { + constructor( + public lastActivity: number, + public el: Friend | number, + public type: UserMembershipType + ) {} +} \ No newline at end of file diff --git a/src/helpers/GroupsHelper.ts b/src/helpers/GroupsHelper.ts index 20ad189..797ee1a 100644 --- a/src/helpers/GroupsHelper.ts +++ b/src/helpers/GroupsHelper.ts @@ -454,6 +454,16 @@ export class GroupsHelper { }); } + /** + * Get the last activity time of a group + * + * @param groupID Target group ID + */ + public static async GetLastActivity(groupID: number) : Promise { + // TODO : implement + return groupID; + } + /** * Turn a database row into a {GroupInfo} object * diff --git a/src/helpers/WebappHelper.ts b/src/helpers/WebappHelper.ts new file mode 100644 index 0000000..3c358e2 --- /dev/null +++ b/src/helpers/WebappHelper.ts @@ -0,0 +1,40 @@ +import { UserMembershipEntry, UserMembershipType } from "../entities/UserMembershipEntry"; +import { FriendsHelper } from "./FriendsHelper"; +import { GroupsHelper } from "./GroupsHelper"; + +/** + * Web applications helper + * + * @author Pierre HUBERT + */ + +export class WebappHelper { + + /** + * Get all the membership of a user + * + * @param userID Target user ID + */ + public static async GetUserMemberships(userID: number) : Promise> { + + // Get the list of friends of the user + const friendsList = await FriendsHelper.GetList(userID); + const groupsList = await GroupsHelper.GetListUser(userID); + + const list : Array = []; + + // Add friends to the list + friendsList.forEach((f) => list.push(new UserMembershipEntry( + f.lastActivityTime, f, UserMembershipType.FRIEND))); + + // Add groups to the list + for(const g of groupsList) list.push(new UserMembershipEntry( + await GroupsHelper.GetLastActivity(g), g, UserMembershipType.GROUP)); + + // Sort entries by latest activities + list.sort((a, b) => b.lastActivity - a.lastActivity); + + return list; + } + +} \ No newline at end of file