fix(slack): bound subteam member cache clocks · openclaw/openclaw@8e90a1c
steipete
·
2026-05-30
·
via Recent Commits to openclaw:main
File tree
extensions/slack/src/monitor/message-handler
| Original file line number | Diff line number | Diff line change |
|---|
@@ -71,6 +71,52 @@ describe("Slack subteam mentions", () => {
|
71 | 71 | expect(client.usergroups.users.list).toHaveBeenCalledTimes(1); |
72 | 72 | }); |
73 | 73 | |
| 74 | +it("drops cached membership lookups when the current clock is not a valid date timestamp", async () => { |
| 75 | +const client = createClient(["U_BOT"]); |
| 76 | + |
| 77 | +await expect( |
| 78 | +isSlackSubteamMentionForBot({ |
| 79 | + client, |
| 80 | +text: "<!subteam^S123> ping", |
| 81 | +botUserId: "U_BOT", |
| 82 | +now: 1_700_000_000_000, |
| 83 | +}), |
| 84 | +).resolves.toBe(true); |
| 85 | +await expect( |
| 86 | +isSlackSubteamMentionForBot({ |
| 87 | + client, |
| 88 | +text: "<!subteam^S123> ping again", |
| 89 | +botUserId: "U_BOT", |
| 90 | +now: Number.NaN, |
| 91 | +}), |
| 92 | +).resolves.toBe(true); |
| 93 | + |
| 94 | +expect(client.usergroups.users.list).toHaveBeenCalledTimes(2); |
| 95 | +}); |
| 96 | + |
| 97 | +it("does not cache membership lookups when the expiry timestamp would exceed the valid date range", async () => { |
| 98 | +const client = createClient(["U_BOT"]); |
| 99 | + |
| 100 | +await expect( |
| 101 | +isSlackSubteamMentionForBot({ |
| 102 | + client, |
| 103 | +text: "<!subteam^S123> ping", |
| 104 | +botUserId: "U_BOT", |
| 105 | +now: 8_640_000_000_000_000, |
| 106 | +}), |
| 107 | +).resolves.toBe(true); |
| 108 | +await expect( |
| 109 | +isSlackSubteamMentionForBot({ |
| 110 | + client, |
| 111 | +text: "<!subteam^S123> ping again", |
| 112 | +botUserId: "U_BOT", |
| 113 | +now: 1_700_000_000_000, |
| 114 | +}), |
| 115 | +).resolves.toBe(true); |
| 116 | + |
| 117 | +expect(client.usergroups.users.list).toHaveBeenCalledTimes(2); |
| 118 | +}); |
| 119 | + |
74 | 120 | it("fails closed when Slack rejects the user-group lookup", async () => { |
75 | 121 | const log = vi.fn(); |
76 | 122 | const client = createClient([]); |
|
| Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | import type { WebClient } from "@slack/web-api"; |
2 | 2 | import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime"; |
| 3 | +import { |
| 4 | +asDateTimestampMs, |
| 5 | +resolveExpiresAtMsFromDurationMs, |
| 6 | +} from "openclaw/plugin-sdk/number-runtime"; |
3 | 7 | import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime"; |
4 | 8 | |
5 | 9 | const SUBTEAM_MENTION_RE = /<!subteam\^([A-Z0-9]+)(?:\|[^>]*)?>/gi; |
@@ -44,8 +48,16 @@ async function readSlackSubteamUsers(params: {
|
44 | 48 | } |
45 | 49 | const cacheKey = `${normalizeSlackId(params.teamId) ?? ""}:${params.subteamId}`; |
46 | 50 | const cached = bySubteam.get(cacheKey); |
47 | | -if (cached && cached.expiresAt > params.now) { |
48 | | -return cached.users; |
| 51 | +const now = asDateTimestampMs(params.now); |
| 52 | +if (cached) { |
| 53 | +if ( |
| 54 | +now !== undefined && |
| 55 | +asDateTimestampMs(cached.expiresAt) !== undefined && |
| 56 | +cached.expiresAt > now |
| 57 | +) { |
| 58 | +return cached.users; |
| 59 | +} |
| 60 | +bySubteam.delete(cacheKey); |
49 | 61 | } |
50 | 62 | |
51 | 63 | try { |
@@ -62,10 +74,15 @@ async function readSlackSubteamUsers(params: {
|
62 | 74 | const users = new Set( |
63 | 75 | (response.users ?? []).map((userId) => normalizeSlackId(userId)).filter(Boolean) as string[], |
64 | 76 | ); |
65 | | -bySubteam.set(cacheKey, { |
66 | | -expiresAt: params.now + SUBTEAM_MEMBER_CACHE_TTL_MS, |
67 | | - users, |
| 77 | +const expiresAt = resolveExpiresAtMsFromDurationMs(SUBTEAM_MEMBER_CACHE_TTL_MS, { |
| 78 | +nowMs: params.now, |
68 | 79 | }); |
| 80 | +if (expiresAt !== undefined) { |
| 81 | +bySubteam.set(cacheKey, { |
| 82 | + expiresAt, |
| 83 | + users, |
| 84 | +}); |
| 85 | +} |
69 | 86 | return users; |
70 | 87 | } catch (err) { |
71 | 88 | params.log?.( |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。