forked from SableClient/Sable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.ts
More file actions
629 lines (553 loc) · 19.1 KB
/
Copy pathmatrix.ts
File metadata and controls
629 lines (553 loc) · 19.1 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
import type { EncryptedAttachmentInfo } from 'browser-encrypt-attachment';
import { decryptAttachment } from 'browser-encrypt-attachment';
import { Channel, convertFileSrc, invoke, isTauri } from '@tauri-apps/api/core';
import type {
AccountDataEvents,
EventTimelineSet,
MatrixClient,
MatrixEvent,
Room,
RoomMember,
TimelineEvents,
UploadProgress,
UploadResponse,
} from '$types/matrix-sdk';
import {
EventTimeline,
MatrixError,
EventType,
KnownMembership,
MediaPrefix,
} from '$types/matrix-sdk';
import to from 'await-to-js';
import type { IImageInfo, IThumbnailContent, IVideoInfo } from '$types/matrix/common';
import * as Sentry from '@sentry/react';
import { encryptBlobInWorker } from '$utils/mediaWorker';
import { encryptAttachmentStreaming } from '$utils/attachmentCrypto';
import { getEventReactions, getStateEvent } from './room';
import { getReactionContent } from './messageReaction';
import { matchMxId, validMxId } from './mxIdHelper';
import {
fetchMediaBlob,
getCurrentMediaSessionScope,
type MediaTransportOptions,
} from './mediaTransport';
const DOMAIN_REGEX = /\b(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}\b/;
const TAURI_MEDIA_CACHE_VERSION = '__sable_media_cache=2';
const TAURI_MEDIA_PATH_PREFIXES = [
'/_matrix/client/v1/media/',
'/_matrix/media/v3/download/',
'/_matrix/media/v3/thumbnail/',
'/_matrix/media/r0/download/',
'/_matrix/media/r0/thumbnail/',
];
export const isServerName = (serverName: string): boolean => DOMAIN_REGEX.test(serverName);
export const getMxIdLocalPart = (userId: string): string | undefined => matchMxId(userId)?.[2];
export const isUserId = (id: string): boolean => validMxId(id) && id.startsWith('@');
export const isRoomId = (id: string): boolean => id.startsWith('!');
export const isRoomAlias = (id: string): boolean => validMxId(id) && id.startsWith('#');
export const getCanonicalAliasRoomId = (mx: MatrixClient, alias: string): string | undefined =>
mx
.getRooms()
?.find(
(room) =>
room.getCanonicalAlias() === alias &&
getStateEvent(room, EventType.RoomTombstone) === undefined
)?.roomId;
export const getCanonicalAliasOrRoomId = (mx: MatrixClient, roomId: string): string => {
const room = mx.getRoom(roomId);
if (!room) return roomId;
if (getStateEvent(room, EventType.RoomTombstone) !== undefined) return roomId;
const alias = room.getCanonicalAlias();
if (alias && getCanonicalAliasRoomId(mx, alias) === roomId) {
return alias;
}
return roomId;
};
export const getImageInfo = (img: HTMLImageElement, fileOrBlob: File | Blob): IImageInfo => {
const info: IImageInfo = {};
info.w = img.width;
info.h = img.height;
info.mimetype = fileOrBlob.type;
info.size = fileOrBlob.size;
return info;
};
export const getVideoInfo = (video: HTMLVideoElement, fileOrBlob: File | Blob): IVideoInfo => {
const info: IVideoInfo = {};
info.duration = Number.isNaN(video.duration) ? undefined : Math.floor(video.duration * 1000);
info.w = video.videoWidth;
info.h = video.videoHeight;
info.mimetype = fileOrBlob.type;
info.size = fileOrBlob.size;
return info;
};
export const getThumbnailContent = (thumbnailInfo: {
thumbnail: File | Blob;
encInfo: EncryptedAttachmentInfo | undefined;
mxc: string;
width: number;
height: number;
}): IThumbnailContent => {
const { thumbnail, encInfo, mxc, width, height } = thumbnailInfo;
const content: IThumbnailContent = {
thumbnail_info: {
mimetype: thumbnail.type,
size: thumbnail.size,
w: width,
h: height,
},
};
if (encInfo) {
content.thumbnail_file = {
...encInfo,
url: mxc,
};
} else {
content.thumbnail_url = mxc;
}
return content;
};
const getUploadFileName = (content: File | Blob): string => {
if (content instanceof File) return content.name;
const mimeSuffix = content.type.split('/')[1]?.split('+')[0]?.toLowerCase();
const extension = mimeSuffix && /^[a-z0-9]+$/.test(mimeSuffix) ? mimeSuffix : undefined;
return `upload-${Date.now()}${extension ? `.${extension}` : ''}`;
};
export const encryptFile = async <T extends File | Blob>(
file: T
): Promise<{
encInfo: EncryptedAttachmentInfo;
file: File;
originalFile: T;
}> => {
let blob: Blob;
let info: EncryptedAttachmentInfo;
try {
({ blob, info } = await encryptBlobInWorker(file));
} catch {
({ blob, info } = await encryptAttachmentStreaming(file));
}
const fileName = getUploadFileName(file);
const encFile = new File([blob], fileName, {
type: file.type,
});
return {
encInfo: info,
file: encFile,
originalFile: file,
};
};
export const decryptFile = async (
dataBuffer: ArrayBuffer,
type: string,
encInfo: EncryptedAttachmentInfo
): Promise<Blob> => {
const dataArray = await decryptAttachment(dataBuffer, encInfo);
const blob = new Blob([dataArray], { type });
return blob;
};
export type TUploadContent = File;
export type UploadContentOpts = {
name?: string;
type?: string;
includeFilename?: boolean;
progressHandler?: (progress: UploadProgress) => void;
abortController?: AbortController;
};
/**
* matrix-js-sdk's `MatrixClient.uploadContent` uploads via `XMLHttpRequest` (to
* expose progress events), which bypasses the client's configured `fetchFn`. In
* the Tauri webview that XHR is subject to CORS / Private Network Access checks
* and gets blocked, so uploads to the homeserver fail. Route the upload through
* our Tauri-aware `fetch` instead, keeping the SDK path (with progress) on web.
*/
const UPLOAD_CHUNK_SIZE = 2 * 1024 * 1024;
const blobToBase64 = (blob: Blob): Promise<string> =>
new Promise((resolve, reject) => {
const reader = new FileReader();
reader.addEventListener('load', () => {
const result = reader.result as string;
resolve(result.slice(result.indexOf(',') + 1));
});
reader.addEventListener('error', () => reject(reader.error));
reader.readAsDataURL(blob);
});
const tauriUploadAbortControllers = new WeakMap<Promise<UploadResponse>, AbortController>();
type UploadFileType = TUploadContent | Blob | XMLHttpRequestBodyInit;
export const uploadContentToServer = (
mx: MatrixClient,
file: UploadFileType,
opts: UploadContentOpts = {}
): Promise<UploadResponse> => {
if (!isTauri()) {
return mx.uploadContent(file, opts);
}
const abortController = opts.abortController ?? new AbortController();
const includeFilename = opts.includeFilename ?? true;
const isFile = file instanceof File;
const contentType =
opts.type || (file instanceof Blob ? file.type : '') || 'application/octet-stream';
const fileName = opts.name ?? (isFile ? file.name : undefined);
const url = new URL(`${mx.baseUrl}${MediaPrefix.V3}/upload`);
if (includeFilename && fileName) {
url.searchParams.set('filename', fileName);
}
const accessToken = mx.getAccessToken();
const requestId =
globalThis.crypto?.randomUUID?.() ??
`upload-${Date.now()}-${Math.random().toString(16).slice(2)}`;
const promise = (async (): Promise<UploadResponse> => {
const blob = file instanceof Blob ? file : new Blob([file as BlobPart]);
const total = blob.size;
const throwIfAborted = () => {
if (abortController.signal.aborted) {
throw new DOMException('The operation was aborted', 'AbortError');
}
};
const onAbort = () => {
void invoke('abort_native_upload', { requestId });
};
abortController.signal.addEventListener('abort', onAbort, { once: true });
const half = Math.floor(total / 2);
const onProgress = new Channel<{ loaded: number; total: number }>();
if (opts.progressHandler) {
// eslint-disable-next-line unicorn/prefer-add-event-listener -- Channel only exposes onmessage
onProgress.onmessage = (payload) => {
const sent = payload.total ? payload.loaded / payload.total : 0;
opts.progressHandler?.({ loaded: half + Math.floor(sent * (total - half)), total });
};
}
const writeChunk = async (start: number) => {
const chunk = await blobToBase64(blob.slice(start, start + UPLOAD_CHUNK_SIZE));
await invoke('upload_write_chunk', { requestId, chunk });
};
try {
for (let offset = 0; offset < total; offset += UPLOAD_CHUNK_SIZE) {
throwIfAborted();
const end = Math.min(offset + UPLOAD_CHUNK_SIZE, total);
// eslint-disable-next-line no-await-in-loop -- sequential chunks bound webview memory
await writeChunk(offset);
opts.progressHandler?.({ loaded: Math.floor(end / 2), total });
}
throwIfAborted();
const result = await invoke<{ status: number; body: string }>('native_upload', {
requestId,
url: url.toString(),
contentType,
authorization: accessToken ? `Bearer ${accessToken}` : null,
onProgress,
});
if (result.status < 200 || result.status >= 300) {
let parsed: { errcode?: string; error?: string } = {};
try {
parsed = JSON.parse(result.body);
} catch {
// Non-JSON error body; fall back to the status code.
}
throw new MatrixError({
errcode: parsed.errcode,
error: parsed.error ?? `Upload failed with status ${result.status}`,
});
}
return JSON.parse(result.body) as UploadResponse;
} catch (err) {
void invoke('abort_native_upload', { requestId });
throw err;
} finally {
abortController.signal.removeEventListener('abort', onAbort);
}
})();
tauriUploadAbortControllers.set(promise, abortController);
void promise.finally(() => tauriUploadAbortControllers.delete(promise));
return promise;
};
export const cancelUploadContent = (
mx: MatrixClient,
promise: Promise<UploadResponse>
): boolean => {
const abortController = tauriUploadAbortControllers.get(promise);
if (abortController) {
abortController.abort();
return true;
}
return mx.cancelUpload(promise);
};
export type ContentUploadOptions = {
name?: string;
fileType?: string;
hideFilename?: boolean;
onPromise?: (promise: Promise<UploadResponse>) => void;
onProgress?: (progress: UploadProgress) => void;
onSuccess: (mxc: string) => void;
onError: (error: MatrixError) => void;
};
export const uploadContent = async (
mx: MatrixClient,
file: TUploadContent,
options: ContentUploadOptions
) => {
const { name, fileType, hideFilename, onProgress, onPromise, onSuccess, onError } = options;
const uploadStart = performance.now();
const uploadPromise = uploadContentToServer(mx, file, {
name,
type: fileType,
includeFilename: !hideFilename,
progressHandler: onProgress,
});
onPromise?.(uploadPromise);
try {
const data = await uploadPromise;
const mxc = data.content_uri;
if (mxc) {
const mediaType = file.type.split('/')[0] || 'unknown';
Sentry.metrics.distribution(
'sable.media.upload_latency_ms',
performance.now() - uploadStart,
{
attributes: { type: mediaType },
}
);
Sentry.metrics.distribution('sable.media.upload_bytes', file.size, {
attributes: { type: mediaType },
});
onSuccess(mxc);
} else {
Sentry.metrics.count('sable.media.upload_error', 1, {
attributes: { reason: 'no_uri' },
});
onError(new MatrixError(data));
}
} catch (e: unknown) {
Sentry.metrics.count('sable.media.upload_error', 1, {
attributes: { reason: 'exception' },
});
const err = e as { message?: string; name?: string };
const error = typeof err?.message === 'string' ? err.message : undefined;
const errcode = typeof err?.name === 'string' ? err.name : undefined;
onError(new MatrixError({ error, errcode }));
}
};
export const matrixEventByRecency = (m1: MatrixEvent, m2: MatrixEvent) => m2.getTs() - m1.getTs();
export const factoryEventSentBy = (senderId: string) => (ev: MatrixEvent) =>
ev.getSender() === senderId;
export const eventWithShortcode = (ev: MatrixEvent) =>
typeof ev.getContent().shortcode === 'string';
export const getDMRoomFor = (mx: MatrixClient, userId: string): Room | undefined => {
const dmLikeRooms = mx
.getRooms()
.filter(
(room) =>
room.getMyMembership() === (KnownMembership.Join as string) &&
room.hasEncryptionStateEvent() &&
room.getMembers().length <= 2
);
return dmLikeRooms.find((room) => room.getMember(userId));
};
export const guessDmRoomUserId = (room: Room, myUserId: string): string => {
const getOldestMember = (members: RoomMember[]): RoomMember | undefined => {
let oldestMemberTs: number | undefined;
let oldestMember: RoomMember | undefined;
const pickOldestMember = (member: RoomMember) => {
if (member.userId === myUserId) return;
if (
oldestMemberTs === undefined ||
(member.events.member && member.events.member.getTs() < oldestMemberTs)
) {
oldestMember = member;
oldestMemberTs = member.events.member?.getTs();
}
};
members.forEach(pickOldestMember);
return oldestMember;
};
// Pick the joined user who's been here longest (and isn't us),
const member = getOldestMember(room.getJoinedMembers());
if (member) return member.userId;
// if there are no joined members other than us, use the oldest member
const member1 = getOldestMember(
room.getLiveTimeline().getState(EventTimeline.FORWARDS)?.getMembers() ?? []
);
return member1?.userId ?? myUserId;
};
export const addRoomIdToMDirect = async (
mx: MatrixClient,
roomId: string,
userId: string
): Promise<void> => {
const mDirectsEvent = mx.getAccountData(
EventType.Direct as string as unknown as keyof AccountDataEvents
);
let userIdToRoomIds: Record<string, string[]> = {};
if (typeof mDirectsEvent !== 'undefined')
userIdToRoomIds = structuredClone(mDirectsEvent.getContent());
// remove it from the lists of any others users
// (it can only be a DM room for one person)
Object.keys(userIdToRoomIds).forEach((targetUserId) => {
const roomIds = userIdToRoomIds[targetUserId]!;
if (targetUserId !== userId) {
const indexOfRoomId = roomIds.indexOf(roomId);
if (indexOfRoomId > -1) {
roomIds.splice(indexOfRoomId, 1);
}
}
});
const roomIds = userIdToRoomIds[userId] || [];
if (roomIds.indexOf(roomId) === -1) {
roomIds.push(roomId);
}
userIdToRoomIds[userId] = roomIds;
await mx.setAccountData(
EventType.Direct as string as unknown as keyof AccountDataEvents,
userIdToRoomIds
);
};
export const removeRoomIdFromMDirect = async (mx: MatrixClient, roomId: string): Promise<void> => {
const mDirectsEvent = mx.getAccountData(
EventType.Direct as string as unknown as keyof AccountDataEvents
);
let userIdToRoomIds: Record<string, string[]> = {};
if (typeof mDirectsEvent !== 'undefined')
userIdToRoomIds = structuredClone(mDirectsEvent.getContent());
Object.keys(userIdToRoomIds).forEach((targetUserId) => {
const roomIds = userIdToRoomIds[targetUserId]!;
const indexOfRoomId = roomIds.indexOf(roomId);
if (indexOfRoomId > -1) {
roomIds.splice(indexOfRoomId, 1);
}
});
await mx.setAccountData(
EventType.Direct as string as unknown as keyof AccountDataEvents,
userIdToRoomIds
);
};
export const rewriteAuthenticatedMediaUrl = (httpUrl: string | null): string | null => {
if (!httpUrl) return null;
if (!isTauri()) return httpUrl;
const sourceUrl = httpUrl.startsWith('sable-media://')
? httpUrl.slice('sable-media://'.length)
: httpUrl;
let parsedUrl: URL;
try {
parsedUrl = new URL(sourceUrl);
} catch {
return httpUrl;
}
if (
(parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') ||
parsedUrl.origin === 'null' ||
!TAURI_MEDIA_PATH_PREFIXES.some((path) => parsedUrl.pathname.startsWith(path))
) {
return httpUrl;
}
if (httpUrl.includes(TAURI_MEDIA_CACHE_VERSION)) return httpUrl;
const mediaUrl = httpUrl.startsWith('sable-media://')
? httpUrl
: convertFileSrc(httpUrl, 'sable-media');
// Session-scoped so the cacheable response is never shared across accounts.
const sessionScope = encodeURIComponent(getCurrentMediaSessionScope());
const separator = mediaUrl.includes('?') ? '&' : '?';
return `${mediaUrl}${separator}${TAURI_MEDIA_CACHE_VERSION}&__sable_media_session=${sessionScope}`;
};
export const mxcUrlToHttp = (
mx: MatrixClient,
mxcUrl: string,
useAuthentication?: boolean,
width?: number,
height?: number,
resizeMethod?: string,
allowDirectLinks?: boolean
): string | null => {
const httpUrl = mx.mxcUrlToHttp(
mxcUrl.replace(/^["']|["']$/g, ''),
width,
height,
resizeMethod,
allowDirectLinks,
undefined,
useAuthentication
);
if (httpUrl && isTauri()) {
return rewriteAuthenticatedMediaUrl(httpUrl);
}
return httpUrl;
};
export const downloadMedia = async (src: string, options?: MediaTransportOptions): Promise<Blob> =>
fetchMediaBlob(src, options);
export const downloadEncryptedMedia = async (
src: string,
decryptContent: (buf: ArrayBuffer) => Promise<Blob>
): Promise<Blob> => {
const encryptedContent = await downloadMedia(src);
return decryptContent(await encryptedContent.arrayBuffer());
};
const sleepForMs = (ms: number) =>
new Promise((resolve) => {
setTimeout(resolve, ms);
});
export const rateLimitedActions = async <T, R = void>(
data: T[],
callback: (item: T, index: number) => Promise<R>,
maxRetryCount?: number
) => {
let retryCount = 0;
let actionInterval = 0;
const performAction = async (dataItem: T, index: number) => {
const [err] = await to<R, MatrixError>(callback(dataItem, index));
if (err?.httpStatus === 429) {
if (retryCount === maxRetryCount) {
return;
}
const waitMS = err.getRetryAfterMs() ?? 3000;
actionInterval = waitMS * 1.5;
await sleepForMs(waitMS);
retryCount += 1;
await performAction(dataItem, index);
}
};
for (let i = 0; i < data.length; i += 1) {
const dataItem = data[i]!;
retryCount = 0;
// oxlint-disable-next-line no-await-in-loop
await performAction(dataItem, i);
if (actionInterval > 0) {
// oxlint-disable-next-line no-await-in-loop
await sleepForMs(actionInterval);
}
}
};
export const toggleReaction = (
mx: MatrixClient,
room: Room,
targetEventId: string,
key: string,
shortcode?: string,
timelineSet?: EventTimelineSet
) => {
const relations = getEventReactions(
timelineSet ?? room.getUnfilteredTimelineSet(),
targetEventId
);
const allReactions = relations?.getSortedAnnotationsByKey() ?? [];
const [, reactionsSet] = allReactions.find(([k]) => k === key) ?? [];
const reactions: MatrixEvent[] = reactionsSet ? Array.from(reactionsSet) : [];
const myReaction = reactions.find(factoryEventSentBy(mx.getUserId()!));
if (myReaction && myReaction.isRelation?.()) {
const eventId = myReaction.getId();
if (eventId) mx.redactEvent(room.roomId, eventId);
return;
}
const rShortcode =
shortcode || (reactions.find(eventWithShortcode)?.getContent().shortcode as string | undefined);
// send the reaction
mx.sendEvent(
room.roomId,
EventType.Reaction as string as unknown as keyof TimelineEvents,
getReactionContent(
targetEventId,
key,
mx,
room,
rShortcode
) as TimelineEvents[keyof TimelineEvents]
);
};