@devanachat/sdk 0.15.0 → 0.16.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,6 +4,30 @@ 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.16.0]
8
+
9
+ ### Added
10
+
11
+ - `GetPermissionOverwrites` takes a list: `queries`, each a `location` with a `target`, answered with
12
+ `PermissionOverwritesList` - its `results` hold one `PermissionOverwrites` for every query, in their order, an empty
13
+ `allow`/`deny` where nothing is set. One request takes at most 50 queries referring to at most 4 different
14
+ locations, and fails as a whole when you may not read one of them. The types: `GetPermissionOverwritesBatch`,
15
+ `GetPermissionOverwritesQuery` and the `PermissionOverwritesList` event.
16
+
17
+ ### Changed
18
+
19
+ - `PermissionsManager` fetches every overwrite a permission check needs with one request instead of one request per
20
+ layer and role: a topic of a space where you hold 3 roles takes 1 request instead of 13, and so does every reconnect.
21
+ Checks of the room or the space of a topic already checked need no request; a role you get later is fetched alone.
22
+ `getOverwrites()` sends a list of one query and, as before, answers from the cache, which follows only your own
23
+ overwrites and those of your roles in the locations you belong to; for live overwrites of another target send
24
+ `GetPermissionOverwrites` yourself.
25
+
26
+ ### Deprecated
27
+
28
+ - `GetPermissionOverwrites` with a single `location` and `target` (`GetSinglePermissionOverwrites`), answered with
29
+ `PermissionOverwrites`. The server still accepts it for now; send `queries` instead.
30
+
7
31
  ## [0.15.0]
8
32
 
9
33
  ### Added
