LCOV - code coverage report
Current view: top level - lib/src - room.dart (source / functions) Hit Total Coverage
Test: merged.info Lines: 725 972 74.6 %
Date: 2025-01-06 12:44:40 Functions: 0 0 -

          Line data    Source code
       1             : /*
       2             :  *   Famedly Matrix SDK
       3             :  *   Copyright (C) 2019, 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 'dart:async';
      20             : import 'dart:convert';
      21             : import 'dart:math';
      22             : 
      23             : import 'package:async/async.dart';
      24             : import 'package:collection/collection.dart';
      25             : import 'package:html_unescape/html_unescape.dart';
      26             : 
      27             : import 'package:matrix/matrix.dart';
      28             : import 'package:matrix/src/models/timeline_chunk.dart';
      29             : import 'package:matrix/src/utils/cached_stream_controller.dart';
      30             : import 'package:matrix/src/utils/file_send_request_credentials.dart';
      31             : import 'package:matrix/src/utils/markdown.dart';
      32             : import 'package:matrix/src/utils/marked_unread.dart';
      33             : import 'package:matrix/src/utils/space_child.dart';
      34             : 
      35             : /// max PDU size for server to accept the event with some buffer incase the server adds unsigned data f.ex age
      36             : /// https://spec.matrix.org/v1.9/client-server-api/#size-limits
      37             : const int maxPDUSize = 60000;
      38             : 
      39             : const String messageSendingStatusKey =
      40             :     'com.famedly.famedlysdk.message_sending_status';
      41             : 
      42             : const String fileSendingStatusKey =
      43             :     'com.famedly.famedlysdk.file_sending_status';
      44             : 
      45             : /// Represents a Matrix room.
      46             : class Room {
      47             :   /// The full qualified Matrix ID for the room in the format '!localid:server.abc'.
      48             :   final String id;
      49             : 
      50             :   /// Membership status of the user for this room.
      51             :   Membership membership;
      52             : 
      53             :   /// The count of unread notifications.
      54             :   int notificationCount;
      55             : 
      56             :   /// The count of highlighted notifications.
      57             :   int highlightCount;
      58             : 
      59             :   /// A token that can be supplied to the from parameter of the rooms/{roomId}/messages endpoint.
      60             :   String? prev_batch;
      61             : 
      62             :   RoomSummary summary;
      63             : 
      64             :   /// The room states are a key value store of the key (`type`,`state_key`) => State(event).
      65             :   /// In a lot of cases the `state_key` might be an empty string. You **should** use the
      66             :   /// methods `getState()` and `setState()` to interact with the room states.
      67             :   Map<String, Map<String, StrippedStateEvent>> states = {};
      68             : 
      69             :   /// Key-Value store for ephemerals.
      70             :   Map<String, BasicRoomEvent> ephemerals = {};
      71             : 
      72             :   /// Key-Value store for private account data only visible for this user.
      73             :   Map<String, BasicRoomEvent> roomAccountData = {};
      74             : 
      75             :   final _sendingQueue = <Completer>[];
      76             : 
      77             :   Timer? _clearTypingIndicatorTimer;
      78             : 
      79          68 :   Map<String, dynamic> toJson() => {
      80          34 :         'id': id,
      81         136 :         'membership': membership.toString().split('.').last,
      82          34 :         'highlight_count': highlightCount,
      83          34 :         'notification_count': notificationCount,
      84          34 :         'prev_batch': prev_batch,
      85          68 :         'summary': summary.toJson(),
      86          67 :         'last_event': lastEvent?.toJson(),
      87             :       };
      88             : 
      89          14 :   factory Room.fromJson(Map<String, dynamic> json, Client client) {
      90          14 :     final room = Room(
      91             :       client: client,
      92          14 :       id: json['id'],
      93          14 :       membership: Membership.values.singleWhere(
      94          70 :         (m) => m.toString() == 'Membership.${json['membership']}',
      95           0 :         orElse: () => Membership.join,
      96             :       ),
      97          14 :       notificationCount: json['notification_count'],
      98          14 :       highlightCount: json['highlight_count'],
      99          14 :       prev_batch: json['prev_batch'],
     100          42 :       summary: RoomSummary.fromJson(Map<String, dynamic>.from(json['summary'])),
     101             :     );
     102          14 :     if (json['last_event'] != null) {
     103          39 :       room.lastEvent = Event.fromJson(json['last_event'], room);
     104             :     }
     105             :     return room;
     106             :   }
     107             : 
     108             :   /// Flag if the room is partial, meaning not all state events have been loaded yet
     109             :   bool partial = true;
     110             : 
     111             :   /// Post-loads the room.
     112             :   /// This load all the missing state events for the room from the database
     113             :   /// If the room has already been loaded, this does nothing.
     114           5 :   Future<void> postLoad() async {
     115           5 :     if (!partial) {
     116             :       return;
     117             :     }
     118             :     final allStates =
     119          15 :         await client.database?.getUnimportantRoomEventStatesForRoom(
     120          15 :       client.importantStateEvents.toList(),
     121             :       this,
     122             :     );
     123             : 
     124             :     if (allStates != null) {
     125           8 :       for (final state in allStates) {
     126           3 :         setState(state);
     127             :       }
     128             :     }
     129           5 :     partial = false;
     130             :   }
     131             : 
     132             :   /// Returns the [Event] for the given [typeKey] and optional [stateKey].
     133             :   /// If no [stateKey] is provided, it defaults to an empty string.
     134             :   /// This returns either a `StrippedStateEvent` for rooms with membership
     135             :   /// "invite" or a `User`/`Event`. If you need additional information like
     136             :   /// the Event ID or originServerTs you need to do a type check like:
     137             :   /// ```dart
     138             :   /// if (state is Event) { /*...*/ }
     139             :   /// ```
     140          36 :   StrippedStateEvent? getState(String typeKey, [String stateKey = '']) =>
     141         108 :       states[typeKey]?[stateKey];
     142             : 
     143             :   /// Adds the [state] to this room and overwrites a state with the same
     144             :   /// typeKey/stateKey key pair if there is one.
     145          36 :   void setState(StrippedStateEvent state) {
     146             :     // Ignore other non-state events
     147          36 :     final stateKey = state.stateKey;
     148             : 
     149             :     // For non invite rooms this is usually an Event and we should validate
     150             :     // the room ID:
     151          36 :     if (state is Event) {
     152          36 :       final roomId = state.roomId;
     153          72 :       if (roomId != id) {
     154           0 :         Logs().wtf('Tried to set state event for wrong room!');
     155           0 :         assert(roomId == id);
     156             :         return;
     157             :       }
     158             :     }
     159             : 
     160             :     if (stateKey == null) {
     161           6 :       Logs().w(
     162           6 :         'Tried to set a non state event with type "${state.type}" as state event for a room',
     163             :       );
     164           3 :       assert(stateKey != null);
     165             :       return;
     166             :     }
     167             : 
     168         180 :     (states[state.type] ??= {})[stateKey] = state;
     169             : 
     170         144 :     client.onRoomState.add((roomId: id, state: state));
     171             :   }
     172             : 
     173             :   /// ID of the fully read marker event.
     174           3 :   String get fullyRead =>
     175          10 :       roomAccountData['m.fully_read']?.content.tryGet<String>('event_id') ?? '';
     176             : 
     177             :   /// If something changes, this callback will be triggered. Will return the
     178             :   /// room id.
     179             :   @Deprecated('Use `client.onSync` instead and filter for this room ID')
     180             :   final CachedStreamController<String> onUpdate = CachedStreamController();
     181             : 
     182             :   /// If there is a new session key received, this will be triggered with
     183             :   /// the session ID.
     184             :   final CachedStreamController<String> onSessionKeyReceived =
     185             :       CachedStreamController();
     186             : 
     187             :   /// The name of the room if set by a participant.
     188           8 :   String get name {
     189          20 :     final n = getState(EventTypes.RoomName)?.content['name'];
     190           8 :     return (n is String) ? n : '';
     191             :   }
     192             : 
     193             :   /// The pinned events for this room. If there are none this returns an empty
     194             :   /// list.
     195           2 :   List<String> get pinnedEventIds {
     196           6 :     final pinned = getState(EventTypes.RoomPinnedEvents)?.content['pinned'];
     197          12 :     return pinned is Iterable ? pinned.map((e) => e.toString()).toList() : [];
     198             :   }
     199             : 
     200             :   /// Returns the heroes as `User` objects.
     201             :   /// This is very useful if you want to make sure that all users are loaded
     202             :   /// from the database, that you need to correctly calculate the displayname
     203             :   /// and the avatar of the room.
     204           2 :   Future<List<User>> loadHeroUsers() async {
     205             :     // For invite rooms request own user and invitor.
     206           4 :     if (membership == Membership.invite) {
     207           0 :       final ownUser = await requestUser(client.userID!, requestProfile: false);
     208           0 :       if (ownUser != null) await requestUser(ownUser.senderId);
     209             :     }
     210             : 
     211           4 :     var heroes = summary.mHeroes;
     212             :     if (heroes == null) {
     213           0 :       final directChatMatrixID = this.directChatMatrixID;
     214             :       if (directChatMatrixID != null) {
     215           0 :         heroes = [directChatMatrixID];
     216             :       }
     217             :     }
     218             : 
     219           0 :     if (heroes == null) return [];
     220             : 
     221           2 :     return await Future.wait(
     222           2 :       heroes.map(
     223           2 :         (hero) async =>
     224           2 :             (await requestUser(
     225             :               hero,
     226             :               ignoreErrors: true,
     227             :             )) ??
     228           0 :             User(hero, room: this),
     229             :       ),
     230             :     );
     231             :   }
     232             : 
     233             :   /// Returns a localized displayname for this server. If the room is a groupchat
     234             :   /// without a name, then it will return the localized version of 'Group with Alice' instead
     235             :   /// of just 'Alice' to make it different to a direct chat.
     236             :   /// Empty chats will become the localized version of 'Empty Chat'.
     237             :   /// Please note, that necessary room members are lazy loaded. To be sure
     238             :   /// that you have the room members, call and await `Room.loadHeroUsers()`
     239             :   /// before.
     240             :   /// This method requires a localization class which implements [MatrixLocalizations]
     241           4 :   String getLocalizedDisplayname([
     242             :     MatrixLocalizations i18n = const MatrixDefaultLocalizations(),
     243             :   ]) {
     244          10 :     if (name.isNotEmpty) return name;
     245             : 
     246           8 :     final canonicalAlias = this.canonicalAlias.localpart;
     247           2 :     if (canonicalAlias != null && canonicalAlias.isNotEmpty) {
     248             :       return canonicalAlias;
     249             :     }
     250             : 
     251           4 :     final directChatMatrixID = this.directChatMatrixID;
     252           8 :     final heroes = summary.mHeroes ??
     253           0 :         (directChatMatrixID == null ? [] : [directChatMatrixID]);
     254           4 :     if (heroes.isNotEmpty) {
     255             :       final result = heroes
     256           2 :           .where(
     257             :             // removing oneself from the hero list
     258          10 :             (hero) => hero.isNotEmpty && hero != client.userID,
     259             :           )
     260           2 :           .map(
     261           4 :             (hero) => unsafeGetUserFromMemoryOrFallback(hero)
     262           2 :                 .calcDisplayname(i18n: i18n),
     263             :           )
     264           2 :           .join(', ');
     265           2 :       if (isAbandonedDMRoom) {
     266           0 :         return i18n.wasDirectChatDisplayName(result);
     267             :       }
     268             : 
     269           4 :       return isDirectChat ? result : i18n.groupWith(result);
     270             :     }
     271           4 :     if (membership == Membership.invite) {
     272           0 :       final ownMember = unsafeGetUserFromMemoryOrFallback(client.userID!);
     273             : 
     274           0 :       if (ownMember.senderId != ownMember.stateKey) {
     275           0 :         return i18n.invitedBy(
     276           0 :           unsafeGetUserFromMemoryOrFallback(ownMember.senderId)
     277           0 :               .calcDisplayname(i18n: i18n),
     278             :         );
     279             :       }
     280             :     }
     281           4 :     if (membership == Membership.leave) {
     282             :       if (directChatMatrixID != null) {
     283           0 :         return i18n.wasDirectChatDisplayName(
     284           0 :           unsafeGetUserFromMemoryOrFallback(directChatMatrixID)
     285           0 :               .calcDisplayname(i18n: i18n),
     286             :         );
     287             :       }
     288             :     }
     289           2 :     return i18n.emptyChat;
     290             :   }
     291             : 
     292             :   /// The topic of the room if set by a participant.
     293           2 :   String get topic {
     294           6 :     final t = getState(EventTypes.RoomTopic)?.content['topic'];
     295           2 :     return t is String ? t : '';
     296             :   }
     297             : 
     298             :   /// The avatar of the room if set by a participant.
     299             :   /// Please note, that necessary room members are lazy loaded. To be sure
     300             :   /// that you have the room members, call and await `Room.loadHeroUsers()`
     301             :   /// before.
     302           4 :   Uri? get avatar {
     303             :     // Check content of `m.room.avatar`
     304             :     final avatarUrl =
     305           8 :         getState(EventTypes.RoomAvatar)?.content.tryGet<String>('url');
     306             :     if (avatarUrl != null) {
     307           2 :       return Uri.tryParse(avatarUrl);
     308             :     }
     309             : 
     310             :     // Room has no avatar and is not a direct chat
     311           4 :     final directChatMatrixID = this.directChatMatrixID;
     312             :     if (directChatMatrixID != null) {
     313           0 :       return unsafeGetUserFromMemoryOrFallback(directChatMatrixID).avatarUrl;
     314             :     }
     315             : 
     316             :     return null;
     317             :   }
     318             : 
     319             :   /// The address in the format: #roomname:homeserver.org.
     320           5 :   String get canonicalAlias {
     321          11 :     final alias = getState(EventTypes.RoomCanonicalAlias)?.content['alias'];
     322           5 :     return (alias is String) ? alias : '';
     323             :   }
     324             : 
     325             :   /// Sets the canonical alias. If the [canonicalAlias] is not yet an alias of
     326             :   /// this room, it will create one.
     327           0 :   Future<void> setCanonicalAlias(String canonicalAlias) async {
     328           0 :     final aliases = await client.getLocalAliases(id);
     329           0 :     if (!aliases.contains(canonicalAlias)) {
     330           0 :       await client.setRoomAlias(canonicalAlias, id);
     331             :     }
     332           0 :     await client.setRoomStateWithKey(id, EventTypes.RoomCanonicalAlias, '', {
     333             :       'alias': canonicalAlias,
     334             :     });
     335             :   }
     336             : 
     337             :   String? _cachedDirectChatMatrixId;
     338             : 
     339             :   /// If this room is a direct chat, this is the matrix ID of the user.
     340             :   /// Returns null otherwise.
     341          36 :   String? get directChatMatrixID {
     342             :     // Calculating the directChatMatrixId can be expensive. We cache it and
     343             :     // validate the cache instead every time.
     344          36 :     final cache = _cachedDirectChatMatrixId;
     345             :     if (cache != null) {
     346          12 :       final roomIds = client.directChats[cache];
     347          12 :       if (roomIds is List && roomIds.contains(id)) {
     348             :         return cache;
     349             :       }
     350             :     }
     351             : 
     352          72 :     if (membership == Membership.invite) {
     353           0 :       final userID = client.userID;
     354             :       if (userID == null) return null;
     355           0 :       final invitation = getState(EventTypes.RoomMember, userID);
     356           0 :       if (invitation != null && invitation.content['is_direct'] == true) {
     357           0 :         return _cachedDirectChatMatrixId = invitation.senderId;
     358             :       }
     359             :     }
     360             : 
     361         108 :     final mxId = client.directChats.entries
     362          52 :         .firstWhereOrNull((MapEntry<String, dynamic> e) {
     363          16 :       final roomIds = e.value;
     364          48 :       return roomIds is List<dynamic> && roomIds.contains(id);
     365           8 :     })?.key;
     366          50 :     if (mxId?.isValidMatrixId == true) return _cachedDirectChatMatrixId = mxId;
     367          36 :     return _cachedDirectChatMatrixId = null;
     368             :   }
     369             : 
     370             :   /// Wheither this is a direct chat or not
     371          72 :   bool get isDirectChat => directChatMatrixID != null;
     372             : 
     373             :   Event? lastEvent;
     374             : 
     375          35 :   void setEphemeral(BasicRoomEvent ephemeral) {
     376         105 :     ephemerals[ephemeral.type] = ephemeral;
     377          70 :     if (ephemeral.type == 'm.typing') {
     378          35 :       _clearTypingIndicatorTimer?.cancel();
     379         142 :       _clearTypingIndicatorTimer = Timer(client.typingIndicatorTimeout, () {
     380           4 :         ephemerals.remove('m.typing');
     381             :       });
     382             :     }
     383             :   }
     384             : 
     385             :   /// Returns a list of all current typing users.
     386           1 :   List<User> get typingUsers {
     387           4 :     final typingMxid = ephemerals['m.typing']?.content['user_ids'];
     388           1 :     return (typingMxid is List)
     389             :         ? typingMxid
     390           1 :             .cast<String>()
     391           2 :             .map(unsafeGetUserFromMemoryOrFallback)
     392           1 :             .toList()
     393           0 :         : [];
     394             :   }
     395             : 
     396             :   /// Your current client instance.
     397             :   final Client client;
     398             : 
     399          38 :   Room({
     400             :     required this.id,
     401             :     this.membership = Membership.join,
     402             :     this.notificationCount = 0,
     403             :     this.highlightCount = 0,
     404             :     this.prev_batch,
     405             :     required this.client,
     406             :     Map<String, BasicRoomEvent>? roomAccountData,
     407             :     RoomSummary? summary,
     408             :     this.lastEvent,
     409          38 :   })  : roomAccountData = roomAccountData ?? <String, BasicRoomEvent>{},
     410             :         summary = summary ??
     411          76 :             RoomSummary.fromJson({
     412             :               'm.joined_member_count': 0,
     413             :               'm.invited_member_count': 0,
     414          38 :               'm.heroes': [],
     415             :             });
     416             : 
     417             :   /// The default count of how much events should be requested when requesting the
     418             :   /// history of this room.
     419             :   static const int defaultHistoryCount = 30;
     420             : 
     421             :   /// Checks if this is an abandoned DM room where the other participant has
     422             :   /// left the room. This is false when there are still other users in the room
     423             :   /// or the room is not marked as a DM room.
     424           2 :   bool get isAbandonedDMRoom {
     425           2 :     final directChatMatrixID = this.directChatMatrixID;
     426             : 
     427             :     if (directChatMatrixID == null) return false;
     428             :     final dmPartnerMembership =
     429           0 :         unsafeGetUserFromMemoryOrFallback(directChatMatrixID).membership;
     430           0 :     return dmPartnerMembership == Membership.leave &&
     431           0 :         summary.mJoinedMemberCount == 1 &&
     432           0 :         summary.mInvitedMemberCount == 0;
     433             :   }
     434             : 
     435             :   /// Calculates the displayname. First checks if there is a name, then checks for a canonical alias and
     436             :   /// then generates a name from the heroes.
     437           0 :   @Deprecated('Use `getLocalizedDisplayname()` instead')
     438           0 :   String get displayname => getLocalizedDisplayname();
     439             : 
     440             :   /// When was the last event received.
     441          35 :   DateTime get latestEventReceivedTime =>
     442         105 :       lastEvent?.originServerTs ?? DateTime.now();
     443             : 
     444             :   /// Call the Matrix API to change the name of this room. Returns the event ID of the
     445             :   /// new m.room.name event.
     446           6 :   Future<String> setName(String newName) => client.setRoomStateWithKey(
     447           2 :         id,
     448             :         EventTypes.RoomName,
     449             :         '',
     450           2 :         {'name': newName},
     451             :       );
     452             : 
     453             :   /// Call the Matrix API to change the topic of this room.
     454           6 :   Future<String> setDescription(String newName) => client.setRoomStateWithKey(
     455           2 :         id,
     456             :         EventTypes.RoomTopic,
     457             :         '',
     458           2 :         {'topic': newName},
     459             :       );
     460             : 
     461             :   /// Add a tag to the room.
     462           6 :   Future<void> addTag(String tag, {double? order}) => client.setRoomTag(
     463           4 :         client.userID!,
     464           2 :         id,
     465             :         tag,
     466           2 :         Tag(
     467             :           order: order,
     468             :         ),
     469             :       );
     470             : 
     471             :   /// Removes a tag from the room.
     472           6 :   Future<void> removeTag(String tag) => client.deleteRoomTag(
     473           4 :         client.userID!,
     474           2 :         id,
     475             :         tag,
     476             :       );
     477             : 
     478             :   // Tag is part of client-to-server-API, so it uses strict parsing.
     479             :   // For roomAccountData, permissive parsing is more suitable,
     480             :   // so it is implemented here.
     481          35 :   static Tag _tryTagFromJson(Object o) {
     482          35 :     if (o is Map<String, dynamic>) {
     483          35 :       return Tag(
     484          70 :         order: o.tryGet<num>('order', TryGet.silent)?.toDouble(),
     485          70 :         additionalProperties: Map.from(o)..remove('order'),
     486             :       );
     487             :     }
     488           0 :     return Tag();
     489             :   }
     490             : 
     491             :   /// Returns all tags for this room.
     492          35 :   Map<String, Tag> get tags {
     493         140 :     final tags = roomAccountData['m.tag']?.content['tags'];
     494             : 
     495          35 :     if (tags is Map) {
     496             :       final parsedTags =
     497         140 :           tags.map((k, v) => MapEntry<String, Tag>(k, _tryTagFromJson(v)));
     498         105 :       parsedTags.removeWhere((k, v) => !TagType.isValid(k));
     499             :       return parsedTags;
     500             :     }
     501             : 
     502          35 :     return {};
     503             :   }
     504             : 
     505           2 :   bool get markedUnread {
     506           2 :     return MarkedUnread.fromJson(
     507           6 :       roomAccountData[EventType.markedUnread]?.content ??
     508           4 :           roomAccountData[EventType.oldMarkedUnread]?.content ??
     509           2 :           {},
     510           2 :     ).unread;
     511             :   }
     512             : 
     513             :   /// Checks if the last event has a read marker of the user.
     514             :   /// Warning: This compares the origin server timestamp which might not map
     515             :   /// to the real sort order of the timeline.
     516           2 :   bool get hasNewMessages {
     517           2 :     final lastEvent = this.lastEvent;
     518             : 
     519             :     // There is no known event or the last event is only a state fallback event,
     520             :     // we assume there is no new messages.
     521             :     if (lastEvent == null ||
     522           8 :         !client.roomPreviewLastEvents.contains(lastEvent.type)) {
     523             :       return false;
     524             :     }
     525             : 
     526             :     // Read marker is on the last event so no new messages.
     527           2 :     if (lastEvent.receipts
     528           2 :         .any((receipt) => receipt.user.senderId == client.userID!)) {
     529             :       return false;
     530             :     }
     531             : 
     532             :     // If the last event is sent, we mark the room as read.
     533           8 :     if (lastEvent.senderId == client.userID) return false;
     534             : 
     535             :     // Get the timestamp of read marker and compare
     536           6 :     final readAtMilliseconds = receiptState.global.latestOwnReceipt?.ts ?? 0;
     537           6 :     return readAtMilliseconds < lastEvent.originServerTs.millisecondsSinceEpoch;
     538             :   }
     539             : 
     540          70 :   LatestReceiptState get receiptState => LatestReceiptState.fromJson(
     541          72 :         roomAccountData[LatestReceiptState.eventType]?.content ??
     542          35 :             <String, dynamic>{},
     543             :       );
     544             : 
     545             :   /// Returns true if this room is unread. To check if there are new messages
     546             :   /// in muted rooms, use [hasNewMessages].
     547           8 :   bool get isUnread => notificationCount > 0 || markedUnread;
     548             : 
     549             :   /// Returns true if this room is to be marked as unread. This extends
     550             :   /// [isUnread] to rooms with [Membership.invite].
     551           8 :   bool get isUnreadOrInvited => isUnread || membership == Membership.invite;
     552             : 
     553           0 :   @Deprecated('Use waitForRoomInSync() instead')
     554           0 :   Future<SyncUpdate> get waitForSync => waitForRoomInSync();
     555             : 
     556             :   /// Wait for the room to appear in join, leave or invited section of the
     557             :   /// sync.
     558           0 :   Future<SyncUpdate> waitForRoomInSync() async {
     559           0 :     return await client.waitForRoomInSync(id);
     560             :   }
     561             : 
     562             :   /// Sets an unread flag manually for this room. This changes the local account
     563             :   /// data model before syncing it to make sure
     564             :   /// this works if there is no connection to the homeserver. This does **not**
     565             :   /// set a read marker!
     566           2 :   Future<void> markUnread(bool unread) async {
     567           4 :     final content = MarkedUnread(unread).toJson();
     568           2 :     await _handleFakeSync(
     569           2 :       SyncUpdate(
     570             :         nextBatch: '',
     571           2 :         rooms: RoomsUpdate(
     572           2 :           join: {
     573           4 :             id: JoinedRoomUpdate(
     574           2 :               accountData: [
     575           2 :                 BasicRoomEvent(
     576             :                   content: content,
     577           2 :                   roomId: id,
     578             :                   type: EventType.markedUnread,
     579             :                 ),
     580             :               ],
     581             :             ),
     582             :           },
     583             :         ),
     584             :       ),
     585             :     );
     586           4 :     await client.setAccountDataPerRoom(
     587           4 :       client.userID!,
     588           2 :       id,
     589             :       EventType.markedUnread,
     590             :       content,
     591             :     );
     592             :   }
     593             : 
     594             :   /// Returns true if this room has a m.favourite tag.
     595         105 :   bool get isFavourite => tags[TagType.favourite] != null;
     596             : 
     597             :   /// Sets the m.favourite tag for this room.
     598           2 :   Future<void> setFavourite(bool favourite) =>
     599           2 :       favourite ? addTag(TagType.favourite) : removeTag(TagType.favourite);
     600             : 
     601             :   /// Call the Matrix API to change the pinned events of this room.
     602           0 :   Future<String> setPinnedEvents(List<String> pinnedEventIds) =>
     603           0 :       client.setRoomStateWithKey(
     604           0 :         id,
     605             :         EventTypes.RoomPinnedEvents,
     606             :         '',
     607           0 :         {'pinned': pinnedEventIds},
     608             :       );
     609             : 
     610             :   /// returns the resolved mxid for a mention string, or null if none found
     611           4 :   String? getMention(String mention) => getParticipants()
     612           8 :       .firstWhereOrNull((u) => u.mentionFragments.contains(mention))
     613           2 :       ?.id;
     614             : 
     615             :   /// Sends a normal text message to this room. Returns the event ID generated
     616             :   /// by the server for this message.
     617           5 :   Future<String?> sendTextEvent(
     618             :     String message, {
     619             :     String? txid,
     620             :     Event? inReplyTo,
     621             :     String? editEventId,
     622             :     bool parseMarkdown = true,
     623             :     bool parseCommands = true,
     624             :     String msgtype = MessageTypes.Text,
     625             :     String? threadRootEventId,
     626             :     String? threadLastEventId,
     627             :   }) {
     628             :     if (parseCommands) {
     629          10 :       return client.parseAndRunCommand(
     630             :         this,
     631             :         message,
     632             :         inReplyTo: inReplyTo,
     633             :         editEventId: editEventId,
     634             :         txid: txid,
     635             :         threadRootEventId: threadRootEventId,
     636             :         threadLastEventId: threadLastEventId,
     637             :       );
     638             :     }
     639           5 :     final event = <String, dynamic>{
     640             :       'msgtype': msgtype,
     641             :       'body': message,
     642             :     };
     643             :     if (parseMarkdown) {
     644           5 :       final html = markdown(
     645           5 :         event['body'],
     646           0 :         getEmotePacks: () => getImagePacksFlat(ImagePackUsage.emoticon),
     647           5 :         getMention: getMention,
     648             :       );
     649             :       // if the decoded html is the same as the body, there is no need in sending a formatted message
     650          25 :       if (HtmlUnescape().convert(html.replaceAll(RegExp(r'<br />\n?'), '\n')) !=
     651           5 :           event['body']) {
     652           3 :         event['format'] = 'org.matrix.custom.html';
     653           3 :         event['formatted_body'] = html;
     654             :       }
     655             :     }
     656           5 :     return sendEvent(
     657             :       event,
     658             :       txid: txid,
     659             :       inReplyTo: inReplyTo,
     660             :       editEventId: editEventId,
     661             :       threadRootEventId: threadRootEventId,
     662             :       threadLastEventId: threadLastEventId,
     663             :     );
     664             :   }
     665             : 
     666             :   /// Sends a reaction to an event with an [eventId] and the content [key] into a room.
     667             :   /// Returns the event ID generated by the server for this reaction.
     668           3 :   Future<String?> sendReaction(String eventId, String key, {String? txid}) {
     669           3 :     return sendEvent(
     670           3 :       {
     671           3 :         'm.relates_to': {
     672             :           'rel_type': RelationshipTypes.reaction,
     673             :           'event_id': eventId,
     674             :           'key': key,
     675             :         },
     676             :       },
     677             :       type: EventTypes.Reaction,
     678             :       txid: txid,
     679             :     );
     680             :   }
     681             : 
     682             :   /// Sends the location with description [body] and geo URI [geoUri] into a room.
     683             :   /// Returns the event ID generated by the server for this message.
     684           2 :   Future<String?> sendLocation(String body, String geoUri, {String? txid}) {
     685           2 :     final event = <String, dynamic>{
     686             :       'msgtype': 'm.location',
     687             :       'body': body,
     688             :       'geo_uri': geoUri,
     689             :     };
     690           2 :     return sendEvent(event, txid: txid);
     691             :   }
     692             : 
     693             :   final Map<String, MatrixFile> sendingFilePlaceholders = {};
     694             :   final Map<String, MatrixImageFile> sendingFileThumbnails = {};
     695             : 
     696             :   /// Sends a [file] to this room after uploading it. Returns the mxc uri of
     697             :   /// the uploaded file. If [waitUntilSent] is true, the future will wait until
     698             :   /// the message event has received the server. Otherwise the future will only
     699             :   /// wait until the file has been uploaded.
     700             :   /// Optionally specify [extraContent] to tack on to the event.
     701             :   ///
     702             :   /// In case [file] is a [MatrixImageFile], [thumbnail] is automatically
     703             :   /// computed unless it is explicitly provided.
     704             :   /// Set [shrinkImageMaxDimension] to for example `1600` if you want to shrink
     705             :   /// your image before sending. This is ignored if the File is not a
     706             :   /// [MatrixImageFile].
     707           3 :   Future<String?> sendFileEvent(
     708             :     MatrixFile file, {
     709             :     String? txid,
     710             :     Event? inReplyTo,
     711             :     String? editEventId,
     712             :     int? shrinkImageMaxDimension,
     713             :     MatrixImageFile? thumbnail,
     714             :     Map<String, dynamic>? extraContent,
     715             :     String? threadRootEventId,
     716             :     String? threadLastEventId,
     717             :   }) async {
     718           2 :     txid ??= client.generateUniqueTransactionId();
     719           6 :     sendingFilePlaceholders[txid] = file;
     720             :     if (thumbnail != null) {
     721           0 :       sendingFileThumbnails[txid] = thumbnail;
     722             :     }
     723             : 
     724             :     // Create a fake Event object as a placeholder for the uploading file:
     725           3 :     final syncUpdate = SyncUpdate(
     726             :       nextBatch: '',
     727           3 :       rooms: RoomsUpdate(
     728           3 :         join: {
     729           6 :           id: JoinedRoomUpdate(
     730           3 :             timeline: TimelineUpdate(
     731           3 :               events: [
     732           3 :                 MatrixEvent(
     733           3 :                   content: {
     734           3 :                     'msgtype': file.msgType,
     735           3 :                     'body': file.name,
     736           3 :                     'filename': file.name,
     737             :                   },
     738             :                   type: EventTypes.Message,
     739             :                   eventId: txid,
     740           6 :                   senderId: client.userID!,
     741           3 :                   originServerTs: DateTime.now(),
     742           3 :                   unsigned: {
     743           6 :                     messageSendingStatusKey: EventStatus.sending.intValue,
     744           3 :                     'transaction_id': txid,
     745           3 :                     ...FileSendRequestCredentials(
     746           0 :                       inReplyTo: inReplyTo?.eventId,
     747             :                       editEventId: editEventId,
     748             :                       shrinkImageMaxDimension: shrinkImageMaxDimension,
     749             :                       extraContent: extraContent,
     750           3 :                     ).toJson(),
     751             :                   },
     752             :                 ),
     753             :               ],
     754             :             ),
     755             :           ),
     756             :         },
     757             :       ),
     758             :     );
     759             : 
     760             :     MatrixFile uploadFile = file; // ignore: omit_local_variable_types
     761             :     // computing the thumbnail in case we can
     762           3 :     if (file is MatrixImageFile &&
     763             :         (thumbnail == null || shrinkImageMaxDimension != null)) {
     764           0 :       syncUpdate.rooms!.join!.values.first.timeline!.events!.first
     765           0 :               .unsigned![fileSendingStatusKey] =
     766           0 :           FileSendingStatus.generatingThumbnail.name;
     767           0 :       await _handleFakeSync(syncUpdate);
     768           0 :       thumbnail ??= await file.generateThumbnail(
     769           0 :         nativeImplementations: client.nativeImplementations,
     770           0 :         customImageResizer: client.customImageResizer,
     771             :       );
     772             :       if (shrinkImageMaxDimension != null) {
     773           0 :         file = await MatrixImageFile.shrink(
     774           0 :           bytes: file.bytes,
     775           0 :           name: file.name,
     776             :           maxDimension: shrinkImageMaxDimension,
     777           0 :           customImageResizer: client.customImageResizer,
     778           0 :           nativeImplementations: client.nativeImplementations,
     779             :         );
     780             :       }
     781             : 
     782           0 :       if (thumbnail != null && file.size < thumbnail.size) {
     783             :         thumbnail = null; // in this case, the thumbnail is not usefull
     784             :       }
     785             :     }
     786             : 
     787             :     // Check media config of the server before sending the file. Stop if the
     788             :     // Media config is unreachable or the file is bigger than the given maxsize.
     789             :     try {
     790           6 :       final mediaConfig = await client.getConfig();
     791           3 :       final maxMediaSize = mediaConfig.mUploadSize;
     792           9 :       if (maxMediaSize != null && maxMediaSize < file.bytes.lengthInBytes) {
     793           0 :         throw FileTooBigMatrixException(file.bytes.lengthInBytes, maxMediaSize);
     794             :       }
     795             :     } catch (e) {
     796           0 :       Logs().d('Config error while sending file', e);
     797           0 :       syncUpdate.rooms!.join!.values.first.timeline!.events!.first
     798           0 :           .unsigned![messageSendingStatusKey] = EventStatus.error.intValue;
     799           0 :       await _handleFakeSync(syncUpdate);
     800             :       rethrow;
     801             :     }
     802             : 
     803             :     MatrixFile? uploadThumbnail =
     804             :         thumbnail; // ignore: omit_local_variable_types
     805             :     EncryptedFile? encryptedFile;
     806             :     EncryptedFile? encryptedThumbnail;
     807           3 :     if (encrypted && client.fileEncryptionEnabled) {
     808           0 :       syncUpdate.rooms!.join!.values.first.timeline!.events!.first
     809           0 :           .unsigned![fileSendingStatusKey] = FileSendingStatus.encrypting.name;
     810           0 :       await _handleFakeSync(syncUpdate);
     811           0 :       encryptedFile = await file.encrypt();
     812           0 :       uploadFile = encryptedFile.toMatrixFile();
     813             : 
     814             :       if (thumbnail != null) {
     815           0 :         encryptedThumbnail = await thumbnail.encrypt();
     816           0 :         uploadThumbnail = encryptedThumbnail.toMatrixFile();
     817             :       }
     818             :     }
     819             :     Uri? uploadResp, thumbnailUploadResp;
     820             : 
     821          12 :     final timeoutDate = DateTime.now().add(client.sendTimelineEventTimeout);
     822             : 
     823          21 :     syncUpdate.rooms!.join!.values.first.timeline!.events!.first
     824           9 :         .unsigned![fileSendingStatusKey] = FileSendingStatus.uploading.name;
     825             :     while (uploadResp == null ||
     826             :         (uploadThumbnail != null && thumbnailUploadResp == null)) {
     827             :       try {
     828           6 :         uploadResp = await client.uploadContent(
     829           3 :           uploadFile.bytes,
     830           3 :           filename: uploadFile.name,
     831           3 :           contentType: uploadFile.mimeType,
     832             :         );
     833             :         thumbnailUploadResp = uploadThumbnail != null
     834           0 :             ? await client.uploadContent(
     835           0 :                 uploadThumbnail.bytes,
     836           0 :                 filename: uploadThumbnail.name,
     837           0 :                 contentType: uploadThumbnail.mimeType,
     838             :               )
     839             :             : null;
     840           0 :       } on MatrixException catch (_) {
     841           0 :         syncUpdate.rooms!.join!.values.first.timeline!.events!.first
     842           0 :             .unsigned![messageSendingStatusKey] = EventStatus.error.intValue;
     843           0 :         await _handleFakeSync(syncUpdate);
     844             :         rethrow;
     845             :       } catch (_) {
     846           0 :         if (DateTime.now().isAfter(timeoutDate)) {
     847           0 :           syncUpdate.rooms!.join!.values.first.timeline!.events!.first
     848           0 :               .unsigned![messageSendingStatusKey] = EventStatus.error.intValue;
     849           0 :           await _handleFakeSync(syncUpdate);
     850             :           rethrow;
     851             :         }
     852           0 :         Logs().v('Send File into room failed. Try again...');
     853           0 :         await Future.delayed(Duration(seconds: 1));
     854             :       }
     855             :     }
     856             : 
     857             :     // Send event
     858           3 :     final content = <String, dynamic>{
     859           6 :       'msgtype': file.msgType,
     860           6 :       'body': file.name,
     861           6 :       'filename': file.name,
     862           6 :       if (encryptedFile == null) 'url': uploadResp.toString(),
     863             :       if (encryptedFile != null)
     864           0 :         'file': {
     865           0 :           'url': uploadResp.toString(),
     866           0 :           'mimetype': file.mimeType,
     867             :           'v': 'v2',
     868           0 :           'key': {
     869             :             'alg': 'A256CTR',
     870             :             'ext': true,
     871           0 :             'k': encryptedFile.k,
     872           0 :             'key_ops': ['encrypt', 'decrypt'],
     873             :             'kty': 'oct',
     874             :           },
     875           0 :           'iv': encryptedFile.iv,
     876           0 :           'hashes': {'sha256': encryptedFile.sha256},
     877             :         },
     878           6 :       'info': {
     879           3 :         ...file.info,
     880             :         if (thumbnail != null && encryptedThumbnail == null)
     881           0 :           'thumbnail_url': thumbnailUploadResp.toString(),
     882             :         if (thumbnail != null && encryptedThumbnail != null)
     883           0 :           'thumbnail_file': {
     884           0 :             'url': thumbnailUploadResp.toString(),
     885           0 :             'mimetype': thumbnail.mimeType,
     886             :             'v': 'v2',
     887           0 :             'key': {
     888             :               'alg': 'A256CTR',
     889             :               'ext': true,
     890           0 :               'k': encryptedThumbnail.k,
     891           0 :               'key_ops': ['encrypt', 'decrypt'],
     892             :               'kty': 'oct',
     893             :             },
     894           0 :             'iv': encryptedThumbnail.iv,
     895           0 :             'hashes': {'sha256': encryptedThumbnail.sha256},
     896             :           },
     897           0 :         if (thumbnail != null) 'thumbnail_info': thumbnail.info,
     898           0 :         if (thumbnail?.blurhash != null &&
     899           0 :             file is MatrixImageFile &&
     900           0 :             file.blurhash == null)
     901           0 :           'xyz.amorgan.blurhash': thumbnail!.blurhash,
     902             :       },
     903           0 :       if (extraContent != null) ...extraContent,
     904             :     };
     905           3 :     final eventId = await sendEvent(
     906             :       content,
     907             :       txid: txid,
     908             :       inReplyTo: inReplyTo,
     909             :       editEventId: editEventId,
     910             :       threadRootEventId: threadRootEventId,
     911             :       threadLastEventId: threadLastEventId,
     912             :     );
     913           6 :     sendingFilePlaceholders.remove(txid);
     914           6 :     sendingFileThumbnails.remove(txid);
     915             :     return eventId;
     916             :   }
     917             : 
     918             :   /// Calculates how secure the communication is. When all devices are blocked or
     919             :   /// verified, then this returns [EncryptionHealthState.allVerified]. When at
     920             :   /// least one device is not verified, then it returns
     921             :   /// [EncryptionHealthState.unverifiedDevices]. Apps should display this health
     922             :   /// state next to the input text field to inform the user about the current
     923             :   /// encryption security level.
     924           2 :   Future<EncryptionHealthState> calcEncryptionHealthState() async {
     925           2 :     final users = await requestParticipants();
     926           2 :     users.removeWhere(
     927           2 :       (u) =>
     928           8 :           !{Membership.invite, Membership.join}.contains(u.membership) ||
     929           8 :           !client.userDeviceKeys.containsKey(u.id),
     930             :     );
     931             : 
     932           2 :     if (users.any(
     933           2 :       (u) =>
     934          12 :           client.userDeviceKeys[u.id]!.verified != UserVerifiedStatus.verified,
     935             :     )) {
     936             :       return EncryptionHealthState.unverifiedDevices;
     937             :     }
     938             : 
     939             :     return EncryptionHealthState.allVerified;
     940             :   }
     941             : 
     942          11 :   Future<String?> _sendContent(
     943             :     String type,
     944             :     Map<String, dynamic> content, {
     945             :     String? txid,
     946             :   }) async {
     947           0 :     txid ??= client.generateUniqueTransactionId();
     948             : 
     949          15 :     final mustEncrypt = encrypted && client.encryptionEnabled;
     950             : 
     951             :     final sendMessageContent = mustEncrypt
     952           2 :         ? await client.encryption!
     953           2 :             .encryptGroupMessagePayload(id, content, type: type)
     954             :         : content;
     955             : 
     956          22 :     return await client.sendMessage(
     957          11 :       id,
     958          11 :       sendMessageContent.containsKey('ciphertext')
     959             :           ? EventTypes.Encrypted
     960             :           : type,
     961             :       txid,
     962             :       sendMessageContent,
     963             :     );
     964             :   }
     965             : 
     966           3 :   String _stripBodyFallback(String body) {
     967           3 :     if (body.startsWith('> <@')) {
     968             :       var temp = '';
     969             :       var inPrefix = true;
     970           4 :       for (final l in body.split('\n')) {
     971           4 :         if (inPrefix && (l.isEmpty || l.startsWith('> '))) {
     972             :           continue;
     973             :         }
     974             : 
     975             :         inPrefix = false;
     976           4 :         temp += temp.isEmpty ? l : ('\n$l');
     977             :       }
     978             : 
     979             :       return temp;
     980             :     } else {
     981             :       return body;
     982             :     }
     983             :   }
     984             : 
     985             :   /// Sends an event to this room with this json as a content. Returns the
     986             :   /// event ID generated from the server.
     987             :   /// It uses list of completer to make sure events are sending in a row.
     988          11 :   Future<String?> sendEvent(
     989             :     Map<String, dynamic> content, {
     990             :     String type = EventTypes.Message,
     991             :     String? txid,
     992             :     Event? inReplyTo,
     993             :     String? editEventId,
     994             :     String? threadRootEventId,
     995             :     String? threadLastEventId,
     996             :   }) async {
     997             :     // Create new transaction id
     998             :     final String messageID;
     999             :     if (txid == null) {
    1000           6 :       messageID = client.generateUniqueTransactionId();
    1001             :     } else {
    1002             :       messageID = txid;
    1003             :     }
    1004             : 
    1005             :     if (inReplyTo != null) {
    1006             :       var replyText =
    1007          12 :           '<${inReplyTo.senderId}> ${_stripBodyFallback(inReplyTo.body)}';
    1008          15 :       replyText = replyText.split('\n').map((line) => '> $line').join('\n');
    1009           3 :       content['format'] = 'org.matrix.custom.html';
    1010             :       // be sure that we strip any previous reply fallbacks
    1011           6 :       final replyHtml = (inReplyTo.formattedText.isNotEmpty
    1012           2 :               ? inReplyTo.formattedText
    1013           9 :               : htmlEscape.convert(inReplyTo.body).replaceAll('\n', '<br>'))
    1014           3 :           .replaceAll(
    1015           3 :         RegExp(
    1016             :           r'<mx-reply>.*</mx-reply>',
    1017             :           caseSensitive: false,
    1018             :           multiLine: false,
    1019             :           dotAll: true,
    1020             :         ),
    1021             :         '',
    1022             :       );
    1023           3 :       final repliedHtml = content.tryGet<String>('formatted_body') ??
    1024             :           htmlEscape
    1025           6 :               .convert(content.tryGet<String>('body') ?? '')
    1026           3 :               .replaceAll('\n', '<br>');
    1027           3 :       content['formatted_body'] =
    1028          15 :           '<mx-reply><blockquote><a href="https://matrix.to/#/${inReplyTo.roomId!}/${inReplyTo.eventId}">In reply to</a> <a href="https://matrix.to/#/${inReplyTo.senderId}">${inReplyTo.senderId}</a><br>$replyHtml</blockquote></mx-reply>$repliedHtml';
    1029             :       // We escape all @room-mentions here to prevent accidental room pings when an admin
    1030             :       // replies to a message containing that!
    1031           3 :       content['body'] =
    1032           9 :           '${replyText.replaceAll('@room', '@\u200broom')}\n\n${content.tryGet<String>('body') ?? ''}';
    1033           6 :       content['m.relates_to'] = {
    1034           3 :         'm.in_reply_to': {
    1035           3 :           'event_id': inReplyTo.eventId,
    1036             :         },
    1037             :       };
    1038             :     }
    1039             : 
    1040             :     if (threadRootEventId != null) {
    1041           2 :       content['m.relates_to'] = {
    1042           1 :         'event_id': threadRootEventId,
    1043           1 :         'rel_type': RelationshipTypes.thread,
    1044           1 :         'is_falling_back': inReplyTo == null,
    1045           1 :         if (inReplyTo != null) ...{
    1046           1 :           'm.in_reply_to': {
    1047           1 :             'event_id': inReplyTo.eventId,
    1048             :           },
    1049           1 :         } else ...{
    1050             :           if (threadLastEventId != null)
    1051           2 :             'm.in_reply_to': {
    1052             :               'event_id': threadLastEventId,
    1053             :             },
    1054             :         },
    1055             :       };
    1056             :     }
    1057             : 
    1058             :     if (editEventId != null) {
    1059           2 :       final newContent = content.copy();
    1060           2 :       content['m.new_content'] = newContent;
    1061           4 :       content['m.relates_to'] = {
    1062             :         'event_id': editEventId,
    1063             :         'rel_type': RelationshipTypes.edit,
    1064             :       };
    1065           4 :       if (content['body'] is String) {
    1066           6 :         content['body'] = '* ${content['body']}';
    1067             :       }
    1068           4 :       if (content['formatted_body'] is String) {
    1069           0 :         content['formatted_body'] = '* ${content['formatted_body']}';
    1070             :       }
    1071             :     }
    1072          11 :     final sentDate = DateTime.now();
    1073          11 :     final syncUpdate = SyncUpdate(
    1074             :       nextBatch: '',
    1075          11 :       rooms: RoomsUpdate(
    1076          11 :         join: {
    1077          22 :           id: JoinedRoomUpdate(
    1078          11 :             timeline: TimelineUpdate(
    1079          11 :               events: [
    1080          11 :                 MatrixEvent(
    1081             :                   content: content,
    1082             :                   type: type,
    1083             :                   eventId: messageID,
    1084          22 :                   senderId: client.userID!,
    1085             :                   originServerTs: sentDate,
    1086          11 :                   unsigned: {
    1087          11 :                     messageSendingStatusKey: EventStatus.sending.intValue,
    1088             :                     'transaction_id': messageID,
    1089             :                   },
    1090             :                 ),
    1091             :               ],
    1092             :             ),
    1093             :           ),
    1094             :         },
    1095             :       ),
    1096             :     );
    1097          11 :     await _handleFakeSync(syncUpdate);
    1098          11 :     final completer = Completer();
    1099          22 :     _sendingQueue.add(completer);
    1100          33 :     while (_sendingQueue.first != completer) {
    1101           0 :       await _sendingQueue.first.future;
    1102             :     }
    1103             : 
    1104          44 :     final timeoutDate = DateTime.now().add(client.sendTimelineEventTimeout);
    1105             :     // Send the text and on success, store and display a *sent* event.
    1106             :     String? res;
    1107             : 
    1108             :     while (res == null) {
    1109             :       try {
    1110          11 :         res = await _sendContent(
    1111             :           type,
    1112             :           content,
    1113             :           txid: messageID,
    1114             :         );
    1115             :       } catch (e, s) {
    1116           4 :         if (e is MatrixException &&
    1117           4 :             e.retryAfterMs != null &&
    1118           0 :             !DateTime.now()
    1119           0 :                 .add(Duration(milliseconds: e.retryAfterMs!))
    1120           0 :                 .isAfter(timeoutDate)) {
    1121           0 :           Logs().w(
    1122           0 :             'Ratelimited while sending message, waiting for ${e.retryAfterMs}ms',
    1123             :           );
    1124           0 :           await Future.delayed(Duration(milliseconds: e.retryAfterMs!));
    1125           4 :         } else if (e is MatrixException ||
    1126           2 :             e is EventTooLarge ||
    1127           0 :             DateTime.now().isAfter(timeoutDate)) {
    1128           8 :           Logs().w('Problem while sending message', e, s);
    1129          28 :           syncUpdate.rooms!.join!.values.first.timeline!.events!.first
    1130          12 :               .unsigned![messageSendingStatusKey] = EventStatus.error.intValue;
    1131           4 :           await _handleFakeSync(syncUpdate);
    1132           4 :           completer.complete();
    1133           8 :           _sendingQueue.remove(completer);
    1134           4 :           if (e is EventTooLarge ||
    1135          12 :               (e is MatrixException && e.error == MatrixError.M_FORBIDDEN)) {
    1136             :             rethrow;
    1137             :           }
    1138             :           return null;
    1139             :         } else {
    1140           0 :           Logs()
    1141           0 :               .w('Problem while sending message: $e Try again in 1 seconds...');
    1142           0 :           await Future.delayed(Duration(seconds: 1));
    1143             :         }
    1144             :       }
    1145             :     }
    1146          77 :     syncUpdate.rooms!.join!.values.first.timeline!.events!.first
    1147          33 :         .unsigned![messageSendingStatusKey] = EventStatus.sent.intValue;
    1148          88 :     syncUpdate.rooms!.join!.values.first.timeline!.events!.first.eventId = res;
    1149          11 :     await _handleFakeSync(syncUpdate);
    1150          11 :     completer.complete();
    1151          22 :     _sendingQueue.remove(completer);
    1152             : 
    1153             :     return res;
    1154             :   }
    1155             : 
    1156             :   /// Call the Matrix API to join this room if the user is not already a member.
    1157             :   /// If this room is intended to be a direct chat, the direct chat flag will
    1158             :   /// automatically be set.
    1159           0 :   Future<void> join({
    1160             :     /// In case of the room is not found on the server, the client leaves the
    1161             :     /// room and rethrows the exception.
    1162             :     bool leaveIfNotFound = true,
    1163             :   }) async {
    1164           0 :     final dmId = directChatMatrixID;
    1165             :     try {
    1166             :       // If this is a DM, mark it as a DM first, because otherwise the current member
    1167             :       // event might be the join event already and there is also a race condition there for SDK users.
    1168           0 :       if (dmId != null) await addToDirectChat(dmId);
    1169             : 
    1170             :       // now join
    1171           0 :       await client.joinRoomById(id);
    1172           0 :     } on MatrixException catch (exception) {
    1173           0 :       if (dmId != null) await removeFromDirectChat();
    1174             :       if (leaveIfNotFound &&
    1175           0 :           membership == Membership.invite &&
    1176             :           // Right now Synapse responses with `M_UNKNOWN` when the room can not
    1177             :           // be found. This is the case for example when User A invites User B
    1178             :           // to a direct chat and then User A leaves the chat before User B
    1179             :           // joined.
    1180             :           // See: https://github.com/element-hq/synapse/issues/1533
    1181           0 :           exception.error == MatrixError.M_UNKNOWN) {
    1182           0 :         await leave();
    1183             :       }
    1184             :       rethrow;
    1185             :     }
    1186             :     return;
    1187             :   }
    1188             : 
    1189             :   /// Call the Matrix API to leave this room. If this room is set as a direct
    1190             :   /// chat, this will be removed too.
    1191           1 :   Future<void> leave() async {
    1192             :     try {
    1193           3 :       await client.leaveRoom(id);
    1194           0 :     } on MatrixException catch (e, s) {
    1195           0 :       if ([MatrixError.M_NOT_FOUND, MatrixError.M_UNKNOWN].contains(e.error)) {
    1196           0 :         Logs().w(
    1197             :           'Unable to leave room. Deleting manually from database...',
    1198             :           e,
    1199             :           s,
    1200             :         );
    1201           0 :         await _handleFakeSync(
    1202           0 :           SyncUpdate(
    1203             :             nextBatch: '',
    1204           0 :             rooms: RoomsUpdate(
    1205           0 :               leave: {
    1206           0 :                 id: LeftRoomUpdate(),
    1207             :               },
    1208             :             ),
    1209             :           ),
    1210             :         );
    1211             :       }
    1212             :       rethrow;
    1213             :     }
    1214             :     return;
    1215             :   }
    1216             : 
    1217             :   /// Call the Matrix API to forget this room if you already left it.
    1218           0 :   Future<void> forget() async {
    1219           0 :     await client.database?.forgetRoom(id);
    1220           0 :     await client.forgetRoom(id);
    1221             :     // Update archived rooms, otherwise an archived room may still be in the
    1222             :     // list after a forget room call
    1223           0 :     final roomIndex = client.archivedRooms.indexWhere((r) => r.room.id == id);
    1224           0 :     if (roomIndex != -1) {
    1225           0 :       client.archivedRooms.removeAt(roomIndex);
    1226             :     }
    1227             :     return;
    1228             :   }
    1229             : 
    1230             :   /// Call the Matrix API to kick a user from this room.
    1231          20 :   Future<void> kick(String userID) => client.kick(id, userID);
    1232             : 
    1233             :   /// Call the Matrix API to ban a user from this room.
    1234          20 :   Future<void> ban(String userID) => client.ban(id, userID);
    1235             : 
    1236             :   /// Call the Matrix API to unban a banned user from this room.
    1237          20 :   Future<void> unban(String userID) => client.unban(id, userID);
    1238             : 
    1239             :   /// Set the power level of the user with the [userID] to the value [power].
    1240             :   /// Returns the event ID of the new state event. If there is no known
    1241             :   /// power level event, there might something broken and this returns null.
    1242             :   /// Please note, that you need to await the power level state from sync before
    1243             :   /// the changes are actually applied. Especially if you want to set multiple
    1244             :   /// power levels at once, you need to await each change in the sync, to not
    1245             :   /// override those.
    1246           5 :   Future<String> setPower(String userId, int power) async {
    1247             :     final powerLevelMapCopy =
    1248          13 :         getState(EventTypes.RoomPowerLevels)?.content.copy() ?? {};
    1249             : 
    1250           5 :     var users = powerLevelMapCopy['users'];
    1251             : 
    1252           5 :     if (users is! Map<String, Object?>) {
    1253             :       if (users != null) {
    1254           4 :         Logs().v(
    1255           6 :           'Repairing Power Level "users" has the wrong type "${powerLevelMapCopy['users'].runtimeType}"',
    1256             :         );
    1257             :       }
    1258          10 :       users = powerLevelMapCopy['users'] = <String, Object?>{};
    1259             :     }
    1260             : 
    1261           5 :     users[userId] = power;
    1262             : 
    1263          10 :     return await client.setRoomStateWithKey(
    1264           5 :       id,
    1265             :       EventTypes.RoomPowerLevels,
    1266             :       '',
    1267             :       powerLevelMapCopy,
    1268             :     );
    1269             :   }
    1270             : 
    1271             :   /// Call the Matrix API to invite a user to this room.
    1272           3 :   Future<void> invite(
    1273             :     String userID, {
    1274             :     String? reason,
    1275             :   }) =>
    1276           6 :       client.inviteUser(
    1277           3 :         id,
    1278             :         userID,
    1279             :         reason: reason,
    1280             :       );
    1281             : 
    1282             :   /// Request more previous events from the server. [historyCount] defines how many events should
    1283             :   /// be received maximum. When the request is answered, [onHistoryReceived] will be triggered **before**
    1284             :   /// the historical events will be published in the onEvent stream. [filter] allows you to specify a
    1285             :   /// [StateFilter] object to filter the events, which can include various criteria such as event types
    1286             :   /// (e.g., [EventTypes.Message]) and other state-related filters. The [StateFilter] object will have
    1287             :   /// [lazyLoadMembers] set to true by default, but this can be overridden.
    1288             :   /// Returns the actual count of received timeline events.
    1289           3 :   Future<int> requestHistory({
    1290             :     int historyCount = defaultHistoryCount,
    1291             :     void Function()? onHistoryReceived,
    1292             :     direction = Direction.b,
    1293             :     StateFilter? filter,
    1294             :   }) async {
    1295           3 :     final prev_batch = this.prev_batch;
    1296             : 
    1297           3 :     final storeInDatabase = !isArchived;
    1298             : 
    1299             :     // Ensure stateFilter is not null and set lazyLoadMembers to true if not already set
    1300           3 :     filter ??= StateFilter(lazyLoadMembers: true);
    1301           3 :     filter.lazyLoadMembers ??= true;
    1302             : 
    1303             :     if (prev_batch == null) {
    1304             :       throw 'Tried to request history without a prev_batch token';
    1305             :     }
    1306           6 :     final resp = await client.getRoomEvents(
    1307           3 :       id,
    1308             :       direction,
    1309             :       from: prev_batch,
    1310             :       limit: historyCount,
    1311           6 :       filter: jsonEncode(filter.toJson()),
    1312             :     );
    1313             : 
    1314           2 :     if (onHistoryReceived != null) onHistoryReceived();
    1315           6 :     this.prev_batch = resp.end;
    1316             : 
    1317           3 :     Future<void> loadFn() async {
    1318           9 :       if (!((resp.chunk.isNotEmpty) && resp.end != null)) return;
    1319             : 
    1320           6 :       await client.handleSync(
    1321           3 :         SyncUpdate(
    1322             :           nextBatch: '',
    1323           3 :           rooms: RoomsUpdate(
    1324           6 :             join: membership == Membership.join
    1325           1 :                 ? {
    1326           2 :                     id: JoinedRoomUpdate(
    1327           1 :                       state: resp.state,
    1328           1 :                       timeline: TimelineUpdate(
    1329             :                         limited: false,
    1330           1 :                         events: direction == Direction.b
    1331           1 :                             ? resp.chunk
    1332           0 :                             : resp.chunk.reversed.toList(),
    1333             :                         prevBatch:
    1334           2 :                             direction == Direction.b ? resp.end : resp.start,
    1335             :                       ),
    1336             :                     ),
    1337             :                   }
    1338             :                 : null,
    1339           6 :             leave: membership != Membership.join
    1340           2 :                 ? {
    1341           4 :                     id: LeftRoomUpdate(
    1342           2 :                       state: resp.state,
    1343           2 :                       timeline: TimelineUpdate(
    1344             :                         limited: false,
    1345           2 :                         events: direction == Direction.b
    1346           2 :                             ? resp.chunk
    1347           0 :                             : resp.chunk.reversed.toList(),
    1348             :                         prevBatch:
    1349           4 :                             direction == Direction.b ? resp.end : resp.start,
    1350             :                       ),
    1351             :                     ),
    1352             :                   }
    1353             :                 : null,
    1354             :           ),
    1355             :         ),
    1356             :         direction: Direction.b,
    1357             :       );
    1358             :     }
    1359             : 
    1360           6 :     if (client.database != null) {
    1361          12 :       await client.database?.transaction(() async {
    1362             :         if (storeInDatabase) {
    1363           6 :           await client.database?.setRoomPrevBatch(resp.end, id, client);
    1364             :         }
    1365           3 :         await loadFn();
    1366             :       });
    1367             :     } else {
    1368           0 :       await loadFn();
    1369             :     }
    1370             : 
    1371           6 :     return resp.chunk.length;
    1372             :   }
    1373             : 
    1374             :   /// Sets this room as a direct chat for this user if not already.
    1375           8 :   Future<void> addToDirectChat(String userID) async {
    1376          16 :     final directChats = client.directChats;
    1377          16 :     if (directChats[userID] is List) {
    1378           0 :       if (!directChats[userID].contains(id)) {
    1379           0 :         directChats[userID].add(id);
    1380             :       } else {
    1381             :         return;
    1382             :       } // Is already in direct chats
    1383             :     } else {
    1384          24 :       directChats[userID] = [id];
    1385             :     }
    1386             : 
    1387          16 :     await client.setAccountData(
    1388          16 :       client.userID!,
    1389             :       'm.direct',
    1390             :       directChats,
    1391             :     );
    1392             :     return;
    1393             :   }
    1394             : 
    1395             :   /// Removes this room from all direct chat tags.
    1396           1 :   Future<void> removeFromDirectChat() async {
    1397           3 :     final directChats = client.directChats.copy();
    1398           2 :     for (final k in directChats.keys) {
    1399           1 :       final directChat = directChats[k];
    1400           3 :       if (directChat is List && directChat.contains(id)) {
    1401           2 :         directChat.remove(id);
    1402             :       }
    1403             :     }
    1404             : 
    1405           4 :     directChats.removeWhere((_, v) => v is List && v.isEmpty);
    1406             : 
    1407           3 :     if (directChats == client.directChats) {
    1408             :       return;
    1409             :     }
    1410             : 
    1411           2 :     await client.setAccountData(
    1412           2 :       client.userID!,
    1413             :       'm.direct',
    1414             :       directChats,
    1415             :     );
    1416             :     return;
    1417             :   }
    1418             : 
    1419             :   /// Get the user fully read marker
    1420           0 :   @Deprecated('Use fullyRead marker')
    1421           0 :   String? get userFullyReadMarker => fullyRead;
    1422             : 
    1423           2 :   bool get isFederated =>
    1424           6 :       getState(EventTypes.RoomCreate)?.content.tryGet<bool>('m.federate') ??
    1425             :       true;
    1426             : 
    1427             :   /// Sets the position of the read marker for a given room, and optionally the
    1428             :   /// read receipt's location.
    1429             :   /// If you set `public` to false, only a private receipt will be sent. A private receipt is always sent if `mRead` is set. If no value is provided, the default from the `client` is used.
    1430             :   /// You can leave out the `eventId`, which will not update the read marker but just send receipts, but there are few cases where that makes sense.
    1431           4 :   Future<void> setReadMarker(
    1432             :     String? eventId, {
    1433             :     String? mRead,
    1434             :     bool? public,
    1435             :   }) async {
    1436           8 :     await client.setReadMarker(
    1437           4 :       id,
    1438             :       mFullyRead: eventId,
    1439           8 :       mRead: (public ?? client.receiptsPublicByDefault) ? mRead : null,
    1440             :       // we always send the private receipt, because there is no reason not to.
    1441             :       mReadPrivate: mRead,
    1442             :     );
    1443             :     return;
    1444             :   }
    1445             : 
    1446           0 :   Future<TimelineChunk?> getEventContext(String eventId) async {
    1447           0 :     final resp = await client.getEventContext(
    1448           0 :       id, eventId,
    1449             :       limit: Room.defaultHistoryCount,
    1450             :       // filter: jsonEncode(StateFilter(lazyLoadMembers: true).toJson()),
    1451             :     );
    1452             : 
    1453           0 :     final events = [
    1454           0 :       if (resp.eventsAfter != null) ...resp.eventsAfter!.reversed,
    1455           0 :       if (resp.event != null) resp.event!,
    1456           0 :       if (resp.eventsBefore != null) ...resp.eventsBefore!,
    1457           0 :     ].map((e) => Event.fromMatrixEvent(e, this)).toList();
    1458             : 
    1459             :     // Try again to decrypt encrypted events but don't update the database.
    1460           0 :     if (encrypted && client.database != null && client.encryptionEnabled) {
    1461           0 :       for (var i = 0; i < events.length; i++) {
    1462           0 :         if (events[i].type == EventTypes.Encrypted &&
    1463           0 :             events[i].content['can_request_session'] == true) {
    1464           0 :           events[i] = await client.encryption!.decryptRoomEvent(events[i]);
    1465             :         }
    1466             :       }
    1467             :     }
    1468             : 
    1469           0 :     final chunk = TimelineChunk(
    1470           0 :       nextBatch: resp.end ?? '',
    1471           0 :       prevBatch: resp.start ?? '',
    1472             :       events: events,
    1473             :     );
    1474             : 
    1475             :     return chunk;
    1476             :   }
    1477             : 
    1478             :   /// This API updates the marker for the given receipt type to the event ID
    1479             :   /// specified. In general you want to use `setReadMarker` instead to set private
    1480             :   /// and public receipt as well as the marker at the same time.
    1481           0 :   @Deprecated(
    1482             :     'Use setReadMarker with mRead set instead. That allows for more control and there are few cases to not send a marker at the same time.',
    1483             :   )
    1484             :   Future<void> postReceipt(
    1485             :     String eventId, {
    1486             :     ReceiptType type = ReceiptType.mRead,
    1487             :   }) async {
    1488           0 :     await client.postReceipt(
    1489           0 :       id,
    1490             :       ReceiptType.mRead,
    1491             :       eventId,
    1492             :     );
    1493             :     return;
    1494             :   }
    1495             : 
    1496             :   /// Is the room archived
    1497          15 :   bool get isArchived => membership == Membership.leave;
    1498             : 
    1499             :   /// Creates a timeline from the store. Returns a [Timeline] object. If you
    1500             :   /// just want to update the whole timeline on every change, use the [onUpdate]
    1501             :   /// callback. For updating only the parts that have changed, use the
    1502             :   /// [onChange], [onRemove], [onInsert] and the [onHistoryReceived] callbacks.
    1503             :   /// This method can also retrieve the timeline at a specific point by setting
    1504             :   /// the [eventContextId]
    1505           4 :   Future<Timeline> getTimeline({
    1506             :     void Function(int index)? onChange,
    1507             :     void Function(int index)? onRemove,
    1508             :     void Function(int insertID)? onInsert,
    1509             :     void Function()? onNewEvent,
    1510             :     void Function()? onUpdate,
    1511             :     String? eventContextId,
    1512             :   }) async {
    1513           4 :     await postLoad();
    1514             : 
    1515             :     List<Event> events;
    1516             : 
    1517           4 :     if (!isArchived) {
    1518           6 :       events = await client.database?.getEventList(
    1519             :             this,
    1520             :             limit: defaultHistoryCount,
    1521             :           ) ??
    1522           0 :           <Event>[];
    1523             :     } else {
    1524           6 :       final archive = client.getArchiveRoomFromCache(id);
    1525           6 :       events = archive?.timeline.events.toList() ?? [];
    1526           6 :       for (var i = 0; i < events.length; i++) {
    1527             :         // Try to decrypt encrypted events but don't update the database.
    1528           2 :         if (encrypted && client.encryptionEnabled) {
    1529           0 :           if (events[i].type == EventTypes.Encrypted) {
    1530           0 :             events[i] = await client.encryption!.decryptRoomEvent(events[i]);
    1531             :           }
    1532             :         }
    1533             :       }
    1534             :     }
    1535             : 
    1536           4 :     var chunk = TimelineChunk(events: events);
    1537             :     // Load the timeline arround eventContextId if set
    1538             :     if (eventContextId != null) {
    1539           0 :       if (!events.any((Event event) => event.eventId == eventContextId)) {
    1540             :         chunk =
    1541           0 :             await getEventContext(eventContextId) ?? TimelineChunk(events: []);
    1542             :       }
    1543             :     }
    1544             : 
    1545           4 :     final timeline = Timeline(
    1546             :       room: this,
    1547             :       chunk: chunk,
    1548             :       onChange: onChange,
    1549             :       onRemove: onRemove,
    1550             :       onInsert: onInsert,
    1551             :       onNewEvent: onNewEvent,
    1552             :       onUpdate: onUpdate,
    1553             :     );
    1554             : 
    1555             :     // Fetch all users from database we have got here.
    1556             :     if (eventContextId == null) {
    1557          16 :       final userIds = events.map((event) => event.senderId).toSet();
    1558           8 :       for (final userId in userIds) {
    1559           4 :         if (getState(EventTypes.RoomMember, userId) != null) continue;
    1560          12 :         final dbUser = await client.database?.getUser(userId, this);
    1561           0 :         if (dbUser != null) setState(dbUser);
    1562             :       }
    1563             :     }
    1564             : 
    1565             :     // Try again to decrypt encrypted events and update the database.
    1566           4 :     if (encrypted && client.encryptionEnabled) {
    1567             :       // decrypt messages
    1568           0 :       for (var i = 0; i < chunk.events.length; i++) {
    1569           0 :         if (chunk.events[i].type == EventTypes.Encrypted) {
    1570             :           if (eventContextId != null) {
    1571             :             // for the fragmented timeline, we don't cache the decrypted
    1572             :             //message in the database
    1573           0 :             chunk.events[i] = await client.encryption!.decryptRoomEvent(
    1574           0 :               chunk.events[i],
    1575             :             );
    1576           0 :           } else if (client.database != null) {
    1577             :             // else, we need the database
    1578           0 :             await client.database?.transaction(() async {
    1579           0 :               for (var i = 0; i < chunk.events.length; i++) {
    1580           0 :                 if (chunk.events[i].content['can_request_session'] == true) {
    1581           0 :                   chunk.events[i] = await client.encryption!.decryptRoomEvent(
    1582           0 :                     chunk.events[i],
    1583           0 :                     store: !isArchived,
    1584             :                     updateType: EventUpdateType.history,
    1585             :                   );
    1586             :                 }
    1587             :               }
    1588             :             });
    1589             :           }
    1590             :         }
    1591             :       }
    1592             :     }
    1593             : 
    1594             :     return timeline;
    1595             :   }
    1596             : 
    1597             :   /// Returns all participants for this room. With lazy loading this
    1598             :   /// list may not be complete. Use [requestParticipants] in this
    1599             :   /// case.
    1600             :   /// List `membershipFilter` defines with what membership do you want the
    1601             :   /// participants, default set to
    1602             :   /// [[Membership.join, Membership.invite, Membership.knock]]
    1603          35 :   List<User> getParticipants([
    1604             :     List<Membership> membershipFilter = const [
    1605             :       Membership.join,
    1606             :       Membership.invite,
    1607             :       Membership.knock,
    1608             :     ],
    1609             :   ]) {
    1610          70 :     final members = states[EventTypes.RoomMember];
    1611             :     if (members != null) {
    1612          35 :       return members.entries
    1613         175 :           .where((entry) => entry.value.type == EventTypes.RoomMember)
    1614         140 :           .map((entry) => entry.value.asUser(this))
    1615         140 :           .where((user) => membershipFilter.contains(user.membership))
    1616          35 :           .toList();
    1617             :     }
    1618           6 :     return <User>[];
    1619             :   }
    1620             : 
    1621             :   /// Request the full list of participants from the server. The local list
    1622             :   /// from the store is not complete if the client uses lazy loading.
    1623             :   /// List `membershipFilter` defines with what membership do you want the
    1624             :   /// participants, default set to
    1625             :   /// [[Membership.join, Membership.invite, Membership.knock]]
    1626             :   /// Set [cache] to `false` if you do not want to cache the users in memory
    1627             :   /// for this session which is highly recommended for large public rooms.
    1628             :   /// By default users are only cached in encrypted rooms as encrypted rooms
    1629             :   /// need a full member list.
    1630          33 :   Future<List<User>> requestParticipants([
    1631             :     List<Membership> membershipFilter = const [
    1632             :       Membership.join,
    1633             :       Membership.invite,
    1634             :       Membership.knock,
    1635             :     ],
    1636             :     bool suppressWarning = false,
    1637             :     bool? cache,
    1638             :   ]) async {
    1639          66 :     if (!participantListComplete || partial) {
    1640             :       // we aren't fully loaded, maybe the users are in the database
    1641             :       // We always need to check the database in the partial case, since state
    1642             :       // events won't get written to memory in this case and someone new could
    1643             :       // have joined, while someone else left, which might lead to the same
    1644             :       // count in the completeness check.
    1645         100 :       final users = await client.database?.getUsers(this) ?? [];
    1646          36 :       for (final user in users) {
    1647           3 :         setState(user);
    1648             :       }
    1649             :     }
    1650             : 
    1651             :     // Do not request users from the server if we have already have a complete list locally.
    1652          33 :     if (participantListComplete) {
    1653          33 :       return getParticipants(membershipFilter);
    1654             :     }
    1655             : 
    1656           3 :     cache ??= encrypted;
    1657             : 
    1658           6 :     final memberCount = summary.mJoinedMemberCount;
    1659           3 :     if (!suppressWarning && cache && memberCount != null && memberCount > 100) {
    1660           0 :       Logs().w('''
    1661           0 :         Loading a list of $memberCount participants for the room $id.
    1662             :         This may affect the performance. Please make sure to not unnecessary
    1663             :         request so many participants or suppress this warning.
    1664           0 :       ''');
    1665             :     }
    1666             : 
    1667           9 :     final matrixEvents = await client.getMembersByRoom(id);
    1668             :     final users = matrixEvents
    1669          12 :             ?.map((e) => Event.fromMatrixEvent(e, this).asUser)
    1670           3 :             .toList() ??
    1671           0 :         [];
    1672             : 
    1673             :     if (cache) {
    1674           6 :       for (final user in users) {
    1675           3 :         setState(user); // at *least* cache this in-memory
    1676           9 :         await client.database?.storeEventUpdate(
    1677           3 :           EventUpdate(
    1678           3 :             roomID: id,
    1679             :             type: EventUpdateType.state,
    1680           3 :             content: user.toJson(),
    1681             :           ),
    1682           3 :           client,
    1683             :         );
    1684             :       }
    1685             :     }
    1686             : 
    1687          12 :     users.removeWhere((u) => !membershipFilter.contains(u.membership));
    1688             :     return users;
    1689             :   }
    1690             : 
    1691             :   /// Checks if the local participant list of joined and invited users is complete.
    1692          33 :   bool get participantListComplete {
    1693          33 :     final knownParticipants = getParticipants();
    1694             :     final joinedCount =
    1695         165 :         knownParticipants.where((u) => u.membership == Membership.join).length;
    1696             :     final invitedCount = knownParticipants
    1697         132 :         .where((u) => u.membership == Membership.invite)
    1698          33 :         .length;
    1699             : 
    1700          99 :     return (summary.mJoinedMemberCount ?? 0) == joinedCount &&
    1701          99 :         (summary.mInvitedMemberCount ?? 0) == invitedCount;
    1702             :   }
    1703             : 
    1704           0 :   @Deprecated(
    1705             :     'The method was renamed unsafeGetUserFromMemoryOrFallback. Please prefer requestParticipants.',
    1706             :   )
    1707             :   User getUserByMXIDSync(String mxID) {
    1708           0 :     return unsafeGetUserFromMemoryOrFallback(mxID);
    1709             :   }
    1710             : 
    1711             :   /// Returns the [User] object for the given [mxID] or return
    1712             :   /// a fallback [User] and start a request to get the user
    1713             :   /// from the homeserver.
    1714           8 :   User unsafeGetUserFromMemoryOrFallback(String mxID) {
    1715           8 :     final user = getState(EventTypes.RoomMember, mxID);
    1716             :     if (user != null) {
    1717           6 :       return user.asUser(this);
    1718             :     } else {
    1719           5 :       if (mxID.isValidMatrixId) {
    1720             :         // ignore: discarded_futures
    1721           5 :         requestUser(
    1722             :           mxID,
    1723             :           ignoreErrors: true,
    1724             :         );
    1725             :       }
    1726           5 :       return User(mxID, room: this);
    1727             :     }
    1728             :   }
    1729             : 
    1730             :   // Internal helper to implement requestUser
    1731           8 :   Future<User?> _requestSingleParticipantViaState(
    1732             :     String mxID, {
    1733             :     required bool ignoreErrors,
    1734             :   }) async {
    1735             :     try {
    1736          32 :       Logs().v('Request missing user $mxID in room $id from the server...');
    1737          16 :       final resp = await client.getRoomStateWithKey(
    1738           8 :         id,
    1739             :         EventTypes.RoomMember,
    1740             :         mxID,
    1741             :       );
    1742             : 
    1743             :       // valid member events require a valid membership key
    1744           6 :       final membership = resp.tryGet<String>('membership', TryGet.required);
    1745           6 :       assert(membership != null);
    1746             : 
    1747           6 :       final foundUser = User(
    1748             :         mxID,
    1749             :         room: this,
    1750           6 :         displayName: resp.tryGet<String>('displayname', TryGet.silent),
    1751           6 :         avatarUrl: resp.tryGet<String>('avatar_url', TryGet.silent),
    1752             :         membership: membership,
    1753             :       );
    1754             : 
    1755             :       // Store user in database:
    1756          24 :       await client.database?.transaction(() async {
    1757          18 :         await client.database?.storeEventUpdate(
    1758           6 :           EventUpdate(
    1759           6 :             content: foundUser.toJson(),
    1760           6 :             roomID: id,
    1761             :             type: EventUpdateType.state,
    1762             :           ),
    1763           6 :           client,
    1764             :         );
    1765             :       });
    1766             : 
    1767             :       return foundUser;
    1768           5 :     } on MatrixException catch (_) {
    1769             :       // Ignore if we have no permission
    1770             :       return null;
    1771             :     } catch (e, s) {
    1772             :       if (!ignoreErrors) {
    1773             :         rethrow;
    1774             :       } else {
    1775           6 :         Logs().w('Unable to request the user $mxID from the server', e, s);
    1776             :         return null;
    1777             :       }
    1778             :     }
    1779             :   }
    1780             : 
    1781             :   // Internal helper to implement requestUser
    1782           9 :   Future<User?> _requestUser(
    1783             :     String mxID, {
    1784             :     required bool ignoreErrors,
    1785             :     required bool requestState,
    1786             :     required bool requestProfile,
    1787             :   }) async {
    1788             :     // Is user already in cache?
    1789             : 
    1790             :     // If not in cache, try the database
    1791          12 :     User? foundUser = getState(EventTypes.RoomMember, mxID)?.asUser(this);
    1792             : 
    1793             :     // If the room is not postloaded, check the database
    1794           9 :     if (partial && foundUser == null) {
    1795          16 :       foundUser = await client.database?.getUser(mxID, this);
    1796             :     }
    1797             : 
    1798             :     // If not in the database, try fetching the member from the server
    1799             :     if (requestState && foundUser == null) {
    1800           8 :       foundUser = await _requestSingleParticipantViaState(
    1801             :         mxID,
    1802             :         ignoreErrors: ignoreErrors,
    1803             :       );
    1804             :     }
    1805             : 
    1806             :     // If the user isn't found or they have left and no displayname set anymore, request their profile from the server
    1807             :     if (requestProfile) {
    1808             :       if (foundUser
    1809             :           case null ||
    1810             :               User(
    1811          14 :                 membership: Membership.ban || Membership.leave,
    1812           6 :                 displayName: null
    1813             :               )) {
    1814             :         try {
    1815          10 :           final profile = await client.getUserProfile(mxID);
    1816           2 :           foundUser = User(
    1817             :             mxID,
    1818           2 :             displayName: profile.displayname,
    1819           4 :             avatarUrl: profile.avatarUrl?.toString(),
    1820           6 :             membership: foundUser?.membership.name ?? Membership.leave.name,
    1821             :             room: this,
    1822             :           );
    1823             :         } catch (e, s) {
    1824             :           if (!ignoreErrors) {
    1825             :             rethrow;
    1826             :           } else {
    1827           2 :             Logs()
    1828           4 :                 .w('Unable to request the profile $mxID from the server', e, s);
    1829             :           }
    1830             :         }
    1831             :       }
    1832             :     }
    1833             : 
    1834             :     if (foundUser == null) return null;
    1835             :     // make sure we didn't actually store anything by the time we did those requests
    1836             :     final userFromCurrentState =
    1837          10 :         getState(EventTypes.RoomMember, mxID)?.asUser(this);
    1838             : 
    1839             :     // Set user in the local state if the state changed.
    1840             :     // If we set the state unconditionally, we might end up with a client calling this over and over thinking the user changed.
    1841             :     if (userFromCurrentState == null ||
    1842           9 :         userFromCurrentState.displayName != foundUser.displayName) {
    1843           6 :       setState(foundUser);
    1844             :       // ignore: deprecated_member_use_from_same_package
    1845          18 :       onUpdate.add(id);
    1846             :     }
    1847             : 
    1848             :     return foundUser;
    1849             :   }
    1850             : 
    1851             :   final Map<
    1852             :       ({
    1853             :         String mxID,
    1854             :         bool ignoreErrors,
    1855             :         bool requestState,
    1856             :         bool requestProfile,
    1857             :       }),
    1858             :       AsyncCache<User?>> _inflightUserRequests = {};
    1859             : 
    1860             :   /// Requests a missing [User] for this room. Important for clients using
    1861             :   /// lazy loading. If the user can't be found this method tries to fetch
    1862             :   /// the displayname and avatar from the server if [requestState] is true.
    1863             :   /// If that fails, it falls back to requesting the global profile if
    1864             :   /// [requestProfile] is true.
    1865           9 :   Future<User?> requestUser(
    1866             :     String mxID, {
    1867             :     bool ignoreErrors = false,
    1868             :     bool requestState = true,
    1869             :     bool requestProfile = true,
    1870             :   }) async {
    1871          18 :     assert(mxID.isValidMatrixId);
    1872             : 
    1873             :     final parameters = (
    1874             :       mxID: mxID,
    1875             :       ignoreErrors: ignoreErrors,
    1876             :       requestState: requestState,
    1877             :       requestProfile: requestProfile,
    1878             :     );
    1879             : 
    1880          27 :     final cache = _inflightUserRequests[parameters] ??= AsyncCache.ephemeral();
    1881             : 
    1882             :     try {
    1883           9 :       final user = await cache.fetch(
    1884          18 :         () => _requestUser(
    1885             :           mxID,
    1886             :           ignoreErrors: ignoreErrors,
    1887             :           requestState: requestState,
    1888             :           requestProfile: requestProfile,
    1889             :         ),
    1890             :       );
    1891          18 :       _inflightUserRequests.remove(parameters);
    1892             :       return user;
    1893             :     } catch (_) {
    1894           2 :       _inflightUserRequests.remove(parameters);
    1895             :       rethrow;
    1896             :     }
    1897             :   }
    1898             : 
    1899             :   /// Searches for the event in the local cache and then on the server if not
    1900             :   /// found. Returns null if not found anywhere.
    1901           4 :   Future<Event?> getEventById(String eventID) async {
    1902             :     try {
    1903          12 :       final dbEvent = await client.database?.getEventById(eventID, this);
    1904             :       if (dbEvent != null) return dbEvent;
    1905          12 :       final matrixEvent = await client.getOneRoomEvent(id, eventID);
    1906           4 :       final event = Event.fromMatrixEvent(matrixEvent, this);
    1907          12 :       if (event.type == EventTypes.Encrypted && client.encryptionEnabled) {
    1908             :         // attempt decryption
    1909           6 :         return await client.encryption?.decryptRoomEvent(event);
    1910             :       }
    1911             :       return event;
    1912           2 :     } on MatrixException catch (err) {
    1913           4 :       if (err.errcode == 'M_NOT_FOUND') {
    1914             :         return null;
    1915             :       }
    1916             :       rethrow;
    1917             :     }
    1918             :   }
    1919             : 
    1920             :   /// Returns the power level of the given user ID.
    1921             :   /// If a user_id is in the users list, then that user_id has the associated
    1922             :   /// power level. Otherwise they have the default level users_default.
    1923             :   /// If users_default is not supplied, it is assumed to be 0. If the room
    1924             :   /// contains no m.room.power_levels event, the room’s creator has a power
    1925             :   /// level of 100, and all other users have a power level of 0.
    1926           8 :   int getPowerLevelByUserId(String userId) {
    1927          14 :     final powerLevelMap = getState(EventTypes.RoomPowerLevels)?.content;
    1928             : 
    1929             :     final userSpecificPowerLevel =
    1930          12 :         powerLevelMap?.tryGetMap<String, Object?>('users')?.tryGet<int>(userId);
    1931             : 
    1932           6 :     final defaultUserPowerLevel = powerLevelMap?.tryGet<int>('users_default');
    1933             : 
    1934             :     final fallbackPowerLevel =
    1935          18 :         getState(EventTypes.RoomCreate)?.senderId == userId ? 100 : 0;
    1936             : 
    1937             :     return userSpecificPowerLevel ??
    1938             :         defaultUserPowerLevel ??
    1939             :         fallbackPowerLevel;
    1940             :   }
    1941             : 
    1942             :   /// Returns the user's own power level.
    1943          24 :   int get ownPowerLevel => getPowerLevelByUserId(client.userID!);
    1944             : 
    1945             :   /// Returns the power levels from all users for this room or null if not given.
    1946           0 :   @Deprecated('Use `getPowerLevelByUserId(String userId)` instead')
    1947             :   Map<String, int>? get powerLevels {
    1948             :     final powerLevelState =
    1949           0 :         getState(EventTypes.RoomPowerLevels)?.content['users'];
    1950           0 :     return (powerLevelState is Map<String, int>) ? powerLevelState : null;
    1951             :   }
    1952             : 
    1953             :   /// Uploads a new user avatar for this room. Returns the event ID of the new
    1954             :   /// m.room.avatar event. Leave empty to remove the current avatar.
    1955           2 :   Future<String> setAvatar(MatrixFile? file) async {
    1956             :     final uploadResp = file == null
    1957             :         ? null
    1958           8 :         : await client.uploadContent(file.bytes, filename: file.name);
    1959           4 :     return await client.setRoomStateWithKey(
    1960           2 :       id,
    1961             :       EventTypes.RoomAvatar,
    1962             :       '',
    1963           2 :       {
    1964           4 :         if (uploadResp != null) 'url': uploadResp.toString(),
    1965             :       },
    1966             :     );
    1967             :   }
    1968             : 
    1969             :   /// The level required to ban a user.
    1970           4 :   bool get canBan =>
    1971           8 :       (getState(EventTypes.RoomPowerLevels)?.content.tryGet<int>('ban') ??
    1972           4 :           50) <=
    1973           4 :       ownPowerLevel;
    1974             : 
    1975             :   /// returns if user can change a particular state event by comparing `ownPowerLevel`
    1976             :   /// with possible overrides in `events`, if not present compares `ownPowerLevel`
    1977             :   /// with state_default
    1978           6 :   bool canChangeStateEvent(String action) {
    1979          18 :     return powerForChangingStateEvent(action) <= ownPowerLevel;
    1980             :   }
    1981             : 
    1982             :   /// returns the powerlevel required for changing the `action` defaults to
    1983             :   /// state_default if `action` isn't specified in events override.
    1984             :   /// If there is no state_default in the m.room.power_levels event, the
    1985             :   /// state_default is 50. If the room contains no m.room.power_levels event,
    1986             :   /// the state_default is 0.
    1987           6 :   int powerForChangingStateEvent(String action) {
    1988          10 :     final powerLevelMap = getState(EventTypes.RoomPowerLevels)?.content;
    1989             :     if (powerLevelMap == null) return 0;
    1990             :     return powerLevelMap
    1991           4 :             .tryGetMap<String, Object?>('events')
    1992           4 :             ?.tryGet<int>(action) ??
    1993           4 :         powerLevelMap.tryGet<int>('state_default') ??
    1994             :         50;
    1995             :   }
    1996             : 
    1997             :   /// if returned value is not null `EventTypes.GroupCallMember` is present
    1998             :   /// and group calls can be used
    1999           2 :   bool get groupCallsEnabledForEveryone {
    2000           4 :     final powerLevelMap = getState(EventTypes.RoomPowerLevels)?.content;
    2001             :     if (powerLevelMap == null) return false;
    2002           4 :     return powerForChangingStateEvent(EventTypes.GroupCallMember) <=
    2003           2 :         getDefaultPowerLevel(powerLevelMap);
    2004             :   }
    2005             : 
    2006           4 :   bool get canJoinGroupCall => canChangeStateEvent(EventTypes.GroupCallMember);
    2007             : 
    2008             :   /// sets the `EventTypes.GroupCallMember` power level to users default for
    2009             :   /// group calls, needs permissions to change power levels
    2010           2 :   Future<void> enableGroupCalls() async {
    2011           2 :     if (!canChangePowerLevel) return;
    2012           4 :     final currentPowerLevelsMap = getState(EventTypes.RoomPowerLevels)?.content;
    2013             :     if (currentPowerLevelsMap != null) {
    2014             :       final newPowerLevelMap = currentPowerLevelsMap;
    2015           2 :       final eventsMap = newPowerLevelMap.tryGetMap<String, Object?>('events') ??
    2016           2 :           <String, Object?>{};
    2017           4 :       eventsMap.addAll({
    2018           2 :         EventTypes.GroupCallMember: getDefaultPowerLevel(currentPowerLevelsMap),
    2019             :       });
    2020           4 :       newPowerLevelMap.addAll({'events': eventsMap});
    2021           4 :       await client.setRoomStateWithKey(
    2022           2 :         id,
    2023             :         EventTypes.RoomPowerLevels,
    2024             :         '',
    2025             :         newPowerLevelMap,
    2026             :       );
    2027             :     }
    2028             :   }
    2029             : 
    2030             :   /// Takes in `[m.room.power_levels].content` and returns the default power level
    2031           2 :   int getDefaultPowerLevel(Map<String, dynamic> powerLevelMap) {
    2032           2 :     return powerLevelMap.tryGet('users_default') ?? 0;
    2033             :   }
    2034             : 
    2035             :   /// The default level required to send message events. This checks if the
    2036             :   /// user is capable of sending `m.room.message` events.
    2037             :   /// Please be aware that this also returns false
    2038             :   /// if the room is encrypted but the client is not able to use encryption.
    2039             :   /// If you do not want this check or want to check other events like
    2040             :   /// `m.sticker` use `canSendEvent('<event-type>')`.
    2041           2 :   bool get canSendDefaultMessages {
    2042           2 :     if (encrypted && !client.encryptionEnabled) return false;
    2043             : 
    2044           4 :     return canSendEvent(encrypted ? EventTypes.Encrypted : EventTypes.Message);
    2045             :   }
    2046             : 
    2047             :   /// The level required to invite a user.
    2048           2 :   bool get canInvite =>
    2049           6 :       (getState(EventTypes.RoomPowerLevels)?.content.tryGet<int>('invite') ??
    2050           2 :           0) <=
    2051           2 :       ownPowerLevel;
    2052             : 
    2053             :   /// The level required to kick a user.
    2054           4 :   bool get canKick =>
    2055           8 :       (getState(EventTypes.RoomPowerLevels)?.content.tryGet<int>('kick') ??
    2056           4 :           50) <=
    2057           4 :       ownPowerLevel;
    2058             : 
    2059             :   /// The level required to redact an event.
    2060           2 :   bool get canRedact =>
    2061           6 :       (getState(EventTypes.RoomPowerLevels)?.content.tryGet<int>('redact') ??
    2062           2 :           50) <=
    2063           2 :       ownPowerLevel;
    2064             : 
    2065             :   ///   The default level required to send state events. Can be overridden by the events key.
    2066           0 :   bool get canSendDefaultStates {
    2067           0 :     final powerLevelsMap = getState(EventTypes.RoomPowerLevels)?.content;
    2068           0 :     if (powerLevelsMap == null) return 0 <= ownPowerLevel;
    2069           0 :     return (getState(EventTypes.RoomPowerLevels)
    2070           0 :                 ?.content
    2071           0 :                 .tryGet<int>('state_default') ??
    2072           0 :             50) <=
    2073           0 :         ownPowerLevel;
    2074             :   }
    2075             : 
    2076           6 :   bool get canChangePowerLevel =>
    2077           6 :       canChangeStateEvent(EventTypes.RoomPowerLevels);
    2078             : 
    2079             :   /// The level required to send a certain event. Defaults to 0 if there is no
    2080             :   /// events_default set or there is no power level state in the room.
    2081           2 :   bool canSendEvent(String eventType) {
    2082           4 :     final powerLevelsMap = getState(EventTypes.RoomPowerLevels)?.content;
    2083             : 
    2084             :     final pl = powerLevelsMap
    2085           2 :             ?.tryGetMap<String, Object?>('events')
    2086           2 :             ?.tryGet<int>(eventType) ??
    2087           2 :         powerLevelsMap?.tryGet<int>('events_default') ??
    2088             :         0;
    2089             : 
    2090           4 :     return ownPowerLevel >= pl;
    2091             :   }
    2092             : 
    2093             :   /// The power level requirements for specific notification types.
    2094           2 :   bool canSendNotification(String userid, {String notificationType = 'room'}) {
    2095           2 :     final userLevel = getPowerLevelByUserId(userid);
    2096           2 :     final notificationLevel = getState(EventTypes.RoomPowerLevels)
    2097           2 :             ?.content
    2098           2 :             .tryGetMap<String, Object?>('notifications')
    2099           2 :             ?.tryGet<int>(notificationType) ??
    2100             :         50;
    2101             : 
    2102           2 :     return userLevel >= notificationLevel;
    2103             :   }
    2104             : 
    2105             :   /// Returns the [PushRuleState] for this room, based on the m.push_rules stored in
    2106             :   /// the account_data.
    2107           2 :   PushRuleState get pushRuleState {
    2108           4 :     final globalPushRules = client.globalPushRules;
    2109             :     if (globalPushRules == null) {
    2110             :       // We have no push rules specified at all so we fallback to just notify:
    2111             :       return PushRuleState.notify;
    2112             :     }
    2113             : 
    2114           2 :     final overridePushRules = globalPushRules.override;
    2115             :     if (overridePushRules != null) {
    2116           4 :       for (final pushRule in overridePushRules) {
    2117           6 :         if (pushRule.ruleId == id) {
    2118             :           // "dont_notify" and "coalesce" should be ignored in actions since
    2119             :           // https://spec.matrix.org/v1.7/client-server-api/#actions
    2120           2 :           pushRule.actions
    2121           2 :             ..remove('dont_notify')
    2122           2 :             ..remove('coalesce');
    2123           4 :           if (pushRule.actions.isEmpty) {
    2124             :             return PushRuleState.dontNotify;
    2125             :           }
    2126             :           break;
    2127             :         }
    2128             :       }
    2129             :     }
    2130             : 
    2131           2 :     final roomPushRules = globalPushRules.room;
    2132             :     if (roomPushRules != null) {
    2133           4 :       for (final pushRule in roomPushRules) {
    2134           6 :         if (pushRule.ruleId == id) {
    2135             :           // "dont_notify" and "coalesce" should be ignored in actions since
    2136             :           // https://spec.matrix.org/v1.7/client-server-api/#actions
    2137           2 :           pushRule.actions
    2138           2 :             ..remove('dont_notify')
    2139           2 :             ..remove('coalesce');
    2140           4 :           if (pushRule.actions.isEmpty) {
    2141             :             return PushRuleState.mentionsOnly;
    2142             :           }
    2143             :           break;
    2144             :         }
    2145             :       }
    2146             :     }
    2147             : 
    2148             :     return PushRuleState.notify;
    2149             :   }
    2150             : 
    2151             :   /// Sends a request to the homeserver to set the [PushRuleState] for this room.
    2152             :   /// Returns ErrorResponse if something goes wrong.
    2153           2 :   Future<void> setPushRuleState(PushRuleState newState) async {
    2154           4 :     if (newState == pushRuleState) return;
    2155             :     dynamic resp;
    2156             :     switch (newState) {
    2157             :       // All push notifications should be sent to the user
    2158           2 :       case PushRuleState.notify:
    2159           4 :         if (pushRuleState == PushRuleState.dontNotify) {
    2160           6 :           await client.deletePushRule(PushRuleKind.override, id);
    2161           0 :         } else if (pushRuleState == PushRuleState.mentionsOnly) {
    2162           0 :           await client.deletePushRule(PushRuleKind.room, id);
    2163             :         }
    2164             :         break;
    2165             :       // Only when someone mentions the user, a push notification should be sent
    2166           2 :       case PushRuleState.mentionsOnly:
    2167           4 :         if (pushRuleState == PushRuleState.dontNotify) {
    2168           6 :           await client.deletePushRule(PushRuleKind.override, id);
    2169           4 :           await client.setPushRule(
    2170             :             PushRuleKind.room,
    2171           2 :             id,
    2172           2 :             [],
    2173             :           );
    2174           0 :         } else if (pushRuleState == PushRuleState.notify) {
    2175           0 :           await client.setPushRule(
    2176             :             PushRuleKind.room,
    2177           0 :             id,
    2178           0 :             [],
    2179             :           );
    2180             :         }
    2181             :         break;
    2182             :       // No push notification should be ever sent for this room.
    2183           0 :       case PushRuleState.dontNotify:
    2184           0 :         if (pushRuleState == PushRuleState.mentionsOnly) {
    2185           0 :           await client.deletePushRule(PushRuleKind.room, id);
    2186             :         }
    2187           0 :         await client.setPushRule(
    2188             :           PushRuleKind.override,
    2189           0 :           id,
    2190           0 :           [],
    2191           0 :           conditions: [
    2192           0 :             PushCondition(
    2193           0 :               kind: PushRuleConditions.eventMatch.name,
    2194             :               key: 'room_id',
    2195           0 :               pattern: id,
    2196             :             ),
    2197             :           ],
    2198             :         );
    2199             :     }
    2200             :     return resp;
    2201             :   }
    2202             : 
    2203             :   /// Redacts this event. Throws `ErrorResponse` on error.
    2204           1 :   Future<String?> redactEvent(
    2205             :     String eventId, {
    2206             :     String? reason,
    2207             :     String? txid,
    2208             :   }) async {
    2209             :     // Create new transaction id
    2210             :     String messageID;
    2211           2 :     final now = DateTime.now().millisecondsSinceEpoch;
    2212             :     if (txid == null) {
    2213           0 :       messageID = 'msg$now';
    2214             :     } else {
    2215             :       messageID = txid;
    2216             :     }
    2217           1 :     final data = <String, dynamic>{};
    2218           1 :     if (reason != null) data['reason'] = reason;
    2219           2 :     return await client.redactEvent(
    2220           1 :       id,
    2221             :       eventId,
    2222             :       messageID,
    2223             :       reason: reason,
    2224             :     );
    2225             :   }
    2226             : 
    2227             :   /// This tells the server that the user is typing for the next N milliseconds
    2228             :   /// where N is the value specified in the timeout key. Alternatively, if typing is false,
    2229             :   /// it tells the server that the user has stopped typing.
    2230           0 :   Future<void> setTyping(bool isTyping, {int? timeout}) =>
    2231           0 :       client.setTyping(client.userID!, id, isTyping, timeout: timeout);
    2232             : 
    2233             :   /// A room may be public meaning anyone can join the room without any prior action. Alternatively,
    2234             :   /// it can be invite meaning that a user who wishes to join the room must first receive an invite
    2235             :   /// to the room from someone already inside of the room. Currently, knock and private are reserved
    2236             :   /// keywords which are not implemented.
    2237           2 :   JoinRules? get joinRules {
    2238             :     final joinRulesString =
    2239           6 :         getState(EventTypes.RoomJoinRules)?.content.tryGet<String>('join_rule');
    2240             :     return JoinRules.values
    2241           8 :         .singleWhereOrNull((element) => element.text == joinRulesString);
    2242             :   }
    2243             : 
    2244             :   /// Changes the join rules. You should check first if the user is able to change it.
    2245           2 :   Future<void> setJoinRules(JoinRules joinRules) async {
    2246           4 :     await client.setRoomStateWithKey(
    2247           2 :       id,
    2248             :       EventTypes.RoomJoinRules,
    2249             :       '',
    2250           2 :       {
    2251           4 :         'join_rule': joinRules.toString().replaceAll('JoinRules.', ''),
    2252             :       },
    2253             :     );
    2254             :     return;
    2255             :   }
    2256             : 
    2257             :   /// Whether the user has the permission to change the join rules.
    2258           4 :   bool get canChangeJoinRules => canChangeStateEvent(EventTypes.RoomJoinRules);
    2259             : 
    2260             :   /// This event controls whether guest users are allowed to join rooms. If this event
    2261             :   /// is absent, servers should act as if it is present and has the guest_access value "forbidden".
    2262           2 :   GuestAccess get guestAccess {
    2263           2 :     final guestAccessString = getState(EventTypes.GuestAccess)
    2264           2 :         ?.content
    2265           2 :         .tryGet<String>('guest_access');
    2266           2 :     return GuestAccess.values.singleWhereOrNull(
    2267           6 :           (element) => element.text == guestAccessString,
    2268             :         ) ??
    2269             :         GuestAccess.forbidden;
    2270             :   }
    2271             : 
    2272             :   /// Changes the guest access. You should check first if the user is able to change it.
    2273           2 :   Future<void> setGuestAccess(GuestAccess guestAccess) async {
    2274           4 :     await client.setRoomStateWithKey(
    2275           2 :       id,
    2276             :       EventTypes.GuestAccess,
    2277             :       '',
    2278           2 :       {
    2279           2 :         'guest_access': guestAccess.text,
    2280             :       },
    2281             :     );
    2282             :     return;
    2283             :   }
    2284             : 
    2285             :   /// Whether the user has the permission to change the guest access.
    2286           4 :   bool get canChangeGuestAccess => canChangeStateEvent(EventTypes.GuestAccess);
    2287             : 
    2288             :   /// This event controls whether a user can see the events that happened in a room from before they joined.
    2289           2 :   HistoryVisibility? get historyVisibility {
    2290           2 :     final historyVisibilityString = getState(EventTypes.HistoryVisibility)
    2291           2 :         ?.content
    2292           2 :         .tryGet<String>('history_visibility');
    2293           2 :     return HistoryVisibility.values.singleWhereOrNull(
    2294           6 :       (element) => element.text == historyVisibilityString,
    2295             :     );
    2296             :   }
    2297             : 
    2298             :   /// Changes the history visibility. You should check first if the user is able to change it.
    2299           2 :   Future<void> setHistoryVisibility(HistoryVisibility historyVisibility) async {
    2300           4 :     await client.setRoomStateWithKey(
    2301           2 :       id,
    2302             :       EventTypes.HistoryVisibility,
    2303             :       '',
    2304           2 :       {
    2305           2 :         'history_visibility': historyVisibility.text,
    2306             :       },
    2307             :     );
    2308             :     return;
    2309             :   }
    2310             : 
    2311             :   /// Whether the user has the permission to change the history visibility.
    2312           2 :   bool get canChangeHistoryVisibility =>
    2313           2 :       canChangeStateEvent(EventTypes.HistoryVisibility);
    2314             : 
    2315             :   /// Returns the encryption algorithm. Currently only `m.megolm.v1.aes-sha2` is supported.
    2316             :   /// Returns null if there is no encryption algorithm.
    2317          35 :   String? get encryptionAlgorithm =>
    2318         101 :       getState(EventTypes.Encryption)?.parsedRoomEncryptionContent.algorithm;
    2319             : 
    2320             :   /// Checks if this room is encrypted.
    2321          70 :   bool get encrypted => encryptionAlgorithm != null;
    2322             : 
    2323           2 :   Future<void> enableEncryption({int algorithmIndex = 0}) async {
    2324           2 :     if (encrypted) throw ('Encryption is already enabled!');
    2325           2 :     final algorithm = Client.supportedGroupEncryptionAlgorithms[algorithmIndex];
    2326           4 :     await client.setRoomStateWithKey(
    2327           2 :       id,
    2328             :       EventTypes.Encryption,
    2329             :       '',
    2330           2 :       {
    2331             :         'algorithm': algorithm,
    2332             :       },
    2333             :     );
    2334             :     return;
    2335             :   }
    2336             : 
    2337             :   /// Returns all known device keys for all participants in this room.
    2338           7 :   Future<List<DeviceKeys>> getUserDeviceKeys() async {
    2339          14 :     await client.userDeviceKeysLoading;
    2340           7 :     final deviceKeys = <DeviceKeys>[];
    2341           7 :     final users = await requestParticipants();
    2342          11 :     for (final user in users) {
    2343          24 :       final userDeviceKeys = client.userDeviceKeys[user.id]?.deviceKeys.values;
    2344          12 :       if ([Membership.invite, Membership.join].contains(user.membership) &&
    2345             :           userDeviceKeys != null) {
    2346           8 :         for (final deviceKeyEntry in userDeviceKeys) {
    2347           4 :           deviceKeys.add(deviceKeyEntry);
    2348             :         }
    2349             :       }
    2350             :     }
    2351             :     return deviceKeys;
    2352             :   }
    2353             : 
    2354           1 :   Future<void> requestSessionKey(String sessionId, String senderKey) async {
    2355           2 :     if (!client.encryptionEnabled) {
    2356             :       return;
    2357             :     }
    2358           4 :     await client.encryption?.keyManager.request(this, sessionId, senderKey);
    2359             :   }
    2360             : 
    2361          11 :   Future<void> _handleFakeSync(
    2362             :     SyncUpdate syncUpdate, {
    2363             :     Direction? direction,
    2364             :   }) async {
    2365          22 :     if (client.database != null) {
    2366          36 :       await client.database?.transaction(() async {
    2367          18 :         await client.handleSync(syncUpdate, direction: direction);
    2368             :       });
    2369             :     } else {
    2370           4 :       await client.handleSync(syncUpdate, direction: direction);
    2371             :     }
    2372             :   }
    2373             : 
    2374             :   /// Whether this is an extinct room which has been archived in favor of a new
    2375             :   /// room which replaces this. Use `getLegacyRoomInformations()` to get more
    2376             :   /// informations about it if this is true.
    2377           0 :   bool get isExtinct => getState(EventTypes.RoomTombstone) != null;
    2378             : 
    2379             :   /// Returns informations about how this room is
    2380           0 :   TombstoneContent? get extinctInformations =>
    2381           0 :       getState(EventTypes.RoomTombstone)?.parsedTombstoneContent;
    2382             : 
    2383             :   /// Checks if the `m.room.create` state has a `type` key with the value
    2384             :   /// `m.space`.
    2385           2 :   bool get isSpace =>
    2386           8 :       getState(EventTypes.RoomCreate)?.content.tryGet<String>('type') ==
    2387             :       RoomCreationTypes.mSpace;
    2388             : 
    2389             :   /// The parents of this room. Currently this SDK doesn't yet set the canonical
    2390             :   /// flag and is not checking if this room is in fact a child of this space.
    2391             :   /// You should therefore not rely on this and always check the children of
    2392             :   /// the space.
    2393           2 :   List<SpaceParent> get spaceParents =>
    2394           4 :       states[EventTypes.SpaceParent]
    2395           2 :           ?.values
    2396           6 :           .map((state) => SpaceParent.fromState(state))
    2397           8 :           .where((child) => child.via.isNotEmpty)
    2398           2 :           .toList() ??
    2399           2 :       [];
    2400             : 
    2401             :   /// List all children of this space. Children without a `via` domain will be
    2402             :   /// ignored.
    2403             :   /// Children are sorted by the `order` while those without this field will be
    2404             :   /// sorted at the end of the list.
    2405           4 :   List<SpaceChild> get spaceChildren => !isSpace
    2406           0 :       ? throw Exception('Room is not a space!')
    2407           4 :       : (states[EventTypes.SpaceChild]
    2408           2 :               ?.values
    2409           6 :               .map((state) => SpaceChild.fromState(state))
    2410           8 :               .where((child) => child.via.isNotEmpty)
    2411           2 :               .toList() ??
    2412           2 :           [])
    2413           2 :     ..sort(
    2414          10 :       (a, b) => a.order.isEmpty || b.order.isEmpty
    2415           6 :           ? b.order.compareTo(a.order)
    2416           6 :           : a.order.compareTo(b.order),
    2417             :     );
    2418             : 
    2419             :   /// Adds or edits a child of this space.
    2420           0 :   Future<void> setSpaceChild(
    2421             :     String roomId, {
    2422             :     List<String>? via,
    2423             :     String? order,
    2424             :     bool? suggested,
    2425             :   }) async {
    2426           0 :     if (!isSpace) throw Exception('Room is not a space!');
    2427           0 :     via ??= [client.userID!.domain!];
    2428           0 :     await client.setRoomStateWithKey(id, EventTypes.SpaceChild, roomId, {
    2429           0 :       'via': via,
    2430           0 :       if (order != null) 'order': order,
    2431           0 :       if (suggested != null) 'suggested': suggested,
    2432             :     });
    2433           0 :     await client.setRoomStateWithKey(roomId, EventTypes.SpaceParent, id, {
    2434             :       'via': via,
    2435             :     });
    2436             :     return;
    2437             :   }
    2438             : 
    2439             :   /// Generates a matrix.to link with appropriate routing info to share the room
    2440           2 :   Future<Uri> matrixToInviteLink() async {
    2441           4 :     if (canonicalAlias.isNotEmpty) {
    2442           2 :       return Uri.parse(
    2443           6 :         'https://matrix.to/#/${Uri.encodeComponent(canonicalAlias)}',
    2444             :       );
    2445             :     }
    2446           2 :     final List queryParameters = [];
    2447           4 :     final users = await requestParticipants([Membership.join]);
    2448           4 :     final currentPowerLevelsMap = getState(EventTypes.RoomPowerLevels)?.content;
    2449             : 
    2450           2 :     final temp = List<User>.from(users);
    2451           8 :     temp.removeWhere((user) => user.powerLevel < 50);
    2452             :     if (currentPowerLevelsMap != null) {
    2453             :       // just for weird rooms
    2454           2 :       temp.removeWhere(
    2455           0 :         (user) => user.powerLevel < getDefaultPowerLevel(currentPowerLevelsMap),
    2456             :       );
    2457             :     }
    2458             : 
    2459           2 :     if (temp.isNotEmpty) {
    2460           0 :       temp.sort((a, b) => a.powerLevel.compareTo(b.powerLevel));
    2461           0 :       if (temp.last.id.domain != null) {
    2462           0 :         queryParameters.add(temp.last.id.domain!);
    2463             :       }
    2464             :     }
    2465             : 
    2466           2 :     final Map<String, int> servers = {};
    2467           4 :     for (final user in users) {
    2468           4 :       if (user.id.domain != null) {
    2469           6 :         if (servers.containsKey(user.id.domain!)) {
    2470           0 :           servers[user.id.domain!] = servers[user.id.domain!]! + 1;
    2471             :         } else {
    2472           6 :           servers[user.id.domain!] = 1;
    2473             :         }
    2474             :       }
    2475             :     }
    2476           2 :     final sortedServers = Map.fromEntries(
    2477          14 :       servers.entries.toList()..sort((e1, e2) => e2.value.compareTo(e1.value)),
    2478           4 :     ).keys.take(3);
    2479           4 :     for (final server in sortedServers) {
    2480           2 :       if (!queryParameters.contains(server)) {
    2481           2 :         queryParameters.add(server);
    2482             :       }
    2483             :     }
    2484             : 
    2485             :     var queryString = '?';
    2486           8 :     for (var i = 0; i < min(queryParameters.length, 3); i++) {
    2487           2 :       if (i != 0) {
    2488           2 :         queryString += '&';
    2489             :       }
    2490           6 :       queryString += 'via=${queryParameters[i]}';
    2491             :     }
    2492           2 :     return Uri.parse(
    2493           6 :       'https://matrix.to/#/${Uri.encodeComponent(id)}$queryString',
    2494             :     );
    2495             :   }
    2496             : 
    2497             :   /// Remove a child from this space by setting the `via` to an empty list.
    2498           0 :   Future<void> removeSpaceChild(String roomId) => !isSpace
    2499           0 :       ? throw Exception('Room is not a space!')
    2500           0 :       : setSpaceChild(roomId, via: const []);
    2501             : 
    2502           1 :   @override
    2503           4 :   bool operator ==(Object other) => (other is Room && other.id == id);
    2504             : 
    2505           0 :   @override
    2506           0 :   int get hashCode => Object.hashAll([id]);
    2507             : }
    2508             : 
    2509             : enum EncryptionHealthState {
    2510             :   allVerified,
    2511             :   unverifiedDevices,
    2512             : }

Generated by: LCOV version 1.14