Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Apply depth checks to simply query.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Oct 13, 2022
1 parent 7648e76 commit 39266c3
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions synapse/storage/databases/main/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,18 +963,22 @@ async def get_thread_id(self, event_id: str) -> str:
# results from the below query.
sql = """
WITH RECURSIVE related_events AS (
SELECT event_id, relates_to_id, relation_type
SELECT event_id, relates_to_id, relation_type, 0 depth
FROM event_relations
WHERE event_id = ?
UNION SELECT e.event_id, e.relates_to_id, e.relation_type
UNION SELECT e.event_id, e.relates_to_id, e.relation_type, depth + 1
FROM event_relations e
INNER JOIN related_events r ON r.relates_to_id = e.event_id
) SELECT relates_to_id FROM related_events WHERE relation_type = 'm.thread';
WHERE depth <= 3
)
SELECT relates_to_id FROM related_events
WHERE relation_type = 'm.thread'
ORDER BY depth DESC
LIMIT 1;
"""

def _get_thread_id(txn: LoggingTransaction) -> str:
txn.execute(sql, (event_id,))
# TODO Should we ensure there's only a single result here?
row = txn.fetchone()
if row:
return row[0]
Expand Down Expand Up @@ -1020,7 +1024,10 @@ async def get_thread_id_for_receipts(self, event_id: str) -> str:
FROM event_relations e
INNER JOIN related_events r ON r.relates_to_id = e.event_id
WHERE depth <= 3
) SELECT relates_to_id FROM related_events ORDER BY depth DESC LIMIT 1
)
SELECT relates_to_id FROM related_events
ORDER BY depth DESC
LIMIT 1
), ?) AND relation_type = 'm.thread' LIMIT 1;
"""

Expand Down

0 comments on commit 39266c3

Please sign in to comment.