createChatDirectLink method

  1. @override
Future<void> createChatDirectLink(
  1. ChatId chatId,
  2. ChatDirectLinkSlug slug
)
override

Creates a new ChatDirectLink with the specified ChatDirectLinkSlug and deletes the current active ChatDirectLink of the given Chat-group (if any).

Implementation

@override
Future<void> createChatDirectLink(
  ChatId chatId,
  ChatDirectLinkSlug slug,
) async {
  Log.debug('createChatDirectLink($chatId, $slug)', '$runtimeType');

  try {
    // Don't do optimism, as [slug] might be occupied, thus shouldn't set the
    // link right away.
    await Backoff.run(
      () async {
        await _graphQlProvider.createChatDirectLink(slug, groupId: chatId);
      },
      retryIf: (e) => e.isNetworkRelated,
      retries: 10,
    );
  } on CreateChatDirectLinkException catch (e) {
    switch (e.code) {
      case CreateChatDirectLinkErrorCode.artemisUnknown:
      case CreateChatDirectLinkErrorCode.occupied:
        rethrow;

      case CreateChatDirectLinkErrorCode.notGroup:
        // No-op.
        return;

      case CreateChatDirectLinkErrorCode.unknownChat:
        await remove(chatId);
        return;
    }
  }

  final RxChatImpl? chat = chats[chatId];
  chat?.chat.update(
    (c) => c?.directLink = ChatDirectLink(
      slug: slug,
      createdAt: PreciseDateTime.now(),
    ),
  );
}