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