mirror of
				https://gitlab.com/comunic/comunicapiv2
				synced 2025-10-30 17:14:43 +00:00 
			
		
		
		
	Can get the posts of a group
This commit is contained in:
		| @@ -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<Post>) { | ||||
| 		let list = []; | ||||
| 		for (const p of posts) { | ||||
| 			list.push(await this.PostToAPI(h, p)); | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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: { | ||||
|   | ||||
| @@ -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 | ||||
| 	 *  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user