2019-12-13 16:49:58 +00:00
|
|
|
import { RequestHandler } from "../entities/RequestHandler";
|
|
|
|
import { GroupsHelper } from "../helpers/GroupsHelper";
|
2019-12-15 16:37:39 +00:00
|
|
|
import { GroupsAccessLevel, GroupInfo, GroupVisibilityLevel, GroupPostsCreationLevel, GroupRegistrationLevel } from "../entities/Group";
|
|
|
|
import { GroupMembershipLevels } from "../entities/GroupMember";
|
2019-12-24 17:47:55 +00:00
|
|
|
import { time } from "../utils/DateUtils";
|
2019-12-13 16:49:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Groups API controller
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
2019-12-15 16:37:39 +00:00
|
|
|
/**
|
|
|
|
* API groups registration levels
|
|
|
|
*/
|
|
|
|
const GROUPS_REGISTRATION_LEVELS = [];
|
|
|
|
GROUPS_REGISTRATION_LEVELS[GroupRegistrationLevel.OPEN_REGISTRATION] = "open";
|
|
|
|
GROUPS_REGISTRATION_LEVELS[GroupRegistrationLevel.MODERATED_REGISTRATION] = "moderated";
|
|
|
|
GROUPS_REGISTRATION_LEVELS[GroupRegistrationLevel.CLOSED_REGISTRATION] = "closed";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* API groups membership levels
|
|
|
|
*/
|
|
|
|
const GROUPS_MEMBERSHIP_LEVELS = [];
|
|
|
|
GROUPS_MEMBERSHIP_LEVELS[GroupMembershipLevels.ADMINISTRATOR] = "administrator";
|
|
|
|
GROUPS_MEMBERSHIP_LEVELS[GroupMembershipLevels.MODERATOR] = "moderator";
|
|
|
|
GROUPS_MEMBERSHIP_LEVELS[GroupMembershipLevels.MEMBER] = "member";
|
|
|
|
GROUPS_MEMBERSHIP_LEVELS[GroupMembershipLevels.INVITED] = "invited";
|
|
|
|
GROUPS_MEMBERSHIP_LEVELS[GroupMembershipLevels.PENDING] = "pending";
|
|
|
|
GROUPS_MEMBERSHIP_LEVELS[GroupMembershipLevels.VISITOR] = "visitor";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* API groups visibility levels
|
|
|
|
*/
|
|
|
|
const GROUPS_VISIBILITY_LEVELS = [];
|
|
|
|
GROUPS_VISIBILITY_LEVELS[GroupVisibilityLevel.OPEN_GROUP] = "open";
|
|
|
|
GROUPS_VISIBILITY_LEVELS[GroupVisibilityLevel.PRIVATE_GROUP] = "private";
|
|
|
|
GROUPS_VISIBILITY_LEVELS[GroupVisibilityLevel.SECRETE_GROUP] = "secrete";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* API posts creation levels
|
|
|
|
*/
|
|
|
|
const GROUPS_POSTS_LEVELS = [];
|
|
|
|
GROUPS_POSTS_LEVELS[GroupPostsCreationLevel.POSTS_LEVEL_MODERATORS] = "moderators";
|
|
|
|
GROUPS_POSTS_LEVELS[GroupPostsCreationLevel.POSTS_LEVEL_ALL_MEMBERS] = "members";
|
|
|
|
|
|
|
|
|
2019-12-13 16:49:58 +00:00
|
|
|
export class GroupsController {
|
|
|
|
|
2019-12-24 17:47:55 +00:00
|
|
|
/**
|
|
|
|
* Create a new group
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async Create(h: RequestHandler) {
|
|
|
|
const name = h.postString("name", 3);
|
|
|
|
|
|
|
|
const groupID = await GroupsHelper.Create({
|
|
|
|
name: name,
|
|
|
|
userID: h.getUserId(),
|
|
|
|
timeCreate: time()
|
|
|
|
});
|
|
|
|
|
|
|
|
h.send({
|
|
|
|
success: "The group has been successfully created!",
|
|
|
|
id: groupID
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:49:58 +00:00
|
|
|
/**
|
|
|
|
* Get the list of groups of the user
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async GetListUser(h: RequestHandler) {
|
|
|
|
h.send(await GroupsHelper.GetListUser(h.getUserId()));
|
|
|
|
}
|
|
|
|
|
2019-12-13 17:30:08 +00:00
|
|
|
/**
|
|
|
|
* Get information about a single group
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async GetInfoSingle(h: RequestHandler) {
|
|
|
|
const groupID = await h.postGroupIDWithAccess("id", GroupsAccessLevel.LIMITED_ACCESS);
|
2019-12-15 16:37:39 +00:00
|
|
|
const groupInfo = await GroupsHelper.GetInfo(groupID);
|
|
|
|
|
2019-12-15 16:54:44 +00:00
|
|
|
h.send(await this.GroupInfoToAPI(groupInfo, h));
|
2019-12-15 16:37:39 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 17:32:44 +00:00
|
|
|
/**
|
|
|
|
* Get information about multiple users
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async GetInfoMultiple(h: RequestHandler) {
|
|
|
|
const ids = h.postNumbersList("list");
|
|
|
|
|
|
|
|
const result = {};
|
|
|
|
|
|
|
|
for (const id of ids) {
|
|
|
|
|
|
|
|
// Check group existence & user authorization
|
|
|
|
if(!await GroupsHelper.Exists(id)
|
|
|
|
|| await GroupsHelper.GetAccessLevel(id, h.getUserId()) < GroupsAccessLevel.LIMITED_ACCESS)
|
|
|
|
h.error(404, "Group " + id + " not found");
|
|
|
|
|
|
|
|
const group = await GroupsHelper.GetInfo(id);
|
|
|
|
|
|
|
|
result[id] = await this.GroupInfoToAPI(group, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
h.send(result);
|
|
|
|
}
|
|
|
|
|
2019-12-24 18:10:45 +00:00
|
|
|
/**
|
|
|
|
* Get advanced information about a group
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async GetAdvancedInfo(h: RequestHandler) {
|
|
|
|
const groupID = await h.postGroupIDWithAccess("id", GroupsAccessLevel.VIEW_ACCESS);
|
|
|
|
|
|
|
|
const group = await GroupsHelper.GetInfo(groupID);
|
|
|
|
|
2019-12-24 18:15:12 +00:00
|
|
|
h.send(await this.GroupInfoToAPI(group, h, true));
|
2019-12-24 18:10:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-15 16:37:39 +00:00
|
|
|
/**
|
|
|
|
* Turn a GroupInfo object into a valid API object
|
|
|
|
*
|
|
|
|
* @param info Information about the group
|
2019-12-24 18:15:12 +00:00
|
|
|
* @param h Request handler
|
|
|
|
* @param advanced Specify whether advanced information should be returned
|
|
|
|
* in the request or not
|
2019-12-15 16:37:39 +00:00
|
|
|
* @returns Generated object
|
|
|
|
*/
|
2019-12-24 18:15:12 +00:00
|
|
|
private static async GroupInfoToAPI(info: GroupInfo, h: RequestHandler, advanced: boolean = false) : Promise<any> {
|
2019-12-15 16:54:44 +00:00
|
|
|
|
|
|
|
const membership = await GroupsHelper.GetMembershipInfo(info.id, h.getUserId())
|
|
|
|
|
2019-12-24 18:15:12 +00:00
|
|
|
const data = {
|
2019-12-15 16:37:39 +00:00
|
|
|
id: info.id,
|
|
|
|
name: info.name,
|
|
|
|
icon_url: info.logoURL,
|
|
|
|
number_members: info.membersCount,
|
|
|
|
visibility: GROUPS_VISIBILITY_LEVELS[info.visiblity],
|
|
|
|
registration_level: GROUPS_REGISTRATION_LEVELS[info.registrationLevel],
|
|
|
|
posts_level: GROUPS_POSTS_LEVELS[info.postsCreationLevel],
|
2019-12-15 16:54:44 +00:00
|
|
|
virtual_directory: info.virtualDirectory ? info.virtualDirectory : "null",
|
|
|
|
|
|
|
|
membership: GROUPS_MEMBERSHIP_LEVELS[membership ? membership.level : GroupMembershipLevels.VISITOR],
|
|
|
|
following: membership ? membership.following : false
|
2019-12-15 16:37:39 +00:00
|
|
|
}
|
2019-12-24 18:15:12 +00:00
|
|
|
|
|
|
|
if(advanced) {
|
|
|
|
data["time_create"] = info.timeCreate;
|
|
|
|
data["description"] = info.hasDescription ? info.description : "null";
|
|
|
|
data["url"] = info.url ? info.hasURL : "null";
|
|
|
|
|
|
|
|
//TODO : add likes information
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2019-12-13 17:30:08 +00:00
|
|
|
}
|
2019-12-13 16:49:58 +00:00
|
|
|
}
|