InvitationLink.parse constructor

InvitationLink.parse(
  1. String text
)

Parses a full link, or the bare @alice/<id> its fragment holds.

Throws a FormatException for anything that is not a whole invitation.

Implementation

factory InvitationLink.parse(String text) {
  final trimmed = text.trim();
  final hash = trimmed.indexOf('#');
  try {
    final fragment = Uri.decodeComponent(
      hash >= 0 ? trimmed.substring(hash + 1) : trimmed,
    );
    final match = RegExp(r'^(@[^/\s]+)/([0-9a-f]{32})$').firstMatch(fragment);
    if (match == null) throw FormatException('not an invitation link', text);
    return InvitationLink(inviter: match[1]!.toAtsign(), id: match[2]!);
  } on FormatException {
    rethrow;
  } catch (e) {
    throw FormatException('not an invitation link: $e', text);
  }
}