Skip to content

Commit 8d4069e

Browse files
Working RemoveDeviceFromDatabase
1 parent 9d38792 commit 8d4069e

File tree

1 file changed

+68
-5
lines changed

1 file changed

+68
-5
lines changed

fractal_database_matrix/operations.py

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ async def user_leave_room(
311311
) as client:
312312
res = await client.room_leave(room_id)
313313
if isinstance(res, RoomLeaveError):
314+
if "not in room" in res.message:
315+
return None
314316
raise Exception(res.message)
315317

316318

@@ -1313,7 +1315,6 @@ def create_durable_operations(
13131315
"""
13141316
from fractal_database.models import DurableOperation
13151317

1316-
# remove the user from the main space
13171318
DurableOperation.objects.create(
13181319
instance=instance,
13191320
module=cls.operation_module(),
@@ -1361,6 +1362,36 @@ async def run(self, operation: "DurableOperation") -> None:
13611362
return None
13621363

13631364

1365+
class RemoveUserFromDatabase(RemoveUserFromRoom):
1366+
@classmethod
1367+
def create_durable_operations(
1368+
cls,
1369+
instance: "DatabaseMembership",
1370+
channel: "ReplicationChannel",
1371+
):
1372+
"""
1373+
Create the optional operations (tasks) for removing a user from a Matrix Database
1374+
"""
1375+
from fractal_database.models import DurableOperation
1376+
1377+
# create operations to remove the user from all of the rooms on the channel
1378+
for room_id_label in channel.metadata.keys():
1379+
DurableOperation.objects.create(
1380+
instance=instance,
1381+
module=RemoveUserFromRoom.operation_module(),
1382+
channel=channel,
1383+
metadata={"room_id_label": room_id_label},
1384+
)
1385+
1386+
return None
1387+
1388+
async def run(self, operation: "DurableOperation") -> None:
1389+
# this operation needs to simply create the operations to remove the user from the rooms
1390+
# those will individually handle the actual removal
1391+
# this does not need to do anything else
1392+
return None
1393+
1394+
13641395
class RemoveDeviceFromRoom(RemoveUserFromRoom):
13651396
async def run(self, operation: "DurableOperation") -> None:
13661397
try:
@@ -1369,8 +1400,10 @@ async def run(self, operation: "DurableOperation") -> None:
13691400
raise Exception("room_id_label must be specified in operation metadata")
13701401

13711402
model_class = operation.content_type.model_class() # type: ignore
1372-
membership: "DeviceMembership" = await model_class.objects.select_related("device").aget(
1373-
pk=operation.object_id
1403+
membership: "DeviceMembership" = (
1404+
await model_class.objects.select_related("device")
1405+
.prefetch_related("device__matrixcredentials_set")
1406+
.aget(pk=operation.object_id)
13741407
) # type: ignore
13751408

13761409
# fetch channel in order to get room_id for the group to invite user to
@@ -1380,9 +1413,9 @@ async def run(self, operation: "DurableOperation") -> None:
13801413
.aget(pk=operation.channel_id)
13811414
) # type: ignore
13821415

1383-
device_creds = membership.device.matrixcredentials_set.filter(
1416+
device_creds = await membership.device.matrixcredentials_set.filter(
13841417
homeserver=channel.homeserver
1385-
).first()
1418+
).afirst()
13861419
if not device_creds:
13871420
# FIXME: Determine if logged in user is an admin in the room. If so, they can "kick" the device (only works if device is not admin)
13881421
raise Exception(
@@ -1406,6 +1439,36 @@ async def run(self, operation: "DurableOperation") -> None:
14061439
return None
14071440

14081441

1442+
class RemoveDeviceFromDatabase(RemoveDeviceFromRoom):
1443+
@classmethod
1444+
def create_durable_operations(
1445+
cls,
1446+
instance: "DatabaseMembership",
1447+
channel: "ReplicationChannel",
1448+
):
1449+
"""
1450+
Create the optional operations (tasks) for removing a device from a Matrix Database
1451+
"""
1452+
from fractal_database.models import DurableOperation
1453+
1454+
# create the operations to remove the device from all of the rooms on the channel
1455+
for room_id_label in channel.metadata.keys():
1456+
DurableOperation.objects.create(
1457+
instance=instance,
1458+
module=RemoveDeviceFromRoom.operation_module(),
1459+
channel=channel,
1460+
metadata={"room_id_label": room_id_label},
1461+
)
1462+
1463+
return None
1464+
1465+
async def run(self, operation: "DurableOperation") -> None:
1466+
# this operation needs to simply create the operations to remove the device from the rooms
1467+
# those operations will individually handle the actual removal
1468+
# this does not need to do anything else
1469+
return None
1470+
1471+
14091472
class InviteDatabaseMemberToSpace(MatrixOperation):
14101473
@classmethod
14111474
def create_durable_operations(

0 commit comments

Comments
 (0)