Skip to content

Commit 177ae09

Browse files
committed
fix(mattermost): honor envelope-level channel ids in the durable inspector
Posts can carry their channel id on the post, the event data, or the broadcast envelope — the monitor dispatch honors all three, but the ingress inspector and claim-side validator required the nested field, rejecting valid posts as permanent and (via the storage-failure escalation) tearing down the socket for a failure that never happened, losing posts reconnect cannot replay. Both sites accept the three shapes; regression proves an envelope-level post dispatches.
1 parent c37bb9e commit 177ae09

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎extensions/mattermost/src/mattermost/monitor-ingress.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,32 @@ describe("Mattermost durable ingress", () => {
299299
});
300300
});
301301

302+
it("accepts a post whose channel id arrives at the envelope level", async () => {
303+
await withQueue(async (queue) => {
304+
const dispatched: string[] = [];
305+
const ingress = startMonitor(queue, async (post, _payload, lifecycle) => {
306+
dispatched.push(post.id);
307+
await lifecycle.onAdopted();
308+
});
309+
try {
310+
// The monitor dispatch honors post/data/broadcast channel ids; the
311+
// durable inspector must not reject the envelope-level shapes.
312+
await ingress.receive(
313+
JSON.stringify({
314+
event: "posted",
315+
data: {
316+
channel_id: "channel-envelope",
317+
post: JSON.stringify({ id: "post-envelope", user_id: "user-1", message: "hi" }),
318+
},
319+
}),
320+
);
321+
await vi.waitFor(() => expect(dispatched).toEqual(["post-envelope"]));
322+
} finally {
323+
await ingress.stop();
324+
}
325+
});
326+
});
327+
302328
it("dead-letters malformed persisted payloads without retry", async () => {
303329
await withQueue(async (queue) => {
304330
await queue.enqueue(

‎extensions/mattermost/src/mattermost/monitor-ingress.ts‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,17 @@ function inspectMattermostIngressEvent(rawEvent: string): {
175175
const data = isRecord(envelope.data) ? envelope.data : null;
176176
const post = parseRawPost(data?.post);
177177
const eventId = requiredString(post.id, "post.id");
178-
const channelId = requiredString(post.channel_id, "post.channel_id");
178+
// Mattermost can carry the channel id on the post, the event data, or the
179+
// broadcast envelope (the monitor dispatch honors all three). Rejecting the
180+
// envelope-level shapes as permanent would drop valid posts and tear the
181+
// socket down for a storage failure that never happened.
182+
const broadcast = isRecord(envelope.broadcast) ? envelope.broadcast : null;
183+
const channelId =
184+
typeof post.channel_id === "string" && post.channel_id.trim()
185+
? post.channel_id.trim()
186+
: typeof data?.channel_id === "string" && data.channel_id.trim()
187+
? data.channel_id.trim()
188+
: requiredString(broadcast?.channel_id, "channel_id");
179189
return { eventId, laneKey: `channel:${channelId}` };
180190
}
181191

@@ -194,7 +204,13 @@ function parseClaimedEvent(
194204
);
195205
}
196206
const post = parseMattermostPost(payload.data?.post);
197-
if (!post || post.id !== eventId || !post.channel_id?.trim()) {
207+
// Channel id may live on the post, the event data, or the broadcast — the
208+
// durable inspector accepted all three, so the claim-side check must too.
209+
const claimedChannelId =
210+
post?.channel_id?.trim() ||
211+
payload.data?.channel_id?.trim() ||
212+
payload.broadcast?.channel_id?.trim();
213+
if (!post || post.id !== eventId || !claimedChannelId) {
198214
throw new MattermostIngressPermanentError(
199215
"invalid-event",
200216
`Mattermost ingress row ${eventId} has invalid post identity.`,

0 commit comments

Comments
 (0)