refactor: null safe matrix_id_string_extension

This commit is contained in:
Lukas Lihotzki 2021-09-23 13:41:46 +02:00 committed by Krille Fear
parent 3b1c6e3d2b
commit 1d0202e14e
2 changed files with 17 additions and 19 deletions

View File

@ -1350,7 +1350,7 @@ class MultiKey {
extension HiveKeyExtension on String { extension HiveKeyExtension on String {
String get toHiveKey => isValidMatrixId String get toHiveKey => isValidMatrixId
? '$sigil${Uri.encodeComponent(localpart)}:${Uri.encodeComponent(domain)}' ? '$sigil${Uri.encodeComponent(localpart!)}:${Uri.encodeComponent(domain!)}'
: Uri.encodeComponent(this); : Uri.encodeComponent(this);
} }

View File

@ -1,4 +1,3 @@
// @dart=2.9
/* /*
* Famedly Matrix SDK * Famedly Matrix SDK
* Copyright (C) 2020, 2021 Famedly GmbH * Copyright (C) 2020, 2021 Famedly GmbH
@ -32,7 +31,7 @@ extension MatrixIdExtension on String {
} }
bool get isValidMatrixId { bool get isValidMatrixId {
if (isEmpty ?? true) return false; if (isEmpty) return false;
if (length > maxLength) return false; if (length > maxLength) return false;
if (!validSigils.contains(substring(0, 1))) { if (!validSigils.contains(substring(0, 1))) {
return false; return false;
@ -50,17 +49,17 @@ extension MatrixIdExtension on String {
return true; return true;
} }
String get sigil => isValidMatrixId ? substring(0, 1) : null; String? get sigil => isValidMatrixId ? substring(0, 1) : null;
String get localpart => isValidMatrixId ? _getParts().first : null; String? get localpart => isValidMatrixId ? _getParts().first : null;
String get domain => isValidMatrixId ? _getParts().last : null; String? get domain => isValidMatrixId ? _getParts().last : null;
bool equals(String other) => toLowerCase() == other?.toLowerCase(); bool equals(String? other) => toLowerCase() == other?.toLowerCase();
/// Parse a matrix identifier string into a Uri. Primary and secondary identifiers /// Parse a matrix identifier string into a Uri. Primary and secondary identifiers
/// are stored in pathSegments. The query string is stored as such. /// are stored in pathSegments. The query string is stored as such.
Uri _parseIdentifierIntoUri() { Uri? _parseIdentifierIntoUri() {
const matrixUriPrefix = 'matrix:'; const matrixUriPrefix = 'matrix:';
const matrixToPrefix = 'https://matrix.to/#/'; const matrixToPrefix = 'https://matrix.to/#/';
if (toLowerCase().startsWith(matrixUriPrefix)) { if (toLowerCase().startsWith(matrixUriPrefix)) {
@ -86,25 +85,24 @@ extension MatrixIdExtension on String {
substring(matrixToPrefix.length - 1) substring(matrixToPrefix.length - 1)
.replaceAllMapped( .replaceAllMapped(
RegExp(r'(?<=/)[#!@+][^:]*:|(\?.*$)'), RegExp(r'(?<=/)[#!@+][^:]*:|(\?.*$)'),
(m) => m.group(0).replaceAllMapped( (m) => m[0]!.replaceAllMapped(
RegExp(m.group(1) != null ? '' : '[/?]'), RegExp(m.group(1) != null ? '' : '[/?]'),
(m) => Uri.encodeComponent(m.group(0)))) (m) => Uri.encodeComponent(m.group(0)!)))
.replaceAll('#', '%23')); .replaceAll('#', '%23'));
} else { } else {
return Uri( return Uri(
pathSegments: RegExp(r'/((?:[#!@+][^:]*:)?[^/?]*)(?:\?.*$)?') pathSegments: RegExp(r'/((?:[#!@+][^:]*:)?[^/?]*)(?:\?.*$)?')
.allMatches('/$this') .allMatches('/$this')
.map((m) => m.group(1)), .map((m) => m[1]!),
query: RegExp(r'(?:/(?:[#!@+][^:]*:)?[^/?]*)*\?(.*$)') query: RegExp(r'(?:/(?:[#!@+][^:]*:)?[^/?]*)*\?(.*$)')
.firstMatch('/$this') .firstMatch('/$this')?[1]);
?.group(1));
} }
} }
/// Separate a matrix identifier string into a primary indentifier, a secondary identifier, /// Separate a matrix identifier string into a primary indentifier, a secondary identifier,
/// a query string and already parsed `via` parameters. A matrix identifier string /// a query string and already parsed `via` parameters. A matrix identifier string
/// can be an mxid, a matrix.to-url or a matrix-uri. /// can be an mxid, a matrix.to-url or a matrix-uri.
MatrixIdentifierStringExtensionResults parseIdentifierIntoParts() { MatrixIdentifierStringExtensionResults? parseIdentifierIntoParts() {
final uri = _parseIdentifierIntoUri(); final uri = _parseIdentifierIntoUri();
if (uri == null) return null; if (uri == null) return null;
final primary = uri.pathSegments.isNotEmpty ? uri.pathSegments[0] : null; final primary = uri.pathSegments.isNotEmpty ? uri.pathSegments[0] : null;
@ -124,15 +122,15 @@ extension MatrixIdExtension on String {
class MatrixIdentifierStringExtensionResults { class MatrixIdentifierStringExtensionResults {
final String primaryIdentifier; final String primaryIdentifier;
final String secondaryIdentifier; final String? secondaryIdentifier;
final String queryString; final String? queryString;
final Set<String> via; final Set<String> via;
final String action; final String? action;
MatrixIdentifierStringExtensionResults( MatrixIdentifierStringExtensionResults(
{this.primaryIdentifier, {required this.primaryIdentifier,
this.secondaryIdentifier, this.secondaryIdentifier,
this.queryString, this.queryString,
this.via, this.via = const {},
this.action}); this.action});
} }