updateMessage method

JsonMap updateMessage(
  1. String principalId,
  2. String messageId,
  3. int expectedVersion,
  4. JsonMap body,
)

Applies a MessageUpdate body to messageId of principalId when expectedVersion matches (04 13.3, 06 If-Match).

Throws FakeProblem: 404, 403 forbidden for another author, 410 message_deleted, 412 version_conflict with the current revision.

Implementation

JsonMap updateMessage(
  String principalId,
  String messageId,
  int expectedVersion,
  JsonMap body,
) {
  final m = visibleMessage(principalId, messageId);
  if (m['deleted_at'] != null) {
    throw const FakeProblem(410, 'message_deleted');
  }
  if ((m['author']! as JsonMap)['id'] != principalId) {
    throw const FakeProblem(403, 'forbidden');
  }
  if (m['version'] != expectedVersion) {
    throw FakeProblem(412, 'version_conflict', current: Map.of(m));
  }
  final earlier = revisions.putIfAbsent(messageId, () => []);
  earlier.add({
    'revision': earlier.length + 1,
    'content_md': m['content_md'],
    'edited_by': principalId,
    'created_at': m['edited_at'] ?? m['created_at'],
  });
  if (body.containsKey('content_md')) m['content_md'] = body['content_md'];
  if (body.containsKey('mention_ids')) m['mention_ids'] = body['mention_ids'];
  m
    ..['version'] = (m['version']! as int) + 1
    ..['edited_at'] = stamp();
  _messageUpdated(m, principalId);
  return m;
}