From a258f949c1626f7b8ef5af5658e5a7b7c29140a9 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 26 Dec 2019 14:26:46 +0100 Subject: [PATCH] Can get the list of members of a group --- src/controllers/GroupsController.ts | 30 ++++++++++++++++++++++++++++- src/controllers/Routes.ts | 2 ++ src/helpers/GroupsHelper.ts | 21 +++++++++++++++++--- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/controllers/GroupsController.ts b/src/controllers/GroupsController.ts index 8c3b066..5af73a7 100644 --- a/src/controllers/GroupsController.ts +++ b/src/controllers/GroupsController.ts @@ -1,7 +1,7 @@ import { RequestHandler } from "../entities/RequestHandler"; import { GroupsHelper, PATH_GROUPS_LOGOS } from "../helpers/GroupsHelper"; import { GroupsAccessLevel, GroupInfo, GroupVisibilityLevel, GroupPostsCreationLevel, GroupRegistrationLevel } from "../entities/Group"; -import { GroupMembershipLevels } from "../entities/GroupMember"; +import { GroupMembershipLevels, GroupMember } from "../entities/GroupMember"; import { time } from "../utils/DateUtils"; import { LikesHelper, LikesType } from "../helpers/LikesHelper"; import { GroupSettings } from "../entities/GroupSettings"; @@ -275,6 +275,20 @@ export class GroupsController { }) } + /** + * Get the entire list of members of the group + * + * @param h Request handler + */ + public static async GetMembers(h: RequestHandler) { + const groupID = await h.postGroupIDWithAccess("id", GroupsAccessLevel.MODERATOR_ACCESS); + + const members = await GroupsHelper.GetListMembers(groupID); + + // Parse the list of members + h.send(members.map((m) => this.GroupMemberToAPI(m))); + } + /** * Turn a GroupInfo object into a valid API object * @@ -313,4 +327,18 @@ export class GroupsController { return data; } + + /** + * Convert a {GroupMember} object into an API entry + * + * @param m Group Member to transform + */ + private static GroupMemberToAPI(m: GroupMember) : Object { + return { + user_id: m.userID, + group_id: m.groupID, + time_create: m.timeCreate, + level: GROUPS_MEMBERSHIP_LEVELS[m.level] + }; + } } \ No newline at end of file diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index a73f897..515f8f3 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -104,4 +104,6 @@ export const Routes : Route[] = [ {path: "/groups/upload_logo", cb: (h) => GroupsController.UploadLogo(h)}, {path: "/groups/delete_logo", cb: (h) => GroupsController.DeleteLogo(h)}, + + {path: "/groups/get_members", cb: (h) => GroupsController.GetMembers(h)}, ] \ No newline at end of file diff --git a/src/helpers/GroupsHelper.ts b/src/helpers/GroupsHelper.ts index d330d8a..2032f85 100644 --- a/src/helpers/GroupsHelper.ts +++ b/src/helpers/GroupsHelper.ts @@ -292,6 +292,21 @@ export class GroupsHelper { return currID < 1 || currID == groupID; } + /** + * Get the entire list of members of a group + * + * @param groupID Target Group ID + */ + public static async GetListMembers(groupID: number) : Promise> { + const members = await DatabaseHelper.Query({ + table: GROUPS_MEMBERS_TABLE, + where: { + groups_id: groupID + } + }); + + return members.map((row) => this.DbToGroupMember(row)); + } /** * Count the number of members of a group @@ -337,12 +352,12 @@ export class GroupsHelper { * @param row Database entry * @returns Generated object */ - private static async DbToGroupMember(row: any) : Promise { + private static DbToGroupMember(row: any) : GroupMember { return new GroupMember({ id: row.id, groupID: row.groups_id, - userID: row.user_id, - timeCreate: row.time_create, + userID: Number(row.user_id), + timeCreate: Number(row.time_create), level: row.level, following: row.following == 1 });