From a1194312de17bf29d81ac51c3d66122ba69d00a0 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 1 Apr 2020 11:18:38 +0200 Subject: [PATCH] Send an event when a conversation message is created --- src/helpers/ConversationsHelper.ts | 17 ++++++++++++++++- src/helpers/EventsHelper.ts | 12 +++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/helpers/ConversationsHelper.ts b/src/helpers/ConversationsHelper.ts index fc66bcd..7c7994d 100644 --- a/src/helpers/ConversationsHelper.ts +++ b/src/helpers/ConversationsHelper.ts @@ -437,7 +437,7 @@ export class ConversationsHelper { const t = time(); // Insert the message in the database - await DatabaseHelper.InsertRow( + const id = await DatabaseHelper.InsertRow( MESSAGES_TABLE, { conv_id: message.convID, @@ -448,6 +448,16 @@ export class ConversationsHelper { } ); + // Generate full message info + const newMessage = new ConversationMessage({ + id: id, + timeSent: t, + userID: message.userID, + convID: message.convID, + message: message.message, + imagePath: message.imagePath + }); + // Update the last activity of the conversation await DatabaseHelper.UpdateRows({ table: LIST_TABLE, @@ -489,6 +499,11 @@ export class ConversationsHelper { await EventsHelper.Emit("updated_number_unread_conversations", { usersID: listToNotify }) + + // Notify about newly created message + await EventsHelper.Emit("sent_conversation_message", { + msg: newMessage + }) } /** diff --git a/src/helpers/EventsHelper.ts b/src/helpers/EventsHelper.ts index 965d61b..ee0c555 100644 --- a/src/helpers/EventsHelper.ts +++ b/src/helpers/EventsHelper.ts @@ -1,5 +1,5 @@ -import { EventEmitter } from "events"; import { APIClient } from "../entities/APIClient"; +import { ConversationMessage } from "../entities/ConversationMessage"; /** * Events manager @@ -19,17 +19,23 @@ export interface UpdatedNotificationsNumberEvent { } // When some users have an updated number of unread conversations -export interface UpdateNumberUnreadConversations { +export interface UpdateNumberUnreadConversationsEvent { usersID: number[] } +// When a new conversatino message is sent +export interface SentNewConversationMessageEvent { + msg: ConversationMessage +} + /** * Global map of all possible events */ export interface EventsMap { "destroyed_login_tokens": DestroyedLoginTokensEvent, "updated_number_notifications": UpdatedNotificationsNumberEvent, - "updated_number_unread_conversations": UpdateNumberUnreadConversations + "updated_number_unread_conversations": UpdateNumberUnreadConversationsEvent, + "sent_conversation_message": SentNewConversationMessageEvent } export class EventsHelper {