Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions apps/api/src/handlers/slack/helpers/event-normalization.test.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 44 additions & 1 deletion apps/api/src/handlers/slack/helpers/event-normalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import type {
AutomatedSlackAppMentionEvent,
SlackWebhookEvent,
} from '../types.js';
import { mentionsSlackBot } from './mention-routing.js';
import {
getMentionedSlackUserIds,
mentionsSlackBot,
} from './mention-routing.js';

function normalizeSlackReactionName(reaction: string): string {
return reaction.trim().toLowerCase().split('::')[0] ?? '';
Expand Down Expand Up @@ -207,6 +210,46 @@ export function isRoomoteAuthoredSlackEvent(
);
}

// Automated (app/bot-authored) messages that mention users without mentioning
// the installed Roomote bot are discarded, which is invisible in logs and has
// hidden real misconfigurations — e.g. a Slack workflow whose message template
// still mentions the bot user of a previous Roomote installation. Surface a
// log line naming the mentioned IDs so those drops are diagnosable.
export function getIgnoredAutomatedSlackMentionLog(
event: SlackWebhookEvent,
slackInstallation: SlackInstallation,
): string | null {
if (event.type !== 'message' && event.type !== 'app_mention') {
return null;
}

if (isRoomoteAuthoredSlackEvent(event, slackInstallation)) {
return null;
}

const slackEvent = event as SlackEvent;
const mentionedUserIds = getMentionedSlackUserIds(slackEvent);

if (
mentionedUserIds.length === 0 ||
(slackInstallation.botUserId &&
mentionedUserIds.includes(slackInstallation.botUserId))
) {
return null;
}

const subtype = getSlackEventStringField(event, 'subtype') ?? 'none';
const appId = getSlackEventStringField(event, 'app_id') ?? 'none';
const botId = getSlackEventStringField(event, 'bot_id') ?? 'none';

return (
`[SlackWebhook] Ignoring automated message mentioning [${mentionedUserIds.join(', ')}] ` +
`but not the Roomote bot user ${slackInstallation.botUserId} ` +
`(channel=${slackEvent.channel}, ts=${slackEvent.ts}, thread_ts=${slackEvent.thread_ts ?? 'none'}, ` +
`subtype=${subtype}, app_id=${appId}, bot_id=${botId})`
);
}

export function isRoutableAutomatedSlackAppMention(
event: SlackWebhookEvent,
slackInstallation: SlackInstallation,
Expand Down
4 changes: 3 additions & 1 deletion apps/api/src/handlers/slack/helpers/mention-routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export function getSlackMentionDirectiveText(
return candidateLines.join(' ');
}

function getMentionedSlackUserIds(message: SlackMentionTextSource): string[] {
export function getMentionedSlackUserIds(
message: SlackMentionTextSource,
): string[] {
return Array.from(
getSlackMentionDirectiveText(message).matchAll(/<@([^>|]+)(?:\|[^>]+)?>/g),
)
Expand Down
10 changes: 10 additions & 0 deletions apps/api/src/handlers/slack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { dispatchSlackEvent } from './dispatch/events.js';
import { handleSlackInteractivePayload } from './dispatch/interactive.js';
import {
getIgnoredAutomatedSlackMentionLog,
getSlackWebhookEventLogDetails,
isAppAuthoredSlackEvent,
isRoomoteAuthoredSlackEvent,
Expand Down Expand Up @@ -155,6 +156,15 @@ slack.post('/', async (c) => {
(!isTopLevelAppMessageEvent ||
isRoomoteAuthoredSlackEvent(event, slackInstallation))
) {
const ignoredMentionLog = getIgnoredAutomatedSlackMentionLog(
event,
slackInstallation,
);

if (ignoredMentionLog) {
apiLogger.info(ignoredMentionLog);
}

return c.json({ ok: true });
}

Expand Down
Loading