Skip to content

Commit

Permalink
Improve _q_list_invitations filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
vxgmichel committed Jan 7, 2025
1 parent 330be69 commit fec5944
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
19 changes: 10 additions & 9 deletions server/parsec/components/postgresql/invite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
"""
)
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion server/tests/common/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
;
"""
Expand Down

0 comments on commit fec5944

Please sign in to comment.