Skip to content

Commit 53db423

Browse files
committed
refactor(channels): satisfy durable ingress gates
1 parent 7a0f9b8 commit 53db423

14 files changed

Lines changed: 287 additions & 258 deletions

‎extensions/nextcloud-talk/src/monitor-runtime.abort.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe("Nextcloud Talk monitor abort", () => {
3131
},
3232
},
3333
},
34-
runtime: { error: vi.fn(), log: vi.fn() },
34+
runtime: { error: vi.fn(), log: vi.fn(), exit: vi.fn() as never },
3535
abortSignal: abortController.signal,
3636
createSpool,
3737
createServer,

‎extensions/nextcloud-talk/src/monitor.durable-ack.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { describe, expect, it, vi } from "vitest";
33
import { createSignedCreateMessageRequest } from "./monitor.test-fixtures.js";
44
import { startWebhookServer } from "./monitor.test-harness.js";
5-
import { NextcloudTalkWebhookPayloadError } from "./webhook-spool.js";
5+
import { NextcloudTalkWebhookPayloadError } from "./webhook-spool-state.js";
66

77
describe("Nextcloud Talk durable webhook acknowledgement", () => {
88
it("waits for durable admission before acknowledging", async () => {

‎extensions/nextcloud-talk/src/monitor.replay.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { createSignedCreateMessageRequest } from "./monitor.test-fixtures.js";
77
import { startWebhookServer } from "./monitor.test-harness.js";
88
import { generateNextcloudTalkSignature } from "./signature.js";
99
import type { NextcloudTalkInboundMessage, NextcloudTalkWebhookServerOptions } from "./types.js";
10-
import { inspectNextcloudTalkWebhookEnvelope } from "./webhook-spool.js";
10+
import { inspectNextcloudTalkWebhookEnvelope } from "./webhook-spool-state.js";
1111

1212
type TestWebhookServerOptions = Omit<NextcloudTalkWebhookServerOptions, "onWebhook"> & {
1313
onMessage: (rawBody: string) => void | Promise<void>;

‎extensions/nextcloud-talk/src/monitor.test-harness.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { AddressInfo } from "node:net";
33
import { afterEach } from "vitest";
44
import { createNextcloudTalkWebhookServer as createRawNextcloudTalkWebhookServer } from "./monitor.js";
55
import type { NextcloudTalkWebhookServerOptions } from "./types.js";
6-
import { inspectNextcloudTalkWebhookEnvelope } from "./webhook-spool.js";
6+
import { inspectNextcloudTalkWebhookEnvelope } from "./webhook-spool-state.js";
77

88
type WebhookHarness = {
99
webhookUrl: string;

‎extensions/nextcloud-talk/src/monitor.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from "openclaw/plugin-sdk/webhook-ingress";
1010
import { extractNextcloudTalkHeaders, verifyNextcloudTalkSignature } from "./signature.js";
1111
import type { NextcloudTalkWebhookHeaders, NextcloudTalkWebhookServerOptions } from "./types.js";
12-
import { NextcloudTalkWebhookPayloadError } from "./webhook-spool.js";
12+
import { NextcloudTalkWebhookPayloadError } from "./webhook-spool-state.js";
1313

1414
const DEFAULT_WEBHOOK_MAX_BODY_BYTES = 1024 * 1024;
1515
const PREAUTH_WEBHOOK_MAX_BODY_BYTES = 64 * 1024;
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Nextcloud Talk plugin module owns webhook ingress identity and legacy-state migration.
2+
import type { ChannelIngressQueue } from "openclaw/plugin-sdk/channel-outbound";
3+
4+
export const NEXTCLOUD_TALK_INGRESS_PAYLOAD_VERSION = 1;
5+
6+
export type NextcloudTalkIngressPayload = {
7+
version: 1;
8+
receivedAt: number;
9+
rawEvent: string;
10+
};
11+
12+
export type NextcloudTalkLegacyReplayEntry = {
13+
key: string;
14+
seenAt: number;
15+
};
16+
17+
export type NextcloudTalkLegacyReplayStore = {
18+
entries: () => Promise<Array<{ value: NextcloudTalkLegacyReplayEntry }>>;
19+
clear: () => Promise<void>;
20+
};
21+
22+
export class NextcloudTalkWebhookPayloadError extends Error {
23+
constructor(message: string, options?: ErrorOptions) {
24+
super(message, options);
25+
this.name = "NextcloudTalkWebhookPayloadError";
26+
}
27+
}
28+
29+
function isRecord(value: unknown): value is Record<string, unknown> {
30+
return typeof value === "object" && value !== null && !Array.isArray(value);
31+
}
32+
33+
export function parseRawObject(rawEvent: string): Record<string, unknown> {
34+
let parsed: unknown;
35+
try {
36+
parsed = JSON.parse(rawEvent);
37+
} catch (error) {
38+
throw new NextcloudTalkWebhookPayloadError("Nextcloud Talk webhook contains invalid JSON.", {
39+
cause: error,
40+
});
41+
}
42+
if (!isRecord(parsed)) {
43+
throw new NextcloudTalkWebhookPayloadError("Nextcloud Talk webhook must be a JSON object.");
44+
}
45+
return parsed;
46+
}
47+
48+
export function requiredString(value: unknown, field: string): string {
49+
if (typeof value === "string" && value.trim()) {
50+
return value.trim();
51+
}
52+
throw new NextcloudTalkWebhookPayloadError(`Nextcloud Talk webhook is missing ${field}.`);
53+
}
54+
55+
export function inspectNextcloudTalkWebhookEnvelope(
56+
rawEvent: string,
57+
): { eventId: string; laneKey: string } | null {
58+
const envelope = parseRawObject(rawEvent);
59+
if (envelope.type !== "Create") {
60+
return null;
61+
}
62+
const object = isRecord(envelope.object) ? envelope.object : null;
63+
if (object?.type !== undefined && object.type !== "Note") {
64+
return null;
65+
}
66+
if (!object) {
67+
throw new NextcloudTalkWebhookPayloadError("Nextcloud Talk webhook is missing object.");
68+
}
69+
const target = isRecord(envelope.target) ? envelope.target : null;
70+
return {
71+
eventId: requiredString(object.id, "object.id"),
72+
laneKey: `room:${requiredString(target?.id, "target.id")}`,
73+
};
74+
}
75+
76+
function parseLegacyReplayKey(key: string): { messageId: string; roomId: string } | null {
77+
const separator = key.lastIndexOf(":");
78+
const roomId = key.slice(0, separator).trim();
79+
const messageId = key.slice(separator + 1).trim();
80+
return separator > 0 && roomId && messageId ? { messageId, roomId } : null;
81+
}
82+
83+
/** Convert the shipped replay guard's live window into durable completion tombstones. */
84+
export async function migrateNextcloudTalkLegacyReplayState(params: {
85+
queue: ChannelIngressQueue<NextcloudTalkIngressPayload>;
86+
store: NextcloudTalkLegacyReplayStore;
87+
}): Promise<number> {
88+
const entries = await params.store.entries();
89+
let migrated = 0;
90+
for (const entry of entries) {
91+
const identity = parseLegacyReplayKey(entry.value.key);
92+
if (!identity || !Number.isFinite(entry.value.seenAt)) {
93+
continue;
94+
}
95+
const marker: NextcloudTalkIngressPayload = {
96+
version: NEXTCLOUD_TALK_INGRESS_PAYLOAD_VERSION,
97+
receivedAt: entry.value.seenAt,
98+
rawEvent: "",
99+
};
100+
const result = await params.queue.enqueue(identity.messageId, marker, {
101+
receivedAt: entry.value.seenAt,
102+
laneKey: `room:${identity.roomId}`,
103+
});
104+
const ownsMarker =
105+
result.kind === "accepted" ||
106+
(result.kind === "pending" && result.record.payload.rawEvent === "");
107+
if (ownsMarker) {
108+
const completed = await params.queue.complete(identity.messageId, {
109+
completedAt: entry.value.seenAt,
110+
});
111+
if (!completed) {
112+
throw new Error(`Failed to migrate Nextcloud Talk replay key ${entry.value.key}.`);
113+
}
114+
}
115+
// Any existing ingress row already rejects the retired guard's duplicate.
116+
migrated += 1;
117+
}
118+
await params.store.clear();
119+
return migrated;
120+
}

‎extensions/nextcloud-talk/src/webhook-spool.test.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ import {
88
} from "openclaw/plugin-sdk/plugin-state-test-runtime";
99
import { afterEach, describe, expect, it, vi } from "vitest";
1010
import { createSignedCreateMessageRequest } from "./monitor.test-fixtures.js";
11-
import {
12-
createNextcloudTalkWebhookSpool,
13-
migrateNextcloudTalkLegacyReplayState,
14-
} from "./webhook-spool.js";
11+
import { migrateNextcloudTalkLegacyReplayState } from "./webhook-spool-state.js";
12+
import { createNextcloudTalkWebhookSpool } from "./webhook-spool.js";
1513

1614
type NextcloudTalkIngressQueue = NonNullable<
1715
Parameters<typeof createNextcloudTalkWebhookSpool>[0]["queue"]

‎extensions/nextcloud-talk/src/webhook-spool.ts‎

Lines changed: 11 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,18 @@ import {
1919
} from "./replay-migration-contract.js";
2020
import { getNextcloudTalkRuntime } from "./runtime.js";
2121
import type { NextcloudTalkInboundMessage, NextcloudTalkWebhookPayload } from "./types.js";
22+
import {
23+
inspectNextcloudTalkWebhookEnvelope,
24+
migrateNextcloudTalkLegacyReplayState,
25+
NEXTCLOUD_TALK_INGRESS_PAYLOAD_VERSION,
26+
NextcloudTalkWebhookPayloadError,
27+
parseRawObject,
28+
requiredString,
29+
type NextcloudTalkIngressPayload,
30+
type NextcloudTalkLegacyReplayEntry,
31+
type NextcloudTalkLegacyReplayStore,
32+
} from "./webhook-spool-state.js";
2233

23-
const NEXTCLOUD_TALK_INGRESS_PAYLOAD_VERSION = 1;
2434
const NEXTCLOUD_TALK_INGRESS_POLL_INTERVAL_MS = 500;
2535
const NEXTCLOUD_TALK_INGRESS_PRUNE_INTERVAL_MS = 60 * 60 * 1_000;
2636
const NEXTCLOUD_TALK_INGRESS_COMPLETED_TTL_MS = 30 * 24 * 60 * 60 * 1_000;
@@ -49,12 +59,6 @@ const NextcloudTalkWebhookPayloadSchema: z.ZodType<NextcloudTalkWebhookPayload>
4959
}),
5060
});
5161

52-
type NextcloudTalkIngressPayload = {
53-
version: 1;
54-
receivedAt: number;
55-
rawEvent: string;
56-
};
57-
5862
export type NextcloudTalkIngressLifecycle = Parameters<
5963
typeof bindIngressLifecycleToReplyOptions
6064
>[0];
@@ -66,70 +70,6 @@ type NextcloudTalkIngressMonitor = {
6670
waitForIdle: () => Promise<void>;
6771
};
6872

69-
type NextcloudTalkLegacyReplayEntry = {
70-
key: string;
71-
seenAt: number;
72-
};
73-
74-
type NextcloudTalkLegacyReplayStore = {
75-
entries: () => Promise<Array<{ value: NextcloudTalkLegacyReplayEntry }>>;
76-
clear: () => Promise<void>;
77-
};
78-
79-
export class NextcloudTalkWebhookPayloadError extends Error {
80-
constructor(message: string, options?: ErrorOptions) {
81-
super(message, options);
82-
this.name = "NextcloudTalkWebhookPayloadError";
83-
}
84-
}
85-
86-
function isRecord(value: unknown): value is Record<string, unknown> {
87-
return typeof value === "object" && value !== null && !Array.isArray(value);
88-
}
89-
90-
function parseRawObject(rawEvent: string): Record<string, unknown> {
91-
let parsed: unknown;
92-
try {
93-
parsed = JSON.parse(rawEvent);
94-
} catch (error) {
95-
throw new NextcloudTalkWebhookPayloadError("Nextcloud Talk webhook contains invalid JSON.", {
96-
cause: error,
97-
});
98-
}
99-
if (!isRecord(parsed)) {
100-
throw new NextcloudTalkWebhookPayloadError("Nextcloud Talk webhook must be a JSON object.");
101-
}
102-
return parsed;
103-
}
104-
105-
function requiredString(value: unknown, field: string): string {
106-
if (typeof value === "string" && value.trim()) {
107-
return value.trim();
108-
}
109-
throw new NextcloudTalkWebhookPayloadError(`Nextcloud Talk webhook is missing ${field}.`);
110-
}
111-
112-
export function inspectNextcloudTalkWebhookEnvelope(
113-
rawEvent: string,
114-
): { eventId: string; laneKey: string } | null {
115-
const envelope = parseRawObject(rawEvent);
116-
if (envelope.type !== "Create") {
117-
return null;
118-
}
119-
const object = isRecord(envelope.object) ? envelope.object : null;
120-
if (object?.type !== undefined && object.type !== "Note") {
121-
return null;
122-
}
123-
if (!object) {
124-
throw new NextcloudTalkWebhookPayloadError("Nextcloud Talk webhook is missing object.");
125-
}
126-
const target = isRecord(envelope.target) ? envelope.target : null;
127-
return {
128-
eventId: requiredString(object.id, "object.id"),
129-
laneKey: `room:${requiredString(target?.id, "target.id")}`,
130-
};
131-
}
132-
13373
function parseClaimedMessage(
13474
payload: NextcloudTalkIngressPayload,
13575
claimedId: string,
@@ -182,52 +122,6 @@ function resolveNonRetryableFailure(error: unknown) {
182122
return null;
183123
}
184124

185-
function parseLegacyReplayKey(key: string): { messageId: string; roomId: string } | null {
186-
const separator = key.lastIndexOf(":");
187-
const roomId = key.slice(0, separator).trim();
188-
const messageId = key.slice(separator + 1).trim();
189-
return separator > 0 && roomId && messageId ? { messageId, roomId } : null;
190-
}
191-
192-
/** Convert the shipped replay guard's live window into durable completion tombstones. */
193-
export async function migrateNextcloudTalkLegacyReplayState(params: {
194-
queue: ChannelIngressQueue<NextcloudTalkIngressPayload>;
195-
store: NextcloudTalkLegacyReplayStore;
196-
}): Promise<number> {
197-
const entries = await params.store.entries();
198-
let migrated = 0;
199-
for (const entry of entries) {
200-
const identity = parseLegacyReplayKey(entry.value.key);
201-
if (!identity || !Number.isFinite(entry.value.seenAt)) {
202-
continue;
203-
}
204-
const marker: NextcloudTalkIngressPayload = {
205-
version: NEXTCLOUD_TALK_INGRESS_PAYLOAD_VERSION,
206-
receivedAt: entry.value.seenAt,
207-
rawEvent: "",
208-
};
209-
const result = await params.queue.enqueue(identity.messageId, marker, {
210-
receivedAt: entry.value.seenAt,
211-
laneKey: `room:${identity.roomId}`,
212-
});
213-
const ownsMarker =
214-
result.kind === "accepted" ||
215-
(result.kind === "pending" && result.record.payload.rawEvent === "");
216-
if (ownsMarker) {
217-
const completed = await params.queue.complete(identity.messageId, {
218-
completedAt: entry.value.seenAt,
219-
});
220-
if (!completed) {
221-
throw new Error(`Failed to migrate Nextcloud Talk replay key ${entry.value.key}.`);
222-
}
223-
}
224-
// Any existing ingress row already rejects the retired guard's duplicate.
225-
migrated += 1;
226-
}
227-
await params.store.clear();
228-
return migrated;
229-
}
230-
231125
export function createNextcloudTalkWebhookSpool(options: {
232126
accountId: string;
233127
queue?: ChannelIngressQueue<NextcloudTalkIngressPayload>;

0 commit comments

Comments
 (0)