@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/build/index.mjs CHANGED
@@ -3564,6 +3564,7 @@ class Permissions {
3564
3564
 
3565
3565
 
3566
3566
  const getOvId = (location, target) => [location.spaceId, location.roomId, location.topicId, target?.type, target?.userId, target?.roleId].filter(Boolean).join('/');
3567
+ const FETCH_CHUNK_SIZE = 50;
3567
3568
  const getOvIdByObject = overwrites => getOvId(overwrites.location, overwrites.target);
3568
3569
  class PermissionsManager extends EventTarget {
3569
3570
  overwrites = new IndexedCollection();
@@ -3583,23 +3584,18 @@ class PermissionsManager extends EventTarget {
3583
3584
  this.tracker.client.on('RoomMemberUpdated', ev => this.handleRoomMemberUpdated(ev));
3584
3585
  this.tracker.client.on('Session', ev => this.handleSession(ev));
3585
3586
  }
3587
+
3588
+ /**
3589
+ * Cached: kept up to date only for your own overwrites and those of your roles in the locations you belong to.
3590
+ * For live overwrites of another target send `GetPermissionOverwrites` yourself.
3591
+ */
3586
3592
  async getOverwrites(location, target) {
3587
3593
  this.validateLocation(location);
3588
- const id = getOvId(location, target);
3589
- if (this.overwritesPromises.notExist(id)) {
3590
- this.overwritesPromises.registerByFunction(async () => {
3591
- const result = await this.tracker.client.send('GetPermissionOverwrites', {
3592
- location,
3593
- target
3594
- });
3595
- if (result.error) {
3596
- throw result.error;
3597
- }
3598
- this.handlePermissionOverwrites(result.data);
3599
- }, id);
3600
- }
3601
- await this.overwritesPromises.get(id);
3602
- return this.overwrites.get(id);
3594
+ await this.fetchOverwrites([{
3595
+ location,
3596
+ target
3597
+ }]);
3598
+ return this.overwrites.get(getOvId(location, target));
3603
3599
  }
3604
3600
  async check(permissionNames, location) {
3605
3601
  if (!permissionNames.length) {
@@ -3634,54 +3630,111 @@ class PermissionsManager extends EventTarget {
3634
3630
  if (location.roomId && !room || location.spaceId && !(await this.tracker.spaces.get())?.has(location.spaceId)) {
3635
3631
  return this.fetchComputedPermissions(location);
3636
3632
  }
3637
- const userId = (await this.tracker.getMe()).id;
3633
+ const me = {
3634
+ type: 'User',
3635
+ userId: (await this.tracker.getMe()).id
3636
+ };
3638
3637
  const [spaceMember, roomMember] = await this.fetchMembersOrFail(location);
3639
3638
  const userRoles = [...(spaceMember?.roles ?? []), ...(roomMember?.roles ?? [])];
3640
- const promises = [
3641
- // Global user overwrites
3642
- this.getOverwrites({}, {
3643
- type: 'User',
3644
- userId
3645
- }).then(v => v.overwrites)];
3639
+ const layers = [{
3640
+ location: {},
3641
+ withRoles: false
3642
+ }];
3646
3643
  if (location.spaceId && (await this.tracker.spaces.get())?.has(location.spaceId)) {
3647
- const filterLocation = {
3648
- spaceId: location.spaceId
3649
- };
3650
- promises.push(this.collectRoleOverwrites(filterLocation, userRoles));
3651
- promises.push(this.getOverwrites(filterLocation, {
3652
- type: 'User',
3653
- userId
3654
- }).then(v => v.overwrites));
3644
+ layers.push({
3645
+ location: {
3646
+ spaceId: location.spaceId
3647
+ },
3648
+ withRoles: true
3649
+ });
3655
3650
  }
3656
3651
  if (room) {
3657
- const filterLocation = {
3658
- spaceId: location.spaceId,
3659
- roomId: location.roomId
3660
- };
3661
- if (userRoles.length) {
3662
- promises.push(this.collectRoleOverwrites(filterLocation, userRoles));
3663
- }
3664
- promises.push(this.getOverwrites(filterLocation, {
3665
- type: 'User',
3666
- userId
3667
- }).then(v => v.overwrites));
3652
+ layers.push({
3653
+ location: {
3654
+ spaceId: location.spaceId,
3655
+ roomId: location.roomId
3656
+ },
3657
+ withRoles: userRoles.length > 0
3658
+ });
3668
3659
  }
3669
3660
  if (location.topicId && (await this.tracker.rooms.getTopics(location.roomId))?.has(location.topicId)) {
3670
- if (userRoles.length) {
3671
- promises.push(this.collectRoleOverwrites(location, userRoles));
3661
+ layers.push({
3662
+ location,
3663
+ withRoles: userRoles.length > 0
3664
+ });
3665
+ }
3666
+ const queries = [];
3667
+ for (const layer of layers) {
3668
+ if (layer.withRoles) {
3669
+ queries.push(...userRoles.map(roleId => ({
3670
+ location: layer.location,
3671
+ target: {
3672
+ type: 'Role',
3673
+ roleId
3674
+ }
3675
+ })));
3676
+ }
3677
+ queries.push({
3678
+ location: layer.location,
3679
+ target: me
3680
+ });
3681
+ }
3682
+ await this.fetchOverwrites(queries);
3683
+ const values = [];
3684
+ for (const layer of layers) {
3685
+ if (layer.withRoles) {
3686
+ values.push(...this.collectRoleOverwrites(layer.location, userRoles));
3672
3687
  }
3673
- promises.push(this.getOverwrites(location, {
3674
- type: 'User',
3675
- userId
3676
- }).then(v => v.overwrites));
3688
+ values.push(this.getFetchedOverwrites(layer.location, me).overwrites);
3677
3689
  }
3678
- return this.resolveOverwritesHierarchy([].concat(...(await Promise.all(promises))));
3690
+ return this.resolveOverwritesHierarchy(values);
3691
+ }
3692
+
3693
+ // Chunked by queries only: a calculation refers to at most 4 locations, the server limit for one request
3694
+ async fetchOverwrites(queries) {
3695
+ const missing = new Map();
3696
+ for (const query of queries) {
3697
+ const id = getOvId(query.location, query.target);
3698
+ if (this.overwritesPromises.notExist(id)) {
3699
+ missing.set(id, query);
3700
+ }
3701
+ }
3702
+ const pending = [...missing];
3703
+ for (let i = 0; i < pending.length; i += FETCH_CHUNK_SIZE) {
3704
+ const chunk = pending.slice(i, i + FETCH_CHUNK_SIZE);
3705
+ const request = this.requestOverwrites(chunk.map(([, query]) => query));
3706
+ for (const [id] of chunk) {
3707
+ this.overwritesPromises.register(request, id);
3708
+ }
3709
+ }
3710
+ await Promise.all(queries.map(query => this.overwritesPromises.get(getOvId(query.location, query.target))));
3711
+ }
3712
+ async requestOverwrites(queries) {
3713
+ const result = await this.tracker.client.send('GetPermissionOverwrites', {
3714
+ queries
3715
+ });
3716
+ if (result.error) {
3717
+ throw result.error;
3718
+ }
3719
+ const results = 'results' in result.data ? result.data.results : [];
3720
+ if (results.length !== queries.length) {
3721
+ throw new Error(`Expected ${queries.length} permission overwrites, received ${results.length}`);
3722
+ }
3723
+ for (const overwrites of results) {
3724
+ this.handlePermissionOverwrites(overwrites);
3725
+ }
3726
+ }
3727
+ getFetchedOverwrites(location, target) {
3728
+ const overwrites = this.overwrites.get(getOvId(location, target));
3729
+ if (!overwrites) {
3730
+ throw new Error(`Permission overwrites of ${getOvId(location, target)} are not fetched`);
3731
+ }
3732
+ return overwrites;
3679
3733
  }
3680
3734
 
3681
3735
  /**
3682
3736
  * Arriving overwrites change the permissions only when they replace different ones. The first ones fetched for a
3683
- * key have not been part of any result yet - every calculation needing them awaits their fetch - and a fetch
3684
- * response arrives twice, as the command result and as an event.
3737
+ * key have not been part of any result yet - every calculation needing them awaits their fetch.
3685
3738
  */
3686
3739
  handlePermissionOverwrites(ev) {
3687
3740
  const id = getOvIdByObject(ev);
@@ -3747,11 +3800,11 @@ class PermissionsManager extends EventTarget {
3747
3800
  });
3748
3801
  return ids;
3749
3802
  }
3750
- async collectRoleOverwrites(location, userRoles) {
3751
- const roleOverwrites = await Promise.all(userRoles.map(roleId => this.getOverwrites(location, {
3803
+ collectRoleOverwrites(location, userRoles) {
3804
+ const roleOverwrites = userRoles.map(roleId => this.getFetchedOverwrites(location, {
3752
3805
  type: 'Role',
3753
3806
  roleId
3754
- })));
3807
+ }));
3755
3808
  // The space default role (everyone) has the space id; it is applied before the other roles, as on the server.
3756
3809
  const isDefaultRole = overwrites => overwrites.target.roleId === location.spaceId;
3757
3810
  return [this.mergeRolesOverwrites(roleOverwrites.filter(isDefaultRole)), this.mergeRolesOverwrites(roleOverwrites.filter(overwrites => !isDefaultRole(overwrites)))];