mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-29 07:01:40 +03:00
refactor(slack): dedupe pin event handlers
This commit is contained in:
@@ -5,84 +5,76 @@ import { danger } from "../../../globals.js";
|
|||||||
import { enqueueSystemEvent } from "../../../infra/system-events.js";
|
import { enqueueSystemEvent } from "../../../infra/system-events.js";
|
||||||
import { resolveSlackChannelLabel } from "../channel-config.js";
|
import { resolveSlackChannelLabel } from "../channel-config.js";
|
||||||
|
|
||||||
|
async function handleSlackPinEvent(params: {
|
||||||
|
ctx: SlackMonitorContext;
|
||||||
|
body: unknown;
|
||||||
|
event: unknown;
|
||||||
|
action: "pinned" | "unpinned";
|
||||||
|
contextKeySuffix: "added" | "removed";
|
||||||
|
errorLabel: string;
|
||||||
|
}): Promise<void> {
|
||||||
|
const { ctx, body, event, action, contextKeySuffix, errorLabel } = params;
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (ctx.shouldDropMismatchedSlackEvent(body)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const payload = event as SlackPinEvent;
|
||||||
|
const channelId = payload.channel_id;
|
||||||
|
const channelInfo = channelId ? await ctx.resolveChannelName(channelId) : {};
|
||||||
|
if (
|
||||||
|
!ctx.isChannelAllowed({
|
||||||
|
channelId,
|
||||||
|
channelName: channelInfo?.name,
|
||||||
|
channelType: channelInfo?.type,
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const label = resolveSlackChannelLabel({
|
||||||
|
channelId,
|
||||||
|
channelName: channelInfo?.name,
|
||||||
|
});
|
||||||
|
const userInfo = payload.user ? await ctx.resolveUserName(payload.user) : {};
|
||||||
|
const userLabel = userInfo?.name ?? payload.user ?? "someone";
|
||||||
|
const itemType = payload.item?.type ?? "item";
|
||||||
|
const messageId = payload.item?.message?.ts ?? payload.event_ts;
|
||||||
|
const sessionKey = ctx.resolveSlackSystemEventSessionKey({
|
||||||
|
channelId,
|
||||||
|
channelType: channelInfo?.type ?? undefined,
|
||||||
|
});
|
||||||
|
enqueueSystemEvent(`Slack: ${userLabel} ${action} a ${itemType} in ${label}.`, {
|
||||||
|
sessionKey,
|
||||||
|
contextKey: `slack:pin:${contextKeySuffix}:${channelId ?? "unknown"}:${messageId ?? "unknown"}`,
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
ctx.runtime.error?.(danger(`slack ${errorLabel} handler failed: ${String(err)}`));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function registerSlackPinEvents(params: { ctx: SlackMonitorContext }) {
|
export function registerSlackPinEvents(params: { ctx: SlackMonitorContext }) {
|
||||||
const { ctx } = params;
|
const { ctx } = params;
|
||||||
|
|
||||||
ctx.app.event("pin_added", async ({ event, body }: SlackEventMiddlewareArgs<"pin_added">) => {
|
ctx.app.event("pin_added", async ({ event, body }: SlackEventMiddlewareArgs<"pin_added">) => {
|
||||||
try {
|
await handleSlackPinEvent({
|
||||||
if (ctx.shouldDropMismatchedSlackEvent(body)) {
|
ctx,
|
||||||
return;
|
body,
|
||||||
}
|
event,
|
||||||
|
action: "pinned",
|
||||||
const payload = event as SlackPinEvent;
|
contextKeySuffix: "added",
|
||||||
const channelId = payload.channel_id;
|
errorLabel: "pin added",
|
||||||
const channelInfo = channelId ? await ctx.resolveChannelName(channelId) : {};
|
});
|
||||||
if (
|
|
||||||
!ctx.isChannelAllowed({
|
|
||||||
channelId,
|
|
||||||
channelName: channelInfo?.name,
|
|
||||||
channelType: channelInfo?.type,
|
|
||||||
})
|
|
||||||
) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const label = resolveSlackChannelLabel({
|
|
||||||
channelId,
|
|
||||||
channelName: channelInfo?.name,
|
|
||||||
});
|
|
||||||
const userInfo = payload.user ? await ctx.resolveUserName(payload.user) : {};
|
|
||||||
const userLabel = userInfo?.name ?? payload.user ?? "someone";
|
|
||||||
const itemType = payload.item?.type ?? "item";
|
|
||||||
const messageId = payload.item?.message?.ts ?? payload.event_ts;
|
|
||||||
const sessionKey = ctx.resolveSlackSystemEventSessionKey({
|
|
||||||
channelId,
|
|
||||||
channelType: channelInfo?.type ?? undefined,
|
|
||||||
});
|
|
||||||
enqueueSystemEvent(`Slack: ${userLabel} pinned a ${itemType} in ${label}.`, {
|
|
||||||
sessionKey,
|
|
||||||
contextKey: `slack:pin:added:${channelId ?? "unknown"}:${messageId ?? "unknown"}`,
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
ctx.runtime.error?.(danger(`slack pin added handler failed: ${String(err)}`));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
ctx.app.event("pin_removed", async ({ event, body }: SlackEventMiddlewareArgs<"pin_removed">) => {
|
ctx.app.event("pin_removed", async ({ event, body }: SlackEventMiddlewareArgs<"pin_removed">) => {
|
||||||
try {
|
await handleSlackPinEvent({
|
||||||
if (ctx.shouldDropMismatchedSlackEvent(body)) {
|
ctx,
|
||||||
return;
|
body,
|
||||||
}
|
event,
|
||||||
|
action: "unpinned",
|
||||||
const payload = event as SlackPinEvent;
|
contextKeySuffix: "removed",
|
||||||
const channelId = payload.channel_id;
|
errorLabel: "pin removed",
|
||||||
const channelInfo = channelId ? await ctx.resolveChannelName(channelId) : {};
|
});
|
||||||
if (
|
|
||||||
!ctx.isChannelAllowed({
|
|
||||||
channelId,
|
|
||||||
channelName: channelInfo?.name,
|
|
||||||
channelType: channelInfo?.type,
|
|
||||||
})
|
|
||||||
) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const label = resolveSlackChannelLabel({
|
|
||||||
channelId,
|
|
||||||
channelName: channelInfo?.name,
|
|
||||||
});
|
|
||||||
const userInfo = payload.user ? await ctx.resolveUserName(payload.user) : {};
|
|
||||||
const userLabel = userInfo?.name ?? payload.user ?? "someone";
|
|
||||||
const itemType = payload.item?.type ?? "item";
|
|
||||||
const messageId = payload.item?.message?.ts ?? payload.event_ts;
|
|
||||||
const sessionKey = ctx.resolveSlackSystemEventSessionKey({
|
|
||||||
channelId,
|
|
||||||
channelType: channelInfo?.type ?? undefined,
|
|
||||||
});
|
|
||||||
enqueueSystemEvent(`Slack: ${userLabel} unpinned a ${itemType} in ${label}.`, {
|
|
||||||
sessionKey,
|
|
||||||
contextKey: `slack:pin:removed:${channelId ?? "unknown"}:${messageId ?? "unknown"}`,
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
ctx.runtime.error?.(danger(`slack pin removed handler failed: ${String(err)}`));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user