setAccess method

void setAccess(
  1. String principalId,
  2. String channelId,
  3. String? level
)

Gives principalId level in channelId, or removes its access with a null level, and appends the personal access.changed of 04 8.2 with the new permission version.

A removed channel that was the principal's last one in its space also removes the space; a first channel in a space also adds the space. Nothing happens when nothing changes.

Implementation

void setAccess(String principalId, String channelId, String? level) {
  final channel = channels[channelId]!;
  final previous = channel.access[principalId];
  if (previous == level) return;
  final space = channel.spaceId;
  final sawSpace = space != null && seesSpace(principalId, space);
  if (level == null) {
    channel.access.remove(principalId);
  } else {
    channel.access[principalId] = level;
  }
  final seesSpaceNow = space != null && seesSpace(principalId, space);
  permissionVersion++;
  append(
    'access.changed',
    resource: {'kind': 'principal', 'id': principalId},
    payload: {
      'permission_version': permissionVersion,
      'added': [
        if (previous == null)
          {'kind': 'channel', 'id': channelId, 'level': level},
        if (!sawSpace && seesSpaceNow)
          {'kind': 'space', 'id': space, 'level': level},
      ],
      'removed': [
        if (level == null) {'kind': 'channel', 'id': channelId},
        if (sawSpace && !seesSpaceNow) {'kind': 'space', 'id': space},
      ],
      'changed': [
        if (previous != null && level != null)
          {
            'kind': 'channel',
            'id': channelId,
            'level': level,
            'previous_level': previous,
          },
      ],
    },
    audience: principalId,
  );
}