Skip to content

Commit

Permalink
Fix: can't link assistant to Slack channel if Slack channel is not in…
Browse files Browse the repository at this point in the history
… our DB (#2430)
  • Loading branch information
lasryaric authored Nov 8, 2023
1 parent 4f2cdad commit 502f8ce
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
67 changes: 55 additions & 12 deletions connectors/src/api/slack_channels_linked_with_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as t from "io-ts";
import * as reporter from "io-ts-reporters";
import { Op } from "sequelize";

import { joinChannel } from "@connectors/connectors/slack/lib/channels";
import { getChannels } from "@connectors/connectors/slack/temporal/activities";
import { APIErrorWithStatusCode } from "@connectors/lib/error";
import { sequelize_conn, SlackChannel } from "@connectors/lib/models";
import { apiError, withLogging } from "@connectors/logger/withlogging";
Expand Down Expand Up @@ -66,19 +68,39 @@ const _patchSlackChannelsLinkedWithAgentHandler = async (
new Set(slackChannelIds.filter((id) => !foundSlackChannelIds.has(id)))
);

if (missingSlackChannelIds.length) {
return apiError(req, res, {
api_error: {
type: "not_found",
message: `Slack channel(s) not found: ${missingSlackChannelIds.join(
", "
)}`,
},
status_code: 404,
});
}

await sequelize_conn.transaction(async (t) => {
if (missingSlackChannelIds.length) {
const remoteChannels = (
await getChannels(parseInt(connectorId), false)
).flatMap((c) => (c.id && c.name ? [{ id: c.id, name: c.name }] : []));
const remoteChannelsById = remoteChannels.reduce((acc, ch) => {
acc[ch.id] = ch;
return acc;
}, {} as Record<string, { id: string; name: string }>);
const createdChannels = await Promise.all(
missingSlackChannelIds.map((slackChannelId) => {
const remoteChannel = remoteChannelsById[slackChannelId];
if (!remoteChannel) {
throw new Error(
`Unexpected error: Access to the Slack channel ${slackChannelId} seems lost.`
);
}
return SlackChannel.create(
{
connectorId: parseInt(connectorId),
slackChannelId,
slackChannelName: remoteChannel.name,
agentConfigurationId,
permission: "write",
},
{
transaction: t,
}
);
})
);
slackChannelIds.push(...createdChannels.map((c) => c.slackChannelId));
}
await SlackChannel.update(
{ agentConfigurationId: null },
{
Expand All @@ -98,6 +120,27 @@ const _patchSlackChannelsLinkedWithAgentHandler = async (
)
);
});
const joinPromises = await Promise.all(
slackChannelIds.map((slackChannelId) =>
joinChannel(parseInt(connectorId), slackChannelId)
)
);
for (const joinRes of joinPromises) {
if (joinRes.isErr()) {
return apiError(
req,
res,
{
api_error: {
type: "internal_server_error",
message: `Could not join channel: ${joinRes.error}`,
},
status_code: 400,
},
joinRes.error
);
}
}

res.status(200).json({
success: true,
Expand Down
1 change: 0 additions & 1 deletion connectors/src/connectors/slack/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import logger from "@connectors/logger/logger";
import {
formatMessagesForUpsert,
getBotUserIdMemoized,
getChannels,
getUserName,
} from "./temporal/activities";

Expand Down

0 comments on commit 502f8ce

Please sign in to comment.