@devanachat/sdk 0.7.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.
@@ -10,10 +10,12 @@ import { AbstractRestClient } from "./AbstractRestClient";
10
10
  import { UnreadSummary } from "./state-tracker/FollowedTopicsManager";
11
11
  import { TopicHistoryWindow } from "./state-tracker/TopicHistoryWindow";
12
12
  import { PermissionsManager } from "./state-tracker/PermissionsManager";
13
- export { IndexedCollection, ObservableIndexedCollection, IndexedObjectCollection, ObservableIndexedObjectCollection, Permissions, PermissionDefinition, Layer, WebSocketChatClient, WebApiChatClient, FilesClient, AbstractRestClient, extractUserFromMember, ErrorCode, UserStatus, RoleFlag, SpaceFeature, UserFeature, HistoryRotation, SearchRange, UiPurpose, SpaceIntent, NO_LIMIT, STORAGE_UNIT_BYTES, };
13
+ import { STATE_SNAPSHOT_VERSION } from "./state-tracker/StateSnapshot";
14
+ export { IndexedCollection, ObservableIndexedCollection, IndexedObjectCollection, ObservableIndexedObjectCollection, Permissions, PermissionDefinition, Layer, WebSocketChatClient, WebApiChatClient, FilesClient, AbstractRestClient, extractUserFromMember, ErrorCode, UserStatus, RoleFlag, SpaceFeature, UserFeature, HistoryRotation, SearchRange, UiPurpose, SpaceIntent, NO_LIMIT, STORAGE_UNIT_BYTES, STATE_SNAPSHOT_VERSION, };
14
15
  export type { CommandResult, CommandRequest, CommandResponse, CommandsMap, EventsMap, } from "./AbstractChatClient";
15
16
  export type { WebSocketClientOptions } from "./WebSocketChatClient";
16
17
  export type { WebApiChatClientOptions } from "./WebApiChatClient";
17
18
  export type { RestClientOptions, RestClientResponse } from "./AbstractRestClient";
18
19
  export type { EventHandler, ObservableInterface } from "./EventTarget";
20
+ export type { StateSnapshot, SpaceRoomsSnapshot, RoomFollowedTopicsSnapshot, EmoticonsSnapshot, StateSnapshotIntent, TopicHistorySnapshot, RoomMembersSnapshot, } from "./state-tracker/StateSnapshot";
19
21
  export type { ChatTypes, File, UnreadSummary, TopicHistoryWindow, PermissionsManager, };
@@ -6,6 +6,7 @@ import { PermissionsManager } from "./PermissionsManager";
6
6
  import { EmoticonsManager } from "./EmoticonsManager";
7
7
  import { UsersManager } from "./UsersManager";
8
8
  import { RelationshipsManager } from "./RelationshipsManager";
9
+ import { StateSnapshot, StateSnapshotIntent } from "./StateSnapshot";
9
10
  export declare class ChatStateTracker {
10
11
  readonly client: WebSocketChatClient;
11
12
  /**
@@ -34,8 +35,36 @@ export declare class ChatStateTracker {
34
35
  readonly relationships: RelationshipsManager;
35
36
  private _me;
36
37
  private readonly deferredSession;
38
+ private sessionReceived;
39
+ private snapshotImported;
37
40
  constructor(client: WebSocketChatClient);
38
41
  get me(): User | null;
39
42
  getMe(): Promise<User>;
43
+ /**
44
+ * Export the tracked state to a plain, JSON-serializable object, to be saved and restored with
45
+ * {@link importSnapshot} at the next start of the application, before the connection is established.
46
+ * See {@link StateSnapshot} for what is included.
47
+ * @param intent What to include beyond the state that is always exported, e.g. the message histories.
48
+ * @return Null if there is no state yet (no session has been received or imported).
49
+ */
50
+ exportSnapshot(intent?: StateSnapshotIntent): StateSnapshot | null;
51
+ /**
52
+ * Restore the state exported with {@link exportSnapshot}, so it is available before the connection is
53
+ * established. The collections emit their usual change events and the getters waiting for the session resolve.
54
+ * The first `Session` from the server then replaces the restored state the same way it does on a reconnect:
55
+ * removed spaces and rooms are dropped, the rest is updated in place, the restored message histories and topics
56
+ * other than the default ones are fetched anew (as a window holding the latest messages is on a reconnect), and
57
+ * the lazily loaded lists are refetched on their next access.
58
+ *
59
+ * Allowed only once and only before the first `Session`.
60
+ * @return False if the snapshot was rejected (another version, malformed data, or a session already received) -
61
+ * the state is not changed then.
62
+ */
63
+ importSnapshot(snapshot: unknown): boolean;
40
64
  private handleSession;
65
+ /**
66
+ * A reconnect keeps the followed topics and emoticons of rooms and spaces that are gone; for the restored ones
67
+ * the first session is the only chance to tell they are gone.
68
+ */
69
+ private handleSessionAfterImport;
41
70
  }
