@devanachat/sdk 0.6.0 → 0.8.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.
Files changed (30) hide show
  1. package/CHANGELOG.md +70 -0
  2. package/README.md +58 -0
  3. package/build/index.cjs.js +749 -38
  4. package/build/index.cjs.js.map +1 -1
  5. package/build/index.mjs +748 -39
  6. package/build/index.mjs.map +1 -1
  7. package/build/index.umd.js +1 -1
  8. package/build/index.umd.js.map +1 -1
  9. package/build/types/AbstractChatClient.d.ts +3 -1
  10. package/build/types/Permissions.d.ts +8 -0
  11. package/build/types/index.d.ts +4 -2
  12. package/build/types/protocol/index.d.ts +5 -3
  13. package/build/types/protocol/schemes/RoomSummary.d.ts +1 -0
  14. package/build/types/protocol/schemes/commands/GetComputedPermissions.d.ts +2 -2
  15. package/build/types/protocol/schemes/commands/SearchMessages.d.ts +18 -0
  16. package/build/types/protocol/schemes/events/Error.d.ts +10 -0
  17. package/build/types/protocol/schemes/events/FoundMessages.d.ts +16 -0
  18. package/build/types/state-tracker/ChatStateTracker.d.ts +29 -0
  19. package/build/types/state-tracker/EmoticonsManager.d.ts +16 -0
  20. package/build/types/state-tracker/FollowedTopicsManager.d.ts +16 -0
  21. package/build/types/state-tracker/MessagesManager.d.ts +23 -3
  22. package/build/types/state-tracker/PermissionsManager.d.ts +4 -0
  23. package/build/types/state-tracker/RelationshipsManager.d.ts +11 -0
  24. package/build/types/state-tracker/RoomMessagesHistory.d.ts +21 -3
  25. package/build/types/state-tracker/RoomsManager.d.ts +32 -0
  26. package/build/types/state-tracker/SpacesManager.d.ts +16 -0
  27. package/build/types/state-tracker/StateSnapshot.d.ts +102 -0
  28. package/build/types/state-tracker/TopicHistoryWindow.d.ts +24 -1
  29. package/build/types/state-tracker/UsersManager.d.ts +5 -0
  30. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -4,6 +4,76 @@ 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