@@ -3615,6 +3615,7 @@ class Permissions {
3615
3615
 
3616
3616
 
3617
3617
  const getOvId = (location, target) => [location.spaceId, location.roomId, location.topicId, target?.type, target?.userId, target?.roleId].filter(Boolean).join('/');
3618
+ const FETCH_CHUNK_SIZE = 50;
3618
3619
  const getOvIdByObject = overwrites => getOvId(overwrites.location, overwrites.target);
3619
3620
  class PermissionsManager extends EventTarget {
3620
3621
  overwrites = new IndexedCollection();
@@ -3634,23 +3635,18 @@ class PermissionsManager extends EventTarget {
3634
3635
  this.tracker.client.on('RoomMemberUpdated', ev => this.handleRoomMemberUpdated(ev));
3635
3636
  this.tracker.client.on('Session', ev => this.handleSession(ev));
3636
3637
  }
3638
+
3639
+ /**
3640
+ * Cached: kept up to date only for your own overwrites and those of your roles in the locations you belong to.
3641
+ * For live overwrites of another target send `GetPermissionOverwrites` yourself.
3642
+ */
3637
3643
  async getOverwrites(location, target) {
3638
3644
  this.validateLocation(location);
3639
- const id = getOvId(location, target);
3640
- if (this.overwritesPromises.notExist(id)) {
3641
- this.overwritesPromises.registerByFunction(async () => {
3642
- const result = await this.tracker.client.send('GetPermissionOverwrites', {
3643
- location,
3644
- target
3645
- });
3646
- if (result.error) {
3647
- throw result.error;
3648
- }
3649
- this.handlePermissionOverwrites(result.data);
3650
- }, id);
3651
- }
3652
- await this.overwritesPromises.get(id);
3653
- return this.overwrites.get(id);
3645
+ await this.fetchOverwrites([{
3646
+ location,
3647
+ target
3648
+ }]);
3649
+ return this.overwrites.get(getOvId(location, target));
3654
3650
  }
3655
3651
  async check(permissionNames, location) {
3656
3652
  if (!permissionNames.length) {
@@ -3685,54 +3681,111 @@ class PermissionsManager extends EventTarget {
3685
3681
  if (location.roomId && !room || location.spaceId && !(await this.tracker.spaces.get())?.has(location.spaceId)) {
3686
3682
  return this.fetchComputedPermissions(location);
3687
3683
  }
3688
- const userId = (await this.tracker.getMe()).id;
3684
+ const me = {
3685
+ type: 'User',
3686
+ userId: (await this.tracker.getMe()).id
3687
+ };
3689
3688
  const [spaceMember, roomMember] = await this.fetchMembersOrFail(location);
3690
3689
  const userRoles = [...(spaceMember?.roles ?? []), ...(roomMember?.roles ?? [])];
3691
- const promises = [
3692
- // Global user overwrites
3693
- this.getOverwrites({}, {
3694
- type: 'User',
3695
- userId
3696
- }).then(v => v.overwrites)];
3690
+ const layers = [{
3691
+ location: {},
3692
+ withRoles: false
3693
+ }];
3697
3694
  if (location.spaceId && (await this.tracker.spaces.get())?.has(location.spaceId)) {
3698
- const filterLocation = {
3699
- spaceId: location.spaceId
3700
- };
3701
- promises.push(this.collectRoleOverwrites(filterLocation, userRoles));
3702
- promises.push(this.getOverwrites(filterLocation, {
3703
- type: 'User',
3704
- userId
3705
- }).then(v => v.overwrites));
3695
+ layers.push({
3696
+ location: {
3697
+ spaceId: location.spaceId
3698
+ },
3699
+ withRoles: true
3700
+ });
3706
3701
  }
3707
3702
  if (room) {
3708
- const filterLocation = {
3709
- spaceId: location.spaceId,
3710
- roomId: location.roomId
3711
- };
3712
- if (userRoles.length) {
3713
- promises.push(this.collectRoleOverwrites(filterLocation, userRoles));
3714
- }
3715
- promises.push(this.getOverwrites(filterLocation, {
3716
- type: 'User',
3717
- userId
3718
- }).then(v => v.overwrites));
3703
+ layers.push({
3704
+ location: {
3705
+ spaceId: location.spaceId,
3706
+ roomId: location.roomId
3707
+ },
3708
+ withRoles: userRoles.length > 0
3709
+ });
3719
3710
  }
3720
3711
  if (location.topicId && (await this.tracker.rooms.getTopics(location.roomId))?.has(location.topicId)) {
3721
- if (userRoles.length) {
3722
- promises.push(this.collectRoleOverwrites(location, userRoles));
3712
+ layers.push({
3713
+ location,
3714
+ withRoles: userRoles.length > 0
3715
+ });
3716
+ }
3717
+ const queries = [];
3718
+ for (const layer of layers) {
3719
+ if (layer.withRoles) {
3720
+ queries.push(...userRoles.map(roleId => ({
3721
+ location: layer.location,
3722
+ target: {
3723
+ type: 'Role',
3724
+ roleId
3725
+ }
3726
+ })));
3727
+ }
3728
+ queries.push({
3729
+ location: layer.location,
3730
+ target: me
3731
+ });
3732
+ }
3733
+ await this.fetchOverwrites(queries);
3734
+ const values = [];
3735
+ for (const layer of layers) {
3736
+ if (layer.withRoles) {
3737
+ values.push(...this.collectRoleOverwrites(layer.location, userRoles));
3723
3738
  }
3724
- promises.push(this.getOverwrites(location, {
3725
- type: 'User',
3726
- userId
3727
- }).then(v => v.overwrites));
3739
+ values.push(this.getFetchedOverwrites(layer.location, me).overwrites);
3728
3740
  }
3729
- return this.resolveOverwritesHierarchy([].concat(...(await Promise.all(promises))));
3741
+ return this.resolveOverwritesHierarchy(values);
3742
+ }
3743
+
3744
+ // Chunked by queries only: a calculation refers to at most 4 locations, the server limit for one request
3745
+ async fetchOverwrites(queries) {
3746
+ const missing = new Map();
3747
+ for (const query of queries) {
3748
+ const id = getOvId(query.location, query.target);
3749
+ if (this.overwritesPromises.notExist(id)) {
3750
+ missing.set(id, query);
3751
+ }
3752
+ }
3753
+ const pending = [...missing];
3754
+ for (let i = 0; i < pending.length; i += FETCH_CHUNK_SIZE) {
3755
+ const chunk = pending.slice(i, i + FETCH_CHUNK_SIZE);
3756
+ const request = this.requestOverwrites(chunk.map(([, query]) => query));
3757
+ for (const [id] of chunk) {
3758
+ this.overwritesPromises.register(request, id);
3759
+ }
3760
+ }
3761
+ await Promise.all(queries.map(query => this.overwritesPromises.get(getOvId(query.location, query.target))));
3762
+ }
3763
+ async requestOverwrites(queries) {
3764
+ const result = await this.tracker.client.send('GetPermissionOverwrites', {
3765
+ queries
3766
+ });
3767
+ if (result.error) {
3768
+ throw result.error;
3769
+ }
3770
+ const results = 'results' in result.data ? result.data.results : [];
3771
+ if (results.length !== queries.length) {
3772
+ throw new Error(`Expected ${queries.length} permission overwrites, received ${results.length}`);
3773
+ }
3774
+ for (const overwrites of results) {
3775
+ this.handlePermissionOverwrites(overwrites);
3776
+ }
3777
+ }
3778
+ getFetchedOverwrites(location, target) {
3779
+ const overwrites = this.overwrites.get(getOvId(location, target));
3780
+ if (!overwrites) {
3781
+ throw new Error(`Permission overwrites of ${getOvId(location, target)} are not fetched`);
3782
+ }
3783
+ return overwrites;
3730
3784
  }
3731
3785
 
3732
3786
  /**
3733
3787
  * Arriving overwrites change the permissions only when they replace different ones. The first ones fetched for a
3734
- * key have not been part of any result yet - every calculation needing them awaits their fetch - and a fetch
3735
- * response arrives twice, as the command result and as an event.
3788
+ * key have not been part of any result yet - every calculation needing them awaits their fetch.
3736
3789
  */
3737
3790
  handlePermissionOverwrites(ev) {
3738
3791
  const id = getOvIdByObject(ev);
@@ -3798,11 +3851,11 @@ class PermissionsManager extends EventTarget {
3798
3851
  });
3799
3852
  return ids;
3800
3853
  }
3801
- async collectRoleOverwrites(location, userRoles) {
3802
- const roleOverwrites = await Promise.all(userRoles.map(roleId => this.getOverwrites(location, {
3854
+ collectRoleOverwrites(location, userRoles) {
3855
+ const roleOverwrites = userRoles.map(roleId => this.getFetchedOverwrites(location, {
3803
3856
  type: 'Role',
3804
3857
  roleId
3805
- })));
3858
+ }));
3806
3859
  // The space default role (everyone) has the space id; it is applied before the other roles, as on the server.
3807
3860
  const isDefaultRole = overwrites => overwrites.target.roleId === location.spaceId;
3808
3861
  return [this.mergeRolesOverwrites(roleOverwrites.filter(isDefaultRole)), this.mergeRolesOverwrites(roleOverwrites.filter(overwrites => !isDefaultRole(overwrites)))];