@@ -19,8 +19,18 @@ import {
1919} from "./replay-migration-contract.js" ;
2020import { getNextcloudTalkRuntime } from "./runtime.js" ;
2121import 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 ;
2434const NEXTCLOUD_TALK_INGRESS_POLL_INTERVAL_MS = 500 ;
2535const NEXTCLOUD_TALK_INGRESS_PRUNE_INTERVAL_MS = 60 * 60 * 1_000 ;
2636const 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-
5862export 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-
13373function 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-
231125export function createNextcloudTalkWebhookSpool ( options : {
232126 accountId : string ;
233127 queue ?: ChannelIngressQueue < NextcloudTalkIngressPayload > ;
0 commit comments