@tagea/capacitor-matrix 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,950 @@
1
+ import { WebPlugin } from '@capacitor/core';
2
+ import { createClient, ClientEvent, RoomEvent, RoomMemberEvent, Direction, MsgType, EventType, RelationType, UserEvent, } from 'matrix-js-sdk';
3
+ import { decodeRecoveryKey } from 'matrix-js-sdk/lib/crypto-api/recovery-key';
4
+ import { deriveRecoveryKeyFromPassphrase } from 'matrix-js-sdk/lib/crypto-api/key-passphrase';
5
+ const SESSION_KEY = 'matrix_session';
6
+ export class MatrixWeb extends WebPlugin {
7
+ constructor() {
8
+ super(...arguments);
9
+ this._cryptoCallbacks = {
10
+ getSecretStorageKey: async (opts) => {
11
+ var _a;
12
+ const keyId = Object.keys(opts.keys)[0];
13
+ if (!keyId)
14
+ return null;
15
+ // If we have the raw key cached, use it directly
16
+ if (this.secretStorageKey) {
17
+ return [keyId, this.secretStorageKey];
18
+ }
19
+ // If we have a passphrase, derive the key using the server's stored parameters
20
+ if (this.recoveryPassphrase) {
21
+ const keyInfo = opts.keys[keyId];
22
+ if (keyInfo === null || keyInfo === void 0 ? void 0 : keyInfo.passphrase) {
23
+ const derived = await deriveRecoveryKeyFromPassphrase(this.recoveryPassphrase, keyInfo.passphrase.salt, keyInfo.passphrase.iterations, (_a = keyInfo.passphrase.bits) !== null && _a !== void 0 ? _a : 256);
24
+ this.secretStorageKey = derived;
25
+ return [keyId, derived];
26
+ }
27
+ }
28
+ return null;
29
+ },
30
+ cacheSecretStorageKey: (_keyId, _keyInfo, key) => {
31
+ this.secretStorageKey = key;
32
+ },
33
+ };
34
+ }
35
+ // ── Auth ──────────────────────────────────────────────
36
+ async login(options) {
37
+ const tmpClient = createClient({ baseUrl: options.homeserverUrl });
38
+ const res = await tmpClient.loginWithPassword(options.userId, options.password);
39
+ this.client = createClient({
40
+ baseUrl: options.homeserverUrl,
41
+ accessToken: res.access_token,
42
+ userId: res.user_id,
43
+ deviceId: res.device_id,
44
+ cryptoCallbacks: this._cryptoCallbacks,
45
+ });
46
+ const session = {
47
+ accessToken: res.access_token,
48
+ userId: res.user_id,
49
+ deviceId: res.device_id,
50
+ homeserverUrl: options.homeserverUrl,
51
+ };
52
+ this.persistSession(session);
53
+ return session;
54
+ }
55
+ async loginWithToken(options) {
56
+ this.client = createClient({
57
+ baseUrl: options.homeserverUrl,
58
+ accessToken: options.accessToken,
59
+ userId: options.userId,
60
+ deviceId: options.deviceId,
61
+ cryptoCallbacks: this._cryptoCallbacks,
62
+ });
63
+ const session = {
64
+ accessToken: options.accessToken,
65
+ userId: options.userId,
66
+ deviceId: options.deviceId,
67
+ homeserverUrl: options.homeserverUrl,
68
+ };
69
+ this.persistSession(session);
70
+ return session;
71
+ }
72
+ async logout() {
73
+ if (this.client) {
74
+ this.client.stopClient();
75
+ try {
76
+ await this.client.logout(true);
77
+ }
78
+ catch (_a) {
79
+ // ignore logout errors (e.g. token already invalidated)
80
+ }
81
+ this.client = undefined;
82
+ }
83
+ localStorage.removeItem(SESSION_KEY);
84
+ }
85
+ async getSession() {
86
+ const raw = localStorage.getItem(SESSION_KEY);
87
+ if (!raw)
88
+ return null;
89
+ try {
90
+ return JSON.parse(raw);
91
+ }
92
+ catch (_a) {
93
+ return null;
94
+ }
95
+ }
96
+ // ── Sync ──────────────────────────────────────────────
97
+ async startSync() {
98
+ this.requireClient();
99
+ this.client.on(ClientEvent.Sync, (state, _prev, data) => {
100
+ var _a;
101
+ const mapped = this.mapSyncState(state);
102
+ this.notifyListeners('syncStateChange', {
103
+ state: mapped,
104
+ error: (_a = data === null || data === void 0 ? void 0 : data.error) === null || _a === void 0 ? void 0 : _a.message,
105
+ });
106
+ });
107
+ this.client.on(RoomEvent.Timeline, (event, room) => {
108
+ var _a;
109
+ this.notifyListeners('messageReceived', {
110
+ event: this.serializeEvent(event, room === null || room === void 0 ? void 0 : room.roomId),
111
+ });
112
+ // When an encrypted event arrives, listen for decryption and re-notify
113
+ if (event.isBeingDecrypted() || event.getType() === 'm.room.encrypted') {
114
+ event.once('Event.decrypted', () => {
115
+ this.notifyListeners('messageReceived', {
116
+ event: this.serializeEvent(event, room === null || room === void 0 ? void 0 : room.roomId),
117
+ });
118
+ });
119
+ }
120
+ // When a reaction or redaction arrives, re-emit the parent event with updated aggregated reactions
121
+ if (event.getType() === EventType.Reaction || event.getType() === EventType.RoomRedaction) {
122
+ const rel = (_a = event.getContent()) === null || _a === void 0 ? void 0 : _a['m.relates_to'];
123
+ const targetId = (rel === null || rel === void 0 ? void 0 : rel.event_id) || event.getAssociatedId();
124
+ if (targetId && room) {
125
+ const targetEvent = room.findEventById(targetId);
126
+ if (targetEvent) {
127
+ // Small delay to let the SDK finish aggregation
128
+ setTimeout(() => {
129
+ this.notifyListeners('messageReceived', {
130
+ event: this.serializeEvent(targetEvent, room.roomId),
131
+ });
132
+ }, 100);
133
+ }
134
+ }
135
+ }
136
+ });
137
+ this.client.on(RoomEvent.Receipt, (_event, room) => {
138
+ var _a;
139
+ this.notifyListeners('receiptReceived', {
140
+ roomId: room.roomId,
141
+ });
142
+ // Re-emit own sent messages with updated read status
143
+ const myUserId = (_a = this.client) === null || _a === void 0 ? void 0 : _a.getUserId();
144
+ if (myUserId) {
145
+ const timeline = room.getLiveTimeline().getEvents();
146
+ // Walk backwards through recent events; stop after checking a reasonable batch
147
+ const limit = Math.min(timeline.length, 50);
148
+ for (let i = timeline.length - 1; i >= timeline.length - limit; i--) {
149
+ const evt = timeline[i];
150
+ if (evt.getSender() !== myUserId)
151
+ continue;
152
+ const serialized = this.serializeEvent(evt, room.roomId);
153
+ if (serialized.status === 'read') {
154
+ this.notifyListeners('messageReceived', { event: serialized });
155
+ }
156
+ }
157
+ }
158
+ });
159
+ this.client.on(RoomEvent.Name, (room) => {
160
+ this.notifyListeners('roomUpdated', {
161
+ roomId: room.roomId,
162
+ summary: this.serializeRoom(room),
163
+ });
164
+ });
165
+ this.client.on(RoomMemberEvent.Typing, (_event, member) => {
166
+ var _a, _b;
167
+ const roomId = member === null || member === void 0 ? void 0 : member.roomId;
168
+ if (roomId) {
169
+ const room = this.client.getRoom(roomId);
170
+ if (room) {
171
+ const typingEvent = room.currentState.getStateEvents('m.typing', '');
172
+ const userIds = (_b = (_a = typingEvent === null || typingEvent === void 0 ? void 0 : typingEvent.getContent()) === null || _a === void 0 ? void 0 : _a.user_ids) !== null && _b !== void 0 ? _b : [];
173
+ this.notifyListeners('typingChanged', { roomId, userIds });
174
+ }
175
+ }
176
+ });
177
+ this.client.on(UserEvent.Presence, (_event, user) => {
178
+ var _a, _b, _c;
179
+ this.notifyListeners('presenceChanged', {
180
+ userId: user.userId,
181
+ presence: {
182
+ presence: (_a = user.presence) !== null && _a !== void 0 ? _a : 'offline',
183
+ statusMsg: (_b = user.presenceStatusMsg) !== null && _b !== void 0 ? _b : undefined,
184
+ lastActiveAgo: (_c = user.lastActiveAgo) !== null && _c !== void 0 ? _c : undefined,
185
+ },
186
+ });
187
+ });
188
+ await this.client.startClient({ initialSyncLimit: 20 });
189
+ }
190
+ async stopSync() {
191
+ this.requireClient();
192
+ this.client.stopClient();
193
+ }
194
+ async getSyncState() {
195
+ this.requireClient();
196
+ const raw = this.client.getSyncState();
197
+ return { state: this.mapSyncState(raw) };
198
+ }
199
+ // ── Rooms ─────────────────────────────────────────────
200
+ async createRoom(options) {
201
+ var _a;
202
+ this.requireClient();
203
+ const createOpts = {
204
+ visibility: 'private',
205
+ };
206
+ if (options.name)
207
+ createOpts.name = options.name;
208
+ if (options.topic)
209
+ createOpts.topic = options.topic;
210
+ if ((_a = options.invite) === null || _a === void 0 ? void 0 : _a.length)
211
+ createOpts.invite = options.invite;
212
+ if (options.preset)
213
+ createOpts.preset = options.preset;
214
+ if (options.isDirect)
215
+ createOpts.is_direct = true;
216
+ const initialState = [];
217
+ if (options.isEncrypted) {
218
+ initialState.push({
219
+ type: 'm.room.encryption',
220
+ state_key: '',
221
+ content: { algorithm: 'm.megolm.v1.aes-sha2' },
222
+ });
223
+ }
224
+ if (options.historyVisibility) {
225
+ initialState.push({
226
+ type: 'm.room.history_visibility',
227
+ state_key: '',
228
+ content: { history_visibility: options.historyVisibility },
229
+ });
230
+ }
231
+ if (initialState.length > 0) {
232
+ createOpts.initial_state = initialState;
233
+ }
234
+ const res = await this.client.createRoom(createOpts);
235
+ return { roomId: res.room_id };
236
+ }
237
+ async getRooms() {
238
+ this.requireClient();
239
+ const rooms = this.client.getRooms().map((r) => this.serializeRoom(r));
240
+ return { rooms };
241
+ }
242
+ async getRoomMembers(options) {
243
+ this.requireClient();
244
+ const room = this.client.getRoom(options.roomId);
245
+ if (!room)
246
+ throw new Error(`Room ${options.roomId} not found`);
247
+ await room.loadMembersIfNeeded();
248
+ const members = room.getMembers().map((m) => {
249
+ var _a, _b;
250
+ return ({
251
+ userId: m.userId,
252
+ displayName: (_a = m.name) !== null && _a !== void 0 ? _a : undefined,
253
+ membership: m.membership,
254
+ avatarUrl: (_b = m.getMxcAvatarUrl()) !== null && _b !== void 0 ? _b : undefined,
255
+ });
256
+ });
257
+ return { members };
258
+ }
259
+ async joinRoom(options) {
260
+ this.requireClient();
261
+ const room = await this.client.joinRoom(options.roomIdOrAlias);
262
+ return { roomId: room.roomId };
263
+ }
264
+ async leaveRoom(options) {
265
+ this.requireClient();
266
+ await this.client.leave(options.roomId);
267
+ }
268
+ async forgetRoom(options) {
269
+ this.requireClient();
270
+ await this.client.forget(options.roomId);
271
+ }
272
+ // ── Messaging ─────────────────────────────────────────
273
+ async sendMessage(options) {
274
+ var _a, _b, _c;
275
+ this.requireClient();
276
+ const msgtype = (_a = options.msgtype) !== null && _a !== void 0 ? _a : 'm.text';
277
+ const mediaTypes = ['m.image', 'm.audio', 'm.video', 'm.file'];
278
+ if (mediaTypes.includes(msgtype) && options.fileUri) {
279
+ // Media message: upload file then send
280
+ const response = await fetch(options.fileUri);
281
+ const blob = await response.blob();
282
+ const uploadRes = await this.client.uploadContent(blob, {
283
+ name: options.fileName,
284
+ type: options.mimeType,
285
+ });
286
+ const mxcUrl = uploadRes.content_uri;
287
+ const content = {
288
+ msgtype,
289
+ body: options.body || options.fileName || 'file',
290
+ url: mxcUrl,
291
+ info: {
292
+ mimetype: options.mimeType,
293
+ size: (_b = options.fileSize) !== null && _b !== void 0 ? _b : blob.size,
294
+ },
295
+ };
296
+ const res = await this.client.sendMessage(options.roomId, content);
297
+ return { eventId: res.event_id };
298
+ }
299
+ // Text message
300
+ const msgtypeMap = {
301
+ 'm.text': MsgType.Text,
302
+ 'm.notice': MsgType.Notice,
303
+ 'm.emote': MsgType.Emote,
304
+ };
305
+ const mappedType = (_c = msgtypeMap[msgtype]) !== null && _c !== void 0 ? _c : MsgType.Text;
306
+ const res = await this.client.sendMessage(options.roomId, {
307
+ msgtype: mappedType,
308
+ body: options.body,
309
+ });
310
+ return { eventId: res.event_id };
311
+ }
312
+ async editMessage(options) {
313
+ this.requireClient();
314
+ const content = {
315
+ msgtype: MsgType.Text,
316
+ body: `* ${options.newBody}`,
317
+ 'm.new_content': {
318
+ msgtype: MsgType.Text,
319
+ body: options.newBody,
320
+ },
321
+ 'm.relates_to': {
322
+ rel_type: 'm.replace',
323
+ event_id: options.eventId,
324
+ },
325
+ };
326
+ const res = await this.client.sendMessage(options.roomId, content);
327
+ return { eventId: res.event_id };
328
+ }
329
+ async sendReply(options) {
330
+ var _a, _b;
331
+ this.requireClient();
332
+ const msgtype = (_a = options.msgtype) !== null && _a !== void 0 ? _a : 'm.text';
333
+ const mediaTypes = ['m.image', 'm.audio', 'm.video', 'm.file'];
334
+ let content;
335
+ if (mediaTypes.includes(msgtype) && options.fileUri) {
336
+ // Media reply: upload file then send with reply relation
337
+ const response = await fetch(options.fileUri);
338
+ const blob = await response.blob();
339
+ const uploadRes = await this.client.uploadContent(blob, {
340
+ name: options.fileName,
341
+ type: options.mimeType,
342
+ });
343
+ content = {
344
+ msgtype,
345
+ body: options.body || options.fileName || 'file',
346
+ url: uploadRes.content_uri,
347
+ info: {
348
+ mimetype: options.mimeType,
349
+ size: (_b = options.fileSize) !== null && _b !== void 0 ? _b : blob.size,
350
+ },
351
+ 'm.relates_to': {
352
+ 'm.in_reply_to': {
353
+ event_id: options.replyToEventId,
354
+ },
355
+ },
356
+ };
357
+ }
358
+ else {
359
+ // Text reply
360
+ content = {
361
+ msgtype: MsgType.Text,
362
+ body: options.body,
363
+ 'm.relates_to': {
364
+ 'm.in_reply_to': {
365
+ event_id: options.replyToEventId,
366
+ },
367
+ },
368
+ };
369
+ }
370
+ const res = await this.client.sendMessage(options.roomId, content);
371
+ return { eventId: res.event_id };
372
+ }
373
+ async getRoomMessages(options) {
374
+ var _a, _b, _c, _d, _e;
375
+ this.requireClient();
376
+ const limit = (_a = options.limit) !== null && _a !== void 0 ? _a : 20;
377
+ const room = this.client.getRoom(options.roomId);
378
+ // If no explicit pagination token, return events from the synced timeline
379
+ if (!options.from && room) {
380
+ // Paginate backwards so we have enough events (initial sync may be small)
381
+ try {
382
+ await this.client.scrollback(room, limit);
383
+ }
384
+ catch (_f) {
385
+ // scrollback may fail if there's no more history
386
+ }
387
+ const timeline = room.getLiveTimeline();
388
+ const timelineEvents = timeline.getEvents();
389
+ // Filter out reactions and redactions before slicing — they're aggregated into parent events
390
+ const displayableEvents = timelineEvents.filter((e) => {
391
+ const t = e.getType();
392
+ return t !== EventType.Reaction && t !== EventType.RoomRedaction;
393
+ });
394
+ const events = displayableEvents
395
+ .slice(-limit)
396
+ .map((e) => this.serializeEvent(e, options.roomId))
397
+ .sort((a, b) => a.originServerTs - b.originServerTs);
398
+ const backToken = (_b = timeline.getPaginationToken(Direction.Backward)) !== null && _b !== void 0 ? _b : undefined;
399
+ return { events, nextBatch: backToken };
400
+ }
401
+ // Paginate further back using the token
402
+ const fromToken = (_c = options.from) !== null && _c !== void 0 ? _c : null;
403
+ const res = await this.client.createMessagesRequest(options.roomId, fromToken, limit, Direction.Backward);
404
+ const events = ((_d = res.chunk) !== null && _d !== void 0 ? _d : []).map((e) => {
405
+ var _a;
406
+ return ({
407
+ eventId: e.event_id,
408
+ roomId: options.roomId,
409
+ senderId: e.sender,
410
+ type: e.type,
411
+ content: ((_a = e.content) !== null && _a !== void 0 ? _a : {}),
412
+ originServerTs: e.origin_server_ts,
413
+ });
414
+ });
415
+ return { events, nextBatch: (_e = res.end) !== null && _e !== void 0 ? _e : undefined };
416
+ }
417
+ async markRoomAsRead(options) {
418
+ this.requireClient();
419
+ const room = this.client.getRoom(options.roomId);
420
+ if (room) {
421
+ const event = room.findEventById(options.eventId);
422
+ if (event) {
423
+ await this.client.sendReadReceipt(event);
424
+ return;
425
+ }
426
+ }
427
+ // Fallback to HTTP request if event not found locally
428
+ await this.client.setRoomReadMarkersHttpRequest(options.roomId, options.eventId, options.eventId);
429
+ }
430
+ async refreshEventStatuses(options) {
431
+ this.requireClient();
432
+ const room = this.client.getRoom(options.roomId);
433
+ if (!room)
434
+ return { events: [] };
435
+ const events = [];
436
+ for (const eid of options.eventIds) {
437
+ const event = room.findEventById(eid);
438
+ if (event) {
439
+ events.push(this.serializeEvent(event, options.roomId));
440
+ }
441
+ }
442
+ return { events };
443
+ }
444
+ // ── Redactions & Reactions ───────────────────────────────
445
+ async redactEvent(options) {
446
+ this.requireClient();
447
+ await this.client.redactEvent(options.roomId, options.eventId, undefined, {
448
+ reason: options.reason,
449
+ });
450
+ }
451
+ async sendReaction(options) {
452
+ this.requireClient();
453
+ const myUserId = this.client.getUserId();
454
+ // Check if the user already reacted with this key — if so, toggle off (redact)
455
+ const room = this.client.getRoom(options.roomId);
456
+ if (room && myUserId) {
457
+ try {
458
+ const relations = room.relations.getChildEventsForEvent(options.eventId, RelationType.Annotation, EventType.Reaction);
459
+ if (relations) {
460
+ const existing = relations.getRelations().find((e) => { var _a, _b; return e.getSender() === myUserId && ((_b = (_a = e.getContent()) === null || _a === void 0 ? void 0 : _a['m.relates_to']) === null || _b === void 0 ? void 0 : _b.key) === options.key; });
461
+ if (existing) {
462
+ const existingId = existing.getId();
463
+ if (existingId) {
464
+ await this.client.redactEvent(options.roomId, existingId);
465
+ return { eventId: existingId };
466
+ }
467
+ }
468
+ }
469
+ }
470
+ catch (_a) {
471
+ // fall through to send
472
+ }
473
+ }
474
+ const res = await this.client.sendEvent(options.roomId, EventType.Reaction, {
475
+ 'm.relates_to': {
476
+ rel_type: RelationType.Annotation,
477
+ event_id: options.eventId,
478
+ key: options.key,
479
+ },
480
+ });
481
+ return { eventId: res.event_id };
482
+ }
483
+ // ── Room Management ────────────────────────────────────
484
+ async setRoomName(options) {
485
+ this.requireClient();
486
+ await this.client.setRoomName(options.roomId, options.name);
487
+ }
488
+ async setRoomTopic(options) {
489
+ this.requireClient();
490
+ await this.client.setRoomTopic(options.roomId, options.topic);
491
+ }
492
+ async setRoomAvatar(options) {
493
+ this.requireClient();
494
+ await this.client.sendStateEvent(options.roomId, 'm.room.avatar', { url: options.mxcUrl });
495
+ }
496
+ async inviteUser(options) {
497
+ this.requireClient();
498
+ await this.client.invite(options.roomId, options.userId);
499
+ }
500
+ async kickUser(options) {
501
+ this.requireClient();
502
+ await this.client.kick(options.roomId, options.userId, options.reason);
503
+ }
504
+ async banUser(options) {
505
+ this.requireClient();
506
+ await this.client.ban(options.roomId, options.userId, options.reason);
507
+ }
508
+ async unbanUser(options) {
509
+ this.requireClient();
510
+ await this.client.unban(options.roomId, options.userId);
511
+ }
512
+ // ── Typing ─────────────────────────────────────────────
513
+ async sendTyping(options) {
514
+ var _a;
515
+ this.requireClient();
516
+ await this.client.sendTyping(options.roomId, options.isTyping, (_a = options.timeout) !== null && _a !== void 0 ? _a : 30000);
517
+ }
518
+ // ── Media ──────────────────────────────────────────────
519
+ async getMediaUrl(options) {
520
+ this.requireClient();
521
+ // Use the authenticated media endpoint (Matrix v1.11+)
522
+ const mxcPath = options.mxcUrl.replace('mxc://', '');
523
+ const baseUrl = this.client.getHomeserverUrl().replace(/\/$/, '');
524
+ const accessToken = this.client.getAccessToken();
525
+ const httpUrl = `${baseUrl}/_matrix/client/v1/media/download/${mxcPath}?access_token=${accessToken}`;
526
+ return { httpUrl };
527
+ }
528
+ async getThumbnailUrl(options) {
529
+ var _a;
530
+ this.requireClient();
531
+ const mxcPath = options.mxcUrl.replace('mxc://', '');
532
+ const baseUrl = this.client.getHomeserverUrl().replace(/\/$/, '');
533
+ const accessToken = this.client.getAccessToken();
534
+ const method = (_a = options.method) !== null && _a !== void 0 ? _a : 'scale';
535
+ const httpUrl = `${baseUrl}/_matrix/client/v1/media/thumbnail/${mxcPath}?width=${options.width}&height=${options.height}&method=${method}&access_token=${accessToken}`;
536
+ return { httpUrl };
537
+ }
538
+ async uploadContent(options) {
539
+ this.requireClient();
540
+ const response = await fetch(options.fileUri);
541
+ const blob = await response.blob();
542
+ const res = await this.client.uploadContent(blob, {
543
+ name: options.fileName,
544
+ type: options.mimeType,
545
+ });
546
+ return { contentUri: res.content_uri };
547
+ }
548
+ // ── Presence ───────────────────────────────────────────
549
+ async setPresence(options) {
550
+ this.requireClient();
551
+ await this.client.setPresence({
552
+ presence: options.presence,
553
+ status_msg: options.statusMsg,
554
+ });
555
+ }
556
+ async getPresence(options) {
557
+ var _a, _b, _c;
558
+ this.requireClient();
559
+ const user = this.client.getUser(options.userId);
560
+ return {
561
+ presence: (_a = user === null || user === void 0 ? void 0 : user.presence) !== null && _a !== void 0 ? _a : 'offline',
562
+ statusMsg: (_b = user === null || user === void 0 ? void 0 : user.presenceStatusMsg) !== null && _b !== void 0 ? _b : undefined,
563
+ lastActiveAgo: (_c = user === null || user === void 0 ? void 0 : user.lastActiveAgo) !== null && _c !== void 0 ? _c : undefined,
564
+ };
565
+ }
566
+ // ── Device Management ──────────────────────────────────
567
+ async getDevices() {
568
+ var _a;
569
+ this.requireClient();
570
+ const res = await this.client.getDevices();
571
+ const devices = ((_a = res.devices) !== null && _a !== void 0 ? _a : []).map((d) => {
572
+ var _a, _b, _c;
573
+ return ({
574
+ deviceId: d.device_id,
575
+ displayName: (_a = d.display_name) !== null && _a !== void 0 ? _a : undefined,
576
+ lastSeenTs: (_b = d.last_seen_ts) !== null && _b !== void 0 ? _b : undefined,
577
+ lastSeenIp: (_c = d.last_seen_ip) !== null && _c !== void 0 ? _c : undefined,
578
+ });
579
+ });
580
+ return { devices };
581
+ }
582
+ async deleteDevice(options) {
583
+ this.requireClient();
584
+ await this.client.deleteDevice(options.deviceId, options.auth);
585
+ }
586
+ // ── Push ──────────────────────────────────────────────
587
+ async setPusher(options) {
588
+ var _a;
589
+ this.requireClient();
590
+ await this.client.setPusher({
591
+ pushkey: options.pushkey,
592
+ kind: (_a = options.kind) !== null && _a !== void 0 ? _a : undefined,
593
+ app_id: options.appId,
594
+ app_display_name: options.appDisplayName,
595
+ device_display_name: options.deviceDisplayName,
596
+ lang: options.lang,
597
+ data: options.data,
598
+ });
599
+ }
600
+ // ── Encryption ──────────────────────────────────────────
601
+ async initializeCrypto() {
602
+ this.requireClient();
603
+ const userId = this.client.getUserId();
604
+ const deviceId = this.client.getDeviceId();
605
+ await this.client.initRustCrypto({
606
+ cryptoDatabasePrefix: `matrix-js-sdk/${userId}/${deviceId}`,
607
+ });
608
+ }
609
+ async getEncryptionStatus() {
610
+ this.requireClient();
611
+ const crypto = this.client.getCrypto();
612
+ if (!crypto) {
613
+ return {
614
+ isCrossSigningReady: false,
615
+ crossSigningStatus: { hasMaster: false, hasSelfSigning: false, hasUserSigning: false, isReady: false },
616
+ isKeyBackupEnabled: false,
617
+ isSecretStorageReady: false,
618
+ };
619
+ }
620
+ const csReady = await crypto.isCrossSigningReady();
621
+ const csStatus = await crypto.getCrossSigningStatus();
622
+ const backupVersion = await crypto.getActiveSessionBackupVersion();
623
+ // Use getSecretStorageStatus().defaultKeyId to check if secret storage was
624
+ // set up at all, rather than isSecretStorageReady() which also checks that
625
+ // cross-signing keys are stored (too strict for Phase 1).
626
+ const ssStatus = await crypto.getSecretStorageStatus();
627
+ const ssHasKey = ssStatus.defaultKeyId !== null;
628
+ return {
629
+ isCrossSigningReady: csReady,
630
+ crossSigningStatus: {
631
+ hasMaster: csStatus.publicKeysOnDevice,
632
+ hasSelfSigning: csStatus.privateKeysCachedLocally.selfSigningKey,
633
+ hasUserSigning: csStatus.privateKeysCachedLocally.userSigningKey,
634
+ isReady: csReady,
635
+ },
636
+ isKeyBackupEnabled: backupVersion !== null,
637
+ keyBackupVersion: backupVersion !== null && backupVersion !== void 0 ? backupVersion : undefined,
638
+ isSecretStorageReady: ssHasKey,
639
+ };
640
+ }
641
+ async bootstrapCrossSigning() {
642
+ const crypto = await this.ensureCrypto();
643
+ await crypto.bootstrapCrossSigning({
644
+ setupNewCrossSigning: true,
645
+ authUploadDeviceSigningKeys: async (makeRequest) => {
646
+ var _a;
647
+ // UIA flow: attempt with dummy auth, fall back to session-based retry
648
+ try {
649
+ await makeRequest({ type: 'm.login.dummy' });
650
+ }
651
+ catch (e) {
652
+ const session = (_a = e === null || e === void 0 ? void 0 : e.data) === null || _a === void 0 ? void 0 : _a.session;
653
+ if (session) {
654
+ await makeRequest({ type: 'm.login.dummy', session });
655
+ }
656
+ else {
657
+ throw e;
658
+ }
659
+ }
660
+ },
661
+ });
662
+ }
663
+ async setupKeyBackup() {
664
+ const crypto = await this.ensureCrypto();
665
+ await crypto.resetKeyBackup();
666
+ const version = await crypto.getActiveSessionBackupVersion();
667
+ return { exists: true, version: version !== null && version !== void 0 ? version : undefined, enabled: true };
668
+ }
669
+ async getKeyBackupStatus() {
670
+ this.requireClient();
671
+ const crypto = this.requireCrypto();
672
+ const version = await crypto.getActiveSessionBackupVersion();
673
+ return {
674
+ exists: version !== null,
675
+ version: version !== null && version !== void 0 ? version : undefined,
676
+ enabled: version !== null,
677
+ };
678
+ }
679
+ async restoreKeyBackup(_options) {
680
+ var _a;
681
+ this.requireClient();
682
+ const crypto = this.requireCrypto();
683
+ const result = await crypto.restoreKeyBackup();
684
+ return { importedKeys: (_a = result === null || result === void 0 ? void 0 : result.imported) !== null && _a !== void 0 ? _a : 0 };
685
+ }
686
+ async setupRecovery(options) {
687
+ var _a;
688
+ const crypto = await this.ensureCrypto();
689
+ const keyInfo = await crypto.createRecoveryKeyFromPassphrase(options === null || options === void 0 ? void 0 : options.passphrase);
690
+ this.secretStorageKey = keyInfo.privateKey;
691
+ await crypto.bootstrapSecretStorage({
692
+ createSecretStorageKey: async () => keyInfo,
693
+ setupNewSecretStorage: true,
694
+ setupNewKeyBackup: true,
695
+ });
696
+ return { recoveryKey: (_a = keyInfo.encodedPrivateKey) !== null && _a !== void 0 ? _a : '' };
697
+ }
698
+ async isRecoveryEnabled() {
699
+ const crypto = await this.ensureCrypto();
700
+ const ready = await crypto.isSecretStorageReady();
701
+ return { enabled: ready };
702
+ }
703
+ async recoverAndSetup(options) {
704
+ const crypto = await this.ensureCrypto();
705
+ // Derive/decode the secret storage key
706
+ if (options.recoveryKey) {
707
+ this.secretStorageKey = decodeRecoveryKey(options.recoveryKey);
708
+ }
709
+ else if (options.passphrase) {
710
+ // Store passphrase — the getSecretStorageKey callback will derive
711
+ // the key using the server's stored PBKDF2 params (salt, iterations)
712
+ this.recoveryPassphrase = options.passphrase;
713
+ this.secretStorageKey = undefined; // Clear any stale raw key
714
+ }
715
+ else {
716
+ throw new Error('Either recoveryKey or passphrase must be provided');
717
+ }
718
+ // Load the backup decryption key from secret storage into the Rust crypto store.
719
+ // This triggers the getSecretStorageKey callback.
720
+ try {
721
+ await crypto.loadSessionBackupPrivateKeyFromSecretStorage();
722
+ }
723
+ catch (e) {
724
+ // Clear stale key material so the next attempt starts fresh
725
+ this.secretStorageKey = undefined;
726
+ this.recoveryPassphrase = undefined;
727
+ throw e;
728
+ }
729
+ // Now that the key is stored locally, activate backup in the running client
730
+ await crypto.checkKeyBackupAndEnable();
731
+ }
732
+ async resetRecoveryKey(options) {
733
+ return this.setupRecovery(options);
734
+ }
735
+ async exportRoomKeys(options) {
736
+ this.requireClient();
737
+ const crypto = this.requireCrypto();
738
+ const keys = await crypto.exportRoomKeysAsJson();
739
+ // The exported JSON is not encrypted by default; for passphrase encryption
740
+ // the caller should handle it, or we return the raw JSON
741
+ void options.passphrase; // passphrase encryption is handled natively; on web we return raw
742
+ return { data: keys };
743
+ }
744
+ async importRoomKeys(options) {
745
+ this.requireClient();
746
+ const crypto = this.requireCrypto();
747
+ void options.passphrase; // passphrase decryption handled natively; on web we import raw
748
+ await crypto.importRoomKeysAsJson(options.data);
749
+ return { importedKeys: -1 }; // count not available from importRoomKeysAsJson
750
+ }
751
+ // ── Helpers ───────────────────────────────────────────
752
+ requireClient() {
753
+ if (!this.client) {
754
+ throw new Error('Not logged in. Call login() or loginWithToken() first.');
755
+ }
756
+ }
757
+ requireCrypto() {
758
+ const crypto = this.client.getCrypto();
759
+ if (!crypto) {
760
+ throw new Error('Crypto not initialized. Call initializeCrypto() first.');
761
+ }
762
+ return crypto;
763
+ }
764
+ async ensureCrypto() {
765
+ this.requireClient();
766
+ if (!this.client.getCrypto()) {
767
+ await this.initializeCrypto();
768
+ }
769
+ return this.requireCrypto();
770
+ }
771
+ persistSession(session) {
772
+ localStorage.setItem(SESSION_KEY, JSON.stringify(session));
773
+ }
774
+ serializeEvent(event, fallbackRoomId) {
775
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
776
+ const roomId = (_b = (_a = event.getRoomId()) !== null && _a !== void 0 ? _a : fallbackRoomId) !== null && _b !== void 0 ? _b : '';
777
+ // Redacted events should be marked clearly
778
+ if (event.isRedacted()) {
779
+ return {
780
+ eventId: (_c = event.getId()) !== null && _c !== void 0 ? _c : '',
781
+ roomId,
782
+ senderId: (_d = event.getSender()) !== null && _d !== void 0 ? _d : '',
783
+ type: 'm.room.redaction',
784
+ content: { body: 'Message deleted' },
785
+ originServerTs: event.getTs(),
786
+ };
787
+ }
788
+ const content = Object.assign({}, ((_e = event.getContent()) !== null && _e !== void 0 ? _e : {}));
789
+ // Include aggregated reactions from the room's relations container
790
+ const eventId = event.getId();
791
+ if (eventId && roomId) {
792
+ const room = (_f = this.client) === null || _f === void 0 ? void 0 : _f.getRoom(roomId);
793
+ if (room) {
794
+ try {
795
+ const relations = room.relations.getChildEventsForEvent(eventId, RelationType.Annotation, EventType.Reaction);
796
+ if (relations) {
797
+ const sorted = relations.getSortedAnnotationsByKey();
798
+ if (sorted && sorted.length > 0) {
799
+ content.reactions = sorted.map(([key, events]) => ({
800
+ key,
801
+ count: events.size,
802
+ senders: Array.from(events).map((e) => e.getSender()),
803
+ }));
804
+ }
805
+ }
806
+ }
807
+ catch (_k) {
808
+ // relations may not be available
809
+ }
810
+ }
811
+ }
812
+ // Determine delivery/read status
813
+ let status;
814
+ const readBy = [];
815
+ const myUserId = (_g = this.client) === null || _g === void 0 ? void 0 : _g.getUserId();
816
+ const sender = event.getSender();
817
+ const room = eventId && roomId ? (_h = this.client) === null || _h === void 0 ? void 0 : _h.getRoom(roomId) : undefined;
818
+ if (sender === myUserId && eventId) {
819
+ // Own message — check delivery status
820
+ const evtStatus = event.status; // null = sent & echoed, 'sending', 'sent', etc.
821
+ if (evtStatus === 'sending' || evtStatus === 'encrypting' || evtStatus === 'queued') {
822
+ status = 'sending';
823
+ }
824
+ else {
825
+ // Event is at least sent; check if anyone has read it
826
+ if (room) {
827
+ try {
828
+ const members = room.getJoinedMembers();
829
+ for (const member of members) {
830
+ if (member.userId === myUserId)
831
+ continue;
832
+ if (room.hasUserReadEvent(member.userId, eventId)) {
833
+ readBy.push(member.userId);
834
+ }
835
+ }
836
+ }
837
+ catch (_l) {
838
+ // ignore errors
839
+ }
840
+ }
841
+ status = readBy.length > 0 ? 'read' : 'sent';
842
+ }
843
+ }
844
+ else if (eventId && room) {
845
+ // Other's message — collect who has read it
846
+ try {
847
+ const members = room.getJoinedMembers();
848
+ for (const member of members) {
849
+ if (member.userId === sender)
850
+ continue;
851
+ if (room.hasUserReadEvent(member.userId, eventId)) {
852
+ readBy.push(member.userId);
853
+ }
854
+ }
855
+ }
856
+ catch (_m) {
857
+ // ignore
858
+ }
859
+ }
860
+ // Include unsigned data (e.g. m.relations for edits, transaction_id for local echo)
861
+ const unsignedData = (_j = event.getUnsigned) === null || _j === void 0 ? void 0 : _j.call(event);
862
+ const unsigned = unsignedData && Object.keys(unsignedData).length > 0
863
+ ? unsignedData
864
+ : undefined;
865
+ return {
866
+ eventId: eventId !== null && eventId !== void 0 ? eventId : '',
867
+ roomId,
868
+ senderId: sender !== null && sender !== void 0 ? sender : '',
869
+ type: event.getType(),
870
+ content,
871
+ originServerTs: event.getTs(),
872
+ status,
873
+ readBy: readBy.length > 0 ? readBy : undefined,
874
+ unsigned,
875
+ };
876
+ }
877
+ serializeRoom(room) {
878
+ var _a, _b, _c, _d, _e, _f;
879
+ // Detect DM: check m.direct account data or guess from room state
880
+ let isDirect = false;
881
+ try {
882
+ const directEvent = (_a = this.client) === null || _a === void 0 ? void 0 : _a.getAccountData('m.direct');
883
+ if (directEvent) {
884
+ const directContent = directEvent.getContent();
885
+ for (const roomIds of Object.values(directContent)) {
886
+ if (roomIds.includes(room.roomId)) {
887
+ isDirect = true;
888
+ break;
889
+ }
890
+ }
891
+ }
892
+ }
893
+ catch (_g) {
894
+ // ignore
895
+ }
896
+ // Get avatar URL
897
+ let avatarUrl;
898
+ const avatarEvent = room.currentState.getStateEvents('m.room.avatar', '');
899
+ if (avatarEvent) {
900
+ const mxcUrl = (_b = avatarEvent.getContent()) === null || _b === void 0 ? void 0 : _b.url;
901
+ if (mxcUrl) {
902
+ avatarUrl = mxcUrl;
903
+ }
904
+ }
905
+ return {
906
+ roomId: room.roomId,
907
+ name: room.name,
908
+ topic: (_e = (_d = (_c = room.currentState.getStateEvents('m.room.topic', '')) === null || _c === void 0 ? void 0 : _c.getContent()) === null || _d === void 0 ? void 0 : _d.topic) !== null && _e !== void 0 ? _e : undefined,
909
+ memberCount: room.getJoinedMemberCount(),
910
+ isEncrypted: room.hasEncryptionStateEvent(),
911
+ unreadCount: (_f = room.getUnreadNotificationCount()) !== null && _f !== void 0 ? _f : 0,
912
+ lastEventTs: room.getLastActiveTimestamp() || undefined,
913
+ membership: room.getMyMembership(),
914
+ avatarUrl,
915
+ isDirect,
916
+ };
917
+ }
918
+ async searchUsers(options) {
919
+ var _a;
920
+ this.requireClient();
921
+ const resp = await this.client.searchUserDirectory({
922
+ term: options.searchTerm,
923
+ limit: (_a = options.limit) !== null && _a !== void 0 ? _a : 10,
924
+ });
925
+ return {
926
+ results: resp.results.map((u) => ({
927
+ userId: u.user_id,
928
+ displayName: u.display_name,
929
+ avatarUrl: u.avatar_url,
930
+ })),
931
+ limited: resp.limited,
932
+ };
933
+ }
934
+ mapSyncState(state) {
935
+ switch (state) {
936
+ case 'PREPARED':
937
+ case 'SYNCING':
938
+ case 'CATCHUP':
939
+ case 'RECONNECTING':
940
+ return 'SYNCING';
941
+ case 'ERROR':
942
+ return 'ERROR';
943
+ case 'STOPPED':
944
+ return 'STOPPED';
945
+ default:
946
+ return 'INITIAL';
947
+ }
948
+ }
949
+ }
950
+ //# sourceMappingURL=web.js.map