Skip to content

Commit

Permalink
Merge pull request #59 from linagora/update-is-valid-matrix-id
Browse files Browse the repository at this point in the history
TW-1735: update isValidMatrixId for user id
  • Loading branch information
Te-Z authored May 22, 2024
2 parents 272cb73 + 5a96ead commit 32d3a73
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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

0 comments on commit 32d3a73

Please sign in to comment.