From fef321f0ff3d322da587ca7b26d43de936f24e50 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 30 Nov 2019 17:13:36 +0100 Subject: [PATCH] Can send text messages --- src/controllers/ConversationsController.ts | 23 ++++++++++ src/controllers/Routes.ts | 2 + src/entities/ConversationMessage.ts | 10 +++-- src/helpers/ConversationsHelper.ts | 49 +++++++++++++++++++++- src/helpers/DatabaseHelper.ts | 18 +++++++- 5 files changed, 97 insertions(+), 5 deletions(-) diff --git a/src/controllers/ConversationsController.ts b/src/controllers/ConversationsController.ts index 73b4a5b..b98d597 100644 --- a/src/controllers/ConversationsController.ts +++ b/src/controllers/ConversationsController.ts @@ -202,6 +202,29 @@ export class ConversationsController { h.send(h.send(messages.map(e => this.ConversationMessageToAPI(e)))); } + /** + * Send a new message + * + * @param h Request handler + */ + public static async SendMessage(h: RequestHandler) { + const convID = await this.GetPostConversationId("conversationID", h); + const message = removeHTMLNodes(h.postString("message", 0)); + + if(message.length < 3) + h.error(401, "Message is empty!"); + + // Send message + await ConversationsHelper.SendMessage({ + userID: h.getUserId(), + convID: convID, + message: message, + imagePath: "" + }); + + h.success("Conversation message was sent!"); + } + /** * 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 47f2200..dd68007 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -60,6 +60,8 @@ export const Routes : Route[] = [ {path: "/conversations/refresh", cb: (h) => ConversationsController.RefreshList(h)}, {path: "/conversations/refresh_single", cb: (h) => ConversationsController.RefreshSingleConversation(h)}, + + {path: "/conversations/sendMessage", cb: (h) => ConversationsController.SendMessage(h)}, // Search controller diff --git a/src/entities/ConversationMessage.ts b/src/entities/ConversationMessage.ts index f357df0..05f9cd0 100644 --- a/src/entities/ConversationMessage.ts +++ b/src/entities/ConversationMessage.ts @@ -6,15 +6,19 @@ import { pathUserData } from "../utils/UserDataUtils"; * @author Pierre HUBERT */ -export interface ConversationMessageInfo { - id: number, +// Used directly when creating a new message +export interface BaseConversationMessage { convID: number, userID: number, - timeSent: number, imagePath: string, message: string } +export interface ConversationMessageInfo extends BaseConversationMessage { + id: number, + timeSent: number, +} + export class ConversationMessage implements ConversationMessageInfo { public id: number; public convID: number; diff --git a/src/helpers/ConversationsHelper.ts b/src/helpers/ConversationsHelper.ts index f4f12d8..83b06cc 100644 --- a/src/helpers/ConversationsHelper.ts +++ b/src/helpers/ConversationsHelper.ts @@ -1,7 +1,7 @@ import { Conversation, BaseConversation } from "../entities/Conversation"; import { DatabaseHelper } from "./DatabaseHelper"; import { time } from "../utils/DateUtils"; -import { ConversationMessage } from "../entities/ConversationMessage"; +import { ConversationMessage, BaseConversationMessage } from "../entities/ConversationMessage"; /** * Conversations helper @@ -295,6 +295,53 @@ export class ConversationsHelper { }); } + /** + * Insert a new message into the database + * + * @param message The message to insert + */ + public static async SendMessage(message: BaseConversationMessage) { + + const t = time(); + + // Insert the message in the database + await DatabaseHelper.InsertRow( + MESSAGES_TABLE, + { + conv_id: message.convID, + user_id: message.userID, + time_insert: t, + message: message.message, + image_path: message.imagePath + } + ); + + // Update the last activity of the conversation + await DatabaseHelper.UpdateRows({ + table: LIST_TABLE, + where: { + id: message.convID + }, + set: { + last_active: t, + } + }); + + // Mark all the user of the conversations as unread, except current user + await DatabaseHelper.UpdateRows({ + table: USERS_TABLE, + where: { + conv_id: message.convID + }, + customWhere: "user_id != ?", + customWhereArgs: [message.userID.toString()], + set: { + saw_last_message: 0 + } + }); + + } + /** * Get the list of members of a conversation * diff --git a/src/helpers/DatabaseHelper.ts b/src/helpers/DatabaseHelper.ts index f091b7e..d7d37aa 100644 --- a/src/helpers/DatabaseHelper.ts +++ b/src/helpers/DatabaseHelper.ts @@ -26,6 +26,8 @@ export interface QueryInformation { export interface UpdateInformation { table: string, set: Object, + customWhere ?: string, + customWhereArgs ?: Array, where ?: Object, } @@ -227,8 +229,22 @@ export class DatabaseHelper { } } } - else + + // Security : block unconditionned updates + else if(!info.customWhere) throw Error("Error : Updates without conditions are blocked for security!"); + + // Process custom conditions + if(info.customWhere) { + + if(info.where) + sql += " AND (" + info.customWhere + ") "; + else + sql += " WHERE " + info.customWhere + " "; + + if(info.customWhereArgs) + info.customWhereArgs.forEach(e => args.push(e)); + } // Execute request return await new Promise((resolve, reject) => {