diff --git a/src/slack/monitor/events/channels.ts b/src/slack/monitor/events/channels.ts index 94492da2485..1e69ffe2095 100644 --- a/src/slack/monitor/events/channels.ts +++ b/src/slack/monitor/events/channels.ts @@ -15,6 +15,35 @@ import { resolveSlackChannelLabel } from "../channel-config.js"; export function registerSlackChannelEvents(params: { ctx: SlackMonitorContext }) { const { ctx } = params; + const enqueueChannelSystemEvent = (params: { + kind: "created" | "renamed"; + channelId: string | undefined; + channelName: string | undefined; + }) => { + if ( + !ctx.isChannelAllowed({ + channelId: params.channelId, + channelName: params.channelName, + channelType: "channel", + }) + ) { + return; + } + + const label = resolveSlackChannelLabel({ + channelId: params.channelId, + channelName: params.channelName, + }); + const sessionKey = ctx.resolveSlackSystemEventSessionKey({ + channelId: params.channelId, + channelType: "channel", + }); + enqueueSystemEvent(`Slack channel ${params.kind}: ${label}.`, { + sessionKey, + contextKey: `slack:channel:${params.kind}:${params.channelId ?? params.channelName ?? "unknown"}`, + }); + }; + ctx.app.event( "channel_created", async ({ event, body }: SlackEventMiddlewareArgs<"channel_created">) => { @@ -26,24 +55,7 @@ export function registerSlackChannelEvents(params: { ctx: SlackMonitorContext }) const payload = event as SlackChannelCreatedEvent; const channelId = payload.channel?.id; const channelName = payload.channel?.name; - if ( - !ctx.isChannelAllowed({ - channelId, - channelName, - channelType: "channel", - }) - ) { - return; - } - const label = resolveSlackChannelLabel({ channelId, channelName }); - const sessionKey = ctx.resolveSlackSystemEventSessionKey({ - channelId, - channelType: "channel", - }); - enqueueSystemEvent(`Slack channel created: ${label}.`, { - sessionKey, - contextKey: `slack:channel:created:${channelId ?? channelName ?? "unknown"}`, - }); + enqueueChannelSystemEvent({ kind: "created", channelId, channelName }); } catch (err) { ctx.runtime.error?.(danger(`slack channel created handler failed: ${String(err)}`)); } @@ -61,24 +73,7 @@ export function registerSlackChannelEvents(params: { ctx: SlackMonitorContext }) const payload = event as SlackChannelRenamedEvent; const channelId = payload.channel?.id; const channelName = payload.channel?.name_normalized ?? payload.channel?.name; - if ( - !ctx.isChannelAllowed({ - channelId, - channelName, - channelType: "channel", - }) - ) { - return; - } - const label = resolveSlackChannelLabel({ channelId, channelName }); - const sessionKey = ctx.resolveSlackSystemEventSessionKey({ - channelId, - channelType: "channel", - }); - enqueueSystemEvent(`Slack channel renamed: ${label}.`, { - sessionKey, - contextKey: `slack:channel:renamed:${channelId ?? channelName ?? "unknown"}`, - }); + enqueueChannelSystemEvent({ kind: "renamed", channelId, channelName }); } catch (err) { ctx.runtime.error?.(danger(`slack channel rename handler failed: ${String(err)}`)); } diff --git a/src/slack/monitor/events/messages.ts b/src/slack/monitor/events/messages.ts index 3aacb80c0af..1685f748e58 100644 --- a/src/slack/monitor/events/messages.ts +++ b/src/slack/monitor/events/messages.ts @@ -17,6 +17,31 @@ export function registerSlackMessageEvents(params: { }) { const { ctx, handleSlackMessage } = params; + const resolveSlackChannelSystemEventTarget = async (channelId: string | undefined) => { + const channelInfo = channelId ? await ctx.resolveChannelName(channelId) : {}; + const channelType = channelInfo?.type; + if ( + !ctx.isChannelAllowed({ + channelId, + channelName: channelInfo?.name, + channelType, + }) + ) { + return null; + } + + const label = resolveSlackChannelLabel({ + channelId, + channelName: channelInfo?.name, + }); + const sessionKey = ctx.resolveSlackSystemEventSessionKey({ + channelId, + channelType, + }); + + return { channelInfo, channelType, label, sessionKey }; + }; + ctx.app.event("message", async ({ event, body }: SlackEventMiddlewareArgs<"message">) => { try { if (ctx.shouldDropMismatchedSlackEvent(body)) { @@ -27,28 +52,13 @@ export function registerSlackMessageEvents(params: { if (message.subtype === "message_changed") { const changed = event as SlackMessageChangedEvent; const channelId = changed.channel; - const channelInfo = channelId ? await ctx.resolveChannelName(channelId) : {}; - const channelType = channelInfo?.type; - if ( - !ctx.isChannelAllowed({ - channelId, - channelName: channelInfo?.name, - channelType, - }) - ) { + const target = await resolveSlackChannelSystemEventTarget(channelId); + if (!target) { return; } const messageId = changed.message?.ts ?? changed.previous_message?.ts; - const label = resolveSlackChannelLabel({ - channelId, - channelName: channelInfo?.name, - }); - const sessionKey = ctx.resolveSlackSystemEventSessionKey({ - channelId, - channelType, - }); - enqueueSystemEvent(`Slack message edited in ${label}.`, { - sessionKey, + enqueueSystemEvent(`Slack message edited in ${target.label}.`, { + sessionKey: target.sessionKey, contextKey: `slack:message:changed:${channelId ?? "unknown"}:${messageId ?? changed.event_ts ?? "unknown"}`, }); return; @@ -56,27 +66,12 @@ export function registerSlackMessageEvents(params: { if (message.subtype === "message_deleted") { const deleted = event as SlackMessageDeletedEvent; const channelId = deleted.channel; - const channelInfo = channelId ? await ctx.resolveChannelName(channelId) : {}; - const channelType = channelInfo?.type; - if ( - !ctx.isChannelAllowed({ - channelId, - channelName: channelInfo?.name, - channelType, - }) - ) { + const target = await resolveSlackChannelSystemEventTarget(channelId); + if (!target) { return; } - const label = resolveSlackChannelLabel({ - channelId, - channelName: channelInfo?.name, - }); - const sessionKey = ctx.resolveSlackSystemEventSessionKey({ - channelId, - channelType, - }); - enqueueSystemEvent(`Slack message deleted in ${label}.`, { - sessionKey, + enqueueSystemEvent(`Slack message deleted in ${target.label}.`, { + sessionKey: target.sessionKey, contextKey: `slack:message:deleted:${channelId ?? "unknown"}:${deleted.deleted_ts ?? deleted.event_ts ?? "unknown"}`, }); return; @@ -84,28 +79,13 @@ export function registerSlackMessageEvents(params: { if (message.subtype === "thread_broadcast") { const thread = event as SlackThreadBroadcastEvent; const channelId = thread.channel; - const channelInfo = channelId ? await ctx.resolveChannelName(channelId) : {}; - const channelType = channelInfo?.type; - if ( - !ctx.isChannelAllowed({ - channelId, - channelName: channelInfo?.name, - channelType, - }) - ) { + const target = await resolveSlackChannelSystemEventTarget(channelId); + if (!target) { return; } - const label = resolveSlackChannelLabel({ - channelId, - channelName: channelInfo?.name, - }); const messageId = thread.message?.ts ?? thread.event_ts; - const sessionKey = ctx.resolveSlackSystemEventSessionKey({ - channelId, - channelType, - }); - enqueueSystemEvent(`Slack thread reply broadcast in ${label}.`, { - sessionKey, + enqueueSystemEvent(`Slack thread reply broadcast in ${target.label}.`, { + sessionKey: target.sessionKey, contextKey: `slack:thread:broadcast:${channelId ?? "unknown"}:${messageId ?? "unknown"}`, }); return;