From d362c0faab256494b8244e5e5abcc608116cbb91 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 21 Mar 2020 09:15:21 +0100 Subject: [PATCH] Can get the list of post targets of a user --- src/controllers/PostsController.ts | 16 ++++++++++++ src/controllers/Routes.ts | 2 ++ src/helpers/FriendsHelper.ts | 19 ++++++++++++++ src/helpers/GroupsHelper.ts | 40 ++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+) diff --git a/src/controllers/PostsController.ts b/src/controllers/PostsController.ts index 0e481c4..48acae9 100644 --- a/src/controllers/PostsController.ts +++ b/src/controllers/PostsController.ts @@ -18,6 +18,7 @@ import { pathUserData } from "../utils/UserDataUtils"; import { statSync } from "fs"; import { lookup } from "mime-types"; import { NewSurvey } from "../entities/NewSurvey"; +import { FriendsHelper } from "../helpers/FriendsHelper"; /** * Posts controller @@ -349,6 +350,21 @@ export class PostsController { h.success(); } + /** + * Get the lists of targets where the current user can create posts + * + * @param h Request handler + */ + public static async GetTargets(h: RequestHandler) { + const friends = await FriendsHelper.GetListThatAllowsPostsFromUser(h.getUserId()); + const groups = await GroupsHelper.GetListUserWhereCanCreatePosts(h.getUserId()); + + h.send({ + friends: friends, + groups: groups + }); + } + /** * Send multiple posts to the API * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index cb06747..14c5900 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -200,6 +200,8 @@ export const Routes : Route[] = [ {path: "/posts/delete", cb: (h) => PostsController.DeletePost(h)}, + {path: "/posts/getAvailableTargets", cb: (h) => PostsController.GetTargets(h)}, + // Notifications controller diff --git a/src/helpers/FriendsHelper.ts b/src/helpers/FriendsHelper.ts index 5ff7192..046bf75 100644 --- a/src/helpers/FriendsHelper.ts +++ b/src/helpers/FriendsHelper.ts @@ -44,6 +44,25 @@ export class FriendsHelper { return results.map((r) => this.DbToFriend(r)); } + /** + * Get the list of friends of a given user who allows + * him to create post on their pages + * + * @param userID ID of the user making the request + */ + public static async GetListThatAllowsPostsFromUser(userID: number) : Promise> { + const list = await DatabaseHelper.Query({ + table: FRIENDS_TABLE, + where: { + autoriser_post_page: 1, + ID_amis: userID + }, + fields: ["ID_personne as user_id"] + }); + + return list.map((e) => e.user_id); + } + /** * Get information about a single membership * diff --git a/src/helpers/GroupsHelper.ts b/src/helpers/GroupsHelper.ts index 9a0f7e0..849e222 100644 --- a/src/helpers/GroupsHelper.ts +++ b/src/helpers/GroupsHelper.ts @@ -83,6 +83,46 @@ export class GroupsHelper { return list.map(e => e.groups_id); } + /** + * Get the list of gruops of a user where the users can create + * posts + * + * @param userID The ID of the target user + */ + public static async GetListUserWhereCanCreatePosts(userID: number) : Promise> { + const list = await DatabaseHelper.Query({ + // Members table + table: GROUPS_MEMBERS_TABLE, + tableAlias: "m", + + // Groups liist table + joins: [ + { + table: GROUPS_LIST_TABLE, + tableAlias: "g", + condition: "m.groups_id = g.id" + } + ], + + where: { + user_id: userID + }, + + customWhere: "level = ? OR level = ? OR (level = ? AND posts_level = ?)", + customWhereArgs: [ + GroupMembershipLevels.ADMINISTRATOR.toString(), + GroupMembershipLevels.MODERATOR.toString(), + + GroupMembershipLevels.MEMBER.toString(), + GroupPostsCreationLevel.POSTS_LEVEL_ALL_MEMBERS.toString() + ], + + fields: ["g.id"] + }); + + return list.map((e) => e.id); + } + /** * Get information about a group *