Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(community): enroll @-mentioned users when creating a forum post

Simonec38e38ca28f5
diff --git a/src/web/src/app/api/community/channels/[id]/posts/route.ts b/src/web/src/app/api/community/channels/[id]/posts/route.ts
index f5aba3eec..8355dc4d2 100644
--- a/src/web/src/app/api/community/channels/[id]/posts/route.ts
+++ b/src/web/src/app/api/community/channels/[id]/posts/route.ts
@@ -159,28 +159,27 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
creatorId: ctx.userId,
})
- // Enroll the creator as a participant so the post's notify set (which fan-out
- // now scopes to for a forum_post, exactly like a thread) starts with its
- // author. Done via a direct addThreadParticipants call rather than by routing
- // the first message as `kind:"forum_post"` — that would fire a
- // CHILD_CHANNEL_UPDATE colliding with the CHILD_CHANNEL_CREATE emitted below.
- await queries.communityThread.addThreadParticipants(db, postChannel.id, [
- { userId: ctx.userId, source: "spoke" },
- ])
-
- // Create the first message in the post through the unified pipeline so it
- // gets mention extraction + private-channel audience scoping (the forum's
- // privacy climbs the parent via `isChannelPrivate`) — the direct
- // `createMessage` insert this replaced dropped mentions (plan gap #2). Route
- // as `kind:"channel"` with the post's OWN channelId (NOT `kind:"thread"` —
- // that fires a CHILD_CHANNEL_UPDATE colliding with the CHILD_CHANNEL_CREATE
- // this route already emits below). The emitted MESSAGE_CREATE is deduped by
- // id on the client against that CHILD_CHANNEL_CREATE.
+ // Create the first message in the post through the unified pipeline. Route as
+ // `kind:"forum_post"` (NOT `kind:"channel"`) so the notify-set enroll runs
+ // exactly like a thread: the creator joins as "spoke" AND anyone the post
+ // body @-mentions joins as "mention" — so a person @-ed while creating the
+ // post lands in the members panel, matching reply behavior. `mentionType`
+ // still flows through for @everyone. `skipChildChannelUpdate` suppresses ONLY
+ // the parent CHILD_CHANNEL_UPDATE WS tick (enroll is unaffected) — this route
+ // already emits its own CHILD_CHANNEL_CREATE for the new post below, and the
+ // two would collide. The emitted MESSAGE_CREATE is deduped by id on the
+ // client against that CHILD_CHANNEL_CREATE.
const created = await createCommunityMessage({
db,
authorId: ctx.userId,
- target: { kind: "channel", channelId: postChannel.id, serverId: channel.serverId },
+ target: {
+ kind: "forum_post",
+ channelId: postChannel.id,
+ parentChannelId: channelId,
+ serverId: channel.serverId,
+ },
body: { content, attachments: body.attachments, mentionType: body.mentionType },
+ skipChildChannelUpdate: true,
})
if (!created.ok) return writeError(created.error, created.status)
const message = created.row
diff --git a/src/web/src/lib/community/message-handler.test.ts b/src/web/src/lib/community/message-handler.test.ts
index dc860c4ed..4bfc14f85 100644
--- a/src/web/src/lib/community/message-handler.test.ts
+++ b/src/web/src/lib/community/message-handler.test.ts
@@ -612,6 +612,35 @@ describe("createCommunityMessage — private-channel mention scoping (no auto-ad
])
})
+ it("forum_post + skipChildChannelUpdate: enroll STILL runs, but the parent CHILD_CHANNEL_UPDATE is suppressed", async () => {
+ // The forum-post CREATE path routes the first message as kind:"forum_post"
+ // (so an @-mentioned user enrolls as a participant → appears in members),
+ // AND sets skipChildChannelUpdate to avoid colliding with its own
+ // CHILD_CHANNEL_CREATE. Enroll and the WS tick are decoupled: enroll runs,
+ // the tick does not.
+ mockGetPrivateChannelAudienceUserIds.mockResolvedValue(["author_1", "cara_1"])
+ mockGetMessage.mockResolvedValue(messageRow({ content: "welcome @Cara#0002", channelId: "p1" }))
+
+ await createCommunityMessage({
+ db: {} as never,
+ authorId: "author_1",
+ target: { kind: "forum_post", channelId: "p1", parentChannelId: "forum_1", serverId: "srv_1" },
+ body: { content: "welcome @Cara#0002" },
+ skipChildChannelUpdate: true,
+ })
+
+ // Enroll is unaffected by the flag — Cara joins as a participant.
+ expect(mockAddThreadParticipants).toHaveBeenCalledWith({}, "p1", [
+ { userId: "author_1", source: "spoke" },
+ { userId: "cara_1", source: "mention" },
+ ])
+ // No CHILD_CHANNEL_UPDATE fanned to the parent forum.
+ const childUpdateCalls = mockFanOutToChannel.mock.calls.filter(
+ (c) => (c[1] as { type?: string })?.type === "community:channel.child_update",
+ )
+ expect(childUpdateCalls).toHaveLength(0)
+ })
+
it("public channel: mention of any server member is kept, no roster row", async () => {
mockIsChannelPrivate.mockResolvedValue(false)
mockGetMessage.mockResolvedValue(messageRow({ content: "hey @Bob#0001" }))
diff --git a/src/web/src/lib/community/message-handler.ts b/src/web/src/lib/community/message-handler.ts
index 049f528f9..c2951e7d5 100644
--- a/src/web/src/lib/community/message-handler.ts
+++ b/src/web/src/lib/community/message-handler.ts
@@ -197,6 +197,16 @@ export async function createCommunityMessage(params: {
* conflict — broadcasting before that commit would show a phantom card.
*/
deferBroadcast?: boolean
+ /**
+ * Suppress ONLY the parent `CHILD_CHANNEL_UPDATE` WS emission — participant
+ * enroll (the notify-set write) still runs per `kind`. The forum-post CREATE
+ * path opts in: it routes the post's first message as `kind:"forum_post"` so
+ * mentioned users enroll as participants, but must not fire
+ * CHILD_CHANNEL_UPDATE because it already emits its own CHILD_CHANNEL_CREATE
+ * for the new post (the two would collide). Decouples enroll from the WS tick
+ * so dodging the collision no longer silently skips enrollment.
+ */
+ skipChildChannelUpdate?: boolean
}): Promise<CreateMessageResult> {
const {
db,
@@ -211,6 +221,7 @@ export async function createCommunityMessage(params: {
includeAuthorInFanout,
deferBroadcast,
attachmentIds,
+ skipChildChannelUpdate,
} = params
const content = typeof body.content === "string" ? body.content : ""
@@ -656,7 +667,7 @@ export async function createCommunityMessage(params: {
).catch(() => { })
if (!isDmTarget(target)) {
- if (hasParentChannel(target)) {
+ if (hasParentChannel(target) && !skipChildChannelUpdate) {
const updated = await queries.communityChannel.getChannel(
db,
target.channelId,