1 | |
2 | export interface NextcloudWebhookPayload { |
3 | event: NextcloudEvent; |
4 | user: NextcloudUser; |
5 | time: number; |
6 | } |
7 |
|
8 | export interface NextcloudEvent { |
9 | node: NextcloudNode; |
10 | class: string; |
11 | } |
12 |
|
13 | export interface NextcloudNode { |
14 | id: number; |
15 | path: string; |
16 | |
17 | storage?: string; |
18 | size?: number; |
19 | name?: string; |
20 | } |
21 |
|
22 | export interface NextcloudUser { |
23 | uid: string; |
24 | displayName?: string; |
25 | } |
26 |
|
27 | |
28 | * Nextcloud Trigger Preprocessor |
29 | * |
30 | * This function runs BEFORE the main function. |
31 | * |
32 | * It processes raw Nextcloud webhook trigger data before passing it to `main()`. |
33 | * Supports the available Nextcloud events: |
34 | * - FormSubmittedEvent: Form submissions from Nextcloud Forms |
35 | * - NodeWrittenEvent: File/folder operations (create, update, delete) |
36 | * |
37 | * The returned object determines `main()` parameters based on event type. |
38 | * |
39 | * @param event - Nextcloud trigger data and details |
40 | * @returns Processed data for `main()` |
41 | */ |
42 | export async function preprocessor( |
43 | event: { |
44 | kind: 'nextcloud', |
45 | payload: NextcloudWebhookPayload, |
46 | base_url: string, |
47 | access_token: string |
48 | }, |
49 | ) { |
50 | if (event.kind === 'nextcloud') { |
51 | const payload = event.payload; |
52 |
|
53 | const baseEvent = { |
54 | eventType: payload.event.class, |
55 | userId: payload.user.uid, |
56 | timestamp: new Date(payload.time * 1000).toISOString(), |
57 | }; |
58 |
|
59 | |
60 | const eventTypeMatch = payload.event.class.match(/(\w+)Event$/); |
61 | const eventType = eventTypeMatch ? eventTypeMatch[1] : 'Unknown'; |
62 |
|
63 | |
64 | switch (eventType) { |
65 | case 'FormSubmitted': |
66 | return { |
67 | ...baseEvent, |
68 | category: 'form', |
69 | user: { |
70 | uid: payload.user.uid, |
71 | displayName: payload.user.displayName || '' |
72 | }, |
73 | rawPayload: payload |
74 | }; |
75 |
|
76 | case 'NodeWritten': |
77 | return { |
78 | ...baseEvent, |
79 | category: 'file', |
80 | user: { |
81 | uid: payload.user.uid, |
82 | displayName: payload.user.displayName || '' |
83 | }, |
84 | node: { |
85 | id: payload.event.node.id, |
86 | path: payload.event.node.path, |
87 | storage: payload.event.node.storage, |
88 | size: payload.event.node.size, |
89 | name: payload.event.node.name |
90 | }, |
91 | rawPayload: payload |
92 | }; |
93 |
|
94 | default: |
95 | return { |
96 | ...baseEvent, |
97 | category: 'other', |
98 | eventClass: payload.event.class, |
99 | data: payload.event |
100 | }; |
101 | } |
102 | } |
103 |
|
104 | throw new Error(`Expected nextcloud trigger kind, got: ${event.kind}`); |
105 | } |
106 |
|
107 | |
108 | * Main Function - Handles processed Nextcloud events |
109 | * |
110 | * Called AFTER `preprocessor()`, with its return values. |
111 | * |
112 | * This example demonstrates integrating with Nextcloud APIs using openapi-fetch |
113 | * and windmill-client, following the official Nextcloud Hub example patterns. |
114 | * |
115 | * @param eventType - Type of Nextcloud event class name |
116 | * @param category - Event category ('form', 'file', or 'other') |
117 | * @param userId - ID of the user who triggered the event |
118 | * @param user - User information |
119 | * @param node - Node information (for file events) |
120 | * @param eventClass - Full event class name (for other events) |
121 | * @param data - Generic event data (for other events) |
122 | * @param timestamp - Event timestamp |
123 | * @param rawPayload - Raw event payload from Nextcloud |
124 | */ |
125 | export async function main( |
126 | eventType: string, |
127 | category: string, |
128 | userId?: string, |
129 | user?: { |
130 | uid: string; |
131 | displayName: string; |
132 | }, |
133 | node?: { |
134 | id?: number; |
135 | path?: string; |
136 | storage?: string; |
137 | size?: number; |
138 | name?: string; |
139 | }, |
140 | eventClass?: string, |
141 | data?: any, |
142 | timestamp?: string, |
143 | rawPayload?: NextcloudWebhookPayload, |
144 | ) { |
145 | |
146 | } |