@tagea/capacitor-matrix 1.1.1 → 1.2.1

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 CHANGED
@@ -59,6 +59,8 @@ var capacitorCapMatrix = (function (exports, core, matrixSdkCryptoWasm, matrixJs
59
59
  userId: res.user_id,
60
60
  deviceId: res.device_id,
61
61
  cryptoCallbacks: this._cryptoCallbacks,
62
+ refreshToken: 'jwt-placeholder',
63
+ tokenRefreshFunction: this.createTokenRefreshFunction(),
62
64
  });
63
65
  const session = {
64
66
  accessToken: res.access_token,
@@ -82,6 +84,8 @@ var capacitorCapMatrix = (function (exports, core, matrixSdkCryptoWasm, matrixJs
82
84
  userId: options.userId,
83
85
  deviceId: options.deviceId,
84
86
  cryptoCallbacks: this._cryptoCallbacks,
87
+ refreshToken: 'jwt-placeholder',
88
+ tokenRefreshFunction: this.createTokenRefreshFunction(),
85
89
  });
86
90
  const session = {
87
91
  accessToken: options.accessToken,
@@ -129,6 +133,27 @@ var capacitorCapMatrix = (function (exports, core, matrixSdkCryptoWasm, matrixJs
129
133
  return null;
130
134
  }
131
135
  }
136
+ async updateAccessToken(options) {
137
+ this.requireClient();
138
+ this.client.setAccessToken(options.accessToken);
139
+ // Update persisted session
140
+ const raw = localStorage.getItem(SESSION_KEY);
141
+ if (raw) {
142
+ const session = JSON.parse(raw);
143
+ session.accessToken = options.accessToken;
144
+ this.persistSession(session);
145
+ }
146
+ // Resolve pending tokenRefreshFunction promise (if SDK is waiting)
147
+ if (this._tokenRefreshResolve) {
148
+ const resolve = this._tokenRefreshResolve;
149
+ this._tokenRefreshResolve = undefined;
150
+ if (this._tokenRefreshTimeout) {
151
+ clearTimeout(this._tokenRefreshTimeout);
152
+ this._tokenRefreshTimeout = undefined;
153
+ }
154
+ resolve({ accessToken: options.accessToken, refreshToken: 'jwt-placeholder' });
155
+ }
156
+ }
132
157
  // ── Sync ──────────────────────────────────────────────
133
158
  async startSync() {
134
159
  this.requireClient();
@@ -959,6 +984,20 @@ var capacitorCapMatrix = (function (exports, core, matrixSdkCryptoWasm, matrixJs
959
984
  persistSession(session) {
960
985
  localStorage.setItem(SESSION_KEY, JSON.stringify(session));
961
986
  }
987
+ createTokenRefreshFunction() {
988
+ return (_refreshToken) => {
989
+ this.notifyListeners('tokenRefreshRequired', {});
990
+ return new Promise((resolve, reject) => {
991
+ if (this._tokenRefreshTimeout)
992
+ clearTimeout(this._tokenRefreshTimeout);
993
+ this._tokenRefreshResolve = resolve;
994
+ this._tokenRefreshTimeout = setTimeout(() => {
995
+ this._tokenRefreshResolve = undefined;
996
+ reject(new Error('Token refresh timed out'));
997
+ }, 30000);
998
+ });
999
+ };
1000
+ }
962
1001
  serializeEvent(event, fallbackRoomId) {
963
1002
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
964
1003
  const roomId = (_b = (_a = event.getRoomId()) !== null && _a !== void 0 ? _a : fallbackRoomId) !== null && _b !== void 0 ? _b : '';
@@ -1055,7 +1094,7 @@ var capacitorCapMatrix = (function (exports, core, matrixSdkCryptoWasm, matrixJs
1055
1094
  return Object.assign(Object.assign({ eventId: eventId !== null && eventId !== void 0 ? eventId : '', roomId, senderId: sender !== null && sender !== void 0 ? sender : '', type: event.getType() }, (sk !== undefined && { stateKey: sk })), { content, originServerTs: event.getTs(), status, readBy: readBy.length > 0 ? readBy : undefined, unsigned });
1056
1095
  }
1057
1096
  serializeRoom(room) {
1058
- var _a, _b, _c, _d, _e, _f;
1097
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
1059
1098
  // Detect DM: check m.direct account data or guess from room state
1060
1099
  let isDirect = false;
1061
1100
  try {
@@ -1070,7 +1109,7 @@ var capacitorCapMatrix = (function (exports, core, matrixSdkCryptoWasm, matrixJs
1070
1109
  }
1071
1110
  }
1072
1111
  }
1073
- catch (_g) {
1112
+ catch (_k) {
1074
1113
  // ignore
1075
1114
  }
1076
1115
  // Get avatar URL
@@ -1082,17 +1121,41 @@ var capacitorCapMatrix = (function (exports, core, matrixSdkCryptoWasm, matrixJs
1082
1121
  avatarUrl = mxcUrl;
1083
1122
  }
1084
1123
  }
1124
+ // Build latestEvent from the room's last displayable timeline event
1125
+ let latestEvent;
1126
+ const timeline = room.getLiveTimeline().getEvents();
1127
+ for (let i = timeline.length - 1; i >= 0; i--) {
1128
+ const evt = timeline[i];
1129
+ const evtType = evt.getType();
1130
+ if (evtType === matrixJsSdk.EventType.RoomMessage || evtType === matrixJsSdk.EventType.Reaction) {
1131
+ const relatesTo = (_c = evt.getContent()) === null || _c === void 0 ? void 0 : _c['m.relates_to'];
1132
+ if ((relatesTo === null || relatesTo === void 0 ? void 0 : relatesTo['rel_type']) === matrixJsSdk.RelationType.Replace)
1133
+ continue;
1134
+ const sender = (_d = evt.getSender()) !== null && _d !== void 0 ? _d : '';
1135
+ const senderMember = room.getMember(sender);
1136
+ latestEvent = {
1137
+ roomId: room.roomId,
1138
+ senderId: sender,
1139
+ type: evtType,
1140
+ content: evt.getContent(),
1141
+ originServerTs: evt.getTs(),
1142
+ senderDisplayName: (_e = senderMember === null || senderMember === void 0 ? void 0 : senderMember.name) !== null && _e !== void 0 ? _e : undefined,
1143
+ };
1144
+ break;
1145
+ }
1146
+ }
1085
1147
  return {
1086
1148
  roomId: room.roomId,
1087
1149
  name: room.name,
1088
- 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,
1150
+ topic: (_h = (_g = (_f = room.currentState.getStateEvents('m.room.topic', '')) === null || _f === void 0 ? void 0 : _f.getContent()) === null || _g === void 0 ? void 0 : _g.topic) !== null && _h !== void 0 ? _h : undefined,
1089
1151
  memberCount: room.getJoinedMemberCount(),
1090
1152
  isEncrypted: room.hasEncryptionStateEvent(),
1091
- unreadCount: (_f = room.getUnreadNotificationCount()) !== null && _f !== void 0 ? _f : 0,
1153
+ unreadCount: (_j = room.getUnreadNotificationCount()) !== null && _j !== void 0 ? _j : 0,
1092
1154
  lastEventTs: room.getLastActiveTimestamp() || undefined,
1093
1155
  membership: room.getMyMembership(),
1094
1156
  avatarUrl,
1095
1157
  isDirect,
1158
+ latestEvent,
1096
1159
  };
1097
1160
  }
1098
1161
  async searchUsers(options) {