diff --git a/src/controllers/GroupsController.ts b/src/controllers/GroupsController.ts index 1c8d225..80ed1ca 100644 --- a/src/controllers/GroupsController.ts +++ b/src/controllers/GroupsController.ts @@ -66,7 +66,7 @@ export class GroupsController { const groupID = await h.postGroupIDWithAccess("id", GroupsAccessLevel.LIMITED_ACCESS); const groupInfo = await GroupsHelper.GetInfo(groupID); - h.send(this.GroupInfoToAPI(groupInfo)); + h.send(await this.GroupInfoToAPI(groupInfo, h)); } /** @@ -75,7 +75,10 @@ export class GroupsController { * @param info Information about the group * @returns Generated object */ - private static GroupInfoToAPI(info: GroupInfo) : any { + private static async GroupInfoToAPI(info: GroupInfo, h: RequestHandler) : Promise { + + const membership = await GroupsHelper.GetMembershipInfo(info.id, h.getUserId()) + return { id: info.id, name: info.name, @@ -84,7 +87,10 @@ export class GroupsController { visibility: GROUPS_VISIBILITY_LEVELS[info.visiblity], registration_level: GROUPS_REGISTRATION_LEVELS[info.registrationLevel], posts_level: GROUPS_POSTS_LEVELS[info.postsCreationLevel], - virtual_directory: info.virtualDirectory, + virtual_directory: info.virtualDirectory ? info.virtualDirectory : "null", + + membership: GROUPS_MEMBERSHIP_LEVELS[membership ? membership.level : GroupMembershipLevels.VISITOR], + following: membership ? membership.following : false } } } \ No newline at end of file diff --git a/src/entities/GroupMember.ts b/src/entities/GroupMember.ts index 582ef3e..06e2616 100644 --- a/src/entities/GroupMember.ts +++ b/src/entities/GroupMember.ts @@ -11,4 +11,29 @@ export enum GroupMembershipLevels { INVITED = 3, PENDING = 4, //When the group membership has not been approved yet VISITOR = 5, //Simple visitor +} + +export interface GroupMemberConstructor { + id: number, + userID: number, + groupID: number, + timeCreate: number, + level: GroupMembershipLevels, + following: boolean +} + +export class GroupMember implements GroupMemberConstructor { + id: number; userID: number; + groupID: number; + timeCreate: number; + level: GroupMembershipLevels; + following: boolean; + + constructor(info: GroupMemberConstructor) { + for (const key in info) { + if (info.hasOwnProperty(key)) { + this[key] = info[key]; + } + } + } } \ No newline at end of file diff --git a/src/helpers/GroupsHelper.ts b/src/helpers/GroupsHelper.ts index 0818dbb..8587794 100644 --- a/src/helpers/GroupsHelper.ts +++ b/src/helpers/GroupsHelper.ts @@ -1,6 +1,6 @@ import { DatabaseHelper } from "./DatabaseHelper"; import { GroupsAccessLevel, GroupVisibilityLevel, GroupInfo } from "../entities/Group"; -import { GroupMembershipLevels } from "../entities/GroupMember"; +import { GroupMembershipLevels, GroupMember } from "../entities/GroupMember"; /** * Groups helper @@ -148,6 +148,29 @@ export class GroupsHelper { return GroupsAccessLevel.NO_ACCESS; } + /** + * Get a user membership information + * + * @param groupID Target group ID + * @param userID Target user ID + * @returns Membership info / null if none found + */ + public static async GetMembershipInfo(groupID: number, userID: number): Promise { + const data = await DatabaseHelper.QueryRow({ + table: GROUPS_MEMBERS_TABLE, + where: { + groups_id: groupID, + user_id: userID + } + }); + + if(data == null) + return null; + + return this.DbToGroupMember(data); + } + + /** * Count the number of members of a group * @@ -182,4 +205,21 @@ export class GroupsHelper { virtualDirectory: (row.virtual_directory != null && row.virtual_directory && row.virtual_directory != "null" ? row.virtual_directory : undefined) }); } + + /** + * Turn a database entry into a group membership information object + * + * @param row Database entry + * @returns Generated object + */ + private static async DbToGroupMember(row: any) : Promise { + return new GroupMember({ + id: row.id, + groupID: row.groups_id, + userID: row.user_id, + timeCreate: row.time_create, + level: row.level, + following: row.following == 1 + }); + } } \ No newline at end of file