loadConversation method

Future<void> loadConversation(
  1. Object id
)

Reads one thread with its messages.

A 404 becomes MagicStarterSupportReadStatus.notFound and nothing asks again: a teammate's conversation answers 404 for the same reason a missing one does, and a retry would only repeat the refusal.

Implementation

Future<void> loadConversation(Object id) async {
  final generation = _sessionGeneration;
  final read = ++_threadRead;

  // A different thread must not show the previous one while it loads; the
  // same thread keeps its messages so a refresh does not blink.
  if (_conversation?.id.toString() != id.toString()) _conversation = null;
  _threadStatus = MagicStarterSupportReadStatus.loading;
  refreshUI();

  try {
    final response = await Http.get('/support/conversations/$id');
    if (generation != _sessionGeneration || read != _threadRead) return;

    if (response.notFound) {
      _markThreadNotFound();
      return;
    }

    if (!response.successful) {
      _threadStatus = MagicStarterSupportReadStatus.failed;
      refreshUI();
      return;
    }

    _conversation = MagicStarterSupportConversation.fromMap(
      _payload(response) as Map<String, dynamic>,
    );
    _threadStatus = MagicStarterSupportReadStatus.loaded;
  } catch (e, stackTrace) {
    Log.error(
      '[MagicStarterSupportController.loadConversation] $e\n$stackTrace',
    );
    if (generation != _sessionGeneration || read != _threadRead) return;
    _threadStatus = MagicStarterSupportReadStatus.failed;
  }

  refreshUI();
}