From ff586b06b176c3fd79f7925f51a798defedf6a39 Mon Sep 17 00:00:00 2001 From: Henri Carnot Date: Tue, 17 May 2022 09:25:13 +0000 Subject: [PATCH] fix: don't assume element is a string --- CHANGELOG.md | 3 +++ .../extension_recent_emoji/recent_emoji.dart | 25 +++++++++++++------ pubspec.yaml | 2 +- test/client_test.dart | 25 +++++++++++-------- test/fake_matrix_api.dart | 14 +++++++++++ 5 files changed, 49 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c84638df..fb85ec99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [0.9.6] - 16th May 2022 +- fix: Ignore invalid entries in `io.element.recent_emoji` + ## [0.9.5] - 13th May 2022 - fix: Fix deep copy issue in the fragmented timeline feature and restored it diff --git a/lib/msc_extensions/extension_recent_emoji/recent_emoji.dart b/lib/msc_extensions/extension_recent_emoji/recent_emoji.dart index 255bf55d..e0811dfc 100644 --- a/lib/msc_extensions/extension_recent_emoji/recent_emoji.dart +++ b/lib/msc_extensions/extension_recent_emoji/recent_emoji.dart @@ -22,14 +22,22 @@ extension RecentEmojiExtension on Client { /// /// There's no corresponding standard or MSC, it's just the reverse-engineered /// API from New Vector Ltd. - Map get recentEmojis => Map.fromEntries( - (accountData['io.element.recent_emoji']?.content['recent_emoji'] - as List? ?? - []) - .map( - (e) => MapEntry(e[0] as String, e[1] as int), - ), - ); + Map get recentEmojis { + final recents = {}; + + accountData['io.element.recent_emoji'] + ?.content + .tryGetList('recent_emoji') + ?.forEach((item) { + if (item is List) { + if (item.length > 1 && item[0] is String && item[1] is int) { + recents[item[0]] = item[1]; + } + } + }); + + return recents; + } /// +1 the stated emoji in the account data Future addRecentEmoji(String emoji) async { @@ -44,6 +52,7 @@ extension RecentEmojiExtension on Client { /// sets the raw recent emoji account data. Use [addRecentEmoji] instead Future setRecentEmojiData(Map data) async { + if (userID == null) return; final content = List.from(data.entries.map((e) => [e.key, e.value])); return setAccountData( userID!, 'io.element.recent_emoji', {'recent_emoji': content}); diff --git a/pubspec.yaml b/pubspec.yaml index 2150fe8b..1d19844a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: matrix description: Matrix Dart SDK -version: 0.9.5 +version: 0.9.6 homepage: https://famedly.com repository: https://gitlab.com/famedly/company/frontend/famedlysdk.git diff --git a/test/client_test.dart b/test/client_test.dart index 4e0ed240..53b922b3 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -121,7 +121,7 @@ void main() { } expect(sync.nextBatch == matrix.prevBatch, true); - expect(matrix.accountData.length, 9); + expect(matrix.accountData.length, 10); expect(matrix.getDirectChatFromUserId('@bob:example.com'), '!726s6s6q:example.com'); expect(matrix.rooms[1].directChatMatrixID, '@bob:example.com'); @@ -153,7 +153,7 @@ void main() { expect(matrix.presences['@alice:example.com']?.presence, PresenceType.online); expect(presenceCounter, 1); - expect(accountDataCounter, 9); + expect(accountDataCounter, 10); await Future.delayed(Duration(milliseconds: 50)); expect(matrix.userDeviceKeys.length, 4); expect(matrix.userDeviceKeys['@alice:example.com']?.outdated, false); @@ -207,6 +207,18 @@ void main() { null); }); + test('recentEmoji', () async { + final emojis = matrix.recentEmojis; + + expect(emojis.length, 2); + + expect(emojis['👍️'], 1); + expect(emojis['🖇️'], 0); + + await matrix.addRecentEmoji('🦙'); + // To check if the emoji is properly added, we need to wait for a sync roundtrip + }); + test('Logout', () async { final loginStateFuture = matrix.onLoginStateChanged.stream.first; await matrix.logout(); @@ -322,15 +334,6 @@ void main() { await matrix.setAvatar(testFile); }); - test('recentEmoji', () async { - final client = await getClient(); - final emojis = client.recentEmojis; - expect(emojis.isEmpty, isTrue); - - await client.addRecentEmoji('🦙'); - expect(client.recentEmojis['🦙'], 1); - }); - test('setMuteAllPushNotifications', () async { await matrix.setMuteAllPushNotifications(false); }); diff --git a/test/fake_matrix_api.dart b/test/fake_matrix_api.dart index 37cc278a..fbb713e3 100644 --- a/test/fake_matrix_api.dart +++ b/test/fake_matrix_api.dart @@ -719,6 +719,18 @@ class FakeMatrixApi extends MockClient { } } } + }, + { + 'type': 'io.element.recent_emoji', + 'content': { + 'recent_emoji': [ + ['👍️', 1], + ['🖇️', 0], + ['🙃', 'error'], + [null, null], + [1, ''] + ] + } } ] }, @@ -2195,6 +2207,8 @@ class FakeMatrixApi extends MockClient { 'PUT': { '/client/r0/user/${Uri.encodeComponent('@alice:example.com')}/account_data/io.element.recent_emoji}': (var req) => {}, + '/client/r0/user/%40test%3AfakeServer.notExisting/account_data/io.element.recent_emoji': + (var req) => {}, '/client/r0/user/%40test%3AfakeServer.notExisting/account_data/m.ignored_user_list': (var req) => {}, '/client/r0/presence/${Uri.encodeComponent('@alice:example.com')}/status':