feat: Handle matrix: URIs as per MSC2312
parent
c65066e127
commit
03058d44e6
@ -1,33 +0,0 @@
|
|||||||
import '../app_config.dart';
|
|
||||||
|
|
||||||
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);
|
|
||||||
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,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class MatrixIdentifierStringExtensionResults {
|
|
||||||
final String primaryIdentifier;
|
|
||||||
final String secondaryIdentifier;
|
|
||||||
final String queryString;
|
|
||||||
|
|
||||||
MatrixIdentifierStringExtensionResults(
|
|
||||||
{this.primaryIdentifier, this.secondaryIdentifier, this.queryString});
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
import 'package:flutter_test/flutter_test.dart';
|
|
||||||
import 'package:fluffychat/utils/matrix_identifier_string_extension.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
group('Matrix Identifier String Extension', () {
|
|
||||||
test('parseIdentifierIntoParts', () {
|
|
||||||
var res = '#alias:beep'.parseIdentifierIntoParts();
|
|
||||||
expect(res.primaryIdentifier, '#alias:beep');
|
|
||||||
expect(res.secondaryIdentifier, null);
|
|
||||||
expect(res.queryString, null);
|
|
||||||
res = 'blha'.parseIdentifierIntoParts();
|
|
||||||
expect(res, null);
|
|
||||||
res = '#alias:beep/\$event'.parseIdentifierIntoParts();
|
|
||||||
expect(res.primaryIdentifier, '#alias:beep');
|
|
||||||
expect(res.secondaryIdentifier, '\$event');
|
|
||||||
expect(res.queryString, null);
|
|
||||||
res = '#alias:beep?blubb'.parseIdentifierIntoParts();
|
|
||||||
expect(res.primaryIdentifier, '#alias:beep');
|
|
||||||
expect(res.secondaryIdentifier, null);
|
|
||||||
expect(res.queryString, 'blubb');
|
|
||||||
res = '#alias:beep/\$event?blubb'.parseIdentifierIntoParts();
|
|
||||||
expect(res.primaryIdentifier, '#alias:beep');
|
|
||||||
expect(res.secondaryIdentifier, '\$event');
|
|
||||||
expect(res.queryString, 'blubb');
|
|
||||||
res = '#/\$?:beep/\$event?blubb?b'.parseIdentifierIntoParts();
|
|
||||||
expect(res.primaryIdentifier, '#/\$?:beep');
|
|
||||||
expect(res.secondaryIdentifier, '\$event');
|
|
||||||
expect(res.queryString, 'blubb?b');
|
|
||||||
|
|
||||||
res = 'https://matrix.to/#/#alias:beep'.parseIdentifierIntoParts();
|
|
||||||
expect(res.primaryIdentifier, '#alias:beep');
|
|
||||||
expect(res.secondaryIdentifier, null);
|
|
||||||
expect(res.queryString, null);
|
|
||||||
res = 'https://matrix.to/#/%23alias%3abeep'.parseIdentifierIntoParts();
|
|
||||||
expect(res.primaryIdentifier, '#alias:beep');
|
|
||||||
expect(res.secondaryIdentifier, null);
|
|
||||||
expect(res.queryString, null);
|
|
||||||
res = 'https://matrix.to/#/%23alias%3abeep?boop%F0%9F%A7%A1%F0%9F%A6%8A'
|
|
||||||
.parseIdentifierIntoParts();
|
|
||||||
expect(res.primaryIdentifier, '#alias:beep');
|
|
||||||
expect(res.secondaryIdentifier, null);
|
|
||||||
expect(res.queryString, 'boop%F0%9F%A7%A1%F0%9F%A6%8A');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
Loading…
Reference in New Issue