preview method

Future<CItem<ReceivedInvitation>> preview(
  1. InvitationLink link, {
  2. SealedInvitationContent? outOfBandContent,
})

Fetches an invitation's preview and keeps it, with its content when the content travels in the preview. outOfBandContent is content that travelled some other way, e.g. in an email. It can arrive after the inviter has confirmed this atSign; processConnections then opens it.

Throws a StateError when this atSign already holds an invitation with the same id from a different inviter.

Implementation

Future<CItem<ReceivedInvitation>> preview(
  InvitationLink link, {
  SealedInvitationContent? outOfBandContent,
}) async {
  final existing = await (await receivedInvitations).getOrNull(link.id, me);
  if (existing != null && existing.obj.inviter != link.inviter) {
    throw StateError('${existing.obj.inviter} already sent an invitation '
        'with the id ${link.id}');
  }
  if (existing != null) {
    if (existing.obj.sealedContent != null || outOfBandContent == null) {
      return existing;
    }
    final withContent = _withObj(
      existing,
      existing.obj.withSealedContent(outOfBandContent),
    );
    await (await receivedInvitations).update(withContent);
    return withContent;
  }
  final value = await atClient.get(
    _previewKey(link.inviter, link.id),
    getRequestOptions: GetRequestOptions()..bypassCache = true,
  );
  final preview = InvitationPreview.fromJson(jsonDecode(value.value));
  return (await receivedInvitations).upsert(
    id: link.id,
    obj: ReceivedInvitation(
      inviter: link.inviter,
      publicDetails: preview.publicDetails,
      expiresAt: preview.expiresAt,
      sealedContent: preview.content ?? outOfBandContent,
    ),
    expiresAt: preview.expiresAt.add(_afterExpiry),
  );
}