LCOV - code coverage report
Current view: top level - lib/msc_extensions/extension_recent_emoji - recent_emoji.dart (source / functions) Hit Total Coverage
Test: merged.info Lines: 19 20 95.0 %
Date: 2024-09-04 20:26:16 Functions: 0 0 -

          Line data    Source code
       1             : library extension_recent_emoji;
       2             : 
       3             : import 'package:matrix/matrix.dart';
       4             : 
       5             : /// Syncs recent emojis in account data
       6             : ///
       7             : /// Keeps recently used emojis stored in account data by
       8             : ///
       9             : /// ```js
      10             : /// { // the account data
      11             : ///   "io.element.recent_emoji": {
      12             : ///     "recent_emoji" : {
      13             : ///       "emoji character": n, // number used
      14             : ///     }
      15             : ///   }
      16             : /// }
      17             : /// ```
      18             : ///
      19             : /// Proprietary extension by New Vector Ltd.
      20             : extension RecentEmojiExtension on Client {
      21             :   /// returns the recently used emojis from the account data
      22             :   ///
      23             :   /// There's no corresponding standard or MSC, it's just the reverse-engineered
      24             :   /// API from New Vector Ltd.
      25           1 :   Map<String, int> get recentEmojis {
      26           1 :     final recents = <String, int>{};
      27             : 
      28           2 :     accountData['io.element.recent_emoji']
      29           1 :         ?.content
      30           1 :         .tryGetList('recent_emoji')
      31           2 :         ?.forEach((item) {
      32           1 :       if (item is List) {
      33           6 :         if (item.length > 1 && item[0] is String && item[1] is int) {
      34           3 :           recents[item[0]] = item[1];
      35             :         }
      36             :       }
      37             :     });
      38             : 
      39             :     return recents;
      40             :   }
      41             : 
      42             :   /// +1 the stated emoji in the account data
      43           1 :   Future<void> addRecentEmoji(String emoji) async {
      44           1 :     final data = recentEmojis;
      45           1 :     if (data.containsKey(emoji)) {
      46           0 :       data[emoji] = data[emoji]! + 1;
      47             :     } else {
      48           1 :       data[emoji] = 1;
      49             :     }
      50           1 :     return setRecentEmojiData(data);
      51             :   }
      52             : 
      53             :   /// sets the raw recent emoji account data. Use [addRecentEmoji] instead
      54           1 :   Future<void> setRecentEmojiData(Map<String, int> data) async {
      55           1 :     if (userID == null) return;
      56           7 :     final content = List.from(data.entries.map((e) => [e.key, e.value]));
      57           1 :     return setAccountData(
      58           2 :         userID!, 'io.element.recent_emoji', {'recent_emoji': content});
      59             :   }
      60             : }

Generated by: LCOV version 1.14