Skip to content

Commit ad3a29c

Browse files
committed
Fix room deletion when a room has reactions
Room deletion wasn't working because there isn't a cascade on the foreign key from user_reactions -> reactions, and so deleting a room with reactions present would fail. This first removes all the user_reactions to fix it. (This lack of cascading delete here is intentional: typically, you aren't supposed to delete from the reactions end, but rather from the user_reactions end with removal of a `reaction` being automatically triggered once there are no more referencing user_reactions). Includes a test (which is confirmed broken without the fix included here).
1 parent 6cea9f7 commit ad3a29c

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

sogs/model/room.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,18 @@ def delete(self):
183183
184184
This method does not authenticate.
185185
"""
186-
result = query("DELETE FROM rooms WHERE token = :t", t=self.token)
186+
187+
with db.transaction():
188+
query(
189+
"""
190+
DELETE FROM user_reactions WHERE reaction IN (
191+
SELECT id FROM reactions WHERE message IN (
192+
SELECT id FROM messages WHERE room = (
193+
SELECT id FROM rooms WHERE token = :t)))
194+
""",
195+
t=self.token,
196+
)
197+
result = query("DELETE FROM rooms WHERE token = :t", t=self.token)
187198
if result.rowcount != 1:
188199
raise NoSuchRoom(self.token)
189200

tests/test_rooms.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from sogs.model.room import Room, get_rooms
55
from sogs.model.file import File
66
from sogs import config
7+
from request import sogs_put
78
from util import pad64, from_now
89

910

@@ -64,6 +65,20 @@ def test_delete(room, room2):
6465
assert rooms[0].token == 'test-room'
6566

6667

68+
def test_delete_populated(room, room2, user, client):
69+
assert len(get_rooms()) == 2
70+
71+
# Tests a bug where room deletion would fail if the room had reactions
72+
m = room.add_post(user, "data 1".encode(), pad64("sig 1"))
73+
r = sogs_put(client, f"/room/{room.token}/reaction/{m['id']}/🍆", {}, user)
74+
assert r.status_code == 200
75+
room.delete()
76+
77+
rooms = get_rooms()
78+
assert len(rooms) == 1
79+
assert rooms[0].token == 'room2'
80+
81+
6782
def test_info(room):
6883
inf = room.info
6984
assert inf['id'] == 1

0 commit comments

Comments
 (0)