Skip to content

Commit 27417a4

Browse files
committed
Require either sender or recipient for deletion
Otherwise passing in None (or calling without arguments) deletes all messages for all users, which is unexpected/undesirable.
1 parent f478a1b commit 27417a4

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

sogs/model/message.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,12 @@ def delete_all(*, recip=None, sender=None):
4747
"""Delete all messages sent to a user or from a user.
4848
returns the number of rows affected.
4949
"""
50-
if sender and recip:
51-
raise ValueError("delete_all(): cannot provide both sender and recipient")
50+
if sum(bool(x) for x in (sender, recip)) != 1:
51+
raise ValueError("delete_all(): exactly one of sender or recipient is required")
5252

53-
recip_id = recip.id if recip else None
54-
sender_id = sender.id if sender else None
5553
result = query(
56-
f"""
57-
DELETE FROM inbox
58-
{'WHERE recipient = :recip' if recip else '' }
59-
{'WHERE sender = :sender' if sender else '' }
60-
""",
61-
recip=recip_id,
62-
sender=sender_id,
54+
f"DELETE FROM inbox WHERE {'recipient' if recip else 'sender'} = :id",
55+
id=recip.id if recip else sender.id,
6356
)
6457
return result.rowcount
6558

0 commit comments

Comments
 (0)