polfan-server-js-client 0.1.99932 → 0.1.99933

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.
@@ -5,5 +5,6 @@ import { AuthClient, MyAccountInterface, TokenInterface } from "./AuthClient";
5
5
  import { FilesClient, File } from "./FilesClient";
6
6
  import { Permissions, PermissionDefinition, Layer } from "./Permissions";
7
7
  import * as ChatTypes from './types/src';
8
- export { IndexedCollection, ObservableIndexedCollection, IndexedObjectCollection, ObservableIndexedObjectCollection, Permissions, PermissionDefinition, Layer, WebSocketChatClient, WebApiChatClient, AuthClient, MyAccountInterface, TokenInterface, FilesClient, File };
8
+ import { extractUserFromMember } from "./state-tracker/functions";
9
+ export { IndexedCollection, ObservableIndexedCollection, IndexedObjectCollection, ObservableIndexedObjectCollection, Permissions, PermissionDefinition, Layer, WebSocketChatClient, WebApiChatClient, AuthClient, MyAccountInterface, TokenInterface, FilesClient, File, extractUserFromMember, };
9
10
  export type { ChatTypes };
@@ -4,6 +4,7 @@ import { RoomsManager } from "./RoomsManager";
4
4
  import { SpacesManager } from "./SpacesManager";
5
5
  import { PermissionsManager } from "./PermissionsManager";
6
6
  import { EmoticonsManager } from "./EmoticonsManager";
