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