Skip to content

Commit 4281e90

Browse files
authored
fix(zalouser): route reactions through canonical conversation targets (#118536)
1 parent fad7632 commit 4281e90

2 files changed

Lines changed: 124 additions & 8 deletions

File tree

‎extensions/zalouser/src/channel.adapters.ts‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,30 @@ export const zalouserMessageActions: ChannelMessageActionAdapter = {
254254
return { actions: ["react"] };
255255
},
256256
supportsAction: ({ action }) => action === "react",
257-
handleAction: async ({ action, params, cfg, accountId, toolContext }) => {
257+
handleAction: async ({ action, channel, params, cfg, accountId, toolContext }) => {
258258
if (action !== "react") {
259259
throw new Error(`Zalouser action ${action} not supported`);
260260
}
261261
const { sendReactionZalouser } = await loadZalouserChannelRuntime();
262262
const account = resolveZalouserAccountSync({ cfg, accountId });
263-
const threadId =
263+
const explicitTarget =
264264
(typeof params.threadId === "string" ? params.threadId.trim() : "") ||
265265
(typeof params.to === "string" ? params.to.trim() : "") ||
266-
(typeof params.chatId === "string" ? params.chatId.trim() : "") ||
267-
(toolContext?.currentChannelId?.trim() ?? "");
268-
if (!threadId) {
266+
(typeof params.chatId === "string" ? params.chatId.trim() : "");
267+
const currentTarget = toolContext?.currentChannelId?.trim() ?? "";
268+
const rawTarget = explicitTarget || currentTarget;
269+
if (!rawTarget) {
269270
throw new Error("Zalouser react requires threadId (or to/chatId).");
270271
}
272+
const target = parseZalouserOutboundTarget(rawTarget);
273+
// Core may materialize the ambient ID into `to`; trust only the matching channel context.
274+
const isGroup =
275+
typeof params.isGroup === "boolean"
276+
? params.isGroup
277+
: target.isGroup ||
278+
(rawTarget === currentTarget &&
279+
toolContext?.currentChannelProvider === channel &&
280+
toolContext?.currentChatType === "group");
271281
const emoji = typeof params.emoji === "string" ? params.emoji.trim() : "";
272282
if (!emoji) {
273283
throw new Error("Zalouser react requires emoji.");
@@ -284,8 +294,8 @@ export const zalouserMessageActions: ChannelMessageActionAdapter = {
284294
}
285295
const result = await sendReactionZalouser({
286296
profile: account.profile,
287-
threadId,
288-
isGroup: params.isGroup === true,
297+
threadId: target.threadId,
298+
isGroup,
289299
msgId: ids.msgId,
290300
cliMsgId: ids.cliMsgId,
291301
emoji,
@@ -307,7 +317,7 @@ export const zalouserMessageActions: ChannelMessageActionAdapter = {
307317
details: {
308318
messageId: ids.msgId,
309319
cliMsgId: ids.cliMsgId,
310-
threadId,
320+
threadId: target.threadId,
311321
},
312322
};
313323
},

‎extensions/zalouser/src/channel.test.ts‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,112 @@ describe("zalouser channel policies", () => {
338338
});
339339
});
340340

341+
it.each([
342+
{
343+
name: "prefixed group target",
344+
params: { to: "zalouser:group:group-123" },
345+
threadId: "group-123",
346+
isGroup: true,
347+
},
348+
{
349+
name: "prefixed direct target inside an ambient group",
350+
params: { chatId: "zlu:user:user-456" },
351+
threadId: "user-456",
352+
isGroup: false,
353+
},
354+
{
355+
name: "explicit group override for a bare identifier",
356+
params: { threadId: "bare-group", isGroup: true },
357+
threadId: "bare-group",
358+
isGroup: true,
359+
},
360+
{
361+
name: "explicit direct override for a group target",
362+
params: { to: "group:override-789", isGroup: false },
363+
threadId: "override-789",
364+
isGroup: false,
365+
},
366+
{
367+
name: "ambient group without a target prefix",
368+
params: {},
369+
threadId: "ambient-group",
370+
isGroup: true,
371+
},
372+
{
373+
name: "core-materialized ambient group target",
374+
params: { to: "ambient-group" },
375+
threadId: "ambient-group",
376+
isGroup: true,
377+
},
378+
{
379+
name: "explicit direct target with the ambient group identifier",
380+
params: { to: "user:ambient-group" },
381+
threadId: "ambient-group",
382+
isGroup: false,
383+
},
384+
{
385+
name: "explicit thread target before the fallback target",
386+
params: { threadId: "user:priority-123", to: "group:fallback-456" },
387+
threadId: "priority-123",
388+
isGroup: false,
389+
},
390+
])("routes $name reactions through the canonical target owner", async (testCase) => {
391+
const result = await zalouserMessageActions.handleAction?.({
392+
channel: "zalouser",
393+
action: "react",
394+
params: {
395+
messageId: "111",
396+
cliMsgId: "222",
397+
emoji: "👍",
398+
...testCase.params,
399+
},
400+
cfg: { channels: { zalouser: { enabled: true, profile: "default" } } },
401+
toolContext: {
402+
currentChannelProvider: "zalouser",
403+
currentChannelId: "ambient-group",
404+
currentChatType: "group",
405+
},
406+
});
407+
408+
expect(mockSendReaction).toHaveBeenCalledWith({
409+
profile: "default",
410+
threadId: testCase.threadId,
411+
isGroup: testCase.isGroup,
412+
msgId: "111",
413+
cliMsgId: "222",
414+
emoji: "👍",
415+
remove: false,
416+
});
417+
expect(result?.details).toEqual({
418+
messageId: "111",
419+
cliMsgId: "222",
420+
threadId: testCase.threadId,
421+
});
422+
});
423+
424+
it("does not borrow group routing from another channel with the same conversation id", async () => {
425+
await zalouserMessageActions.handleAction?.({
426+
channel: "zalouser",
427+
action: "react",
428+
params: {
429+
to: "shared-conversation",
430+
messageId: "111",
431+
cliMsgId: "222",
432+
emoji: "👍",
433+
},
434+
cfg: { channels: { zalouser: { enabled: true, profile: "default" } } },
435+
toolContext: {
436+
currentChannelProvider: "slack",
437+
currentChannelId: "shared-conversation",
438+
currentChatType: "group",
439+
},
440+
});
441+
442+
expect(mockSendReaction).toHaveBeenCalledWith(
443+
expect.objectContaining({ threadId: "shared-conversation", isGroup: false }),
444+
);
445+
});
446+
341447
it("honors the selected Zalouser account during discovery", () => {
342448
const actions = zalouserMessageActions;
343449
const cfg = {

0 commit comments

Comments
 (0)