@@ -1,12 +1,28 @@
1
1
  import { ChatStateTracker } from "./ChatStateTracker";
2
2
  import { ObservableIndexedObjectCollection } from "../IndexedObjectCollection";
3
3
  import { Emoticon } from "../protocol";
4
+ import { EmoticonsSnapshot } from "./StateSnapshot";
4
5
  export declare class EmoticonsManager {
5
6
  private tracker;
6
7
  private list;
7
8
  private emoticonsPromises;
8
9
  constructor(tracker: ChatStateTracker);
9
10
  get(spaceId?: string): Promise<ObservableIndexedObjectCollection<Emoticon>>;
11
+ /**
12
+ * For internal use.
13
+ * @internal
14
+ */
15
+ _exportEmoticons(): EmoticonsSnapshot[];
16
+ /**
17
+ * Emoticons of spaces other than the given ones are skipped.
18
+ * @internal
19
+ */
20
+ _importEmoticons(snapshots: EmoticonsSnapshot[], spaceIds: string[]): void;
21
+ /**
22
+ * For internal use.
23
+ * @internal
24
+ */
25
+ _forgetSpacesExcept(spaceIds: string[]): void;
10
26
  private handleEmoticons;
11
27
  private handleNewEmoticon;
12
28
  private handleEmoticonDeleted;
@@ -1,6 +1,7 @@
1
1
  import { EventTarget } from "../EventTarget";
2
2
  import { ChatStateTracker } from "./ChatStateTracker";
3
3
  import { ObservableIndexedObjectCollection } from "../IndexedObjectCollection";
4
+ import { RoomFollowedTopicsSnapshot } from "./StateSnapshot";
4
5
  import { ChatLocation, FollowedTopic } from "../protocol";
5
6
  interface EventMap {
6
7
  change: {};
@@ -50,6 +51,21 @@ export declare class FollowedTopicsManager extends EventTarget<EventMap> {
50
51
  * @internal
51
52
  */
52
53
  _deleteByTopicIds(roomId: string, ...topicIds: string[]): void;
54
+ /**
55
+ * For internal use.
56
+ * @internal
57
+ */
58
+ _exportFollowedTopics(): RoomFollowedTopicsSnapshot[];
59
+ /**
60
+ * Expects the rooms to be imported already - followed topics of other rooms are skipped.
61
+ * @internal
62
+ */
63
+ _importFollowedTopics(snapshots: RoomFollowedTopicsSnapshot[]): void;
64
+ /**
65
+ * For internal use.
66
+ * @internal
67
+ */
68
+ _forgetRoomsExcept(roomIds: string[]): void;
53
69
  private handleSession;
54
70
  private handleNewMessage;
55
71
  private handleFollowedTopicUpdated;
@@ -1,6 +1,8 @@
1
1
  import { ChatStateTracker } from "./ChatStateTracker";
2
- import { Message, ChatLocation } from "../protocol";
2
+ import { Room, Message, ChatLocation, Topic } from "../protocol";
3
3
  import { RoomMessagesHistory } from "./RoomMessagesHistory";
4
+ import { TopicHistoryWindow } from "./TopicHistoryWindow";
5
+ import { TopicHistorySnapshot } from "./StateSnapshot";
4
6
  export declare class MessagesManager {
5
7
  private tracker;
6
8
  private readonly roomHistories;
@@ -18,6 +20,21 @@ export declare class MessagesManager {
18
20
  * @internal
19
21
  */
20
22
  _resolveLastMessage(location: ChatLocation): Promise<Message | null>;
23
+ /**
24
+ * For internal use.
25
+ * @internal
26
+ */
27
+ _importRooms(rooms: Room[]): void;
28
+ /**
29
+ * For internal use.
30
+ * @internal
31
+ */
32
+ _exportLatest(roomId: string, topicId: string, limit: number): ReturnType<TopicHistoryWindow['_exportLatest']>;
33
+ /**
34
+ * For internal use.
35
+ * @internal
36
+ */
37
+ _importHistory(roomId: string, topic: Topic, snapshot: TopicHistorySnapshot): void;
21
38
  private forEachRoomHistory;
22
39
  private createHistoryForNewRoom;
23
40
  private handleRoomDeleted;
@@ -5,9 +5,20 @@ export declare class RelationshipsManager {
5
5
  private tracker;
6
6
  private relationships;
7
7
  private promises;
8
+ private loaded;
8
9
  constructor(tracker: ChatStateTracker);
9
10
  get(): Promise<ObservableIndexedObjectCollection<UserRelationship>>;
10
11
  exists(refUserId: string, type: UserRelationshipType): Promise<boolean>;
12
+ /**
13
+ * For internal use.
14
+ * @internal
15
+ */
16
+ _exportRelationships(): UserRelationship[] | null;
17
+ /**
18
+ * For internal use.
19
+ * @internal
20
+ */
21
+ _importRelationships(relationships: UserRelationship[] | null): void;
11
22
  private handleRelationships;
12
23
  private handleNewRelationship;
13
24
  private handleRelationshipDeleted;
@@ -1,5 +1,5 @@
1
1
  import { ChatStateTracker } from "./ChatStateTracker";
2
- import { MessagesRedacted, NewMessage, NewTopic, Reacted, ReactionUpdated, Room, RoomUpdated, TopicDeleted } from "../protocol";
2
+ import { Message, MessagesRedacted, NewMessage, NewTopic, Reacted, ReactionUpdated, Room, RoomUpdated, Topic, TopicDeleted, UserReaction } from "../protocol";
3
3
  import { TopicHistoryWindow } from "./TopicHistoryWindow";
4
4
  /**
5
5
  * Does not subscribe to the client itself - the messages manager routes the events of its room to it, so a history
@@ -31,6 +31,21 @@ export declare class RoomMessagesHistory {
31
31
  * untouched so their in-memory context survives the reconnect.
32
32
  */
33
33
  resync(room: Room): Promise<void>;
34
+ /**
35
+ * For internal use.
36
+ * @internal
37
+ */
38
+ get _isEphemeral(): boolean;
39
+ /**
40
+ * The window of the topic, if created; unlike getMessagesWindow(), never creates nor fetches one.
41
+ * @internal
42
+ */
43
+ _peekWindow(topicId: string): TopicHistoryWindow | undefined;
44
+ /**
45
+ * For internal use.
46
+ * @internal
47
+ */
48
+ _restoreWindow(topic: Topic, messages: Message[], myReactions: Record<string, UserReaction[]>): void;
34
49
  /**
35
50
  * For internal use.
36
51
  * @internal
@@ -3,6 +3,7 @@ import { Room, RoomMember, Topic } from "../protocol";
3
3
  import { ChatStateTracker } from "./ChatStateTracker";
4
4
  import { MessagesManager } from "./MessagesManager";
5
5
  import { FollowedTopicsManager } from "./FollowedTopicsManager";
6
+ import { RoomMembersSnapshot, StateSnapshotIntent, TopicHistorySnapshot } from "./StateSnapshot";
6
7
  export declare class RoomsManager {
7
8
  private tracker;
8
9
  readonly messages: MessagesManager;
@@ -13,6 +14,7 @@ export declare class RoomsManager {
13
14
  private readonly deferredSession;
14
15
  private readonly membersPromises;
15
16
  private readonly topicsPromises;
17
+ private readonly importedTopicIds;
16
18
  constructor(tracker: ChatStateTracker);
17
19
  /**
18
20
  * Get collection of room members.
@@ -41,6 +43,36 @@ export declare class RoomsManager {
41
43
  * @internal
42
44
  */
43
45
  _deleteTopicsFromRoom(roomId: string, ...topicIds: string[]): void;
46
+ /**
47
+ * For internal use.
48
+ * @internal
49
+ */
50
+ _exportRooms(): Room[];
51
+ /**
52
+ * For internal use.
53
+ * @internal
54
+ */
55
+ _importRooms(rooms: Room[]): void;
56
+ /**
57
+ * For internal use.
58
+ * @internal
59
+ */
60
+ _exportHistories(intent: StateSnapshotIntent): TopicHistorySnapshot[];
61
+ /**
62
+ * Members of private conversations are not exported - they are made of the recipients of the room.
63
+ * @internal
64
+ */
65
+ _exportMembers(roomIds: string[], limit: number): RoomMembersSnapshot[];
66
+ /**
67
+ * Expects the rooms to be imported already - histories and members of other rooms are skipped.
68
+ * @internal
69
+ */
70
+ _importHistories(histories: TopicHistorySnapshot[], roomMembers: RoomMembersSnapshot[]): void;
71
+ /**
72
+ * The session carries only the default topics, so the other restored ones are fetched anew.
73
+ * @internal
74
+ */
75
+ _refreshImportedTopics(): void;
44
76
  private deleteRoom;
45
77
  private deleteRoomsBySpaceId;
46
78
  private handleSpaceMemberUpdated;
@@ -1,6 +1,7 @@
1
1
  import { ChatStateTracker } from "./ChatStateTracker";
2
2
  import { ObservableIndexedObjectCollection } from "../IndexedObjectCollection";
3
3
  import { Role, RoomSummary, Space, SpaceMember } from "../protocol";
4
+ import { SpaceRoomsSnapshot } from "./StateSnapshot";
4
5
  export declare class SpacesManager {
5
6
  private tracker;
6
7
  private readonly list;
@@ -38,6 +39,21 @@ export declare class SpacesManager {
38
39
  * Get a space member representing the current user.
39
40
  */
40
41
  getMe(spaceId: string): Promise<SpaceMember | undefined>;
42
+ /**
43
+ * For internal use.
44
+ * @internal
45
+ */
46
+ _exportSpaces(): Space[];
47
+ /**
48
+ * Room listings of the joined spaces only - of other spaces they are fetched for a moment's look.
49
+ * @internal
50
+ */
51
+ _exportSpaceRooms(): SpaceRoomsSnapshot[];
52
+ /**
53
+ * For internal use.
54
+ * @internal
55
+ */
56
+ _importSpaces(spaces: Space[], spaceRooms: SpaceRoomsSnapshot[]): void;
41
57
  private handleNewRole;
42
58
  private handleNewRoom;
43
59
  private handleRoomUpdated;
@@ -0,0 +1,102 @@
1
+ import { Emoticon, FollowedTopic, Message, Room, RoomMember, RoomSummary, Space, Topic, User, UserReaction, UserRelationship } from "../protocol";
2
+ /**
3
+ * Version of the {@link StateSnapshot} format. A snapshot of any other version is rejected on import.
4
+ */
5
+ export declare const STATE_SNAPSHOT_VERSION = 1;
6
+ /**
7
+ * Plain, JSON-serializable copy of the state tracked by {@link ChatStateTracker}, made to restore the state at the
8
+ * application start (before the connection is established) - see {@link ChatStateTracker.exportSnapshot}.
9
+ *
10
+ * Only the data that lasts is included: the current user, the joined spaces (with roles) and rooms (with the default
11
+ * topics), and the lazily loaded lists already fetched - rooms of spaces, followed topics, emoticons and relationships.
12
+ * The message histories of the topics named in the {@link StateSnapshotIntent} are included too, with what their
13
+ * display needs: the topics referenced by the messages and, optionally, the members of the rooms.
14
+ * Left out on purpose:
15
+ * - presence - every user status is saved as `Offline`, a status from the past would show people online who are not;
16
+ * - members of spaces, and of rooms not asked for - large, made mostly of presence, and refetched cheaply on demand;
17
+ * - other message histories and topics, histories of ephemeral rooms (they live only in the memory of the session),
18
+ * permission overwrites and room previews - loaded on demand once the connection is up, as on a reconnect.
19
+ */
20
+ export interface StateSnapshot {
21
+ version: typeof STATE_SNAPSHOT_VERSION;
22
+ /**
23
+ * Time of the export, in milliseconds since the epoch.
24
+ */
25
+ createdAt: number;
26
+ user: User;
27
+ spaces: Space[];
28
+ rooms: Room[];
29
+ spaceRooms: SpaceRoomsSnapshot[];
30
+ followedTopics: RoomFollowedTopicsSnapshot[];
31
+ emoticons: EmoticonsSnapshot[];
32
+ /**
33
+ * Null when the relationships have not been loaded.
34
+ */
35
+ relationships: UserRelationship[] | null;
36
+ histories: TopicHistorySnapshot[];
37
+ roomMembers: RoomMembersSnapshot[];
38
+ }
39
+ /**
40
+ * What {@link ChatStateTracker.exportSnapshot} should include beyond the state that is always exported.
41
+ */
42
+ export interface StateSnapshotIntent {
43
+ /**
44
+ * Topics to save the latest messages of, e.g. the ones last open in the application. Without `topicId`, the default
45
+ * topic of the room. Only a history window holding the latest messages (the `LATEST` state) is saved.
46
+ */
47
+ histories?: {
48
+ roomId: string;
49
+ topicId?: string;
50
+ }[];
51
+ /**
52
+ * Messages saved per topic, the latest ones. Never fewer than the `fetchLimit` of the window (50 by default), so a
53
+ * restored window tells right whether it reaches the oldest message. Defaults to that `fetchLimit`.
54
+ */
55
+ historyLimit?: number;
56
+ /**
57
+ * Save the members of the rooms of the histories (for the roles, colors and avatars of the authors) when they are
58
+ * loaded and there are not more of them than this. Defaults to 0 - none.
59
+ */
60
+ roomMembersLimit?: number;
61
+ }
62
+ export interface TopicHistorySnapshot {
63
+ roomId: string;
64
+ topicId: string;
65
+ /**
66
+ * Oldest first.
67
+ */
68
+ messages: Message[];
69
+ myReactions: Record<string, UserReaction[]>;
70
+ /**
71
+ * The topic of the history (if not the default one of the room) and the topics the messages refer to, as far as
72
+ * they are known.
73
+ */
74
+ topics: Topic[];
75
+ }
76
+ export interface RoomMembersSnapshot {
77
+ roomId: string;
78
+ members: RoomMember[];
79
+ }
80
+ export interface SpaceRoomsSnapshot {
81
+ spaceId: string;
82
+ summaries: RoomSummary[];
83
+ }
84
+ export interface RoomFollowedTopicsSnapshot {
85
+ roomId: string;
86
+ followedTopics: FollowedTopic[];
87
+ }
88
+ export interface EmoticonsSnapshot {
89
+ /**
90
+ * Null for the global emoticons.
91
+ */
92
+ spaceId: string | null;
93
+ emoticons: Emoticon[];
94
+ }
95
+ export declare function withoutPresence(user: User): User;
96
+ export declare function messageWithoutPresence(message: Message): Message;
97
+ export declare function memberWithoutPresence(member: RoomMember): RoomMember;
98
+ /**
99
+ * Structural check of the data restored from a storage - deep enough that importing it cannot throw, as the state
100
+ * handlers dereference these fields without checking them.
101
+ */
102
+ export declare function isValidStateSnapshot(value: unknown): value is StateSnapshot;
@@ -148,6 +148,20 @@ export declare class TopicHistoryWindow extends TraversableRemoteCollection<Mess
148
148
  fetchNext(): Promise<void>;
149
149
  fetchPrevious(): Promise<void>;
150
150
  jumpTo(id: string): Promise<void>;
151
+ /**
152
+ * The latest messages (oldest first) with the reactions of the user, or null if the window does not hold them.
153
+ * @internal
154
+ */
155
+ _exportLatest(limit: number): {
156
+ messages: Message[];
157
+ myReactions: Record<string, UserReaction[]>;
158
+ } | null;
159
+ /**
160
+ * Fill the window with saved latest messages, as if they were just fetched; the next reset to the latest replaces
161
+ * them.
162
+ * @internal
163
+ */
164
+ _restoreLatest(messages: Message[], myReactions: Record<string, UserReaction[]>): void;
151
165
  /**
152
166
  * For internal use.
153
167
  * @internal
@@ -13,6 +13,11 @@ export declare class UsersManager {
13
13
  * Get all available (cached) user objects at once.
14
14
  */
15
15
  getAvailable(): Promise<ObservableIndexedObjectCollection<User>>;
16
+ /**
17
+ * For internal use.
18
+ * @internal
19
+ */
20
+ _importUser(user: User): void;
16
21
  private handleMembers;
17
22
  private handleSession;
18
23
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devanachat/sdk",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "TypeScript/JavaScript SDK for the Devana chat platform: WebSocket and Web API clients, state tracking, permissions and files.",
5
5
  "keywords": [
6
6
  "devana",