diff --git a/src/controllers/PostsController.ts b/src/controllers/PostsController.ts index 700a321..35752d6 100644 --- a/src/controllers/PostsController.ts +++ b/src/controllers/PostsController.ts @@ -9,6 +9,7 @@ import { SurveyController } from "./SurveyController"; import { LikesHelper, LikesType } from "../helpers/LikesHelper"; import { CommentsHelper } from "../helpers/CommentsHelper"; import { CommentsController } from "./CommentsController"; +import { GroupsAccessLevel } from "../entities/Group"; /** * Posts controller @@ -46,6 +47,31 @@ export class PostsController { const posts = await PostsHelper.GetUserPosts(h.optionnalUserID, userID, startFrom); + await this.SendMultiplePosts(h, posts); + } + + /** + * Get the list of posts of a group + * + * @param h Request handler + */ + public static async GetListGroup(h: RequestHandler) { + const groupID = await h.postGroupIDWithAccess("groupID", GroupsAccessLevel.VIEW_ACCESS); + const startFrom = h.postInt("startFrom", 0); + + const posts = await PostsHelper.GetGroupPosts(h.optionnalUserID, groupID, startFrom); + + await this.SendMultiplePosts(h, posts); + } + + + /** + * Send multiple posts to the API + * + * @param h Request handler + * @param posts The list of post to send + */ + private static async SendMultiplePosts(h: RequestHandler, posts: Array) { let list = []; for (const p of posts) { list.push(await this.PostToAPI(h, p)); diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 2c19b32..167b13f 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -186,6 +186,8 @@ export const Routes : Route[] = [ // Posts controller {path: "/posts/get_user", cb: (h) => PostsController.GetListUser(h), needLogin: false}, + {path: "/posts/get_group", cb: (h) => PostsController.GetListGroup(h), needLogin: false}, + // Notifications controller diff --git a/src/helpers/GroupsHelper.ts b/src/helpers/GroupsHelper.ts index 143a9d5..f9814cf 100644 --- a/src/helpers/GroupsHelper.ts +++ b/src/helpers/GroupsHelper.ts @@ -304,6 +304,11 @@ export class GroupsHelper { * @param userID The ID of target user */ public static async GetMembershipLevel(groupID: number, userID: number) : Promise { + + // If the user is not signed in + if(userID < 1) + return GroupMembershipLevels.VISITOR; + const result = await DatabaseHelper.Query({ table: GROUPS_MEMBERS_TABLE, where: { diff --git a/src/helpers/PostsHelper.ts b/src/helpers/PostsHelper.ts index daef716..19d0e5d 100644 --- a/src/helpers/PostsHelper.ts +++ b/src/helpers/PostsHelper.ts @@ -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> { + + 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 *