+ ## [0.8.0] - Unreleased
8
+
9
+ ### Added
10
+
11
+ - **State snapshot** - restore the tracked state at the start of an application, before the connection is
12
+ established: `client.state.exportSnapshot()` gives a plain, JSON-serializable `StateSnapshot` (or `null` before the
13
+ first session), `client.state.importSnapshot(snapshot)` restores it into a new client. The snapshot holds the current
14
+ user, the joined spaces (with roles) and rooms (with the default topics, PM recipients), and the lists already
15
+ loaded: rooms of spaces, followed topics (unread counters), emoticons and relationships. Presence is left out (every
16
+ user is saved as `Offline`), as are members of spaces and rooms, message histories, permission overwrites and other
17
+ topics.
18
+ The import emits the usual change events of the collections and resolves the getters waiting for the session. The
19
+ first `Session` then replaces the restored state like on a reconnect (removed spaces and rooms dropped, together with
20
+ their followed topics and emoticons; the rest updated in place; the lazily loaded lists refetched on next access).
21
+ A snapshot of another `version` (`STATE_SNAPSHOT_VERSION`, now `1`) or malformed data is rejected with `false`,
22
+ without changing the state and without throwing; so is an import after the first `Session` or a second import.
23
+ - Message histories in the snapshot: `exportSnapshot(intent)` takes a `StateSnapshotIntent` -
24
+ `histories: [{roomId, topicId?}]` (no `topicId` for the default topic of the room), `historyLimit` (messages per
25
+ topic, never fewer than the window's `fetchLimit`, 50 by default) and `roomMembersLimit` (members of the rooms of the
26
+ histories, saved when loaded and not more than that; 0 by default). Only a window holding the latest messages
27
+ (`LATEST`) of a room that is not ephemeral is saved, with the reactions of the user and the topics the messages refer
28
+ to. The import restores the window in the `LATEST` state, so it is shown without a request; the first `Session`
29
+ fetches it anew (as on a reconnect) and fetches the restored topics other than the default ones.
30
+ - Type exports `StateSnapshot`, `StateSnapshotIntent`, `TopicHistorySnapshot`, `RoomMembersSnapshot`,
31
+ `SpaceRoomsSnapshot`, `RoomFollowedTopicsSnapshot`, `EmoticonsSnapshot` and the `STATE_SNAPSHOT_VERSION` constant.
32
+
33
+ ## [0.7.0] - Unreleased
34
+
35
+ ### Added
36
+
37
+ - `SearchMessages` command (answered with `FoundMessages` or an `Error`), typed end to end:
38
+ `client.send('SearchMessages', {query, spaceId, privateMessages, from, in, before, after, during, hasFile, timezone, cursor})`.
39
+ `query` is plain text (words and `"phrases"`, the last word is a prefix); the server recognises no modifiers, so
40
+ the filters (`from`, `in`, `before`, `after`, `during`, `hasFile`) are fields of the command, and parsing a search
41
+ syntax such as `from:ania` is up to the application.
42
+ Results come newest first in pages of 25; pass the returned `cursor` to get the next page (`null` means the end).
43
+ Each result carries the `message`, its `spaceId` and `ranges` of the matches (`{start, end}` in UTF-16 units,
44
+ `end` exclusive, so `content.slice(start, end)` works). Whether the user is in the room of a result is not part of
45
+ it - check your rooms: `(await client.state.rooms.get()).has(roomId)`.
46
+ - Type exports `SearchMessages`, `SearchDate`, `FoundMessages`, `FoundMessage` and `TextRange`.
47
+ - **Room preview** - the history of a room you have not joined (the `PreviewRooms` permission):
48
+ `client.state.rooms.messages.getRoomHistory(roomId)` of a room you have not joined returns a `RoomMessagesHistory` with
49
+ `isPreview` set, whose
50
+ `getMessagesWindow(topicId)` gives the usual `TopicHistoryWindow` (`resetToLatest`, `fetchPrevious`, `fetchNext`,
51
+ `jumpTo`, mirrors, `change`). No events reach such windows; at the latest messages `fetchNext()` fetches the newer
52
+ ones (and loads the latest in a window not fetched yet). It sends only reads - no `Ack`, no change of the unread counters, the room is not added to your rooms. Release it
53
+ with `forgetRoomPreview(roomId)`; joining the room replaces it with the regular history. The default topic is
54
+ `RoomSummary.defaultTopicId`.
55
+ - `PreviewRooms` (`1 << 24`) and `SearchMessages` (`1 << 25`) permissions in `Permissions.list`, definable on a space or
56
+ a room.
57
+ - `RoomSummary.defaultTopicId`: the default topic of the room, given to everyone who may read it (`null` otherwise).
58
+ - `ErrorCode` enum with the codes worth telling apart (`InvalidSearchQueryException`,
59
+ `MessageSearchUnavailableException`, `RoomNotFoundException`, `UserNotFoundException`, ...).
60
+
61
+ ### Changed
62
+
63
+ - `rooms.messages.getRoomHistory(roomId)` never resolves to `undefined`: for a room you have not joined (also one you
64
+ have left, or an unknown id) it gives the preview history, whose fetch methods throw an `Error`
65
+ without access. Tell a joined room by `history.isPreview` or by your rooms.
66
+ - `permissions.check()` and `calculatePermissions()` work for a room or a space you have not joined: they ask the server
67
+ (`GetComputedPermissions`) instead of throwing.
68
+ - Server `feature/search` semantics: `GetRoomMembers`, `GetMessages` (also `around`),
69
+ `GetTopics` and `GetReactionDetails` accept a non-member with `PreviewRooms`; `GetPermissionOverwriteTargets`
70
+ always returns the former members of a room (their overwrites still apply); `GetComputedPermissions`,
71
+ `GetPermissionOverwrites` and `GetOwners` can be read in a room by a user with `ManagePermissions` who is not a
72
+ member. Commands of participation (`CreateMessage`, `React`, `Ack`, ...) still need the membership
73
+ (`UserNotFoundException`).
74
+
75
+ Migrating from `polfan-server-js-client` 0.5.0: see [docs/migration-0.5-to-0.7.md](docs/migration-0.5-to-0.7.md).
76
+
7
77
  ## [0.6.0] - Unreleased
8
78
 
9
79
  The first release under the new name. Earlier versions were published as `polfan-server-js-client`.
