putSidebar method

JsonMap putSidebar(
  1. String principalId,
  2. String spaceId,
  3. JsonMap body, {
  4. String? deviceId,
})

Stores the sidebar state of principalId for spaceId from a SidebarStatePut body and appends the personal sidebar.updated (04 14: last write wins per space).

An absent sort_key keeps the stored one, use_global_order removes it. Throws FakeProblem: 404 for a space the principal does not see, 400 validation_failed for a sort_key outside the fractional index (/sort_key, invalid, as the server's fracindex check of M1-52) or together with use_global_order.

Implementation

JsonMap putSidebar(
  String principalId,
  String spaceId,
  JsonMap body, {
  String? deviceId,
}) {
  if (!spaces.containsKey(spaceId) || !seesSpace(principalId, spaceId)) {
    throw const FakeProblem(404, 'not_found');
  }
  final key = body['sort_key'] as String?;
  final global = body['use_global_order'] == true;
  if (key != null && global) {
    throw const FakeProblem(
      400,
      'validation_failed',
      errors: [
        {'pointer': '/use_global_order', 'code': 'not_allowed'},
      ],
    );
  }
  if (key != null && !FractionalIndex.isValid(key)) {
    throw const FakeProblem(
      400,
      'validation_failed',
      errors: [
        {'pointer': '/sort_key', 'code': 'invalid'},
      ],
    );
  }
  final slot = '$principalId|$spaceId';
  final previous = sidebarStates[slot];
  final sortKey = global ? null : key ?? previous?['sort_key'];
  final state = <String, Object?>{
    'space_id': spaceId,
    'collapsed': body['collapsed'] == true,
    'sort_key': ?sortKey,
    'version': ((previous?['version'] as int?) ?? 0) + 1,
    'updated_by_device_id': ?deviceId,
  };
  sidebarStates[slot] = state;
  append(
    'sidebar.updated',
    resource: {'kind': 'space', 'id': spaceId},
    scope: {'space_id': spaceId},
    payload: {'state': state},
    actor: principalId,
    audience: principalId,
  );
  return state;
}