-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc_model.ts
More file actions
403 lines (334 loc) · 13.7 KB
/
Copy pathrpc_model.ts
File metadata and controls
403 lines (334 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// deno-lint-ignore-file no-explicit-any
import { check, isNonEmpty, isStringRecord, isValidUrl } from './check.ts';
import { BlobReference, FederationRecord } from './domain_model.ts';
import { isValidUuid } from './uuid.ts';
export type RpcRequest = CreateUserRequest
| UpdateUserRequest
| DeleteUserRequest
| CreateNoteRequest
| UpdateNoteRequest
| DeleteNoteRequest
| FederateActivityRequest
| DeleteFromStorageRequest
| LikeObjectRequest
| UndoLikeRequest
| GenerateAdminTokenRequest
| RevokeAdminTokenRequest
| ValidateAdminTokenRequest
;
export type RpcResponse = CreateUserResponse
| UpdateUserResponse
| DeleteUserResponse
| CreateNoteResponse
| UpdateNoteResponse
| DeleteNoteResponse
| FederateActivityResponse
| DeleteFromStorageResponse
| LikeObjectResponse
| UndoLikeResponse
| GenerateAdminTokenResponse
| RevokeAdminTokenResponse
| ValidateAdminTokenResponse
;
// validation
export function isValidUsername(username: string) {
return /^[a-z0-9]{3,15}$/.test(username);
}
function isValidUserName(name: string) {
return /^[a-zA-Z]+( [a-zA-Z]+)*$/.test(name);
}
function isValidBase64(base64: string) {
return /^[a-zA-Z0-9+\/]+=*$/.test(base64);
}
function isValidMediaType(mediaType: string) {
return /^[a-z]+\/[a-z]+$/.test(mediaType);
}
function checkUserMetadata(obj: any): obj is Record<string, string> {
return typeof obj === 'object' && Object.entries(obj).every(v => {
const [ name, value ] = v;
return check('name', name, /^[a-zA-Z]+$/.test(name)) && check('value', value, typeof value === 'string' && isNonEmpty(value));
});
}
// common types
export interface LangString {
readonly lang: string;
readonly value: string;
}
function checkLangString(obj: any): obj is LangString {
return typeof obj === 'object'
&& check('lang', obj.lang, v => typeof v === 'string' && /^[a-zA-Z-]+$/.test(v))
&& check('value', obj.value, v => typeof v === 'string' && isNonEmpty(v))
;
}
export interface Icon {
readonly bytesBase64: string; // <= 128kb
readonly size: number; // width and height
readonly mediaType: string;
}
function checkIcon(obj: any): obj is Icon {
return typeof obj === 'object'
&& check('bytesBase64', obj.bytesBase64, v => typeof v === 'string' && isValidBase64(v))
&& check('size', obj.size, v => typeof v === 'number' && v > 0)
&& check('mediaType', obj.mediaType, v => typeof v === 'string' && isValidMediaType(v))
;
}
export interface Image {
readonly bytesBase64: string; // <= 128kb
readonly width: number;
readonly height: number;
readonly mediaType: string;
}
function checkImage(obj: any): obj is Image {
return typeof obj === 'object'
&& check('bytesBase64', obj.bytesBase64, v => typeof v === 'string' && isValidBase64(v))
&& check('width', obj.width, v => typeof v === 'number' && v > 0)
&& check('height', obj.height, v => typeof v === 'number' && v > 0)
&& check('mediaType', obj.mediaType, v => typeof v === 'string' && isValidMediaType(v))
;
}
// create-user
export interface CreateUserRequest {
readonly kind: 'create-user';
readonly username: string;
readonly name?: string,
readonly summary?: LangString, // html
readonly url?: string,
readonly icon?: Icon, // PNG, GIF or JPG. At most 2 MB. Will be downscaled to 400x400px, seen 220x220, 400x400
readonly image?: Image, // PNG, GIF or JPG. At most 2 MB. Will be downscaled to 1500x500px
readonly manuallyApprovesFollowers?: boolean, // as:manuallyApprovesFollowers proposal https://www.w3.org/wiki/Activity_Streams_extensions
readonly discoverable?: boolean, // toot:discoverable toot = http://joinmastodon.org/ns#
readonly metadata?: Record<string, string>, // ap attachments, schema propertyvalue name to value
}
export function checkCreateUserRequest(obj: any): obj is CreateUserRequest {
return typeof obj === 'object'
&& check('kind', obj.kind, v => v === 'create-user')
&& check('username', obj.username, v => typeof v === 'string' && isValidUsername(v))
&& check('name', obj.name, v => v === undefined || typeof v === 'string' && isValidUserName(v))
&& check('summary', obj.summary, v => v === undefined || checkLangString(v))
&& check('url', obj.url, v => v === undefined || typeof v === 'string' && isValidUrl(v))
&& check('icon', obj.icon, v => v === undefined || checkIcon(v))
&& check('image', obj.image, v => v === undefined || checkImage(v))
&& check('manuallyApprovesFollowers', obj.manuallyApprovesFollowers, v => v === undefined || typeof v === 'boolean')
&& check('discoverable', obj.discoverable, v => v === undefined || typeof v === 'boolean')
&& check('metadata', obj.metadata, v => v === undefined || checkUserMetadata(v))
;
}
export interface CreateUserResponse {
readonly kind: 'create-user';
readonly actorUuid: string;
readonly blobReferences: Record<string, BlobReference>;
readonly activityUuid: string;
}
// update-user
export interface UpdateUserRequest {
readonly kind: 'update-user',
readonly actorUuid: string,
readonly username?: string,
readonly name?: string | null, // null to clear
readonly summary?: LangString | null,
readonly url?: string | null,
readonly icon?: Icon | null,
readonly image?: Image | null,
readonly manuallyApprovesFollowers?: boolean | null,
readonly discoverable?: boolean | null,
readonly metadata?: Record<string, string> | null,
}
export function checkUpdateUserRequest(obj: any): obj is UpdateUserRequest {
return typeof obj === 'object'
&& check('kind', obj.kind, v => v === 'update-user')
&& check('actorUuid', obj.actorUuid, v => typeof v === 'string' && isValidUuid(v))
&& check('username', obj.username, v => v === undefined || v === null || typeof v === 'string' && isValidUserName(v))
&& check('name', obj.name, v => v === undefined || v === null || typeof v === 'string' && isValidUserName(v))
&& check('summary', obj.summary, v => v === undefined || v === null || checkLangString(v))
&& check('url', obj.url, v => v === undefined || v === null || typeof v === 'string' && isValidUrl(v))
&& check('icon', obj.icon, v => v === undefined || v === null || checkIcon(v))
&& check('image', obj.image, v => v === undefined || v === null || checkImage(v))
&& check('manuallyApprovesFollowers', obj.manuallyApprovesFollowers, v => v === undefined || v === null || typeof v === 'boolean')
&& check('discoverable', obj.discoverable, v => v === undefined || v === null || typeof v === 'boolean')
&& check('metadata', obj.metadata, v => v === undefined || v === null || checkUserMetadata(v))
;
}
export interface UpdateUserResponse {
readonly kind: 'update-user';
readonly actorUuid: string;
readonly modified: boolean;
readonly activityUuid?: string; // if modified
}
// delete-user
export interface DeleteUserRequest {
readonly kind: 'delete-user',
readonly actorUuid: string,
}
export interface DeleteUserResponse {
readonly kind: 'delete-user';
readonly actorUuid: string;
readonly deleted: boolean;
}
// create-note
export interface CreateNoteRequest {
readonly kind: 'create-note';
readonly actorUuid: string;
readonly inReplyTo?: string; // e.g. https://example.social/users/someone/statuses/123123123123123123
readonly content: LangString; // e.g. <p>Hello world</p>
readonly to: readonly string[]; // e.g. https://example.social/users/someone
readonly cc?: readonly string[]; // e.g. https://www.w3.org/ns/activitystreams#Public
}
export function checkCreateNoteRequest(obj: any): obj is CreateNoteRequest {
return isStringRecord(obj)
&& check('kind', obj.kind, v => v === 'create-note')
&& check('actorUuid', obj.actorUuid, v => typeof v === 'string' && isValidUuid(v))
&& check('inReplyTo', obj.inReplyTo, v => v === undefined || typeof v === 'string' && isValidUrl(v))
&& check('content', obj.content, v => checkLangString(v))
&& check('to', obj.to, v => Array.isArray(v) && v.every(w => typeof w === 'string' && isValidUrl(w)))
&& check('cc', obj.cc, v => v === undefined || Array.isArray(v) && v.every(w => typeof w === 'string' && isValidUrl(w)))
;
}
export interface CreateNoteResponse {
readonly kind: 'create-note';
readonly objectUuid: string,
readonly activityUuid: string,
}
// update-note
export interface UpdateNoteRequest {
readonly kind: 'update-note';
readonly objectUuid: string;
readonly content: LangString; // e.g. <p>Hello world</p>
}
export function checkUpdateNoteRequest(obj: any): obj is UpdateNoteRequest {
return isStringRecord(obj)
&& check('kind', obj.kind, v => v === 'update-note')
&& check('objectUuid', obj.objectUuid, v => typeof v === 'string' && isValidUuid(v))
&& check('content', obj.content, v => checkLangString(v))
;
}
export interface UpdateNoteResponse {
readonly kind: 'update-note';
readonly objectUuid: string;
readonly modified: boolean;
readonly activityUuid?: string; // if modified
}
// delete-note
export interface DeleteNoteRequest {
readonly kind: 'delete-note';
readonly objectUuid: string;
}
export function checkDeleteNoteRequest(obj: any): obj is DeleteNoteRequest {
return isStringRecord(obj)
&& check('kind', obj.kind, v => v === 'delete-note')
&& check('objectUuid', obj.objectUuid, v => typeof v === 'string' && isValidUuid(v))
;
}
export interface DeleteNoteResponse {
readonly kind: 'delete-note';
readonly objectUuid: string;
readonly activityUuid: string;
}
// federate-activity
export interface FederateActivityRequest {
readonly kind: 'federate-activity';
readonly activityUuid: string;
readonly dryRun?: boolean;
}
export function checkFederateActivityRequest(obj: any): obj is FederateActivityRequest {
return isStringRecord(obj)
&& check('kind', obj.kind, v => v === 'federate-activity')
&& check('activityUuid', obj.activityUuid, v => typeof v === 'string' && isValidUuid(v))
&& check('dryRun', obj.dryRun, v => v === undefined || typeof v === 'boolean')
;
}
export interface FederateActivityResponse {
readonly kind: 'federate-activity';
readonly record: FederationRecord;
readonly recipientLogs: Record<string, string[]>; // key = recipient iri
readonly modified: boolean;
}
// delete-from-storage
export interface DeleteFromStorageRequest {
readonly kind: 'delete-from-storage';
readonly domain: string;
readonly key: string;
}
export function checkDeleteFromStorageRequest(obj: any): obj is DeleteFromStorageRequest {
return isStringRecord(obj)
&& check('kind', obj.kind, v => v === 'delete-from-storage')
&& check('domain', obj.domain, v => typeof v === 'string')
&& check('key', obj.key, v => typeof v === 'string')
;
}
export interface DeleteFromStorageResponse {
readonly kind: 'delete-from-storage';
readonly existed: boolean;
}
// like object
export interface LikeObjectRequest {
readonly kind: 'like-object';
readonly actorUuid: string;
readonly objectId: string; // remote objectId, ie the canonical "id" of the object/Note coming back from an AP request
}
export function checkLikeObjectRequest(obj: any): obj is LikeObjectRequest {
return isStringRecord(obj)
&& check('kind', obj.kind, v => v === 'like-object')
&& check('actorUuid', obj.actorUuid, v => typeof v === 'string' && isValidUuid(v))
&& check('objectId', obj.objectId, v => typeof v === 'string' && isValidUrl(v))
;
}
export interface LikeObjectResponse {
readonly kind: 'like-object';
readonly activityUuid: string;
}
// undo like
export interface UndoLikeRequest {
readonly kind: 'undo-like';
readonly activityUuid: string;
}
export function checkUndoLikeRequest(obj: any): obj is UndoLikeRequest {
return isStringRecord(obj)
&& check('kind', obj.kind, v => v === 'undo-like')
&& check('activityUuid', obj.activityUuid, v => typeof v === 'string' && isValidUuid(v))
;
}
export interface UndoLikeResponse {
readonly kind: 'undo-like';
readonly activityUuid: string;
}
// generate admin token
export interface GenerateAdminTokenRequest {
readonly kind: 'generate-admin-token';
}
export function checkGenerateAdminTokenRequest(obj: any): obj is GenerateAdminTokenRequest {
return isStringRecord(obj)
&& check('kind', obj.kind, v => v === 'generate-admin-token')
;
}
export interface GenerateAdminTokenResponse {
readonly kind: 'generate-admin-token';
readonly token: string;
}
// revoke admin token
export interface RevokeAdminTokenRequest {
readonly kind: 'revoke-admin-token';
}
export function checkRevokeAdminTokenRequest(obj: any): obj is RevokeAdminTokenRequest {
return isStringRecord(obj)
&& check('kind', obj.kind, v => v === 'revoke-admin-token')
;
}
export interface RevokeAdminTokenResponse {
readonly kind: 'revoke-admin-token';
readonly existed: boolean;
}
// validate admin token
export interface ValidateAdminTokenRequest {
readonly kind: 'validate-admin-token';
readonly token: string;
}
export function checkValidateAdminTokenRequest(obj: any): obj is ValidateAdminTokenRequest {
return isStringRecord(obj)
&& check('kind', obj.kind, v => v === 'validate-admin-token')
&& check('token', obj.token, v => typeof v === 'string')
;
}
export interface ValidateAdminTokenResponse {
readonly kind: 'validate-admin-token';
readonly valid: boolean;
}