From 5987509cd916ea7469c135e33a9fa6562168a52c Mon Sep 17 00:00:00 2001 From: Stanislas Polu Date: Mon, 27 Nov 2023 13:24:53 +0100 Subject: [PATCH] Avoid slack-messages-count sequential queries to SQL when generating contentFragment (#2676) --- connectors/src/connectors/slack/bot.ts | 20 +++++++++++--------- connectors/src/lib/models/slack.ts | 1 + 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/connectors/src/connectors/slack/bot.ts b/connectors/src/connectors/slack/bot.ts index 8a847900ef91..a49f3d5e866c 100644 --- a/connectors/src/connectors/slack/bot.ts +++ b/connectors/src/connectors/slack/bot.ts @@ -626,6 +626,15 @@ async function makeContentFragment( let next_cursor = undefined; let shouldTake = false; + + const slackBotMessages = await SlackChatBotMessage.findAll({ + where: { + connectorId: connector.id, + channelId: channelId, + threadTs: threadTs, + }, + }); + do { const replies: ConversationsRepliesResponse = await slackClient.conversations.replies({ @@ -657,15 +666,8 @@ async function makeContentFragment( continue; } if (shouldTake) { - const slackChatBotMessage = await SlackChatBotMessage.findOne({ - where: { - connectorId: connector.id, - messageTs: m.ts, - channelId: channelId, - }, - }); - if (slackChatBotMessage) { - // If this message is a mention to the bot, we don't send it as a content fragment + if (slackBotMessages.find((sbm) => sbm.messageTs === m.ts)) { + // If this message is a mention to the bot, we don't send it as a content fragment. continue; } allMessages.push(m); diff --git a/connectors/src/lib/models/slack.ts b/connectors/src/lib/models/slack.ts index 1f3d8c647bea..ee68ac97ed42 100644 --- a/connectors/src/lib/models/slack.ts +++ b/connectors/src/lib/models/slack.ts @@ -281,6 +281,7 @@ SlackChatBotMessage.init( { sequelize: sequelize_conn, modelName: "slack_chat_bot_messages", + indexes: [{ fields: ["connectorId", "channelId", "threadTs"] }], } ); Connector.hasOne(SlackChatBotMessage);