From 25159e37185f7af0aa2cc1d98d80926d48f5f525 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 4 Jan 2020 11:30:33 +0100 Subject: [PATCH] Get the latest posts of the user --- src/controllers/PostsController.ts | 14 ++++++ src/controllers/Routes.ts | 2 + src/helpers/FriendsHelper.ts | 4 +- src/helpers/GroupsHelper.ts | 6 ++- src/helpers/PostsHelper.ts | 72 ++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 3 deletions(-) diff --git a/src/controllers/PostsController.ts b/src/controllers/PostsController.ts index 35752d6..0fe50dc 100644 --- a/src/controllers/PostsController.ts +++ b/src/controllers/PostsController.ts @@ -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 diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 167b13f..8752a40 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -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 diff --git a/src/helpers/FriendsHelper.ts b/src/helpers/FriendsHelper.ts index d4681c0..5ff7192 100644 --- a/src/helpers/FriendsHelper.ts +++ b/src/helpers/FriendsHelper.ts @@ -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> { + public static async GetList(userID: number, onlyAccepted = false) : Promise> { const results = await DatabaseHelper.Query({ table: FRIENDS_TABLE + " f", where: { ID_personne: userID, }, + customWhere: onlyAccepted ? "actif = 1" : "", joins: [ { table: USER_TABLE + " u", diff --git a/src/helpers/GroupsHelper.ts b/src/helpers/GroupsHelper.ts index f9814cf..82f9b82 100644 --- a/src/helpers/GroupsHelper.ts +++ b/src/helpers/GroupsHelper.ts @@ -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> { + public static async GetListUser(userID: number, onlyFollowed: boolean = false) : Promise> { const list = await DatabaseHelper.Query({ table: GROUPS_MEMBERS_TABLE, where: { - user_id: userID + user_id: userID, }, + customWhere: onlyFollowed ? "following = 1" : "", fields: ["groups_id"] }); diff --git a/src/helpers/PostsHelper.ts b/src/helpers/PostsHelper.ts index dacfef9..352ed96 100644 --- a/src/helpers/PostsHelper.ts +++ b/src/helpers/PostsHelper.ts @@ -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 *