1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2025-06-20 00:25:17 +00:00

Can get the posts of a group

This commit is contained in:
2020-01-04 10:27:54 +01:00
parent ca5dbbd5f6
commit bfd7f4cf7b
4 changed files with 83 additions and 0 deletions

View File

@ -304,6 +304,11 @@ export class GroupsHelper {
* @param userID The ID of target user
*/
public static async GetMembershipLevel(groupID: number, userID: number) : Promise<GroupMembershipLevels> {
// If the user is not signed in
if(userID < 1)
return GroupMembershipLevels.VISITOR;
const result = await DatabaseHelper.Query({
table: GROUPS_MEMBERS_TABLE,
where: {

View File

@ -108,6 +108,56 @@ export class PostsHelper {
return entries.map((r) => this.DBToPost(r));
}
/**
* Get the posts of a group
*
* @param userID The ID of the user making the request
* @param groupID The ID of the target group
* @param startFrom Start point. 0 for none
*/
public static async GetGroupPosts(userID: number, groupID: number,
startFrom: number = 0, limit: number = 10) : Promise<Array<Post>> {
if(limit < 1)
throw new Error("Limit (" + limit +") is invalid!");
// Check membership of the user
const membership = await GroupsHelper.GetMembershipLevel(groupID, userID);
const canSeeAllPosts = membership <= GroupMembershipLevels.MEMBER;
const visibilityLevel = canSeeAllPosts ? PostVisibilityLevel.VISIBILITY_GROUP_MEMBERS : PostVisibilityLevel.VISIBILITY_PUBLIC;
// Prepare request
// === VISIBILITY RESTRICTION ===
let customWhere = "(niveau_visibilite <= ?)";
let customWhereArgs = [visibilityLevel.toString()];
// == /VISIBILITY RESTRICTION ===
// === START POINT ===
if(startFrom != 0) {
customWhere += " AND ID <= ?";
customWhereArgs.push(startFrom.toString());
}
// == /START POINT ===
const results = await DatabaseHelper.Query({
table: TABLE_NAME,
where: {
group_id: groupID
},
customWhere: customWhere,
customWhereArgs: customWhereArgs,
order: "ID DESC",
limit: limit
});
return results.map((r) => this.DBToPost(r));
}
/**
* Get the access level of a user over a post
*