[Identifier] Implement string extension

This commit is contained in:
Christian Pauly 2020-02-10 12:33:18 +01:00
parent 4bf6a4bcb6
commit 9395b8fcd3
6 changed files with 80 additions and 5 deletions

View File

@ -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';

View File

@ -421,7 +421,7 @@ class Client {
Future<List<User>> loadFamedlyContacts() async {
List<User> contacts = [];
Room contactDiscoveryRoom =
this.getRoomByAlias("#famedlyContactDiscovery:${userID.split(":")[1]}");
this.getRoomByAlias("#famedlyContactDiscovery:${userID.domain}");
if (contactDiscoveryRoom != null) {
contacts = await contactDiscoveryRoom.requestParticipants();
} else {

View File

@ -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<String> heroes = [];
if (mHeroes != null &&

View File

@ -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.

View File

@ -0,0 +1,26 @@
extension MatrixIdExtension on String {
static const Set<String> 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<String> 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();
}

View File

@ -0,0 +1,50 @@
/*
* Copyright (c) 2019 Zender & Kurtz GbR.
*
* Authors:
* Christian Pauly <krille@famedly.com>
* Marcel Radzio <mtrnord@famedly.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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");
});
});
}