putReadState method

JsonMap putReadState(
  1. String principalId,
  2. String threadId,
  3. int lastRead, {
  4. String? deviceId,
})

Sets the read state of principalId in threadId and appends read_state.updated.

Implementation

JsonMap putReadState(
  String principalId,
  String threadId,
  int lastRead, {
  String? deviceId,
}) {
  final thread = visibleThread(principalId, threadId);
  final key = '$principalId|$threadId';
  final previous = readStates[key];
  final state = <String, Object?>{
    'thread_id': threadId,
    'channel_id': thread['channel_id'],
    'last_read_seq': lastRead,
    'mention_count': _mentions(
      principalId,
      _unread(principalId, threadId, lastRead),
    ),
    'version': ((previous?['version'] as int?) ?? 0) + 1,
    'updated_by_device_id': ?deviceId,
    'updated_at': stamp(),
  };
  readStates[key] = state;
  append(
    'read_state.updated',
    resource: {'kind': 'thread', 'id': threadId, 'version': state['version']},
    scope: _scopeOf(threadId),
    payload: {'read_state': state},
    actor: principalId,
    audience: principalId,
  );
  return state;
}