archiveChat method

  1. @override
Future<void> archiveChat(
  1. ChatId id,
  2. bool archive
)
override

Marks the specified Chat as archived for the authenticated MyUser.

Implementation

@override
Future<void> archiveChat(ChatId id, bool archive) async {
  Log.debug('archiveChat($id, $archive)', '$runtimeType');

  RxChatImpl? chat = chats[id];
  ChatData? monologData;

  // [Chat.isArchived] will be changed by [RxChatImpl]'s own remote event
  // handler. Chat will be moved from [paginated] to [archived]
  // via [RxChatImpl].
  chat?.chat.update((c) => c?.isArchived = archive);

  try {
    // If this [Chat] is local monolog, make it remote first.
    if (id.isLocalWith(me)) {
      monologData = _chat(
        await _graphQlProvider.createMonologChat(isHidden: true),
      );

      // Dispose and delete local monolog, since it's just been replaced with
      // a remote one.
      await remove(id);

      id = monologData.chat.value.id;
      await _monologLocal.upsert(me, monolog = id);
    }

    if (archive && chat?.chat.value.favoritePosition != null) {
      await unfavoriteChat(id);
    }

    // [Chat.isArchived] will be changed by [RxChatImpl]'s own remote event
    // handler. Chat will be moved from [paginated] to [archived]
    // via [RxChatImpl].
    try {
      await Backoff.run(
        () async {
          await _graphQlProvider.toggleChatArchivation(id, archive);
        },
        retryIf: (e) => e.isNetworkRelated,
        retries: 10,
      );
    } on ToggleChatArchivationException catch (e) {
      switch (e.code) {
        case ToggleChatArchivationErrorCode.artemisUnknown:
          rethrow;

        case ToggleChatArchivationErrorCode.unknownChat:
          // No-op.
          break;
      }
    }
  } catch (_) {
    chat?.chat.update((c) => c?.isArchived = !archive);

    rethrow;
  }
}