mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-11 10:01:41 +00:00
refactor(slack): dedupe modal lifecycle interaction handlers
This commit is contained in:
@@ -1115,5 +1115,35 @@ describe("registerSlackInteractionEvents", () => {
|
|||||||
);
|
);
|
||||||
expect(options.sessionKey).toBe("agent:main:slack:channel:C99");
|
expect(options.sessionKey).toBe("agent:main:slack:channel:C99");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("defaults modal close isCleared to false when Slack omits the flag", async () => {
|
||||||
|
enqueueSystemEventMock.mockReset();
|
||||||
|
const { ctx, getViewClosedHandler } = createContext();
|
||||||
|
registerSlackInteractionEvents({ ctx: ctx as never });
|
||||||
|
const viewClosedHandler = getViewClosedHandler();
|
||||||
|
expect(viewClosedHandler).toBeTruthy();
|
||||||
|
|
||||||
|
const ack = vi.fn().mockResolvedValue(undefined);
|
||||||
|
await viewClosedHandler!({
|
||||||
|
ack,
|
||||||
|
body: {
|
||||||
|
user: { id: "U901" },
|
||||||
|
view: {
|
||||||
|
id: "V901",
|
||||||
|
callback_id: "openclaw:deploy_form",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(ack).toHaveBeenCalled();
|
||||||
|
expect(enqueueSystemEventMock).toHaveBeenCalledTimes(1);
|
||||||
|
const [eventText] = enqueueSystemEventMock.mock.calls[0] as [string];
|
||||||
|
const payload = JSON.parse(eventText.replace("Slack interaction: ", "")) as {
|
||||||
|
interactionType: string;
|
||||||
|
isCleared?: boolean;
|
||||||
|
};
|
||||||
|
expect(payload.interactionType).toBe("view_closed");
|
||||||
|
expect(payload.isCleared).toBe(false);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
const selectedDateTimeEpoch = 1_771_632_300;
|
const selectedDateTimeEpoch = 1_771_632_300;
|
||||||
|
|||||||
@@ -98,6 +98,8 @@ type SlackModalEventBase = {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type SlackModalInteractionKind = "view_submission" | "view_closed";
|
||||||
|
|
||||||
function readOptionValues(options: unknown): string[] | undefined {
|
function readOptionValues(options: unknown): string[] | undefined {
|
||||||
if (!Array.isArray(options)) {
|
if (!Array.isArray(options)) {
|
||||||
return undefined;
|
return undefined;
|
||||||
@@ -442,6 +444,45 @@ function resolveSlackModalEventBase(params: {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function emitSlackModalLifecycleEvent(params: {
|
||||||
|
ctx: SlackMonitorContext;
|
||||||
|
body: SlackModalBody;
|
||||||
|
interactionType: SlackModalInteractionKind;
|
||||||
|
contextPrefix: "slack:interaction:view" | "slack:interaction:view-closed";
|
||||||
|
}): void {
|
||||||
|
const { callbackId, userId, viewId, sessionRouting, payload } = resolveSlackModalEventBase({
|
||||||
|
ctx: params.ctx,
|
||||||
|
body: params.body,
|
||||||
|
});
|
||||||
|
const isViewClosed = params.interactionType === "view_closed";
|
||||||
|
const isCleared = params.body.is_cleared === true;
|
||||||
|
const eventPayload = isViewClosed
|
||||||
|
? {
|
||||||
|
interactionType: params.interactionType,
|
||||||
|
...payload,
|
||||||
|
isCleared,
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
interactionType: params.interactionType,
|
||||||
|
...payload,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isViewClosed) {
|
||||||
|
params.ctx.runtime.log?.(
|
||||||
|
`slack:interaction view_closed callback=${callbackId} user=${userId} cleared=${isCleared}`,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
params.ctx.runtime.log?.(
|
||||||
|
`slack:interaction view_submission callback=${callbackId} user=${userId} inputs=${payload.inputs.length}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
enqueueSystemEvent(`Slack interaction: ${JSON.stringify(eventPayload)}`, {
|
||||||
|
sessionKey: sessionRouting.sessionKey,
|
||||||
|
contextKey: [params.contextPrefix, callbackId, viewId, userId].filter(Boolean).join(":"),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export function registerSlackInteractionEvents(params: { ctx: SlackMonitorContext }) {
|
export function registerSlackInteractionEvents(params: { ctx: SlackMonitorContext }) {
|
||||||
const { ctx } = params;
|
const { ctx } = params;
|
||||||
if (typeof ctx.app.action !== "function") {
|
if (typeof ctx.app.action !== "function") {
|
||||||
@@ -611,26 +652,11 @@ export function registerSlackInteractionEvents(params: { ctx: SlackMonitorContex
|
|||||||
new RegExp(`^${OPENCLAW_ACTION_PREFIX}`),
|
new RegExp(`^${OPENCLAW_ACTION_PREFIX}`),
|
||||||
async ({ ack, body }: { ack: () => Promise<void>; body: unknown }) => {
|
async ({ ack, body }: { ack: () => Promise<void>; body: unknown }) => {
|
||||||
await ack();
|
await ack();
|
||||||
|
emitSlackModalLifecycleEvent({
|
||||||
const modalBody = body as SlackModalBody;
|
|
||||||
const { callbackId, userId, viewId, sessionRouting, payload } = resolveSlackModalEventBase({
|
|
||||||
ctx,
|
ctx,
|
||||||
body: modalBody,
|
body: body as SlackModalBody,
|
||||||
});
|
|
||||||
const eventPayload = {
|
|
||||||
interactionType: "view_submission",
|
interactionType: "view_submission",
|
||||||
...payload,
|
contextPrefix: "slack:interaction:view",
|
||||||
};
|
|
||||||
|
|
||||||
ctx.runtime.log?.(
|
|
||||||
`slack:interaction view_submission callback=${callbackId} user=${userId} inputs=${payload.inputs.length}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
enqueueSystemEvent(`Slack interaction: ${JSON.stringify(eventPayload)}`, {
|
|
||||||
sessionKey: sessionRouting.sessionKey,
|
|
||||||
contextKey: ["slack:interaction:view", callbackId, viewId, userId]
|
|
||||||
.filter(Boolean)
|
|
||||||
.join(":"),
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -652,29 +678,11 @@ export function registerSlackInteractionEvents(params: { ctx: SlackMonitorContex
|
|||||||
new RegExp(`^${OPENCLAW_ACTION_PREFIX}`),
|
new RegExp(`^${OPENCLAW_ACTION_PREFIX}`),
|
||||||
async ({ ack, body }: { ack: () => Promise<void>; body: unknown }) => {
|
async ({ ack, body }: { ack: () => Promise<void>; body: unknown }) => {
|
||||||
await ack();
|
await ack();
|
||||||
|
emitSlackModalLifecycleEvent({
|
||||||
const modalBody = body as SlackModalBody;
|
|
||||||
const { callbackId, userId, viewId, sessionRouting, payload } = resolveSlackModalEventBase({
|
|
||||||
ctx,
|
ctx,
|
||||||
body: modalBody,
|
body: body as SlackModalBody,
|
||||||
});
|
|
||||||
const eventPayload = {
|
|
||||||
interactionType: "view_closed",
|
interactionType: "view_closed",
|
||||||
...payload,
|
contextPrefix: "slack:interaction:view-closed",
|
||||||
isCleared: modalBody.is_cleared === true,
|
|
||||||
};
|
|
||||||
|
|
||||||
ctx.runtime.log?.(
|
|
||||||
`slack:interaction view_closed callback=${callbackId} user=${userId} cleared=${
|
|
||||||
modalBody.is_cleared === true
|
|
||||||
}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
enqueueSystemEvent(`Slack interaction: ${JSON.stringify(eventPayload)}`, {
|
|
||||||
sessionKey: sessionRouting.sessionKey,
|
|
||||||
contextKey: ["slack:interaction:view-closed", callbackId, viewId, userId]
|
|
||||||
.filter(Boolean)
|
|
||||||
.join(":"),
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user