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: 20 21 95.2 %
Date: 2024-11-12 07:37:08 Functions: 0 0 -

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

Generated by: LCOV version 1.14