fluffychat/lib/utils/matrix_identifier_string_extension.dart

34 lines
1.2 KiB
Dart
Raw Normal View History

import '../app_config.dart';
2020-09-19 19:21:33 +02:00
extension MatrixIdentifierStringExtension on String {
/// Separates room identifiers with an event id and possibly a query parameter into its components.
MatrixIdentifierStringExtensionResults parseIdentifierIntoParts() {
final isUrl = startsWith(AppConfig.inviteLinkPrefix);
var s = this;
if (isUrl) {
// as we decode a component we may only call it on the url part *before* the "query" part
final parts = replaceFirst(AppConfig.inviteLinkPrefix, '').split('?');
s = Uri.decodeComponent(parts.removeAt(0)) + '?' + parts.join('?');
}
final match = RegExp(r'^([#!@+][^:]*:[^\/?]*)(?:\/(\$[^?]*))?(?:\?(.*))?$')
.firstMatch(s);
2020-09-19 19:21:33 +02:00
if (match == null) {
return null;
}
return MatrixIdentifierStringExtensionResults(
primaryIdentifier: match.group(1),
secondaryIdentifier: match.group(2),
queryString: match.group(3)?.isNotEmpty ?? false ? match.group(3) : null,
2020-09-19 19:21:33 +02:00
);
}
}
class MatrixIdentifierStringExtensionResults {
final String primaryIdentifier;
final String secondaryIdentifier;
2020-09-19 19:21:33 +02:00
final String queryString;
MatrixIdentifierStringExtensionResults(
{this.primaryIdentifier, this.secondaryIdentifier, this.queryString});
2020-09-19 19:21:33 +02:00
}