Merge pull request #2150 from famedly/krille/leave-rooms-on-ignore-user

feat: Leave DM rooms and invite when ignoring a user
This commit is contained in:
Krille-chan 2025-09-29 09:44:11 +02:00 committed by GitHub
commit a44dfbe58f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 2 deletions

View File

@ -3771,10 +3771,30 @@ class Client extends MatrixApi {
/// Ignore another user. This will clear the local cached messages to /// Ignore another user. This will clear the local cached messages to
/// hide all previous messages from this user. /// hide all previous messages from this user.
Future<void> ignoreUser(String userId) async { Future<void> ignoreUser(
String userId, {
/// Whether to also decline all invites and leave DM rooms with this user.
bool leaveRooms = true,
}) async {
if (!userId.isValidMatrixId) { if (!userId.isValidMatrixId) {
throw Exception('$userId is not a valid mxid!'); throw Exception('$userId is not a valid mxid!');
} }
if (leaveRooms) {
for (final room in rooms) {
final isInviteFromUser = room.membership == Membership.invite &&
room.getState(EventTypes.RoomMember, userID!)?.senderId == userId;
if (room.directChatMatrixID == userId || isInviteFromUser) {
try {
await room.leave();
} catch (e, s) {
Logs().w('Unable to leave room with blocked user $userId', e, s);
}
}
}
}
await setAccountData(userID!, 'm.ignored_user_list', { await setAccountData(userID!, 'm.ignored_user_list', {
'ignored_users': Map.fromEntries( 'ignored_users': Map.fromEntries(
(ignoredUsers..add(userId)).map((key) => MapEntry(key, {})), (ignoredUsers..add(userId)).map((key) => MapEntry(key, {})),

View File

@ -1181,7 +1181,7 @@ class Event extends MatrixEvent {
); );
} }
/// Returns the mentioned userIds and wether the event includes an @room /// Returns the mentioned userIds and whether the event includes an @room
/// mention. This is only determined by the `m.mention` object in the event /// mention. This is only determined by the `m.mention` object in the event
/// content. /// content.
({List<String> userIds, bool room}) get mentions { ({List<String> userIds, bool room}) get mentions {