From 9395b8fcd3f5db5d4ed513c178dc6b1a832747a4 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Mon, 10 Feb 2020 12:33:18 +0100 Subject: [PATCH] =?UTF-8?q?[Identifier]=C2=A0Implement=20string=20extensio?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/famedlysdk.dart | 1 + lib/src/client.dart | 2 +- lib/src/room.dart | 2 +- lib/src/user.dart | 4 +- lib/src/utils/matrix_id_string_extension.dart | 26 ++++++++++ test/matrix_id_string_extension_test.dart | 50 +++++++++++++++++++ 6 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 lib/src/utils/matrix_id_string_extension.dart create mode 100644 test/matrix_id_string_extension_test.dart diff --git a/lib/famedlysdk.dart b/lib/famedlysdk.dart index 8e90acd..8e6649d 100644 --- a/lib/famedlysdk.dart +++ b/lib/famedlysdk.dart @@ -29,6 +29,7 @@ export 'package:famedlysdk/src/sync/user_update.dart'; export 'package:famedlysdk/src/utils/device_keys_list.dart'; export 'package:famedlysdk/src/utils/matrix_exception.dart'; export 'package:famedlysdk/src/utils/matrix_file.dart'; +export 'package:famedlysdk/src/utils/matrix_id_string_extension.dart'; export 'package:famedlysdk/src/utils/mx_content.dart'; export 'package:famedlysdk/src/utils/open_id_credentials.dart'; export 'package:famedlysdk/src/utils/profile.dart'; diff --git a/lib/src/client.dart b/lib/src/client.dart index b305f3a..f6018f2 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -421,7 +421,7 @@ class Client { Future> loadFamedlyContacts() async { List contacts = []; Room contactDiscoveryRoom = - this.getRoomByAlias("#famedlyContactDiscovery:${userID.split(":")[1]}"); + this.getRoomByAlias("#famedlyContactDiscovery:${userID.domain}"); if (contactDiscoveryRoom != null) { contacts = await contactDiscoveryRoom.requestParticipants(); } else { diff --git a/lib/src/room.dart b/lib/src/room.dart index 8a47c25..ed6f6ae 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -213,7 +213,7 @@ class Room { if (canonicalAlias != null && canonicalAlias.isNotEmpty && canonicalAlias.length > 3) { - return canonicalAlias.substring(1, canonicalAlias.length).split(":")[0]; + return canonicalAlias.localpart; } List heroes = []; if (mHeroes != null && diff --git a/lib/src/user.dart b/lib/src/user.dart index 7489f19..49e94ff 100644 --- a/lib/src/user.dart +++ b/lib/src/user.dart @@ -103,9 +103,7 @@ class User extends Event { /// Returns the displayname or the local part of the Matrix ID if the user /// has no displayname. String calcDisplayname() => (displayName == null || displayName.isEmpty) - ? (stateKey != null - ? stateKey.replaceFirst("@", "").split(":")[0] - : "Unknown User") + ? (stateKey != null ? stateKey.localpart : "Unknown User") : displayName; /// Call the Matrix API to kick this user from this room. diff --git a/lib/src/utils/matrix_id_string_extension.dart b/lib/src/utils/matrix_id_string_extension.dart new file mode 100644 index 0000000..4064fb9 --- /dev/null +++ b/lib/src/utils/matrix_id_string_extension.dart @@ -0,0 +1,26 @@ +extension MatrixIdExtension on String { + static const Set VALID_SIGILS = {"@", "!", "#", "\$", "+"}; + + static const int MAX_LENGTH = 255; + + bool get isValidMatrixId { + if (this.length > MAX_LENGTH) return false; + if (!VALID_SIGILS.contains(this.substring(0, 1))) { + return false; + } + final List parts = this.substring(1).split(":"); + if (parts.length != 2 || parts[0].isEmpty || parts[1].isEmpty) { + return false; + } + return true; + } + + String get sigil => isValidMatrixId ? this.substring(0, 1) : null; + + String get localpart => + isValidMatrixId ? this.substring(1).split(":").first : null; + + String get domain => isValidMatrixId ? this.substring(1).split(":")[1] : null; + + bool equals(String other) => this.toLowerCase() == other.toLowerCase(); +} diff --git a/test/matrix_id_string_extension_test.dart b/test/matrix_id_string_extension_test.dart new file mode 100644 index 0000000..b33865c --- /dev/null +++ b/test/matrix_id_string_extension_test.dart @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2019 Zender & Kurtz GbR. + * + * Authors: + * Christian Pauly + * Marcel Radzio + * + * This file is part of famedlysdk. + * + * famedlysdk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * famedlysdk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with famedlysdk. If not, see . + */ + +import 'package:test/test.dart'; +import 'package:famedlysdk/src/utils/matrix_id_string_extension.dart'; + +void main() { + /// All Tests related to the ChatTime + group("Matrix ID String Extension", () { + test("Matrix ID String Extension", () async { + final String mxId = "@test:example.com"; + expect(mxId.isValidMatrixId, true); + expect("#test:example.com".isValidMatrixId, true); + expect("!test:example.com".isValidMatrixId, true); + expect("+test:example.com".isValidMatrixId, true); + expect("\$test:example.com".isValidMatrixId, true); + expect("test:example.com".isValidMatrixId, false); + expect("@testexample.com".isValidMatrixId, false); + expect("@:example.com".isValidMatrixId, false); + expect("@test:".isValidMatrixId, false); + expect(mxId.sigil, "@"); + expect("#test:example.com".sigil, "#"); + expect("!test:example.com".sigil, "!"); + expect("+test:example.com".sigil, "+"); + expect("\$test:example.com".sigil, "\$"); + expect(mxId.localpart, "test"); + expect(mxId.domain, "example.com"); + }); + }); +}