Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TW-1735: update isValidMatrixId to fit documentation #58

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/src/utils/matrix_id_string_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,24 @@ extension MatrixIdExtension on String {
}
// all other matrix IDs have to have a domain
final parts = _getParts();
// if the localpart starts with an @, checks if the user id is valid
if (substring(0, 1) == '@') {
return _matchesUserIdRegExp(this);
}
// the localpart can be an empty string, e.g. for aliases
if (parts.length != 2 || parts[1].isEmpty) {
return false;
}
return true;
}

bool _matchesUserIdRegExp(String text) {
final globalRegExp = RegExp(
r'^@([a-z0-9.=_\-\+]+):((?:[a-zA-Z0-9\-]+\.)*[a-zA-Z]{2,}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|\[(?:[0-9a-fA-F:.]+)\])(?::\d{1,5})?$');

return globalRegExp.hasMatch(text);
}

String? get sigil => isValidMatrixId ? substring(0, 1) : null;

String? get localpart => isValidMatrixId ? _getParts().first : null;
Expand Down
5 changes: 4 additions & 1 deletion test/matrix_id_string_extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ void main() {
expect('\$testevent'.isValidMatrixId, true);
expect('test:example.com'.isValidMatrixId, false);
expect('@testexample.com'.isValidMatrixId, false);
expect('@:example.com'.isValidMatrixId, true);
expect('@:example.com'.isValidMatrixId, false);
expect('@@test:example.com'.isValidMatrixId, false);
expect('#:example.com'.isValidMatrixId, true);
expect('@test:'.isValidMatrixId, false);
expect(mxId.sigil, '@');
expect('#test:example.com'.sigil, '#');
Expand All @@ -44,6 +46,7 @@ void main() {
expect(mxId.domain, 'example.com');
expect(mxId.equals('@Test:example.com'), true);
expect(mxId.equals('@test:example.org'), false);
expect('@user:127.0.0.1:8448'.localpart, 'user');
expect('@user:domain:8448'.localpart, 'user');
expect('@user:domain:8448'.domain, 'domain:8448');
});
Expand Down
Loading