@devanachat/sdk 0.9.0 → 0.10.0

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/build/index.mjs CHANGED
@@ -1903,7 +1903,7 @@ let UserStatus = /*#__PURE__*/function (UserStatus) {
1903
1903
  /**
1904
1904
  * Version of the {@link StateSnapshot} format. A snapshot of any other version is rejected on import.
1905
1905
  */
1906
- const STATE_SNAPSHOT_VERSION = 1;
1906
+ const STATE_SNAPSHOT_VERSION = 2;
1907
1907
 
1908
1908
  /**
1909
1909
  * Plain, JSON-serializable copy of the state tracked by {@link ChatStateTracker}, made to restore the state at the
@@ -1973,6 +1973,9 @@ function hasId(value) {
1973
1973
  function isUser(value) {
1974
1974
  return hasId(value);
1975
1975
  }
1976
+ function isPmParticipant(value) {
1977
+ return isObject(value) && isUser(value.user) && typeof value.flags === 'number';
1978
+ }
1976
1979
  function isRole(value) {
1977
1980
  return hasId(value) && typeof value.priority === 'number';
1978
1981
  }
@@ -1989,7 +1992,7 @@ function isFullMessage(value) {
1989
1992
  return hasId(value) && isObject(value.location) && isString(value.location.roomId) && isString(value.location.topicId) && isObject(value.author) && isUser(value.author.user) && Array.isArray(value.reactions);
1990
1993
  }
1991
1994
  function isRoom(value) {
1992
- return hasId(value) && isString(value.type) && (value.spaceId === null || isString(value.spaceId)) && (value.defaultTopic === null || isTopic(value.defaultTopic)) && (value.recipients === null || isArrayOf(value.recipients, isUser)) && isObject(value.history) && isString(value.history.mode);
1995
+ return hasId(value) && isString(value.type) && (value.spaceId === null || isString(value.spaceId)) && (value.defaultTopic === null || isTopic(value.defaultTopic)) && (value.participants === null || isArrayOf(value.participants, isPmParticipant)) && (value.recipients === null || isArrayOf(value.recipients, isUser)) && isObject(value.history) && isString(value.history.mode);
1993
1996
  }
1994
1997
  function isSpaceRooms(value) {
1995
1998
  return isObject(value) && isString(value.spaceId) && isArrayOf(value.summaries, hasId);
@@ -2018,12 +2021,84 @@ function isRoomMember(value) {
2018
2021
  function isRoomMembers(value) {
2019
2022
  return isObject(value) && isString(value.roomId) && isArrayOf(value.members, isRoomMember);
2020
2023
  }
2024
+ ;// ./src/protocol/schemes/PmParticipant.ts
2025
+ let PmParticipantFlag = /*#__PURE__*/function (PmParticipantFlag) {
2026
+ PmParticipantFlag[PmParticipantFlag["IgnoresRoom"] = 1] = "IgnoresRoom";
2027
+ return PmParticipantFlag;
2028
+ }({});
2029
+ ;// ./src/state-tracker/functions.ts
2030
+
2031
+
2032
+ /**
2033
+ * @return New objects of the other roles whose priority has to move; the given roles are left intact.
2034
+ */
2035
+ function reorderRolesOnPriorityUpdate(allRoles, oldRole, updatedRole) {
2036
+ // If the priority has changed, adjust the rest of roles
2037
+ const increased = updatedRole.priority - oldRole.priority > 0;
2038
+ const decreased = !increased;
2039
+ const changedRoles = [];
2040
+ allRoles.forEach(role => {
2041
+ if (role.id === updatedRole.id) {
2042
+ // Skip the updated role
2043
+ return;
2044
+ }
2045
+ if (increased && oldRole.priority <= role.priority) {
2046
+ changedRoles.push({
2047
+ ...role,
2048
+ priority: role.priority - 1
2049
+ });
2050
+ }
2051
+ if (decreased && updatedRole.priority <= role.priority) {
2052
+ changedRoles.push({
2053
+ ...role,
2054
+ priority: role.priority + 1
2055
+ });
2056
+ }
2057
+ });
2058
+ return changedRoles;
2059
+ }
2060
+ function extractUserFromMember(member) {
2061
+ return member.user ?? member.spaceMember?.user;
2062
+ }
2063
+
2064
+ /**
2065
+ * Whether the room is a private group conversation - 3 or more participants, including the ones ignoring it.
2066
+ */
2067
+ function isGroupPm(room) {
2068
+ return room.type === 'Pm' && (room.participants?.length ?? 0) >= 3;
2069
+ }
2070
+
2071
+ /**
2072
+ * Participants of a private conversation taking part in it: without the ones ignoring it and without the user
2073
+ * `meId`. Empty for notes and for rooms of spaces.
2074
+ */
2075
+ function getPmPeers(room, meId) {
2076
+ return getActivePmParticipants(room).filter(participant => participant.user.id !== meId);
2077
+ }
2078
+ function getActivePmParticipants(room) {
2079
+ return (room.participants ?? []).filter(participant => !(participant.flags & PmParticipantFlag.IgnoresRoom));
2080
+ }
2081
+ function isSameUser(a, b) {
2082
+ const keys = Object.keys(a);
2083
+ if (keys.length !== Object.keys(b).length) {
2084
+ return false;
2085
+ }
2086
+ return keys.every(key => {
2087
+ const valueA = a[key];
2088
+ const valueB = b[key];
2089
+ if (Array.isArray(valueA) && Array.isArray(valueB)) {
2090
+ return valueA.length === valueB.length && valueA.every((item, index) => item === valueB[index]);
2091
+ }
2092
+ return valueA === valueB;
2093
+ });
2094
+ }
2021
2095
  ;// ./src/state-tracker/RoomsManager.ts
2022
2096
 
2023
2097
 
2024
2098
 
2025
2099
 
2026
2100
 
2101
+
2027
2102
  class RoomsManager {
2028
2103
  messages;
2029
2104
  followedTopics;
@@ -2152,9 +2227,13 @@ class RoomsManager {
2152
2227
  * @internal
2153
2228
  */
2154
2229
  _exportRooms() {
2155
- return this.list.items.map(room => room.recipients ? {
2230
+ return this.list.items.map(room => room.participants ? {
2156
2231
  ...room,
2157
- recipients: room.recipients.map(withoutPresence)
2232
+ participants: room.participants.map(participant => ({
2233
+ ...participant,
2234
+ user: withoutPresence(participant.user)
2235
+ })),
2236
+ recipients: room.recipients?.map(withoutPresence) ?? null
2158
2237
  } : room);
2159
2238
  }
2160
2239
 
@@ -2206,7 +2285,7 @@ class RoomsManager {
2206
2285
  }
2207
2286
 
2208
2287
  /**
2209
- * Members of private conversations are not exported - they are made of the recipients of the room.
2288
+ * Members of private conversations are not exported - they are made of the participants of the room.
2210
2289
  * @internal
2211
2290
  */
2212
2291
  _exportMembers(roomIds, limit) {
@@ -2332,6 +2411,9 @@ class RoomsManager {
2332
2411
  }
2333
2412
  const members = this.members.get(ev.roomId);
2334
2413
  const member = members.get(ev.userId);
2414
+ if (!member) {
2415
+ return;
2416
+ }
2335
2417
  const user = member.spaceMember?.user ?? member.user;
2336
2418
 
2337
2419
  // Preserving user object, because it's not included in event
@@ -2376,6 +2458,7 @@ class RoomsManager {
2376
2458
  handleRoomUpdated(ev) {
2377
2459
  if (this.list.has(ev.room.id)) {
2378
2460
  this.list.set(ev.room);
2461
+ this.setPmMembers(ev.room);
2379
2462
  }
2380
2463
  }
2381
2464
  handleRoomDeleted(ev) {
@@ -2399,27 +2482,39 @@ class RoomsManager {
2399
2482
  if (room.defaultTopic) {
2400
2483
  this.addJoinedRoomTopics(room.id, room.defaultTopic);
2401
2484
  }
2402
- if (room.type === 'Pm' && room.recipients) {
2403
- // Treat PM recipients as normal room members.
2404
- // We are registering fake promise in `memberPromises`
2405
- // because GetMembers are not supported for PM rooms.
2406
- this.handleRoomMembers({
2407
- id: room.id,
2408
- members: room.recipients.map(user => ({
2409
- user,
2410
- spaceMember: null,
2411
- roles: null,
2412
- customColor: null,
2413
- customNick: null,
2414
- customAvatar: null,
2415
- extras: ''
2416
- }))
2417
- });
2418
- this.membersPromises.register(Promise.resolve(), room.id);
2419
- }
2485
+ this.setPmMembers(room);
2420
2486
  }
2421
2487
  this.list.set(...rooms);
2422
2488
  }
2489
+ setPmMembers(room) {
2490
+ if (room.type !== 'Pm' || !room.participants) {
2491
+ return;
2492
+ }
2493
+ const current = this.members.get(room.id);
2494
+ const members = getActivePmParticipants(room).map(({
2495
+ user
2496
+ }) => {
2497
+ const member = current?.get(user.id);
2498
+ return member && isSameUser(member.user, user) ? member : {
2499
+ user,
2500
+ spaceMember: null,
2501
+ roles: null,
2502
+ customColor: null,
2503
+ customNick: null,
2504
+ customAvatar: null,
2505
+ extras: ''
2506
+ };
2507
+ });
2508
+ if (current?.length === members.length && members.every(member => current.get(member.user.id) === member)) {
2509
+ return;
2510
+ }
2511
+ this.handleRoomMembers({
2512
+ id: room.id,
2513
+ members
2514
+ });
2515
+ // GetRoomMembers is not supported for private conversations
2516
+ this.membersPromises.register(Promise.resolve(), room.id);
2517
+ }
2423
2518
  handleRoomLeft(ev) {
2424
2519
  this.deleteRoom(ev.id);
2425
2520
  }
@@ -2483,13 +2578,17 @@ class RoomsManager {
2483
2578
  });
2484
2579
  });
2485
2580
 
2486
- // Update recipients users
2581
+ // Update users of private conversation participants
2487
2582
  const newRooms = [];
2488
2583
  this.list.items.forEach(room => {
2489
- if (room.recipients?.some(user => user.id === ev.user.id)) {
2584
+ if (room.participants?.some(participant => participant.user.id === ev.user.id)) {
2490
2585
  newRooms.push({
2491
2586
  ...room,
2492
- recipients: room.recipients.map(user => user.id === ev.user.id ? ev.user : user)
2587
+ participants: room.participants.map(participant => participant.user.id === ev.user.id ? {
2588
+ ...participant,
2589
+ user: ev.user
2590
+ } : participant),
2591
+ recipients: room.recipients?.map(user => user.id === ev.user.id ? ev.user : user) ?? null
2493
2592
  });
2494
2593
  }
2495
2594
  });
@@ -2541,38 +2640,6 @@ class RoomsManager {
2541
2640
  }
2542
2641
  }
2543
2642
  }
2544
- ;// ./src/state-tracker/functions.ts
2545
- /**
2546
- * @return New objects of the other roles whose priority has to move; the given roles are left intact.
2547
- */
2548
- function reorderRolesOnPriorityUpdate(allRoles, oldRole, updatedRole) {
2549
- // If the priority has changed, adjust the rest of roles
2550
- const increased = updatedRole.priority - oldRole.priority > 0;
2551
- const decreased = !increased;
2552
- const changedRoles = [];
2553
- allRoles.forEach(role => {
2554
- if (role.id === updatedRole.id) {
2555
- // Skip the updated role
2556
- return;
2557
- }
2558
- if (increased && oldRole.priority <= role.priority) {
2559
- changedRoles.push({
2560
- ...role,
2561
- priority: role.priority - 1
2562
- });
2563
- }
2564
- if (decreased && updatedRole.priority <= role.priority) {
2565
- changedRoles.push({
2566
- ...role,
2567
- priority: role.priority + 1
2568
- });
2569
- }
2570
- });
2571
- return changedRoles;
2572
- }
2573
- function extractUserFromMember(member) {
2574
- return member.user ?? member.spaceMember?.user;
2575
- }
2576
2643
  ;// ./src/state-tracker/SpacesManager.ts
2577
2644
 
2578
2645
 
@@ -3448,6 +3515,9 @@ class UsersManager {
3448
3515
  tracker.client.on('RoomMembers', event => this.handleMembers(event.members));
3449
3516
  tracker.client.on('Messages', event => this.handleUsers(event.messages.map(message => message.author.user)));
3450
3517
  tracker.client.on('NewMessage', event => this.handleUsers([event.message.author.user]));
3518
+ tracker.client.on('RoomJoined', event => this.handleRooms([event.room]));
3519
+ tracker.client.on('RoomUpdated', event => this.handleRooms([event.room]));
3520
+ tracker.client.on('IgnoredRooms', event => this.handleRooms(event.rooms));
3451
3521
  tracker.client.on('Session', event => this.handleSession(event));
3452
3522
  }
3453
3523
 
@@ -3465,14 +3535,31 @@ class UsersManager {
3465
3535
  _importUser(user) {
3466
3536
  this.handleUsers([user]);
3467
3537
  }
3538
+
3539
+ /**
3540
+ * For internal use.
3541
+ * @internal
3542
+ */
3543
+ _importRooms(rooms) {
3544
+ this.handleRooms(rooms);
3545
+ }
3468
3546
  handleMembers(members) {
3469
3547
  this.handleUsers(members.map(extractUserFromMember));
3470
3548
  }
3549
+ handleRooms(rooms) {
3550
+ const users = new Map();
3551
+ for (const room of rooms) {
3552
+ for (const participant of room.participants ?? []) {
3553
+ users.set(participant.user.id, participant.user);
3554
+ }
3555
+ }
3556
+ this.handleUsers([...users.values()]);
3557
+ }
3471
3558
  handleSession(session) {
3472
3559
  // Keep the "seen users" cache across reconnects so bound user lists do
3473
- // not blank out; just ensure our own user is present/updated. Stale
3474
- // entries are refreshed as member/message collections refetch.
3560
+ // not blank out. Stale entries are refreshed as member/message collections refetch.
3475
3561
  this.handleUsers([session.user]);
3562
+ this.handleRooms(session.state.rooms);
3476
3563
  }
3477
3564
 
3478
3565
  /**
@@ -3494,20 +3581,6 @@ class UsersManager {
3494
3581
  this.users.set(...changedUsers);
3495
3582
  }
3496
3583
  }
3497
- function isSameUser(a, b) {
3498
- const keys = Object.keys(a);
3499
- if (keys.length !== Object.keys(b).length) {
3500
- return false;
3501
- }
3502
- return keys.every(key => {
3503
- const valueA = a[key];
3504
- const valueB = b[key];
3505
- if (Array.isArray(valueA) && Array.isArray(valueB)) {
3506
- return valueA.length === valueB.length && valueA.every((item, index) => item === valueB[index]);
3507
- }
3508
- return valueA === valueB;
3509
- });
3510
- }
3511
3584
  ;// ./src/state-tracker/RelationshipsManager.ts
3512
3585
 
3513
3586
 
@@ -3703,6 +3776,7 @@ class ChatStateTracker {
3703
3776
  try {
3704
3777
  this._me = snapshot.user;
3705
3778
  this.users._importUser(snapshot.user);
3779
+ this.users._importRooms(snapshot.rooms);
3706
3780
  this.rooms._importRooms(snapshot.rooms);
3707
3781
  this.spaces._importSpaces(snapshot.spaces, snapshot.spaceRooms);
3708
3782
  this.rooms.followedTopics._importFollowedTopics(snapshot.followedTopics);
@@ -4216,6 +4290,7 @@ let ErrorCode = /*#__PURE__*/function (ErrorCode) {
4216
4290
  ErrorCode["InvalidSearchQuery"] = "InvalidSearchQueryException";
4217
4291
  ErrorCode["MessageSearchUnavailable"] = "MessageSearchUnavailableException";
4218
4292
  ErrorCode["MessageNotFound"] = "MessageNotFoundException";
4293
+ ErrorCode["RoomIgnored"] = "RoomIgnoredException";
4219
4294
  ErrorCode["RoomNotFound"] = "RoomNotFoundException";
4220
4295
  ErrorCode["TopicNotFound"] = "TopicNotFoundException";
4221
4296
  ErrorCode["UserNotFound"] = "UserNotFoundException";
@@ -4354,6 +4429,6 @@ let SpaceIntent = /*#__PURE__*/function (SpaceIntent) {
4354
4429
 
4355
4430
 
4356
4431
 
4357
- export { AbstractRestClient, ErrorCode, FilesClient, HistoryRotation, IndexedCollection, IndexedObjectCollection, Layer, NO_LIMIT, ObservableIndexedCollection, ObservableIndexedObjectCollection, PermissionDefinition, Permissions, RoleFlag, STATE_SNAPSHOT_VERSION, STORAGE_UNIT_BYTES, SearchRange, SpaceFeature, SpaceIntent, UiPurpose, UserFeature, UserStatus, WebApiChatClient, WebSocketChatClient, extractUserFromMember };
4432
+ export { AbstractRestClient, ErrorCode, FilesClient, HistoryRotation, IndexedCollection, IndexedObjectCollection, Layer, NO_LIMIT, ObservableIndexedCollection, ObservableIndexedObjectCollection, PermissionDefinition, Permissions, PmParticipantFlag, RoleFlag, STATE_SNAPSHOT_VERSION, STORAGE_UNIT_BYTES, SearchRange, SpaceFeature, SpaceIntent, UiPurpose, UserFeature, UserStatus, WebApiChatClient, WebSocketChatClient, extractUserFromMember, getPmPeers, isGroupPm };
4358
4433
 
4359
4434
  //# sourceMappingURL=index.mjs.map