LCOV - code coverage report
Current view: top level - lib/src/utils - image_pack_extension.dart (source / functions) Hit Total Coverage
Test: merged.info Lines: 41 41 100.0 %
Date: 2024-11-12 07:37:08 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             :             )
      55           6 :             .images[entry.key] = image;
      56           4 :         allMxcs.add(image.url);
      57             :       }
      58             :     }
      59             : 
      60             :     // first we add the user image pack
      61           8 :     addImagePack(client.accountData['im.ponies.user_emotes'], slug: 'user');
      62             :     // next we add all the external image packs
      63           6 :     final packRooms = client.accountData['im.ponies.emote_rooms'];
      64           4 :     final rooms = packRooms?.content.tryGetMap<String, Object?>('rooms');
      65             :     if (packRooms != null && rooms != null) {
      66           4 :       for (final roomEntry in rooms.entries) {
      67           2 :         final roomId = roomEntry.key;
      68           4 :         final room = client.getRoomById(roomId);
      69           2 :         final roomEntryValue = roomEntry.value;
      70           2 :         if (room != null && roomEntryValue is Map<String, Object?>) {
      71           4 :           for (final stateKeyEntry in roomEntryValue.entries) {
      72           2 :             final stateKey = stateKeyEntry.key;
      73             :             final fallbackSlug =
      74          10 :                 '${room.getLocalizedDisplayname()}-${stateKey.isNotEmpty ? '$stateKey-' : ''}${room.id}';
      75           2 :             addImagePack(
      76           2 :               room.getState('im.ponies.room_emotes', stateKey),
      77             :               room: room,
      78             :               slug: fallbackSlug,
      79             :             );
      80             :           }
      81             :         }
      82             :       }
      83             :     }
      84             :     // finally we add all of this rooms state
      85           4 :     final allRoomEmotes = states['im.ponies.room_emotes'];
      86             :     if (allRoomEmotes != null) {
      87           4 :       for (final entry in allRoomEmotes.entries) {
      88           2 :         addImagePack(
      89           2 :           entry.value,
      90             :           room: this,
      91           8 :           slug: (entry.value.stateKey?.isNotEmpty == true)
      92           4 :               ? entry.value.stateKey
      93             :               : 'room',
      94             :         );
      95             :       }
      96             :     }
      97             :     return packs;
      98             :   }
      99             : 
     100             :   /// Get a flat view of all the image packs of a specified [usage], that is a map of all
     101             :   /// slugs to a map of the image code to their mxc url
     102           2 :   Map<String, Map<String, String>> getImagePacksFlat([ImagePackUsage? usage]) =>
     103           4 :       getImagePacks(usage).map(
     104           2 :         (k, v) =>
     105          14 :             MapEntry(k, v.images.map((k, v) => MapEntry(k, v.url.toString()))),
     106             :       );
     107             : }

Generated by: LCOV version 1.14