package/README.md CHANGED
@@ -251,6 +251,30 @@ await topicWindow!.jumpTo('message-id');
251
251
  `topicWindow.state` tells which part of the history is loaded (`LIVE`, `LATEST`, `PAST`, `OLDEST`). Set `topicWindow.limit` to cap
252
252
  how many messages a window keeps in memory - the oldest are dropped when it grows over it.
253
253
 
254
+ ### Preview a room you have not joined
255
+
256
+ With the `PreviewRooms` permission you can read a room without joining it. `getRoomHistory()` of a room you have not
257
+ joined gives its preview (`history.isPreview`); the windows are the same as above, except that no events reach them -
258
+ at the latest messages `fetchNext()` asks the server for the newer ones. Nothing is marked as read and the room does
259
+ not join your rooms.
260
+
261
+ ```ts
262
+ const {hasAll} = await client.state!.permissions.check(['PreviewRooms'], {roomId: 'room-id'});
263
+ const {data} = await client.send('GetRoomSummary', {location: {roomId: 'room-id'}});
264
+
265
+ if (hasAll && data?.summary.defaultTopicId) {
266
+ const history = await client.state!.rooms.messages.getRoomHistory('room-id');
267
+ const topicWindow = await history.getMessagesWindow(data.summary.defaultTopicId); // Or the id of a thread
268
+
269
+ await topicWindow!.jumpTo('message-id'); // Or resetToLatest()
270
+ await topicWindow!.fetchNext(); // At the latest messages: fetch those written since
271
+
272
+ client.state!.rooms.messages.forgetRoomPreview('room-id'); // When the preview is closed
273
+ }
274
+ ```
275
+
276
+ Once you join the room, `getRoomHistory()` returns its regular history and the preview is dropped.
277
+
254
278
  ### Unread messages and mentions
255
279
 
256
280
  ```ts
@@ -287,6 +311,9 @@ client.state!.permissions.on('change', () => {
287
311
  });
288
312
  ```
289
313
 
314
+ For a room or a space you have not joined (e.g. `PreviewRooms` of a room to preview), `check()` asks the server, every
315
+ time - the `change` event does not cover such locations.
316
+
290
317
  The full list of permissions is in `Permissions.list`.
291
318
 
292
319
  ### Users and relationships
@@ -303,6 +330,37 @@ if (await client.state!.relationships.exists('user-id', 'Ignore')) {
303
330
  }
304
331
  ```
305
332
 
333
+ ### Restore the state at startup
334
+
335
+ An application can show the last known state right away, before the connection is established (or with no
336
+ connection at all): save a snapshot of the state and import it into a new client before `connect()`.
337
+
338
+ ```ts
339
+ // While running (e.g. throttled, and when the app goes to the background):
340
+ const snapshot = client.state!.exportSnapshot({
341
+ // Optional: the latest messages of the topics last open, members of their rooms (up to 200)
342
+ histories: [{roomId: lastRoomId}, {roomId: lastRoomId, topicId: lastThreadId}],
343
+ roomMembersLimit: 200,
344
+ }); // plain JSON object, or null before the first session
345
+ if (snapshot) {
346
+ storage.setItem(`snapshot:${accountId}`, JSON.stringify(snapshot));
347
+ }
348
+
349
+ // At the next start:
350
+ const client = new WebSocketChatClient({url, token});
351
+ const restored = client.state!.importSnapshot(JSON.parse(storage.getItem(`snapshot:${accountId}`) ?? 'null'));
352
+ // The spaces, rooms, unread counters, ... are available now; connect in the background.
353
+ client.connect();
354
+ ```
355
+
356
+ `importSnapshot()` returns `false` and changes nothing for a snapshot of another format version
357
+ (`STATE_SNAPSHOT_VERSION`) or malformed data, and after the first `Session`. The first `Session` replaces the
358
+ restored state as a reconnect does - a restored history window is fetched anew, like a window holding the latest
359
+ messages on a reconnect. The snapshot leaves out presence (every user is saved as `Offline`), members of spaces and of
360
+ rooms not asked for, other message histories, histories of ephemeral rooms, permission overwrites and other topics -
361
+ these are fetched once connected. Requests sent before the connection is established are queued while connecting and fail
362
+ when the client is waiting to retry.
363
+
306
364
  ## Package entitlements
307
365
 
308
366
  `GetEntitlements` answers what a space or the current account has. A space package and a user