moveThread method

JsonMap moveThread(
  1. String principalId,
  2. String threadId,
  3. String targetChannelId
)

moveThread: moves threadId into targetChannelId (05 thread.move: manage on source and target; the same effective operator, else 422 operator_mismatch); the ID stays and thread.moved names the source channel.

Implementation

JsonMap moveThread(
  String principalId,
  String threadId,
  String targetChannelId,
) {
  final thread = visibleThread(principalId, threadId);
  final source = thread['channel_id']! as String;
  if (!seesChannel(principalId, targetChannelId)) {
    throw const FakeProblem(404, 'not_found');
  }
  if (!manages(principalId, source) ||
      !manages(principalId, targetChannelId)) {
    throw const FakeProblem(403, 'forbidden');
  }
  final operator = operatorOf(threadId);
  final target = channels[targetChannelId]!.json['operator'] as JsonMap?;
  if (operator != target?['effective_agent_id']) {
    throw const FakeProblem(422, 'operator_mismatch');
  }
  thread
    ..['channel_id'] = targetChannelId
    ..['moved_at'] = stamp()
    ..['version'] = (thread['version']! as int) + 1;
  for (final m in messagesOf(threadId)) {
    m['channel_id'] = targetChannelId;
  }
  append(
    'thread.moved',
    resource: {
      'kind': 'thread',
      'id': threadId,
      'version': thread['version'],
    },
    scope: _scopeOf(threadId),
    payload: {'thread': thread, 'from_channel_id': source},
    actor: principalId,
  );
  return thread;
}