7
+ import { Utils } from "./Utils";
7
8
  export declare class ChatStateTracker {
8
9
  readonly client: WebSocketChatClient;
9
10
  /**
@@ -22,6 +23,10 @@ export declare class ChatStateTracker {
22
23
  * State of the emoticons (global and space-related).
23
24
  */
24
25
  readonly emoticons: EmoticonsManager;
26
+ /**
27
+ * Various utilities.
28
+ */
29
+ readonly utils: Utils;
25
30
  private _me;
26
31
  private readonly deferredSession;
27
32
  constructor(client: WebSocketChatClient);
@@ -0,0 +1,15 @@
1
+ import { ChatStateTracker } from "./ChatStateTracker";
2
+ import { IndexedObjectCollection } from "../IndexedObjectCollection";
3
+ import { Room, RoomSummary, User } from "../types/src";
4
+ export declare class Utils {
5
+ private tracker;
6
+ constructor(tracker: ChatStateTracker);
7
+ /**
8
+ * Collect all room summaries and rooms you joined in one collection.
9
+ */
10
+ collectAllAvailableRooms(): Promise<IndexedObjectCollection<Room | RoomSummary>>;
11
+ /**
12
+ * Collect all users from all spaces and rooms you are in.
13
+ */
14
+ collectAllAvailableUsers(): Promise<IndexedObjectCollection<User>>;
15
+ }
@@ -1,2 +1,3 @@
1
- import { Role } from "../types/src";
1
+ import { Role, RoomMember, SpaceMember, User } from "../types/src";
2
2
  export declare function reorderRolesOnPriorityUpdate(allRoles: Role[], oldRole: Role, updatedRole: Role): Role[];
3
+ export declare function extractUserFromMember(member: RoomMember | SpaceMember): User | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "polfan-server-js-client",
3
- "version": "0.1.99932",
3
+ "version": "0.1.99933",
4
4
  "description": "JavaScript client library for handling communication with Polfan chat server.",
5
5
  "author": "Jarosław Żak",
6
6
  "license": "MIT",
package/src/index.ts CHANGED
@@ -10,6 +10,7 @@ import { AuthClient, MyAccountInterface, TokenInterface } from "./AuthClient";
10
10
  import {FilesClient, File} from "./FilesClient";
11
11
  import { Permissions, PermissionDefinition, Layer } from "./Permissions";
12
12
  import * as ChatTypes from './types/src';
13
+ import {extractUserFromMember} from "./state-tracker/functions";
13
14
 
14
15
  export {
15
16
  IndexedCollection, ObservableIndexedCollection,
@@ -17,7 +18,8 @@ export {
17
18
  Permissions, PermissionDefinition, Layer,
18
19
  WebSocketChatClient, WebApiChatClient,
19
20
  AuthClient, MyAccountInterface, TokenInterface,
20
- FilesClient, File
21
+ FilesClient, File,
22
+ extractUserFromMember,
21
23
  };
22
24
 
23
25
  export type {ChatTypes};
@@ -5,25 +5,34 @@ import {SpacesManager} from "./SpacesManager";
5
5
  import {PermissionsManager} from "./PermissionsManager";
6
6
  import {DeferredTask} from "./AsyncUtils";
7
7
  import {EmoticonsManager} from "./EmoticonsManager";
8
+ import {Utils} from "./Utils";
8
9
 
9
10
  export class ChatStateTracker {
10
11
  /**
11
12
  * State of your permissions.
12
13
  */
13
14
  public readonly permissions = new PermissionsManager(this);
15
+
14
16
  /**
15
17
  * State of the rooms you are in.
16
18
  */
17
19
  public readonly rooms: RoomsManager = new RoomsManager(this);
20
+
18
21
  /**
19
22
  * State of the spaces you are in.
20
23
  */
21
24
  public readonly spaces = new SpacesManager(this);
25
+
22
26
  /**
23
27
  * State of the emoticons (global and space-related).
24
28
  */
25
29
  public readonly emoticons = new EmoticonsManager(this);
26
30
 
31
+ /**
32
+ * Various utilities.
33
+ */
34
+ public readonly utils = new Utils(this);
35
+
27
36
  private _me: User = null;
28
37
  private readonly deferredSession = new DeferredTask();
29
38
 
@@ -0,0 +1,40 @@
1
+ import {ChatStateTracker} from "./ChatStateTracker";
2
+ import {IndexedObjectCollection} from "../IndexedObjectCollection";
3
+ import {Room, RoomSummary, User} from "../types/src";
4
+ import {extractUserFromMember} from "./functions";
5
+
6
+ export class Utils {
7
+ public constructor(private tracker: ChatStateTracker) {}
8
+
9
+ /**
10
+ * Collect all room summaries and rooms you joined in one collection.
11
+ */
12
+ public async collectAllAvailableRooms(): Promise<IndexedObjectCollection<Room | RoomSummary>> {
13
+ const result = new IndexedObjectCollection<Room | RoomSummary>('id');
14
+ const spaces = await this.tracker.spaces.get();
15
+ const collections = await Promise.all([
16
+ ...spaces.items.map(space => this.tracker.spaces.getRooms(space.id)),
17
+ this.tracker.rooms.get(),
18
+ ]);
19
+
20
+ collections.forEach(collection => result.set(...collection.items));
21
+
22
+ return result;
23
+ }
24
+
25
+ /**
26
+ * Collect all users from all spaces and rooms you are in.
27
+ */
28
+ public async collectAllAvailableUsers(): Promise<IndexedObjectCollection<User>> {
29
+ const result = new IndexedObjectCollection<User>('id');
30
+ const [spaces, rooms] = await Promise.all([this.tracker.spaces.get(), this.tracker.rooms.get()]);
31
+ const collections = await Promise.all([
32
+ ...spaces.items.map(space => this.tracker.spaces.getMembers(space.id)),
33
+ ...rooms.items.map(room => this.tracker.rooms.getMembers(room.id)),
34
+ ]);
35
+
36
+ collections.forEach(collection => result.set(...collection.items.map(extractUserFromMember)));
37
+
38
+ return result;
39
+ }
40
+ }
@@ -1,4 +1,4 @@
1
- import {Role} from "../types/src";
1
+ import {Role, RoomMember, SpaceMember, User} from "../types/src";
2
2
 
3
3
  export function reorderRolesOnPriorityUpdate(allRoles: Role[], oldRole: Role, updatedRole: Role): Role[] {
4
4
  // If the priority has changed, adjust the rest of roles
@@ -22,4 +22,8 @@ export function reorderRolesOnPriorityUpdate(allRoles: Role[], oldRole: Role, up
22
22
  });
23
23
 
24
24
  return changedRoles;
25
+ }
26
+
27
+ export function extractUserFromMember(member: RoomMember | SpaceMember): User | null {
28
+ return member.user ?? (member as RoomMember).spaceMember?.user;
25
29
  }