1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-12-28 06:28:51 +00:00

Add membership information when getting group information

This commit is contained in:
Pierre HUBERT 2019-12-15 17:54:44 +01:00
parent 76b0d9605c
commit 415bd461cc
3 changed files with 75 additions and 4 deletions

View File

@ -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<any> {
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
}
}
}

View File

@ -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];
}
}
}
}

View File

@ -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<GroupMember> {
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<GroupMember> {
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
});
}
}