LCOV - code coverage report
Current view: top level - lib/src/utils - room_member_change_type.dart (source / functions) Coverage Total Hit
Test: merged.info Lines: 63.3 % 30 19
Test Date: 2025-01-14 13:39:53 Functions: - 0 0

            Line data    Source code
       1              : import 'package:matrix/matrix.dart';
       2              : 
       3              : /// The kind of what has changed with this m.room.member event to have a
       4              : /// comparable type.
       5              : enum RoomMemberChangeType {
       6              :   /// The user has changed the avatar.
       7              :   avatar,
       8              : 
       9              :   /// The user has changed the displayname.
      10              :   displayname,
      11              : 
      12              :   /// The user has joined the chat from being not a user before. The user
      13              :   /// also was not invited before.
      14              :   join,
      15              : 
      16              :   /// The user was invited before and has joined.
      17              :   acceptInvite,
      18              : 
      19              :   /// The user was invited before and has left.
      20              :   rejectInvite,
      21              : 
      22              :   /// The user was invited before and the invitation got withdrawn by someone.
      23              :   withdrawInvitation,
      24              : 
      25              :   /// The user was joined before and has now left the room by themself.
      26              :   leave,
      27              : 
      28              :   /// The user was joined before and has now been kicked out of the room by
      29              :   /// someone.
      30              :   kick,
      31              : 
      32              :   /// The user has been invited by someone.
      33              :   invite,
      34              : 
      35              :   /// The user has been banned by someone.
      36              :   ban,
      37              : 
      38              :   /// The user was banned before and has been unbanned by someone.
      39              :   unban,
      40              : 
      41              :   /// The user was not a member of the room and now knocks.
      42              :   knock,
      43              : 
      44              :   /// Something else which is not handled yet.
      45              :   other,
      46              : }
      47              : 
      48              : extension RoomMemberChangeTypeExtension on Event {
      49              :   /// Returns the comparable type of this m.room.member event to handle this
      50              :   /// differently in the UI. If the event is not of the type m.room.member,
      51              :   /// this throws an exception!
      52            2 :   RoomMemberChangeType get roomMemberChangeType {
      53            4 :     if (type != EventTypes.RoomMember) {
      54            0 :       throw Exception(
      55            0 :         'Tried to call `roomMemberChangeType` but the Event has a type of `$type`',
      56              :       );
      57              :     }
      58              : 
      59              :     // Has the membership changed?
      60            4 :     final newMembership = content.tryGet<String>('membership') ?? '';
      61            4 :     final oldMembership = prevContent?.tryGet<String>('membership') ?? '';
      62              : 
      63            2 :     if (newMembership != oldMembership) {
      64            4 :       if (oldMembership == 'invite' && newMembership == 'join') {
      65              :         return RoomMemberChangeType.acceptInvite;
      66            4 :       } else if (oldMembership == 'invite' && newMembership == 'leave') {
      67            6 :         if (stateKey == senderId) {
      68              :           return RoomMemberChangeType.rejectInvite;
      69              :         } else {
      70              :           return RoomMemberChangeType.withdrawInvitation;
      71              :         }
      72            4 :       } else if ((oldMembership == 'leave' || oldMembership == '') &&
      73            2 :           newMembership == 'join') {
      74              :         return RoomMemberChangeType.join;
      75            4 :       } else if (oldMembership == 'join' && newMembership == 'ban') {
      76              :         return RoomMemberChangeType.ban;
      77            2 :       } else if (oldMembership == 'join' &&
      78            2 :           newMembership == 'leave' &&
      79            6 :           stateKey != senderId) {
      80              :         return RoomMemberChangeType.kick;
      81            2 :       } else if (oldMembership == 'join' &&
      82            2 :           newMembership == 'leave' &&
      83            0 :           stateKey == senderId) {
      84              :         return RoomMemberChangeType.leave;
      85            4 :       } else if (oldMembership != newMembership && newMembership == 'ban') {
      86              :         return RoomMemberChangeType.ban;
      87            2 :       } else if (oldMembership == 'ban' && newMembership == 'leave') {
      88              :         return RoomMemberChangeType.unban;
      89            2 :       } else if (newMembership == 'invite') {
      90              :         return RoomMemberChangeType.invite;
      91            0 :       } else if (newMembership == 'knock') {
      92              :         return RoomMemberChangeType.knock;
      93              :       }
      94            0 :     } else if (newMembership == 'join') {
      95            0 :       final newAvatar = content.tryGet<String>('avatar_url') ?? '';
      96            0 :       final oldAvatar = prevContent?.tryGet<String>('avatar_url') ?? '';
      97              : 
      98            0 :       final newDisplayname = content.tryGet<String>('displayname') ?? '';
      99            0 :       final oldDisplayname = prevContent?.tryGet<String>('displayname') ?? '';
     100              : 
     101              :       // Has the user avatar changed?
     102            0 :       if (newAvatar != oldAvatar) {
     103              :         return RoomMemberChangeType.avatar;
     104              :       }
     105              :       // Has the user displayname changed?
     106            0 :       else if (newDisplayname != oldDisplayname && stateKey != null) {
     107              :         return RoomMemberChangeType.displayname;
     108              :       }
     109              :     }
     110              :     return RoomMemberChangeType.other;
     111              :   }
     112              : }
        

Generated by: LCOV version 2.0-1