Skip to content

Commit

Permalink
connectors[slack]: update content-nodes to use new itnernalIds (#9324)
Browse files Browse the repository at this point in the history
* connectors[slack]: update content-nodes to use new itnernalIds

* nit
  • Loading branch information
spolu authored Dec 13, 2024
1 parent 1e74127 commit 6b31102
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
36 changes: 24 additions & 12 deletions connectors/src/connectors/slack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import {
getSlackAccessToken,
getSlackClient,
} from "@connectors/connectors/slack/lib/slack_client";
import {
internalIdFromSlackChannelId,
slackChannelIdFromInternalId,
} from "@connectors/connectors/slack/lib/utils";
import { launchSlackSyncWorkflow } from "@connectors/connectors/slack/temporal/client.js";
import {
ExternalOAuthTokenError,
Expand Down Expand Up @@ -377,7 +381,7 @@ export class SlackConnectorManager extends BaseConnectorManager<SlackConfigurati

const resources: ContentNode[] = slackChannels.map((ch) => ({
provider: "slack",
internalId: ch.slackChannelId,
internalId: internalIdFromSlackChannelId(ch.slackChannelId),
parentInternalId: null,
type: "channel",
title: `#${ch.slackChannelName}`,
Expand Down Expand Up @@ -440,7 +444,9 @@ export class SlackConnectorManager extends BaseConnectorManager<SlackConfigurati
await SlackChannel.findAll({
where: {
connectorId: this.connectorId,
slackChannelId: Object.keys(permissions),
slackChannelId: Object.keys(permissions).map((k) =>
slackChannelIdFromInternalId(k)
),
},
})
).reduce(
Expand All @@ -455,18 +461,19 @@ export class SlackConnectorManager extends BaseConnectorManager<SlackConfigurati

const slackChannelsToSync: string[] = [];
try {
for (const [id, permission] of Object.entries(permissions)) {
let channel = channels[id];
for (const [internalId, permission] of Object.entries(permissions)) {
const slackChannelId = slackChannelIdFromInternalId(internalId);
let channel = channels[slackChannelId];
const slackClient = await getSlackClient(connector.id);
if (!channel) {
const remoteChannel = await slackClient.conversations.info({
channel: id,
channel: slackChannelId,
});
if (!remoteChannel.ok || !remoteChannel.channel?.name) {
logger.error(
{
connectorId: this.connectorId,
channelId: id,
channelId: slackChannelId,
error: remoteChannel.error,
},
"Could not get the Slack channel information"
Expand All @@ -475,12 +482,12 @@ export class SlackConnectorManager extends BaseConnectorManager<SlackConfigurati
new Error("Could not get the Slack channel information.")
);
}
const joinRes = await joinChannel(this.connectorId, id);
const joinRes = await joinChannel(this.connectorId, slackChannelId);
if (joinRes.isErr()) {
logger.error(
{
connectorId: this.connectorId,
channelId: id,
channelId: slackChannelId,
error: joinRes.error,
},
"Could not join the Slack channel"
Expand All @@ -493,12 +500,12 @@ export class SlackConnectorManager extends BaseConnectorManager<SlackConfigurati
}
const slackChannel = await SlackChannel.create({
connectorId: this.connectorId,
slackChannelId: id,
slackChannelId: slackChannelId,
slackChannelName: remoteChannel.channel.name,
permission: "none",
private: !!remoteChannel.channel.is_private,
});
channels[id] = slackChannel;
channels[slackChannelId] = slackChannel;
channel = slackChannel;
}

Expand Down Expand Up @@ -566,6 +573,9 @@ export class SlackConnectorManager extends BaseConnectorManager<SlackConfigurati
internalIds: string[];
viewType: ContentNodesViewType;
}): Promise<Result<ContentNode[], Error>> {
const slackChannelIds = internalIds.map((id) =>
slackChannelIdFromInternalId(id)
);
const slackConfig = await SlackConfigurationResource.fetchByConnectorId(
this.connectorId
);
Expand All @@ -580,13 +590,13 @@ export class SlackConnectorManager extends BaseConnectorManager<SlackConfigurati
const channels = await SlackChannel.findAll({
where: {
connectorId: this.connectorId,
slackChannelId: internalIds,
slackChannelId: slackChannelIds,
},
});

const contentNodes: ContentNode[] = channels.map((ch) => ({
provider: "slack",
internalId: ch.slackChannelId,
internalId: internalIdFromSlackChannelId(ch.slackChannelId),
parentInternalId: null,
type: "channel",
title: `#${ch.slackChannelName}`,
Expand All @@ -607,6 +617,8 @@ export class SlackConnectorManager extends BaseConnectorManager<SlackConfigurati
}: {
internalId: string;
}): Promise<Result<string[], Error>> {
// We only ever return permissions at the slack channel level so the parents are always the
// internalId itself.
return new Ok([internalId]);
}

Expand Down
10 changes: 10 additions & 0 deletions connectors/src/connectors/slack/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import _ from "lodash";

export function getWeekStart(date: Date): Date {
const dateCopy = new Date(date);

Expand Down Expand Up @@ -48,3 +50,11 @@ export const timeAgoFrom = (millisSinceEpoch: number) => {
}
return seconds + "s";
};

export function internalIdFromSlackChannelId(channel: string) {
return `slack-channel-${_.last(channel.split("slack-channel-"))!}`;
}

export function slackChannelIdFromInternalId(nodeId: string) {
return _.last(nodeId.split("slack-channel-"))!;
}
11 changes: 7 additions & 4 deletions connectors/src/connectors/slack/temporal/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ import {
import { isSlackWebAPIPlatformError } from "@connectors/connectors/slack/lib/errors";
import { getSlackClient } from "@connectors/connectors/slack/lib/slack_client";
import { getRepliesFromThread } from "@connectors/connectors/slack/lib/thread";
import {
getWeekEnd,
getWeekStart,
internalIdFromSlackChannelId,
} from "@connectors/connectors/slack/lib/utils";
import { dataSourceConfigFromConnector } from "@connectors/lib/api/data_source_config";
import { cacheGet, cacheSet } from "@connectors/lib/cache";
import {
Expand All @@ -43,8 +48,6 @@ import { ConnectorResource } from "@connectors/resources/connector_resource";
import { SlackConfigurationResource } from "@connectors/resources/slack_configuration_resource";
import type { DataSourceConfig } from "@connectors/types/data_source_config";

import { getWeekEnd, getWeekStart } from "../lib/utils";

const logger = mainLogger.child({ provider: "slack" });

// This controls the maximum number of concurrent calls to syncThread and syncNonThreaded.
Expand Down Expand Up @@ -602,7 +605,7 @@ export async function syncNonThreaded(
documentUrl: sourceUrl,
timestampMs: updatedAt,
tags,
parents: [documentId, channelId, `slack-channel-${channelId}`],
parents: [documentId, channelId, internalIdFromSlackChannelId(channelId)],
upsertContext: {
sync_type: isBatchSync ? "batch" : "incremental",
},
Expand Down Expand Up @@ -812,7 +815,7 @@ export async function syncThread(
documentUrl: sourceUrl,
timestampMs: updatedAt,
tags,
parents: [documentId, channelId, `slack-channel-${channelId}`],
parents: [documentId, channelId, internalIdFromSlackChannelId(channelId)],
upsertContext: {
sync_type: isBatchSync ? "batch" : "incremental",
},
Expand Down

0 comments on commit 6b31102

Please sign in to comment.