1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 13:29:22 +00:00

Can get the posts of a group

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

View File

@ -9,6 +9,7 @@ import { SurveyController } from "./SurveyController";
import { LikesHelper, LikesType } from "../helpers/LikesHelper"; import { LikesHelper, LikesType } from "../helpers/LikesHelper";
import { CommentsHelper } from "../helpers/CommentsHelper"; import { CommentsHelper } from "../helpers/CommentsHelper";
import { CommentsController } from "./CommentsController"; import { CommentsController } from "./CommentsController";
import { GroupsAccessLevel } from "../entities/Group";
/** /**
* Posts controller * Posts controller
@ -46,6 +47,31 @@ export class PostsController {
const posts = await PostsHelper.GetUserPosts(h.optionnalUserID, userID, startFrom); 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<Post>) {
let list = []; let list = [];
for (const p of posts) { for (const p of posts) {
list.push(await this.PostToAPI(h, p)); list.push(await this.PostToAPI(h, p));

View File

@ -186,6 +186,8 @@ export const Routes : Route[] = [
// Posts controller // Posts controller
{path: "/posts/get_user", cb: (h) => PostsController.GetListUser(h), needLogin: false}, {path: "/posts/get_user", cb: (h) => PostsController.GetListUser(h), needLogin: false},
{path: "/posts/get_group", cb: (h) => PostsController.GetListGroup(h), needLogin: false},
// Notifications controller // Notifications controller

View File

@ -304,6 +304,11 @@ export class GroupsHelper {
* @param userID The ID of target user * @param userID The ID of target user
*/ */
public static async GetMembershipLevel(groupID: number, userID: number) : Promise<GroupMembershipLevels> { 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({ const result = await DatabaseHelper.Query({
table: GROUPS_MEMBERS_TABLE, table: GROUPS_MEMBERS_TABLE,
where: { where: {

View File

@ -108,6 +108,56 @@ export class PostsHelper {
return entries.map((r) => this.DBToPost(r)); 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 * Get the access level of a user over a post
* *