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
5 changes: 3 additions & 2 deletions src/slack/conversation-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
collectEarlierThreadFiles,
decodeSlackEntities,
isOversize,
messageBodyText,
recentWindow,
} from "./lib.ts";
import type { BotIdentity, Directory } from "./directory.ts";
Expand Down Expand Up @@ -157,7 +158,7 @@ export function createConversationSerializer(deps: {
ts: m.ts as string,
name: messageAuthorName(m, nameById, botNameById),
...(m.user || m.bot_id ? { authorId: String(m.user || m.bot_id) } : {}),
text: decodeSlackEntities(String(m.text ?? "").trim()),
text: decodeSlackEntities(messageBodyText(m).trim()),
...(m.ts === triggerTs ? { isTrigger: true } : {}),
...(m.bot_id ? { isBot: true } : {}),
...(isSelfMessage(m) ? { isSelf: true } : {}),
Expand Down Expand Up @@ -234,7 +235,7 @@ export function createConversationSerializer(deps: {
kind: "thread",
youOpenedIt,
...(starterName ? { starterName } : {}),
...(youOpenedIt && root?.text ? { openerText: decodeSlackEntities(String(root.text).trim()) } : {}),
...(youOpenedIt && root && messageBodyText(root).trim() ? { openerText: decodeSlackEntities(messageBodyText(root).trim()) } : {}),
};
} else {
here = { kind: "top-level" };
Expand Down
5 changes: 3 additions & 2 deletions src/slack/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
isGroupMembershipMessage,
isThreadReply,
mentionsBot,
messageBodyText,
onBotJoinedChannel,
type SurfaceHeaderClient,
shouldProcessMessage,
Expand Down Expand Up @@ -114,7 +115,7 @@ export function registerSlackEvents(
channel: m.channel,
userId: m.user,
...(m.bot_profile?.name || m.username ? { authorName: String(m.bot_profile?.name || m.username) } : {}),
rawText: m.text ?? "",
rawText: messageBodyText(m),
files: (m.files as SlackFile[]) ?? [],
threadTs: m.thread_ts,
ts: m.ts,
Expand Down Expand Up @@ -152,7 +153,7 @@ export function registerSlackEvents(
channel: m.channel,
userId: m.user,
...(m.bot_profile?.name || m.username ? { authorName: String(m.bot_profile?.name || m.username) } : {}),
rawText: m.text ?? "",
rawText: messageBodyText(m),
files: (m.files as SlackFile[]) ?? [],
threadTs: m.thread_ts,
ts: m.ts,
Expand Down
2 changes: 2 additions & 0 deletions src/slack/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export {
channelPrivacyChange,
createRefreshCoalescer,
hasContent,
messageBodyText,
type LegacyAttachment,
isThreadReply,
createThreadTracker,
dmThreadRef,
Expand Down
21 changes: 21 additions & 0 deletions src/slack/message-gating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ export function hasContent(text: string, files: readonly unknown[]): boolean {
return text.trim().length > 0 || files.length > 0;
}

export interface LegacyAttachment {
fallback?: string;
pretext?: string;
title?: string;
text?: string;
}

export function messageBodyText(m: { text?: string; attachments?: LegacyAttachment[] }): string {
if (m.text?.trim()) return m.text;
return (m.attachments ?? [])
.map((a) => {
const composed = [a.pretext, a.title, a.text]
.map((s) => s?.trim())
.filter(Boolean)
.join("\n");
return composed || a.fallback?.trim() || "";
})
.filter(Boolean)
.join("\n");
}

export function isThreadReply(m: { thread_ts?: string; ts?: string }): boolean {
return Boolean(m.thread_ts) && m.thread_ts !== m.ts;
}
Expand Down
4 changes: 2 additions & 2 deletions src/slack/mirror.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { swallow } from "../util/errors.ts";
import { decodeSlackEntities, mentionsBot, resolveMentionsInText } from "./lib.ts";
import { decodeSlackEntities, mentionsBot, messageBodyText, resolveMentionsInText } from "./lib.ts";
import type { SlackCoreClient } from "../api/slack-core-client.ts";
import type { IngestEvent } from "../surface-cache/surface-cache.ts";
import type { BotIdentity, Directory } from "./directory.ts";
Expand Down Expand Up @@ -104,7 +104,7 @@ export function createMirror(deps: {
}
if (!gate.allowed) return;
}
const raw = String(m.text ?? "");
const raw = messageBodyText(m);
const { text, mentions } = await resolveTextMentions(client, decodeSlackEntities(raw));
await pushSurfaceEvents([
{
Expand Down
27 changes: 27 additions & 0 deletions test/slack-message-gating.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
channelPrivacyChange,
createRefreshCoalescer,
hasContent,
messageBodyText,
dmThreadRef,
mentionsBot,
threadHasBotStake,
Expand Down Expand Up @@ -288,3 +289,29 @@ test("createInFlightThreadMap: clear is runId-guarded so a finished run can't un
runs.clear("dm:C1", "run-2");
assert.equal(runs.get("dm:C1"), undefined);
});

test("messageBodyText returns text verbatim when present", () => {
assert.equal(messageBodyText({ text: "hello" }), "hello");
assert.equal(messageBodyText({ text: "hello", attachments: [{ fallback: "ignored" }] }), "hello");
});

test("messageBodyText falls back to attachment content for empty-text bot notifications", () => {
assert.equal(messageBodyText({ text: "" }), "");
assert.equal(messageBodyText({}), "");
assert.equal(
messageBodyText({ text: "", attachments: [{ fallback: "[org/repo] Pull request opened by someone" }] }),
"[org/repo] Pull request opened by someone",
);
assert.equal(
messageBodyText({ text: " ", attachments: [{ pretext: "pre", title: "PR #1", text: "body" }] }),
"pre\nPR #1\nbody",
);
assert.equal(
messageBodyText({ text: "", attachments: [{ fallback: "first" }, { title: "second" }, {}] }),
"first\nsecond",
);
assert.equal(
messageBodyText({ text: "", attachments: [{ fallback: "fb", title: "title only" }] }),
"title only",
);
});