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

Commit

Permalink
Include if the current user participated in the thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Dec 14, 2021
1 parent dbe2173 commit 49339a5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
4 changes: 3 additions & 1 deletion synapse/events/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,8 @@ async def _injected_bundled_aggregations(
(
thread_count,
latest_thread_event,
) = await self.store.get_thread_summary(event_id, room_id)
participated,
) = await self.store.get_thread_summary(event_id, room_id, user_id)
if latest_thread_event:
aggregations[RelationTypes.THREAD] = {
# Don't bundle aggregations as this could recurse forever.
Expand All @@ -521,6 +522,7 @@ async def _injected_bundled_aggregations(
bundle_aggregations=False,
),
"count": thread_count,
"current_user_participated": participated,
}

# If any bundled aggregations were found, include them.
Expand Down
34 changes: 26 additions & 8 deletions synapse/storage/databases/main/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,23 +352,24 @@ def _get_applicable_edit_txn(txn: LoggingTransaction) -> Optional[str]:

@cached()
async def get_thread_summary(
self, event_id: str, room_id: str
) -> Tuple[int, Optional[EventBase]]:
self, event_id: str, room_id: str, user_id: str
) -> Tuple[int, Optional[EventBase], bool]:
"""Get the number of threaded replies, the senders of those replies, and
the latest reply (if any) for the given event.
Args:
event_id: Summarize the thread related to this event ID.
room_id: The room the event belongs to.
user_id: The user requesting the summary.
Returns:
The number of items in the thread and the most recent response, if any.
"""

def _get_thread_summary_txn(
txn: LoggingTransaction,
) -> Tuple[int, Optional[str]]:
# Fetch the count of threaded events and the latest event ID.
) -> Tuple[int, Optional[str], bool]:
# Fetch the latest event ID in the thread.
# TODO Should this only allow m.room.message events.
sql = """
SELECT event_id
Expand All @@ -385,10 +386,11 @@ def _get_thread_summary_txn(
txn.execute(sql, (event_id, room_id, RelationTypes.THREAD))
row = txn.fetchone()
if row is None:
return 0, None
return 0, None, False

latest_event_id = row[0]

# Fetch the number of threaded replies.
sql = """
SELECT COUNT(event_id)
FROM event_relations
Expand All @@ -401,17 +403,33 @@ def _get_thread_summary_txn(
txn.execute(sql, (event_id, room_id, RelationTypes.THREAD))
count = txn.fetchone()[0] # type: ignore[index]

return count, latest_event_id
# Fetch whether the requester has participated or not.
sql = """
SELECT 1
FROM event_relations
INNER JOIN events USING (event_id)
WHERE
relates_to_id = ?
AND room_id = ?
AND relation_type = ?
AND sender = ?
"""

txn.execute(sql, (event_id, room_id, RelationTypes.THREAD, user_id))
row = txn.fetchone()
participated = bool(txn.fetchone())

return count, latest_event_id, participated

count, latest_event_id = await self.db_pool.runInteraction(
count, latest_event_id, participated = await self.db_pool.runInteraction(
"get_thread_summary", _get_thread_summary_txn
)

latest_event = None
if latest_event_id:
latest_event = await self.get_event(latest_event_id, allow_none=True) # type: ignore[attr-defined]

return count, latest_event
return count, latest_event, participated

async def events_have_relations(
self,
Expand Down
3 changes: 3 additions & 0 deletions tests/rest/client/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,9 @@ def assert_bundle(actual):
2,
actual[RelationTypes.THREAD].get("count"),
)
self.assertTrue(
actual[RelationTypes.THREAD].get("current_user_participated")
)
# The latest thread event has some fields that don't matter.
self.assert_dict(
{
Expand Down

0 comments on commit 49339a5

Please sign in to comment.