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