fix: don't assume element is a string

This commit is contained in:
Henri Carnot 2022-05-17 09:25:13 +00:00
parent 7e4096cc63
commit ff586b06b1
5 changed files with 49 additions and 20 deletions

View File

@ -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 ## [0.9.5] - 13th May 2022
- fix: Fix deep copy issue in the fragmented timeline feature and restored it - fix: Fix deep copy issue in the fragmented timeline feature and restored it

View File

@ -22,14 +22,22 @@ extension RecentEmojiExtension on Client {
/// ///
/// There's no corresponding standard or MSC, it's just the reverse-engineered /// There's no corresponding standard or MSC, it's just the reverse-engineered
/// API from New Vector Ltd. /// API from New Vector Ltd.
Map<String, int> get recentEmojis => Map.fromEntries( Map<String, int> get recentEmojis {
(accountData['io.element.recent_emoji']?.content['recent_emoji'] final recents = <String, int>{};
as List<dynamic>? ??
[]) accountData['io.element.recent_emoji']
.map( ?.content
(e) => MapEntry(e[0] as String, e[1] as int), .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 /// +1 the stated emoji in the account data
Future<void> addRecentEmoji(String emoji) async { Future<void> addRecentEmoji(String emoji) async {
@ -44,6 +52,7 @@ extension RecentEmojiExtension on Client {
/// sets the raw recent emoji account data. Use [addRecentEmoji] instead /// sets the raw recent emoji account data. Use [addRecentEmoji] instead
Future<void> setRecentEmojiData(Map<String, int> data) async { Future<void> setRecentEmojiData(Map<String, int> data) async {
if (userID == null) return;
final content = List.from(data.entries.map((e) => [e.key, e.value])); final content = List.from(data.entries.map((e) => [e.key, e.value]));
return setAccountData( return setAccountData(
userID!, 'io.element.recent_emoji', {'recent_emoji': content}); userID!, 'io.element.recent_emoji', {'recent_emoji': content});

View File

@ -1,6 +1,6 @@
name: matrix name: matrix
description: Matrix Dart SDK description: Matrix Dart SDK
version: 0.9.5 version: 0.9.6
homepage: https://famedly.com homepage: https://famedly.com
repository: https://gitlab.com/famedly/company/frontend/famedlysdk.git repository: https://gitlab.com/famedly/company/frontend/famedlysdk.git

View File

@ -121,7 +121,7 @@ void main() {
} }
expect(sync.nextBatch == matrix.prevBatch, true); expect(sync.nextBatch == matrix.prevBatch, true);
expect(matrix.accountData.length, 9); expect(matrix.accountData.length, 10);
expect(matrix.getDirectChatFromUserId('@bob:example.com'), expect(matrix.getDirectChatFromUserId('@bob:example.com'),
'!726s6s6q:example.com'); '!726s6s6q:example.com');
expect(matrix.rooms[1].directChatMatrixID, '@bob:example.com'); expect(matrix.rooms[1].directChatMatrixID, '@bob:example.com');
@ -153,7 +153,7 @@ void main() {
expect(matrix.presences['@alice:example.com']?.presence, expect(matrix.presences['@alice:example.com']?.presence,
PresenceType.online); PresenceType.online);
expect(presenceCounter, 1); expect(presenceCounter, 1);
expect(accountDataCounter, 9); expect(accountDataCounter, 10);
await Future.delayed(Duration(milliseconds: 50)); await Future.delayed(Duration(milliseconds: 50));
expect(matrix.userDeviceKeys.length, 4); expect(matrix.userDeviceKeys.length, 4);
expect(matrix.userDeviceKeys['@alice:example.com']?.outdated, false); expect(matrix.userDeviceKeys['@alice:example.com']?.outdated, false);
@ -207,6 +207,18 @@ void main() {
null); 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 { test('Logout', () async {
final loginStateFuture = matrix.onLoginStateChanged.stream.first; final loginStateFuture = matrix.onLoginStateChanged.stream.first;
await matrix.logout(); await matrix.logout();
@ -322,15 +334,6 @@ void main() {
await matrix.setAvatar(testFile); 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 { test('setMuteAllPushNotifications', () async {
await matrix.setMuteAllPushNotifications(false); await matrix.setMuteAllPushNotifications(false);
}); });

View File

@ -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': { 'PUT': {
'/client/r0/user/${Uri.encodeComponent('@alice:example.com')}/account_data/io.element.recent_emoji}': '/client/r0/user/${Uri.encodeComponent('@alice:example.com')}/account_data/io.element.recent_emoji}':
(var req) => {}, (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': '/client/r0/user/%40test%3AfakeServer.notExisting/account_data/m.ignored_user_list':
(var req) => {}, (var req) => {},
'/client/r0/presence/${Uri.encodeComponent('@alice:example.com')}/status': '/client/r0/presence/${Uri.encodeComponent('@alice:example.com')}/status':