mirror of
				https://gitlab.com/comunic/comunicapiv2
				synced 2025-11-03 19:14:03 +00:00 
			
		
		
		
	Get the latest posts of the user
This commit is contained in:
		@@ -64,6 +64,20 @@ export class PostsController {
 | 
			
		||||
		await this.SendMultiplePosts(h, posts);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Get the list of latest posts
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param h Request handler
 | 
			
		||||
	 */
 | 
			
		||||
	public static async GetLatest(h: RequestHandler) {
 | 
			
		||||
		const startFrom = h.postInt("startFrom", 0);
 | 
			
		||||
		const includeGroups = h.postBool('include_groups', false);
 | 
			
		||||
 | 
			
		||||
		const posts = await PostsHelper.GetLatest(h.getUserId(), startFrom, 10, includeGroups);
 | 
			
		||||
 | 
			
		||||
		await this.SendMultiplePosts(h, posts);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Send multiple posts to the API
 | 
			
		||||
 
 | 
			
		||||
@@ -188,6 +188,8 @@ export const Routes : Route[] = [
 | 
			
		||||
 | 
			
		||||
	{path: "/posts/get_group", cb: (h) => PostsController.GetListGroup(h), needLogin: false},
 | 
			
		||||
 | 
			
		||||
	{path: "/posts/get_latest", cb: (h) => PostsController.GetLatest(h)},
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	// Notifications controller
 | 
			
		||||
 
 | 
			
		||||
@@ -16,13 +16,15 @@ export class FriendsHelper {
 | 
			
		||||
	 * Get the list of friends of the user
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param userID The ID of the user
 | 
			
		||||
	 * @param onlyAccepted Return only accepted friendship
 | 
			
		||||
	 */
 | 
			
		||||
	public static async GetList(userID: number) : Promise<Array<Friend>> {
 | 
			
		||||
	public static async GetList(userID: number, onlyAccepted = false) : Promise<Array<Friend>> {
 | 
			
		||||
		const results = await DatabaseHelper.Query({
 | 
			
		||||
			table: FRIENDS_TABLE + " f",
 | 
			
		||||
			where: {
 | 
			
		||||
				ID_personne: userID,
 | 
			
		||||
			},
 | 
			
		||||
			customWhere: onlyAccepted ? "actif = 1" : "",
 | 
			
		||||
			joins: [
 | 
			
		||||
				{
 | 
			
		||||
					table: USER_TABLE + " u",
 | 
			
		||||
 
 | 
			
		||||
@@ -68,13 +68,15 @@ export class GroupsHelper {
 | 
			
		||||
	 * Get the list of groups of a user
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param userID Target user ID
 | 
			
		||||
	 * @param onlyFollowed Include only the groups the user follow
 | 
			
		||||
	 */
 | 
			
		||||
	public static async GetListUser(userID: number) : Promise<Array<number>> {
 | 
			
		||||
	public static async GetListUser(userID: number, onlyFollowed: boolean = false) : Promise<Array<number>> {
 | 
			
		||||
		const list = await DatabaseHelper.Query({
 | 
			
		||||
			table: GROUPS_MEMBERS_TABLE,
 | 
			
		||||
			where: {
 | 
			
		||||
				user_id: userID
 | 
			
		||||
				user_id: userID,
 | 
			
		||||
			},
 | 
			
		||||
			customWhere: onlyFollowed ? "following = 1" : "",
 | 
			
		||||
			fields: ["groups_id"]
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -158,6 +158,78 @@ export class PostsHelper {
 | 
			
		||||
		return results.map((r) => this.DBToPost(r));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Get the latest posts of a user
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param userID The ID of the user making the request
 | 
			
		||||
	 * @param startFrom Startpoint for the research
 | 
			
		||||
	 * @param limit Limit of the research (default: 10)
 | 
			
		||||
	 * @param includeGroups Specify whether groups can be selected too or not
 | 
			
		||||
	 */
 | 
			
		||||
	public static async GetLatest(userID: number, startFrom: number = 10,
 | 
			
		||||
		limit: number = 10, includeGroups: boolean = false) {
 | 
			
		||||
		
 | 
			
		||||
		if(limit < 1)
 | 
			
		||||
			throw new Error("Limit of the query must be greater than 0!");
 | 
			
		||||
		
 | 
			
		||||
		const visibilityLevel = PostVisibilityLevel.VISIBILITY_FRIENDS;
 | 
			
		||||
 | 
			
		||||
		// Get the list of friends of the user
 | 
			
		||||
		const friendsList = await FriendsHelper.GetList(userID, true);
 | 
			
		||||
 | 
			
		||||
		// Prepare the request on the database
 | 
			
		||||
		
 | 
			
		||||
		// === Membership condition === 
 | 
			
		||||
		let customWhere = "(";
 | 
			
		||||
 | 
			
		||||
		// === FRIENDS POSTS ===
 | 
			
		||||
		customWhere += "(group_id = 0 AND niveau_visibilite <= ? AND (ID_personne = ?";
 | 
			
		||||
		let customWhereArgs = [visibilityLevel.toString(), userID.toString()];
 | 
			
		||||
 | 
			
		||||
		friendsList.forEach((f) => {
 | 
			
		||||
			customWhere += " OR ID_personne = ?";
 | 
			
		||||
			customWhereArgs.push(f.friendID.toString());
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		customWhere += "))"
 | 
			
		||||
		// === FRIENDS POSTS ===
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		// === GROUPS POSTS ===
 | 
			
		||||
		if(includeGroups) {
 | 
			
		||||
 | 
			
		||||
			const groups = await GroupsHelper.GetListUser(userID, true);
 | 
			
		||||
 | 
			
		||||
			groups.forEach((groupID) => {
 | 
			
		||||
				customWhere += " OR group_id = ? ";
 | 
			
		||||
				customWhereArgs.push(groupID.toString());
 | 
			
		||||
			})
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
		// == /GROUPS POSTS ===
 | 
			
		||||
 | 
			
		||||
		customWhere += ")";
 | 
			
		||||
		// === /Membership condition ===
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		// === START POINT ===
 | 
			
		||||
		if(startFrom > 0) {
 | 
			
		||||
			customWhere += "AND ID <= ?";
 | 
			
		||||
			customWhereArgs.push(startFrom.toString());
 | 
			
		||||
		}
 | 
			
		||||
		// == /START POINT ===
 | 
			
		||||
 | 
			
		||||
		const results = await DatabaseHelper.Query({
 | 
			
		||||
			table: TABLE_NAME,
 | 
			
		||||
			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