From 864b6651bbe34948d1152ee2851f87b27fc4a573 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Tue, 9 Mar 2021 19:39:25 +0100 Subject: [PATCH] refactor: Update SDK and enable login with email and phone --- lib/components/matrix.dart | 4 ++-- lib/utils/url_launcher.dart | 2 +- lib/views/chat_details.dart | 5 ++-- lib/views/homeserver_picker.dart | 1 - lib/views/login.dart | 31 ++++++++++++++++++++++++- lib/views/settings_multiple_emotes.dart | 2 +- pubspec.lock | 13 ++++++++--- pubspec.yaml | 1 + 8 files changed, 47 insertions(+), 12 deletions(-) diff --git a/lib/components/matrix.dart b/lib/components/matrix.dart index af5eae8b..a5d28e31 100644 --- a/lib/components/matrix.dart +++ b/lib/components/matrix.dart @@ -79,7 +79,7 @@ class MatrixState extends State with WidgetsBindingObserver { File wallpaper; - void _initWithStore() async { + void _initWithStore() { try { client.init(); } catch (e, s) { @@ -344,7 +344,7 @@ class MatrixState extends State with WidgetsBindingObserver { .where((e) => e.type == EventUpdateType.timeline && [EventTypes.Message, EventTypes.Sticker, EventTypes.Encrypted] - .contains(e.eventType) && + .contains(e.content['type']) && e.content['sender'] != client.userID) .listen(_showLocalNotification); }); diff --git a/lib/utils/url_launcher.dart b/lib/utils/url_launcher.dart index bcc500a5..68f17f97 100644 --- a/lib/utils/url_launcher.dart +++ b/lib/utils/url_launcher.dart @@ -46,7 +46,7 @@ class UrlLauncher { final response = await showFutureLoadingDialog( context: context, future: () => - matrix.client.requestRoomAliasInformations(roomIdOrAlias), + matrix.client.requestRoomAliasInformation(roomIdOrAlias), ); if (response.error != null) { return; // nothing to do, the alias doesn't exist diff --git a/lib/views/chat_details.dart b/lib/views/chat_details.dart index 5fd88ae7..9dadaa88 100644 --- a/lib/views/chat_details.dart +++ b/lib/views/chat_details.dart @@ -87,7 +87,7 @@ class _ChatDetailsState extends State { newAliases.add(canonicalAlias); final response = await showFutureLoadingDialog( context: context, - future: () => room.client.requestRoomAliasInformations(canonicalAlias), + future: () => room.client.requestRoomAliasInformation(canonicalAlias), ); if (response.error != null) { final success = await showFutureLoadingDialog( @@ -327,8 +327,7 @@ class _ChatDetailsState extends State { // okay, we need to test if there are any emote state events other than the default one // if so, we need to be directed to a selection screen for which pack we want to look at // otherwise, we just open the normal one. - if ((room.states - .states['im.ponies.room_emotes'] ?? + if ((room.states['im.ponies.room_emotes'] ?? {}) .keys .any((String s) => s.isNotEmpty)) { diff --git a/lib/views/homeserver_picker.dart b/lib/views/homeserver_picker.dart index 4cec13a1..8b478fea 100644 --- a/lib/views/homeserver_picker.dart +++ b/lib/views/homeserver_picker.dart @@ -36,7 +36,6 @@ class _HomeserverPickerState extends State { context: context, future: () => Matrix.of(context).client.login( type: AuthenticationTypes.token, - userIdentifierType: null, token: token, initialDeviceDisplayName: PlatformInfos.clientName, ), diff --git a/lib/views/login.dart b/lib/views/login.dart index 7738efa9..09e6fc68 100644 --- a/lib/views/login.dart +++ b/lib/views/login.dart @@ -42,8 +42,28 @@ class _LoginState extends State { setState(() => loading = true); try { + final username = usernameController.text; + AuthenticationIdentifier identifier; + if (username.isEmail) { + identifier = AuthenticationThirdPartyIdentifier( + medium: 'email', + address: username, + ); + } else if (username.isPhoneNumber) { + identifier = AuthenticationThirdPartyIdentifier( + medium: 'msisdn', + address: username, + ); + } else { + identifier = AuthenticationUserIdentifier(user: username); + } await matrix.client.login( - user: usernameController.text, + identifier: identifier, + // To stay compatible with older server versions + // ignore: deprecated_member_use + user: identifier.type == AuthenticationIdentifierTypes.userId + ? username + : null, password: passwordController.text, initialDeviceDisplayName: PlatformInfos.clientName); } on MatrixException catch (exception) { @@ -255,3 +275,12 @@ class _LoginState extends State { ); } } + +extension on String { + static final RegExp _emailRegex = RegExp( + r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+"); + static final RegExp _phoneRegex = + RegExp(r'^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$'); + bool get isEmail => _emailRegex.hasMatch(this); + bool get isPhoneNumber => _phoneRegex.hasMatch(this); +} diff --git a/lib/views/settings_multiple_emotes.dart b/lib/views/settings_multiple_emotes.dart index 7e94c65d..e761cf6b 100644 --- a/lib/views/settings_multiple_emotes.dart +++ b/lib/views/settings_multiple_emotes.dart @@ -21,7 +21,7 @@ class MultipleEmotesSettings extends StatelessWidget { stream: room.onUpdate.stream, builder: (context, snapshot) { final packs = - room.states.states['im.ponies.room_emotes'] ?? {}; + room.states['im.ponies.room_emotes'] ?? {}; if (!packs.containsKey('')) { packs[''] = null; } diff --git a/pubspec.lock b/pubspec.lock index 385d524e..834a1d77 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -197,6 +197,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.0.3" + email_validator: + dependency: "direct main" + description: + name: email_validator + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.6" emoji_picker: dependency: "direct main" description: @@ -223,7 +230,7 @@ packages: description: path: "." ref: main - resolved-ref: "82f823f50e8dc96b60a5a1f288bb93bff84b1eb3" + resolved-ref: fb0de1ce5993299b4f12d5a2adaa0324fe5d4f01 url: "https://gitlab.com/famedly/famedlysdk.git" source: git version: "0.0.1" @@ -630,7 +637,7 @@ packages: name: matrix_api_lite url: "https://pub.dartlang.org" source: hosted - version: "0.1.9" + version: "0.2.0" matrix_file_e2ee: dependency: transitive description: @@ -723,7 +730,7 @@ packages: name: olm url: "https://pub.dartlang.org" source: hosted - version: "1.2.1" + version: "1.3.0" open_file: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index b72bde74..1e1b64b8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -84,6 +84,7 @@ dependencies: package_info: ^0.4.3+2 flutter_app_lock: ^1.4.0+1 flutter_screen_lock: ^1.2.6 + email_validator: ^1.0.6 dev_dependencies: flutter_test: