Commit 22767e73 authored by Lars Kruse's avatar Lars Kruse
Browse files

feat(matrix_chat): kick member from group's rooms after leaving the group

parent ffe3f5f3
......@@ -156,3 +156,27 @@ class MatrixBot:
f"Invite request for {gestalt} into {room} was rejected: {result}"
async def kick_gestalt_from_group_rooms(self, group, gestalt, reason=None):
gestalt_matrix_id = MatrixChatGestaltSettings.get_matrix_id(gestalt)
for room in MatrixChatGroupRoom.objects.filter(group=group):
result = await self.client.room_kick(
room.room_id, gestalt_matrix_id, reason=reason
except nio.exceptions.ProtocolError as exc:
logger.warning(f"Failed to kick {gestalt} from {room}: {exc}")
if isinstance(result, nio.responses.RoomKickResponse) or (
isinstance(result, nio.responses.RoomKickError)
and (result.status_code == "M_FORBIDDEN")
# "forbidden" indicates that we were not a member of the room.
# Discard invitations, anyway.
room=room, gestalt=gestalt
f"Kick request for {gestalt} out of {room} was rejected: {result}"
import asyncio
import logging
from django.db.models.signals import post_save
from django.db.models.signals import post_delete, post_save
from django.dispatch import receiver
from huey.contrib.djhuey import db_task
......@@ -134,3 +134,22 @@ def _invite_to_group_rooms(group):
@receiver(post_delete, sender=grouprise.features.memberships.models.Membership)
def kick_room_members_after_leaving_group(sender, instance, **kwargs):
_kick_gestalt_from_group_rooms(, instance.member)
def _kick_gestalt_from_group_rooms(group, gestalt):
async def _kick_gestalt_from_group_rooms_delayed(group, gestalt):
async with MatrixBot() as bot:
await bot.kick_gestalt_from_group_rooms(group, gestalt)
except MatrixError as exc:
f"Failed to kick previous group members ({group}) from matrix rooms: {exc}"
), gestalt))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment