@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/CHANGELOG.md CHANGED
@@ -4,7 +4,41 @@ All notable changes to this project are documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the project follows
5
5
  [semver](https://semver.org) (`0.x` minor releases may break the API).
6
6
 
7
- ## [Unreleased]
7
+ ## [0.10.0]
8
+
9
+ ### Added
10
+
11
+ - Group private conversations: a `Pm` room has 1 (notes), 2 or 3-10 participants; `CreateRoom` takes up to 9
12
+ `recipientIds` and, for a new conversation of 3 or more people, a `name`.
13
+ - `Room.participants` (`PmParticipant[]`, `null` for rooms of spaces): the users of a private conversation with their
14
+ `flags`; `PmParticipantFlag.IgnoresRoom` marks a participant ignoring it. The members of a private conversation
15
+ (`RoomsManager.getMembers()`) are its participants without the ones ignoring it, rebuilt on `RoomUpdated`, and the
16
+ participants land in `UsersManager.getAvailable()`.
17
+ - `isGroupPm(room)` and `getPmPeers(room, meId)` helpers.
18
+ - Ignoring a group conversation: `LeaveRoom` takes `ignore: true` (answered with `RoomLeft`, or `Ok` when the
19
+ user was not its member); `JoinRoom` of the ignoring participant stops ignoring it. `GetIgnoredRooms` returns the
20
+ ignored conversations as `IgnoredRooms`. `CreateRoom` for an ignored conversation fails with
21
+ `ErrorCode.RoomIgnored`, its id in `Error.details.roomId`.
22
+ - `UpdateRoom` changes the name of a group conversation (only `name` is accepted for a `Pm` room).
23
+ - Message type `RoomRename` (content: the new name, empty when the name was removed); `RoomJoin` and `RoomLeave` in a
24
+ group conversation mean stopping and starting to ignore it.
25
+
26
+ ### Changed
27
+
28
+ - **Breaking:** the members of a private conversation and its users in `UsersManager` come from `Room.participants`,
29
+ so a server that does not send `participants` leaves private conversations without members.
30
+ - `STATE_SNAPSHOT_VERSION` is 2 - snapshots of the previous version (rooms without `participants`) are rejected on
31
+ import.
32
+
33
+ ### Deprecated
34
+
35
+ - `Room.recipients` - use `Room.participants`.
36
+
37
+ ### Fixed
38
+
39
+ - `RoomMemberUpdated` of a member missing from the room's collection threw instead of being skipped.
40
+
41
+ ## [0.9.0]
8
42
 
9
43
  ### Added
10
44
 
@@ -51,6 +51,7 @@ __webpack_require__.d(__webpack_exports__, {
51
51
  ObservableIndexedObjectCollection: () => (/* reexport */ ObservableIndexedObjectCollection),
52
52
  PermissionDefinition: () => (/* reexport */ PermissionDefinition),
53
53
  Permissions: () => (/* reexport */ Permissions),
54
+ PmParticipantFlag: () => (/* reexport */ PmParticipantFlag),
54
55
  RoleFlag: () => (/* reexport */ RoleFlag),
55
56
  STATE_SNAPSHOT_VERSION: () => (/* reexport */ STATE_SNAPSHOT_VERSION),
56
57
  STORAGE_UNIT_BYTES: () => (/* reexport */ STORAGE_UNIT_BYTES),
@@ -62,7 +63,9 @@ __webpack_require__.d(__webpack_exports__, {
62
63
  UserStatus: () => (/* reexport */ UserStatus),
63
64
  WebApiChatClient: () => (/* reexport */ WebApiChatClient),
64
65
  WebSocketChatClient: () => (/* reexport */ WebSocketChatClient),
65
- extractUserFromMember: () => (/* reexport */ extractUserFromMember)
66
+ extractUserFromMember: () => (/* reexport */ extractUserFromMember),
67
+ getPmPeers: () => (/* reexport */ getPmPeers),
68
+ isGroupPm: () => (/* reexport */ isGroupPm)
66
69
  });
67
70
 
68
71
  ;// ./src/EventTarget.ts
@@ -1946,7 +1949,7 @@ let UserStatus = /*#__PURE__*/function (UserStatus) {
1946
1949
  /**
1947
1950
  * Version of the {@link StateSnapshot} format. A snapshot of any other version is rejected on import.
1948
1951
  */
1949
- const STATE_SNAPSHOT_VERSION = 1;
1952
+ const STATE_SNAPSHOT_VERSION = 2;
1950
1953
 
1951
1954
  /**
1952
1955
  * Plain, JSON-serializable copy of the state tracked by {@link ChatStateTracker}, made to restore the state at the
@@ -2016,6 +2019,9 @@ function hasId(value) {
2016
2019
  function isUser(value) {
2017
2020
  return hasId(value);
2018
2021
  }
2022
+ function isPmParticipant(value) {
2023
+ return isObject(value) && isUser(value.user) && typeof value.flags === 'number';
2024
+ }
2019
2025
  function isRole(value) {
2020
2026
  return hasId(value) && typeof value.priority === 'number';
2021
2027
  }
@@ -2032,7 +2038,7 @@ function isFullMessage(value) {
2032
2038
  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);
2033
2039
  }
2034
2040
  function isRoom(value) {
2035
- 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);
2041
+ 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);
2036
2042
  }
2037
2043
  function isSpaceRooms(value) {
2038
2044
  return isObject(value) && isString(value.spaceId) && isArrayOf(value.summaries, hasId);
@@ -2061,12 +2067,84 @@ function isRoomMember(value) {
2061
2067
  function isRoomMembers(value) {
2062
2068
  return isObject(value) && isString(value.roomId) && isArrayOf(value.members, isRoomMember);
2063
2069
  }
2070
+ ;// ./src/protocol/schemes/PmParticipant.ts
2071
+ let PmParticipantFlag = /*#__PURE__*/function (PmParticipantFlag) {
2072
+ PmParticipantFlag[PmParticipantFlag["IgnoresRoom"] = 1] = "IgnoresRoom";
2073
+ return PmParticipantFlag;
2074
+ }({});
2075
+ ;// ./src/state-tracker/functions.ts
2076
+
2077
+
2078
+ /**
2079
+ * @return New objects of the other roles whose priority has to move; the given roles are left intact.
2080
+ */
2081
+ function reorderRolesOnPriorityUpdate(allRoles, oldRole, updatedRole) {
2082
+ // If the priority has changed, adjust the rest of roles
2083
+ const increased = updatedRole.priority - oldRole.priority > 0;
2084
+ const decreased = !increased;
2085
+ const changedRoles = [];
2086
+ allRoles.forEach(role => {
2087
+ if (role.id === updatedRole.id) {
2088
+ // Skip the updated role
2089
+ return;
2090
+ }
2091
+ if (increased && oldRole.priority <= role.priority) {
2092
+ changedRoles.push({
2093
+ ...role,
2094
+ priority: role.priority - 1
2095
+ });
2096
+ }
2097
+ if (decreased && updatedRole.priority <= role.priority) {
2098
+ changedRoles.push({
2099
+ ...role,
2100
+ priority: role.priority + 1
2101
+ });
2102
+ }
2103
+ });
2104
+ return changedRoles;
2105
+ }
2106
+ function extractUserFromMember(member) {
2107
+ return member.user ?? member.spaceMember?.user;
2108
+ }
2109
+
2110
+ /**
2111
+ * Whether the room is a private group conversation - 3 or more participants, including the ones ignoring it.
2112
+ */
2113
+ function isGroupPm(room) {
2114
+ return room.type === 'Pm' && (room.participants?.length ?? 0) >= 3;
2115
+ }
2116
+
2117
+ /**
2118
+ * Participants of a private conversation taking part in it: without the ones ignoring it and without the user
2119
+ * `meId`. Empty for notes and for rooms of spaces.
2120
+ */
2121
+ function getPmPeers(room, meId) {
2122
+ return getActivePmParticipants(room).filter(participant => participant.user.id !== meId);
2123
+ }
2124
+ function getActivePmParticipants(room) {
2125
+ return (room.participants ?? []).filter(participant => !(participant.flags & PmParticipantFlag.IgnoresRoom));
2126
+ }
2127
+ function isSameUser(a, b) {
2128
+ const keys = Object.keys(a);
2129
+ if (keys.length !== Object.keys(b).length) {
2130
+ return false;
2131
+ }
2132
+ return keys.every(key => {
2133
+ const valueA = a[key];
2134
+ const valueB = b[key];
2135
+ if (Array.isArray(valueA) && Array.isArray(valueB)) {
2136
+ return valueA.length === valueB.length && valueA.every((item, index) => item === valueB[index]);
2137
+ }
2138
+ return valueA === valueB;
2139
+ });
2140
+ }
2064
2141
  ;// ./src/state-tracker/RoomsManager.ts
2065
2142
 
2066
2143
 
2067
2144
 
2068
2145
 
2069
2146
 
2147
+
2070
2148
  class RoomsManager {
2071
2149
  messages;
2072
2150
  followedTopics;
@@ -2195,9 +2273,13 @@ class RoomsManager {
2195
2273
  * @internal
2196
2274
  */
2197
2275
  _exportRooms() {
2198
- return this.list.items.map(room => room.recipients ? {
2276
+ return this.list.items.map(room => room.participants ? {
2199
2277
  ...room,
2200
- recipients: room.recipients.map(withoutPresence)
2278
+ participants: room.participants.map(participant => ({
2279
+ ...participant,
2280
+ user: withoutPresence(participant.user)
2281
+ })),
2282
+ recipients: room.recipients?.map(withoutPresence) ?? null
2201
2283
  } : room);
2202
2284
  }
2203
2285
 
@@ -2249,7 +2331,7 @@ class RoomsManager {
2249
2331
  }
2250
2332
 
2251
2333
  /**
2252
- * Members of private conversations are not exported - they are made of the recipients of the room.
2334
+ * Members of private conversations are not exported - they are made of the participants of the room.
2253
2335
  * @internal
2254
2336
  */
2255
2337
  _exportMembers(roomIds, limit) {
@@ -2375,6 +2457,9 @@ class RoomsManager {
2375
2457
  }
2376
2458
  const members = this.members.get(ev.roomId);
2377
2459
  const member = members.get(ev.userId);
2460
+ if (!member) {
2461
+ return;
2462
+ }
2378
2463
  const user = member.spaceMember?.user ?? member.user;
2379
2464
 
2380
2465
  // Preserving user object, because it's not included in event
@@ -2419,6 +2504,7 @@ class RoomsManager {
2419
2504
  handleRoomUpdated(ev) {
2420
2505
  if (this.list.has(ev.room.id)) {
2421
2506
  this.list.set(ev.room);
2507
+ this.setPmMembers(ev.room);
2422
2508
  }
2423
2509
  }
2424
2510
  handleRoomDeleted(ev) {
@@ -2442,27 +2528,39 @@ class RoomsManager {
2442
2528
  if (room.defaultTopic) {
2443
2529
  this.addJoinedRoomTopics(room.id, room.defaultTopic);
2444
2530
  }
2445
- if (room.type === 'Pm' && room.recipients) {
2446
- // Treat PM recipients as normal room members.
2447
- // We are registering fake promise in `memberPromises`
2448
- // because GetMembers are not supported for PM rooms.
2449
- this.handleRoomMembers({
2450
- id: room.id,
2451
- members: room.recipients.map(user => ({
2452
- user,
2453
- spaceMember: null,
2454
- roles: null,
2455
- customColor: null,
2456
- customNick: null,
2457
- customAvatar: null,
2458
- extras: ''
2459
- }))
2460
- });
2461
- this.membersPromises.register(Promise.resolve(), room.id);
2462
- }
2531
+ this.setPmMembers(room);
2463
2532
  }
2464
2533
  this.list.set(...rooms);
2465
2534
  }
2535
+ setPmMembers(room) {
2536
+ if (room.type !== 'Pm' || !room.participants) {
2537
+ return;
2538
+ }
2539
+ const current = this.members.get(room.id);
2540
+ const members = getActivePmParticipants(room).map(({
2541
+ user
2542
+ }) => {
2543
+ const member = current?.get(user.id);
2544
+ return member && isSameUser(member.user, user) ? member : {
2545
+ user,
2546
+ spaceMember: null,
2547
+ roles: null,
2548
+ customColor: null,
2549
+ customNick: null,
2550
+ customAvatar: null,
2551
+ extras: ''
2552
+ };
2553
+ });
2554
+ if (current?.length === members.length && members.every(member => current.get(member.user.id) === member)) {
2555
+ return;
2556
+ }
2557
+ this.handleRoomMembers({
2558
+ id: room.id,
2559
+ members
2560
+ });
2561
+ // GetRoomMembers is not supported for private conversations
2562
+ this.membersPromises.register(Promise.resolve(), room.id);
2563
+ }
2466
2564
  handleRoomLeft(ev) {
2467
2565
  this.deleteRoom(ev.id);
2468
2566
  }
@@ -2526,13 +2624,17 @@ class RoomsManager {
2526
2624
  });
2527
2625
  });
2528
2626
 
2529
- // Update recipients users
2627
+ // Update users of private conversation participants
2530
2628
  const newRooms = [];
2531
2629
  this.list.items.forEach(room => {
2532
- if (room.recipients?.some(user => user.id === ev.user.id)) {
2630
+ if (room.participants?.some(participant => participant.user.id === ev.user.id)) {
2533
2631
  newRooms.push({
2534
2632
  ...room,
2535
- recipients: room.recipients.map(user => user.id === ev.user.id ? ev.user : user)
2633
+ participants: room.participants.map(participant => participant.user.id === ev.user.id ? {
2634
+ ...participant,
2635
+ user: ev.user
2636
+ } : participant),
2637
+ recipients: room.recipients?.map(user => user.id === ev.user.id ? ev.user : user) ?? null
2536
2638
  });
2537
2639
  }
2538
2640
  });
@@ -2584,38 +2686,6 @@ class RoomsManager {
2584
2686
  }
2585
2687
  }
2586
2688
  }
2587
- ;// ./src/state-tracker/functions.ts
2588
- /**
2589
- * @return New objects of the other roles whose priority has to move; the given roles are left intact.
2590
- */
2591
- function reorderRolesOnPriorityUpdate(allRoles, oldRole, updatedRole) {
2592
- // If the priority has changed, adjust the rest of roles
2593
- const increased = updatedRole.priority - oldRole.priority > 0;
2594
- const decreased = !increased;
2595
- const changedRoles = [];
2596
- allRoles.forEach(role => {
2597
- if (role.id === updatedRole.id) {
2598
- // Skip the updated role
2599
- return;
2600
- }
2601
- if (increased && oldRole.priority <= role.priority) {
2602
- changedRoles.push({
2603
- ...role,
2604
- priority: role.priority - 1
2605
- });
2606
- }
2607
- if (decreased && updatedRole.priority <= role.priority) {
2608
- changedRoles.push({
2609
- ...role,
2610
- priority: role.priority + 1
2611
- });
2612
- }
2613
- });
2614
- return changedRoles;
2615
- }
2616
- function extractUserFromMember(member) {
2617
- return member.user ?? member.spaceMember?.user;
2618
- }
2619
2689
  ;// ./src/state-tracker/SpacesManager.ts
2620
2690
 
2621
2691
 
@@ -3491,6 +3561,9 @@ class UsersManager {
3491
3561
  tracker.client.on('RoomMembers', event => this.handleMembers(event.members));
3492
3562
  tracker.client.on('Messages', event => this.handleUsers(event.messages.map(message => message.author.user)));
3493
3563
  tracker.client.on('NewMessage', event => this.handleUsers([event.message.author.user]));
3564
+ tracker.client.on('RoomJoined', event => this.handleRooms([event.room]));
3565
+ tracker.client.on('RoomUpdated', event => this.handleRooms([event.room]));
3566
+ tracker.client.on('IgnoredRooms', event => this.handleRooms(event.rooms));
3494
3567
  tracker.client.on('Session', event => this.handleSession(event));
3495
3568
  }
3496
3569
 
@@ -3508,14 +3581,31 @@ class UsersManager {
3508
3581
  _importUser(user) {
3509
3582
  this.handleUsers([user]);
3510
3583
  }
3584
+
3585
+ /**
3586
+ * For internal use.
3587
+ * @internal
3588
+ */
3589
+ _importRooms(rooms) {
3590
+ this.handleRooms(rooms);
3591
+ }
3511
3592
  handleMembers(members) {
3512
3593
  this.handleUsers(members.map(extractUserFromMember));
3513
3594
  }
3595
+ handleRooms(rooms) {
3596
+ const users = new Map();
3597
+ for (const room of rooms) {
3598
+ for (const participant of room.participants ?? []) {
3599
+ users.set(participant.user.id, participant.user);
3600
+ }
3601
+ }
3602
+ this.handleUsers([...users.values()]);
3603
+ }
3514
3604
  handleSession(session) {
3515
3605
  // Keep the "seen users" cache across reconnects so bound user lists do
3516
- // not blank out; just ensure our own user is present/updated. Stale
3517
- // entries are refreshed as member/message collections refetch.
3606
+ // not blank out. Stale entries are refreshed as member/message collections refetch.
3518
3607
  this.handleUsers([session.user]);
3608
+ this.handleRooms(session.state.rooms);
3519
3609
  }
3520
3610
 
3521
3611
  /**
@@ -3537,20 +3627,6 @@ class UsersManager {
3537
3627
  this.users.set(...changedUsers);
3538
3628
  }
3539
3629
  }
3540
- function isSameUser(a, b) {
3541
- const keys = Object.keys(a);
3542
- if (keys.length !== Object.keys(b).length) {
3543
- return false;
3544
- }
3545
- return keys.every(key => {
3546
- const valueA = a[key];
3547
- const valueB = b[key];
3548
- if (Array.isArray(valueA) && Array.isArray(valueB)) {
3549
- return valueA.length === valueB.length && valueA.every((item, index) => item === valueB[index]);
3550
- }
3551
- return valueA === valueB;
3552
- });
3553
- }
3554
3630
  ;// ./src/state-tracker/RelationshipsManager.ts
3555
3631
 
3556
3632
 
@@ -3746,6 +3822,7 @@ class ChatStateTracker {
3746
3822
  try {
3747
3823
  this._me = snapshot.user;
3748
3824
  this.users._importUser(snapshot.user);
3825
+ this.users._importRooms(snapshot.rooms);
3749
3826
  this.rooms._importRooms(snapshot.rooms);
3750
3827
  this.spaces._importSpaces(snapshot.spaces, snapshot.spaceRooms);
3751
3828
  this.rooms.followedTopics._importFollowedTopics(snapshot.followedTopics);
@@ -4259,6 +4336,7 @@ let ErrorCode = /*#__PURE__*/function (ErrorCode) {
4259
4336
  ErrorCode["InvalidSearchQuery"] = "InvalidSearchQueryException";
4260
4337
  ErrorCode["MessageSearchUnavailable"] = "MessageSearchUnavailableException";
4261
4338
  ErrorCode["MessageNotFound"] = "MessageNotFoundException";
4339
+ ErrorCode["RoomIgnored"] = "RoomIgnoredException";
4262
4340
  ErrorCode["RoomNotFound"] = "RoomNotFoundException";
4263
4341
  ErrorCode["TopicNotFound"] = "TopicNotFoundException";
4264
4342
  ErrorCode["UserNotFound"] = "UserNotFoundException";