LCOV - code coverage report
Current view: top level - lib/src/utils - image_pack_extension.dart (source / functions) Hit Total Coverage
Test: merged.info Lines: 38 38 100.0 %
Date: 2024-09-30 15:57:20 Functions: 0 0 -

          Line data    Source code
       1             : /*
       2             :  *   Famedly Matrix SDK
       3             :  *   Copyright (C) 2020, 2021 Famedly GmbH
       4             :  *
       5             :  *   This program is free software: you can redistribute it and/or modify
       6             :  *   it under the terms of the GNU Affero General Public License as
       7             :  *   published by the Free Software Foundation, either version 3 of the
       8             :  *   License, or (at your option) any later version.
       9             :  *
      10             :  *   This program is distributed in the hope that it will be useful,
      11             :  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      13             :  *   GNU Affero General Public License for more details.
      14             :  *
      15             :  *   You should have received a copy of the GNU Affero General Public License
      16             :  *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
      17             :  */
      18             : 
      19             : import 'package:slugify/slugify.dart';
      20             : 
      21             : import 'package:matrix/matrix_api_lite.dart';
      22             : import 'package:matrix/src/room.dart';
      23             : 
      24             : extension ImagePackRoomExtension on Room {
      25             :   /// Get all the active image packs for the specified [usage], mapped by their slug
      26           2 :   Map<String, ImagePackContent> getImagePacks([ImagePackUsage? usage]) {
      27             :     final allMxcs = <Uri>{}; // used for easy deduplication
      28           2 :     final packs = <String, ImagePackContent>{};
      29             : 
      30           2 :     void addImagePack(BasicEvent? event, {Room? room, String? slug}) {
      31             :       if (event == null) return;
      32           2 :       final imagePack = event.parsedImagePackContent;
      33           2 :       final finalSlug = slugify(slug ?? 'pack');
      34           6 :       for (final entry in imagePack.images.entries) {
      35           2 :         final image = entry.value;
      36           4 :         if (allMxcs.contains(image.url)) {
      37             :           continue;
      38             :         }
      39           6 :         final imageUsage = image.usage ?? imagePack.pack.usage;
      40             :         if (usage != null &&
      41             :             imageUsage != null &&
      42           2 :             !imageUsage.contains(usage)) {
      43             :           continue;
      44             :         }
      45             :         packs
      46           2 :             .putIfAbsent(
      47             :                 finalSlug,
      48           6 :                 () => ImagePackContent.fromJson({})
      49           8 :                   ..pack.displayName = imagePack.pack.displayName ??
      50           2 :                       room?.getLocalizedDisplayname() ??
      51             :                       finalSlug
      52          10 :                   ..pack.avatarUrl = imagePack.pack.avatarUrl ?? room?.avatar
      53           8 :                   ..pack.attribution = imagePack.pack.attribution)
      54           6 :             .images[entry.key] = image;
      55           4 :         allMxcs.add(image.url);
      56             :       }
      57             :     }
      58             : 
      59             :     // first we add the user image pack
      60           8 :     addImagePack(client.accountData['im.ponies.user_emotes'], slug: 'user');
      61             :     // next we add all the external image packs
      62           6 :     final packRooms = client.accountData['im.ponies.emote_rooms'];
      63           4 :     final rooms = packRooms?.content.tryGetMap<String, Object?>('rooms');
      64             :     if (packRooms != null && rooms != null) {
      65           4 :       for (final roomEntry in rooms.entries) {
      66           2 :         final roomId = roomEntry.key;
      67           4 :         final room = client.getRoomById(roomId);
      68           2 :         final roomEntryValue = roomEntry.value;
      69           2 :         if (room != null && roomEntryValue is Map<String, Object?>) {
      70           4 :           for (final stateKeyEntry in roomEntryValue.entries) {
      71           2 :             final stateKey = stateKeyEntry.key;
      72             :             final fallbackSlug =
      73          10 :                 '${room.getLocalizedDisplayname()}-${stateKey.isNotEmpty ? '$stateKey-' : ''}${room.id}';
      74           4 :             addImagePack(room.getState('im.ponies.room_emotes', stateKey),
      75             :                 room: room, slug: fallbackSlug);
      76             :           }
      77             :         }
      78             :       }
      79             :     }
      80             :     // finally we add all of this rooms state
      81           4 :     final allRoomEmotes = states['im.ponies.room_emotes'];
      82             :     if (allRoomEmotes != null) {
      83           4 :       for (final entry in allRoomEmotes.entries) {
      84           4 :         addImagePack(entry.value,
      85             :             room: this,
      86           8 :             slug: (entry.value.stateKey?.isNotEmpty == true)
      87           4 :                 ? entry.value.stateKey
      88             :                 : 'room');
      89             :       }
      90             :     }
      91             :     return packs;
      92             :   }
      93             : 
      94             :   /// Get a flat view of all the image packs of a specified [usage], that is a map of all
      95             :   /// slugs to a map of the image code to their mxc url
      96           2 :   Map<String, Map<String, String>> getImagePacksFlat([ImagePackUsage? usage]) =>
      97           6 :       getImagePacks(usage).map((k, v) =>
      98          14 :           MapEntry(k, v.images.map((k, v) => MapEntry(k, v.url.toString()))));
      99             : }

Generated by: LCOV version 1.14