From dc79c42949856710987b59d8ba68c27c0981457b Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 26 Mar 2020 13:44:57 +0100 Subject: [PATCH] Export information about related users --- src/controllers/AccountController.ts | 12 +++++++--- src/entities/AccountExport.ts | 34 ++++++++++++++++++++++++++++ src/helpers/AccountHelper.ts | 7 +++--- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/controllers/AccountController.ts b/src/controllers/AccountController.ts index 36a87f3..15148fc 100644 --- a/src/controllers/AccountController.ts +++ b/src/controllers/AccountController.ts @@ -231,7 +231,7 @@ export class AccountController { // Query the database const data = await AccountHelper.Export(h.getUserId()); - const out: any = { + const out = { userID: data.userID, // General account information @@ -262,7 +262,10 @@ export class AccountController { conversation_messages: {}, // Friends list - friends_list: data.friendsList.map(f => FriendsController.FriendToAPI(f, false)) + friends_list: data.friendsList.map(f => FriendsController.FriendToAPI(f, false)), + + // Related users info + users_info: new Map(), }; // Fill conversation messages entry @@ -272,7 +275,10 @@ export class AccountController { } - // TODO : continue (additional user info) + // Load related users info + for(const id of await data.getRelatedUsersID()) { + out.users_info[id] = await UserController.UserToAPI(await UserHelper.GetUserInfo(id), h, false); + } h.send(out); diff --git a/src/entities/AccountExport.ts b/src/entities/AccountExport.ts index 7924e09..1ca7f5e 100644 --- a/src/entities/AccountExport.ts +++ b/src/entities/AccountExport.ts @@ -13,6 +13,7 @@ import { Movie } from "./Movie"; import { ConversationMessage } from "./ConversationMessage"; import { Conversation } from "./Conversation"; import { Friend } from "./Friend"; +import { CommentsHelper } from "../helpers/CommentsHelper"; export interface AccountExportBuilder { userID: number; @@ -47,4 +48,37 @@ export class AccountExport implements AccountExportBuilder { this[key] = info[key]; } } + + /** + * Get the ID of all the related users + */ + public async getRelatedUsersID() : Promise> { + const set = new Set(); + + // Own user + set.add(this.userID); + + // Friends + this.friendsList.forEach(f => set.add(f.friendID)) + + // Posts + for(const p of this.postsList) { + set.add(p.userID); + if(p.isUserPage) set.add(p.userPageID); + + // Process post comments + (await CommentsHelper.Get(p.id)).forEach(c => set.add(c.userID)) + } + + // Comments + this.comments.forEach(c => set.add(c.userID)) + + // Conversation members + this.conversations.forEach(c => c.members.forEach(id => set.add(id))) + + // Conversations messages + this.conversationsMessages.forEach(c => c.forEach(m => set.add(m.userID))) + + return set; + } } \ No newline at end of file diff --git a/src/helpers/AccountHelper.ts b/src/helpers/AccountHelper.ts index 528a92f..5f18c14 100644 --- a/src/helpers/AccountHelper.ts +++ b/src/helpers/AccountHelper.ts @@ -419,7 +419,9 @@ export class AccountHelper { conversationsMessages: new Map(), // Friends - friendsList: await FriendsHelper.GetList(userID) + friendsList: await FriendsHelper.GetList(userID), + + // TODO : add groups list }) // Process conversation messages @@ -428,9 +430,6 @@ export class AccountHelper { = await ConversationsHelper.GetAllMessages(conv.id); } - - // TODO : continue - return data; } } \ No newline at end of file