From 4406b8c88239cbf4de5e185857c10b8d28b26d65 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 30 Nov 2019 12:24:19 +0100 Subject: [PATCH] Can update following state of a conversation --- src/controllers/ConversationsController.ts | 22 ++++++++ src/controllers/Routes.ts | 1 + src/entities/RequestHandler.ts | 11 +++- src/helpers/ConversationsHelper.ts | 20 +++++++ src/helpers/DatabaseHelper.ts | 66 ++++++++++++++++++++++ 5 files changed, 119 insertions(+), 1 deletion(-) diff --git a/src/controllers/ConversationsController.ts b/src/controllers/ConversationsController.ts index b9fe686..5626a6c 100644 --- a/src/controllers/ConversationsController.ts +++ b/src/controllers/ConversationsController.ts @@ -72,6 +72,28 @@ export class ConversationsController { handler.send(this.ConversationToAPI(conv)); } + /** + * Update conversation settings + * + * @param handler Request handler + */ + public static async UpdateSettings(handler: RequestHandler) : Promise { + const convID = await this.GetPostConversationId("conversationID", handler); + + // Update following state, if required + if(handler.hasPostParameter("following")) { + await ConversationsHelper.SetFollowing( + handler.getUserId(), + convID, + handler.postBool("following") + ); + } + + // TODO : update moderation settings + + handler.success("Conversation information successfully updated!"); + } + /** * Get and return safely a conversation ID specified in a $_POST Request * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 0b503bd..0a58014 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -55,6 +55,7 @@ export const Routes : Route[] = [ {path: "/conversations/getInfoOne", cb: (h) => ConversationsController.GetInfoSingle(h)}, {path: "/conversations/getInfosOne", cb: (h) => ConversationsController.GetInfoSingle(h)}, // Legacy + {path: "/conversations/updateSettings", cb: (h) => ConversationsController.UpdateSettings(h)}, // Search controller {path: "/search/user", cb: (h) => SearchController.SearchUser(h)}, diff --git a/src/entities/RequestHandler.ts b/src/entities/RequestHandler.ts index a04d564..800adb8 100644 --- a/src/entities/RequestHandler.ts +++ b/src/entities/RequestHandler.ts @@ -52,6 +52,15 @@ export class RequestHandler { return param; } + /** + * Check out whether a post parameter is present into the request or not + * + * @param name The name of the post field to check + */ + public hasPostParameter(name: string) : boolean { + return this.getPostParam(name) != undefined; + } + /** * Get an email address included in a post request * @@ -233,7 +242,7 @@ export class RequestHandler { this.responseSent = true; if(should_throw) - throw Error("Could not complete request! ("+ message +")"); + throw Error("Could not complete request! ("+ message +")"); } /** diff --git a/src/helpers/ConversationsHelper.ts b/src/helpers/ConversationsHelper.ts index 5db04b2..3136e11 100644 --- a/src/helpers/ConversationsHelper.ts +++ b/src/helpers/ConversationsHelper.ts @@ -146,6 +146,26 @@ export class ConversationsHelper { }) == 1; } + /** + * Update following state of the conversation + * + * @param userID User to update + * @param convID Target conversation ID + * @param following New status + */ + public static async SetFollowing(userID: number, convID: number, following: boolean) { + await DatabaseHelper.UpdateRows({ + table: USERS_TABLE, + set: { + "following": following ? 1 : 0 + }, + where: { + "conv_id": convID, + "user_id": userID + } + }); + } + /** * Get the list of members of a conversation * diff --git a/src/helpers/DatabaseHelper.ts b/src/helpers/DatabaseHelper.ts index 637af6b..f091b7e 100644 --- a/src/helpers/DatabaseHelper.ts +++ b/src/helpers/DatabaseHelper.ts @@ -23,6 +23,12 @@ export interface QueryInformation { limit ?: number, } +export interface UpdateInformation { + table: string, + set: Object, + where ?: Object, +} + export interface CountQueryInformation { table: string, where ?: Object @@ -177,6 +183,66 @@ export class DatabaseHelper { }); } + /** + * Perform update on the database + * + * @param info Information about the request + * @returns The number of affected rows + */ + static async UpdateRows(info : UpdateInformation) : Promise { + let sql = "UPDATE " + info.table + " SET "; + let args = []; + + // Process updates + let isFirst = true; + for (const key in info.set) { + if (info.set.hasOwnProperty(key)) { + const value = info.set[key]; + + if(!isFirst) + sql += ", "; + else + isFirst = false; + + sql += key + " = ? " + args.push(value); + } + } + + // Process conditions + isFirst = true; + if(info.where) { + sql += " WHERE "; + for (const key in info.where) { + if (info.where.hasOwnProperty(key)) { + const value = info.where[key]; + + if(!isFirst) + sql += " AND "; + else + isFirst = false; + + sql += key + " = ? " + args.push(value); + } + } + } + else + throw Error("Error : Updates without conditions are blocked for security!"); + + // Execute request + return await new Promise((resolve, reject) => { + this.connection.query(sql, args, (err, results, f) => { + if(err){ + reject(err); + return; + } + + resolve(results.affectedRows); + }) + }); + } + /** * Delete entries from a table *