diff --git a/src/controllers/FriendsController.ts b/src/controllers/FriendsController.ts index 61db57b..d2d1033 100644 --- a/src/controllers/FriendsController.ts +++ b/src/controllers/FriendsController.ts @@ -158,7 +158,7 @@ export class FriendsController { * @param h Request handler */ public static async RemoveFriend(h: RequestHandler) { - const friendID = await h.postUserId("friendID"); + const friendID = await h.postFriendId("friendID"); await FriendsHelper.RemoveFriendship(h.getUserId(), friendID); @@ -167,6 +167,20 @@ export class FriendsController { h.success("The friend was removed from the list!"); } + /** + * Update following status of a friendship + * + * @param h Request handler + */ + public static async SetFollowing(h: RequestHandler) { + const friendID = await h.postFriendId("friendID"); + const follow = h.postBool("follow"); + + await FriendsHelper.SetFollowing(h.getUserId(), friendID, follow); + + h.success("Following status upated!"); + } + /** * Turn a friend object into an API entry * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 8d383a1..3bfc6b7 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -91,6 +91,8 @@ export const Routes : Route[] = [ {path: "/friends/remove", cb: (h) => FriendsController.RemoveFriend(h)}, + {path: "/friends/setFollowing", cb: (h) => FriendsController.SetFollowing(h)}, + // Conversations controller {path: "/conversations/create", cb: (h) => ConversationsController.CreateConversation(h)}, diff --git a/src/entities/RequestHandler.ts b/src/entities/RequestHandler.ts index 741a3e8..9ec4bbf 100644 --- a/src/entities/RequestHandler.ts +++ b/src/entities/RequestHandler.ts @@ -10,6 +10,7 @@ import { UserHelper } from "../helpers/UserHelper"; import { GroupsAccessLevel } from "./Group"; import { GroupsHelper } from "../helpers/GroupsHelper"; import { checkVirtualDirectory } from "../utils/VirtualDirsUtils"; +import { FriendsHelper } from "../helpers/FriendsHelper"; /** * Response to a request @@ -209,6 +210,20 @@ export class RequestHandler { return userID; } + /** + * Get the ID of a friend included in a POST request + * + * @param name Name of the POST field + */ + public async postFriendId(name: string) : Promise { + const friendID = await this.postUserId(name); + + if(!await FriendsHelper.AreFriend(this.getUserId(), friendID)) + this.error(401, "You are not friend with this personn!"); + + return friendID; + } + /** * Find user ID based on its email address, included in a POST request * diff --git a/src/helpers/FriendsHelper.ts b/src/helpers/FriendsHelper.ts index d96fdf8..d9a90bd 100644 --- a/src/helpers/FriendsHelper.ts +++ b/src/helpers/FriendsHelper.ts @@ -227,6 +227,26 @@ export class FriendsHelper { }) > 0; } + /** + * Update the following status for a friendship + * + * @param userID The ID of the user updating the status + * @param friendID The ID of the target friend + * @param follow New following status + */ + public static async SetFollowing(userID: number, friendID: number, follow: boolean) { + await DatabaseHelper.UpdateRows({ + table: FRIENDS_TABLE, + where: { + ID_personne: userID, + ID_amis: friendID + }, + set: { + abonnement: follow ? 1 : 0 + } + }) + } + /** * Check whether a user is following a friend or not *