@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.
@@ -64,6 +64,8 @@ class MatrixWeb extends core.WebPlugin {
64
64
  userId: res.user_id,
65
65
  deviceId: res.device_id,
66
66
  cryptoCallbacks: this._cryptoCallbacks,
67
+ refreshToken: 'jwt-placeholder',
68
+ tokenRefreshFunction: this.createTokenRefreshFunction(),
67
69
  });
68
70
  const session = {
69
71
  accessToken: res.access_token,
@@ -87,6 +89,8 @@ class MatrixWeb extends core.WebPlugin {
87
89
  userId: options.userId,
88
90
  deviceId: options.deviceId,
89
91
  cryptoCallbacks: this._cryptoCallbacks,
92
+ refreshToken: 'jwt-placeholder',
93
+ tokenRefreshFunction: this.createTokenRefreshFunction(),
90
94
  });
91
95
  const session = {
92
96
  accessToken: options.accessToken,
@@ -134,6 +138,27 @@ class MatrixWeb extends core.WebPlugin {
134
138
  return null;
135
139
  }
136
140
  }
141
+ async updateAccessToken(options) {
142
+ this.requireClient();
143
+ this.client.setAccessToken(options.accessToken);
144
+ // Update persisted session
145
+ const raw = localStorage.getItem(SESSION_KEY);
146
+ if (raw) {
147
+ const session = JSON.parse(raw);
148
+ session.accessToken = options.accessToken;
149
+ this.persistSession(session);
150
+ }
151
+ // Resolve pending tokenRefreshFunction promise (if SDK is waiting)
152
+ if (this._tokenRefreshResolve) {
153
+ const resolve = this._tokenRefreshResolve;
154
+ this._tokenRefreshResolve = undefined;
155
+ if (this._tokenRefreshTimeout) {
156
+ clearTimeout(this._tokenRefreshTimeout);
157
+ this._tokenRefreshTimeout = undefined;
158
+ }
159
+ resolve({ accessToken: options.accessToken, refreshToken: 'jwt-placeholder' });
160
+ }
161
+ }
137
162
  // ── Sync ──────────────────────────────────────────────
138
163
  async startSync() {
139
164
  this.requireClient();
@@ -964,6 +989,20 @@ class MatrixWeb extends core.WebPlugin {
964
989
  persistSession(session) {
965
990
  localStorage.setItem(SESSION_KEY, JSON.stringify(session));
966
991
  }
992
+ createTokenRefreshFunction() {
993
+ return (_refreshToken) => {
994
+ this.notifyListeners('tokenRefreshRequired', {});
995
+ return new Promise((resolve, reject) => {
996
+ if (this._tokenRefreshTimeout)
997
+ clearTimeout(this._tokenRefreshTimeout);
998
+ this._tokenRefreshResolve = resolve;
999
+ this._tokenRefreshTimeout = setTimeout(() => {
1000
+ this._tokenRefreshResolve = undefined;
1001
+ reject(new Error('Token refresh timed out'));
1002
+ }, 30000);
1003
+ });
1004
+ };
1005
+ }
967
1006
  serializeEvent(event, fallbackRoomId) {
968
1007
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
969
1008
  const roomId = (_b = (_a = event.getRoomId()) !== null && _a !== void 0 ? _a : fallbackRoomId) !== null && _b !== void 0 ? _b : '';
@@ -1060,7 +1099,7 @@ class MatrixWeb extends core.WebPlugin {
1060
1099
  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 });
1061
1100
  }
1062
1101
  serializeRoom(room) {
1063
- var _a, _b, _c, _d, _e, _f;
1102
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
1064
1103
  // Detect DM: check m.direct account data or guess from room state
1065
1104
  let isDirect = false;
1066
1105
  try {
@@ -1075,7 +1114,7 @@ class MatrixWeb extends core.WebPlugin {
1075
1114
  }
1076
1115
  }
1077
1116
  }
1078
- catch (_g) {
1117
+ catch (_k) {
1079
1118
  // ignore
1080
1119
  }
1081
1120
  // Get avatar URL
@@ -1087,17 +1126,41 @@ class MatrixWeb extends core.WebPlugin {
1087
1126
  avatarUrl = mxcUrl;
1088
1127
  }
1089
1128
  }
1129
+ // Build latestEvent from the room's last displayable timeline event
1130
+ let latestEvent;
1131
+ const timeline = room.getLiveTimeline().getEvents();
1132
+ for (let i = timeline.length - 1; i >= 0; i--) {
1133
+ const evt = timeline[i];
1134
+ const evtType = evt.getType();
1135
+ if (evtType === matrixJsSdk.EventType.RoomMessage || evtType === matrixJsSdk.EventType.Reaction) {
1136
+ const relatesTo = (_c = evt.getContent()) === null || _c === void 0 ? void 0 : _c['m.relates_to'];
1137
+ if ((relatesTo === null || relatesTo === void 0 ? void 0 : relatesTo['rel_type']) === matrixJsSdk.RelationType.Replace)
1138
+ continue;
1139
+ const sender = (_d = evt.getSender()) !== null && _d !== void 0 ? _d : '';
1140
+ const senderMember = room.getMember(sender);
1141
+ latestEvent = {
1142
+ roomId: room.roomId,
1143
+ senderId: sender,
1144
+ type: evtType,
1145
+ content: evt.getContent(),
1146
+ originServerTs: evt.getTs(),
1147
+ senderDisplayName: (_e = senderMember === null || senderMember === void 0 ? void 0 : senderMember.name) !== null && _e !== void 0 ? _e : undefined,
1148
+ };
1149
+ break;
1150
+ }
1151
+ }
1090
1152
  return {
1091
1153
  roomId: room.roomId,
1092
1154
  name: room.name,
1093
- 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,
1155
+ 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,
1094
1156
  memberCount: room.getJoinedMemberCount(),
1095
1157
  isEncrypted: room.hasEncryptionStateEvent(),
1096
- unreadCount: (_f = room.getUnreadNotificationCount()) !== null && _f !== void 0 ? _f : 0,
1158
+ unreadCount: (_j = room.getUnreadNotificationCount()) !== null && _j !== void 0 ? _j : 0,
1097
1159
  lastEventTs: room.getLastActiveTimestamp() || undefined,
1098
1160
  membership: room.getMyMembership(),
1099
1161
  avatarUrl,
1100
1162
  isDirect,
1163
+ latestEvent,
1101
1164
  };
1102
1165
  }
1103
1166
  async searchUsers(options) {