From b82c256df97d1436ba5fa72637e8a066b0b7bfd8 Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Thu, 25 Jan 2024 11:54:03 +0330 Subject: [PATCH] [FEATURE]: integrate the events with queues --- package-lock.json | 8 ++++---- package.json | 2 +- src/index.ts | 1 + src/rabbitmq/events/sendMessageEvent.ts | 3 ++- src/rabbitmq/events/sendMessageToChannel.ts | 4 +++- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3c9c5704..1edbc91c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "ISC", "dependencies": { "@sentry/node": "^7.51.2", - "@togethercrew.dev/db": "^3.0.24", + "@togethercrew.dev/db": "^3.0.28", "@togethercrew.dev/tc-messagebroker": "^0.0.44", "babel-jest": "^29.5.0", "bullmq": "^3.14.0", @@ -3039,9 +3039,9 @@ } }, "node_modules/@togethercrew.dev/db": { - "version": "3.0.24", - "resolved": "https://registry.npmjs.org/@togethercrew.dev/db/-/db-3.0.24.tgz", - "integrity": "sha512-YiKS48xNVA2X/e+NJIAc1DtDQMkaFfGYD1mR3FQSx+UeGkQVyaR5l5uKYzi0WryPk5FgP+9Q9qZ6KWhTE30Yrw==", + "version": "3.0.28", + "resolved": "https://registry.npmjs.org/@togethercrew.dev/db/-/db-3.0.28.tgz", + "integrity": "sha512-/juK+lS2VjtpH0YTP5ItWlt4ZHg0xXimIkMc9k0IgVJyBUzmHdflYGe+iModVHIXHwJMNgSdDyqVw7oDp5Pbrg==", "dependencies": { "discord.js": "^14.7.1", "joi": "^17.7.0", diff --git a/package.json b/package.json index 1d79836f..4aa75c38 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "homepage": "https://github.com/Behzad-rabiei/tc-discordBot#readme", "dependencies": { "@sentry/node": "^7.51.2", - "@togethercrew.dev/db": "^3.0.24", + "@togethercrew.dev/db": "^3.0.28", "@togethercrew.dev/tc-messagebroker": "^0.0.44", "babel-jest": "^29.5.0", "bullmq": "^3.14.0", diff --git a/src/index.ts b/src/index.ts index 548b9ece..9f032ee5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,7 @@ import { setupRabbitMQHandlers } from './rabbitmq/RabbitMQHandler'; import { commandService, eventService } from './services'; import './queue/workers/cronWorker'; import './queue/workers/channelMessageWorker'; +import './queue/workers/directMessageWorker'; async function app() { await connectToMongoDB(); diff --git a/src/rabbitmq/events/sendMessageEvent.ts b/src/rabbitmq/events/sendMessageEvent.ts index 25025d87..3e83b14e 100644 --- a/src/rabbitmq/events/sendMessageEvent.ts +++ b/src/rabbitmq/events/sendMessageEvent.ts @@ -5,6 +5,7 @@ import { createPrivateThreadAndSendMessage } from '../../functions/thread'; import parentLogger from '../../config/logger'; import { platformService } from '../../database/services'; import sendDirectMessage from '../../functions/sendDirectMessage'; +import { addDirectMessage } from '../../queue/queues/directMessage'; const logger = parentLogger.child({ module: `${Event.DISCORD_BOT.SEND_MESSAGE}` }); @@ -29,7 +30,7 @@ const notifyUserAboutAnalysisFinish = async ( const upperTextChannel = rawPositionBasedSortedTextChannels[0]; try { - sendDirectMessage({ discordId, message }); + addDirectMessage(discordId, message) } catch (error) { // can not send DM to the user // Will create a private thread and notify him/her about the status if useFallback is true diff --git a/src/rabbitmq/events/sendMessageToChannel.ts b/src/rabbitmq/events/sendMessageToChannel.ts index 192ea09b..365187f2 100644 --- a/src/rabbitmq/events/sendMessageToChannel.ts +++ b/src/rabbitmq/events/sendMessageToChannel.ts @@ -1,6 +1,7 @@ import { Event, MBConnection } from '@togethercrew.dev/tc-messagebroker'; import parentLogger from '../../config/logger'; import { channelService } from '../../services'; +import { addChannelMessage } from '../../queue/queues/channelMessage' const logger = parentLogger.child({ module: `${Event.DISCORD_BOT.SEND_MESSAGE}` }); export async function handleSendMessageToChannel(msg: any) { @@ -13,9 +14,10 @@ export async function handleSendMessageToChannel(msg: any) { const channels = saga.data['channels']; const message = saga.data['message']; + // IS THIS CORRECT WAY? await saga.next(async () => { for await (const channel of channels) { - await channelService.sendChannelMessage(channel, message) + addChannelMessage(channel, message) } });