From fec59447afa6117abe924013b785acf6a7b86668 Mon Sep 17 00:00:00 2001 From: Vincent Michel Date: Mon, 6 Jan 2025 18:36:44 +0100 Subject: [PATCH] Improve _q_list_invitations filtering --- server/parsec/components/postgresql/invite.py | 19 ++++++++++--------- server/tests/common/postgresql.py | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/server/parsec/components/postgresql/invite.py b/server/parsec/components/postgresql/invite.py index 671e9dc4a4d..bdd876c959d 100644 --- a/server/parsec/components/postgresql/invite.py +++ b/server/parsec/components/postgresql/invite.py @@ -379,7 +379,8 @@ class ShamirRecoverySetupInfo: _q_list_invitations = Q( f""" SELECT - invitation._id AS invitation_internal_id, + -- Use DISTINCT to avoid duplicates due to multiple shamir_recovery_share rows + DISTINCT invitation._id AS invitation_internal_id, invitation.token, invitation.type, user_.user_id AS created_by_user_id, @@ -397,9 +398,16 @@ class ShamirRecoverySetupInfo: LEFT JOIN user_ ON device.user_ = user_._id LEFT JOIN human ON human._id = user_.human LEFT JOIN shamir_recovery_setup ON invitation.shamir_recovery = shamir_recovery_setup._id +LEFT JOIN shamir_recovery_share ON shamir_recovery_share.shamir_recovery = shamir_recovery_setup._id +LEFT JOIN user_ AS recipient_user_ ON shamir_recovery_share.recipient = recipient_user_._id WHERE invitation.organization = { q_organization_internal_id("$organization_id") } - AND (user_.user_id = $user_id or invitation.type = 'SHAMIR_RECOVERY') + -- Different invitation types have different filtering rules + AND ( + (invitation.type = 'USER' AND user_.user_id = $user_id) + OR (invitation.type = 'DEVICE' AND user_.user_id = $user_id) + OR (invitation.type = 'SHAMIR_RECOVERY' AND recipient_user_.user_id = $user_id) + ) ORDER BY created_on """ ) @@ -1264,13 +1272,6 @@ async def list( conn, invitation_info.shamir_recovery_setup ) - # The author is not part of the recipients - if not any( - recipient.user_id == author_user_id - for recipient in shamir_recovery_info.recipients - ): - continue - invitation = ShamirRecoveryInvitation( created_by_user_id=invitation_info.created_by_user_id, created_by_device_id=invitation_info.created_by_device_id, diff --git a/server/tests/common/postgresql.py b/server/tests/common/postgresql.py index 8630fbcbcb7..0b9b9228f8c 100644 --- a/server/tests/common/postgresql.py +++ b/server/tests/common/postgresql.py @@ -107,7 +107,7 @@ async def init_db(): SETVAL(PG_GET_SERIAL_SEQUENCE('block_data', '_id'), 23000), SETVAL(PG_GET_SERIAL_SEQUENCE('common_topic', '_id'), 24000), SETVAL(PG_GET_SERIAL_SEQUENCE('sequester_topic', '_id'), 25000), - SETVAL(PG_GET_SERIAL_SEQUENCE('shamir_recovery_topic', '_id'), 2600), + SETVAL(PG_GET_SERIAL_SEQUENCE('shamir_recovery_topic', '_id'), 26000), SETVAL(PG_GET_SERIAL_SEQUENCE('realm_topic', '_id'), 27000) ; """