commandsOf method

List<JsonMap> commandsOf(
  1. String channelId
)

The commands offered in channelId (listChannelCommands): those of agents that see the channel, on the instance, the channel's space or the channel; the operator's first, then by lower-case name, owner and ID. The fake has no guest rule and no app commands.

Implementation

List<JsonMap> commandsOf(String channelId) {
  final channel = world.channels[channelId]!;
  final operator =
      (channel.json['operator'] as JsonMap?)?['effective_agent_id'];
  bool applies(JsonMap c) => switch (c['scope_kind']) {
    'instance' => true,
    'space' => c['scope_id'] == channel.spaceId,
    'channel' => c['scope_id'] == channelId,
    _ => false,
  };
  final list = [
    for (final c in commands.values)
      if (applies(c) &&
          world.seesChannel(c['owner_principal_id']! as String, channelId))
        c,
  ];
  int group(JsonMap c) => c['owner_principal_id'] == operator ? 0 : 1;
  list.sort((a, b) {
    var r = group(a).compareTo(group(b));
    if (r == 0) {
      r = (a['name']! as String).toLowerCase().compareTo(
        (b['name']! as String).toLowerCase(),
      );
    }
    if (r == 0) {
      r = (a['owner_principal_id']! as String).compareTo(
        b['owner_principal_id']! as String,
      );
    }
    if (r == 0) r = (a['id']! as String).compareTo(b['id']! as String);
    return r;
  });
  return list;
}