From 0988d2ac4ada717123ed3e0f7d69b5fbd2b2147b Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 9 Apr 2020 08:22:21 +0200 Subject: [PATCH] Returns the list of conversations with the memberships --- src/controllers/WebAppController.ts | 38 ++++++++++++++++++++++------- src/entities/UserMembershipEntry.ts | 5 ++-- src/helpers/WebappHelper.ts | 9 +++++++ 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/controllers/WebAppController.ts b/src/controllers/WebAppController.ts index 83d7805..ab7a98b 100644 --- a/src/controllers/WebAppController.ts +++ b/src/controllers/WebAppController.ts @@ -3,6 +3,8 @@ import { WebappHelper } from "../helpers/WebappHelper"; import { UserMembershipType } from "../entities/UserMembershipEntry"; import { FriendsController } from "./FriendsController"; import { Friend } from "../entities/Friend"; +import { ConversationsController } from "./ConversationsController"; +import { Conversation } from "../entities/Conversation"; /** @@ -21,15 +23,33 @@ export class WebAppControllers { 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 + h.send(list.map((l) => { + switch(l.type) { + + case UserMembershipType.FRIEND: + return { + type: "friend", + friend: FriendsController.FriendToAPI(l.el) + }; + + + case UserMembershipType.GROUP: + return { + type: "group", + id: l.el, + last_activity: l.lastActivity + } + + + case UserMembershipType.CONVERSATION: + return { + type: "conversation", + conv: ConversationsController.ConversationToAPI(l.el) + } + + default: + h.error(500, "Unsupported membership type detected!"); + } })); } diff --git a/src/entities/UserMembershipEntry.ts b/src/entities/UserMembershipEntry.ts index d82d511..1e4fc87 100644 --- a/src/entities/UserMembershipEntry.ts +++ b/src/entities/UserMembershipEntry.ts @@ -1,4 +1,5 @@ import { Friend } from "./Friend"; +import { Conversation } from "./Conversation"; /** * Get information about a membership @@ -6,12 +7,12 @@ import { Friend } from "./Friend"; * @author Pierre HUBERT */ -export enum UserMembershipType { GROUP, FRIEND } +export enum UserMembershipType { GROUP, FRIEND, CONVERSATION } export class UserMembershipEntry { constructor( public lastActivity: number, - public el: Friend | number, + public el: Friend | Conversation | number, public type: UserMembershipType ) {} } \ No newline at end of file diff --git a/src/helpers/WebappHelper.ts b/src/helpers/WebappHelper.ts index f5a0bf6..bcee41e 100644 --- a/src/helpers/WebappHelper.ts +++ b/src/helpers/WebappHelper.ts @@ -1,6 +1,7 @@ import { UserMembershipEntry, UserMembershipType } from "../entities/UserMembershipEntry"; import { FriendsHelper } from "./FriendsHelper"; import { GroupsHelper } from "./GroupsHelper"; +import { ConversationsHelper } from "./ConversationsHelper"; /** * Web applications helper @@ -20,6 +21,7 @@ export class WebappHelper { // Get the list of friends of the user const friendsList = await FriendsHelper.GetList(userID); const groupsList = await GroupsHelper.GetListUser(userID); + const conversationsList = await ConversationsHelper.GetListUser(userID); const list : Array = []; @@ -31,6 +33,13 @@ export class WebappHelper { for(const g of groupsList) list.push(new UserMembershipEntry( await GroupsHelper.GetLastActivity(userID, g), g, UserMembershipType.GROUP)); + // Add conversations to the list + conversationsList.forEach(c => list.push(new UserMembershipEntry( + c.lastActive, + c, + UserMembershipType.CONVERSATION + ))) + // Sort entries by latest activities list.sort((a, b) => b.lastActivity - a.lastActivity);