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

Commit

Permalink
Consider the root event when calculating if the current user has part…
Browse files Browse the repository at this point in the history
…icipated in a thread.
  • Loading branch information
clokep committed May 17, 2022
1 parent a7086cb commit eaa7916
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
2 changes: 2 additions & 0 deletions changelog.d/12766.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Implement [MSC3816](https://github.com/matrix-org/matrix-spec-proposals/pull/3816): sending the root event in a thread
should count as "participated" in it.
35 changes: 18 additions & 17 deletions synapse/handlers/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from typing import (
TYPE_CHECKING,
Collection,
Dict,
FrozenSet,
Iterable,
List,
Optional,
Tuple,
)
from typing import TYPE_CHECKING, Dict, FrozenSet, Iterable, List, Optional, Tuple

import attr

Expand Down Expand Up @@ -253,13 +244,19 @@ async def get_annotations_for_event(

return filtered_results

async def get_threads_for_events(
self, event_ids: Collection[str], user_id: str, ignored_users: FrozenSet[str]
async def _get_threads_for_events(
self,
events_by_id: Dict[str, EventBase],
relations_by_id: Dict[str, str],
user_id: str,
ignored_users: FrozenSet[str],
) -> Dict[str, _ThreadAggregation]:
"""Get the bundled aggregations for threads for the requested events.
Args:
event_ids: Events to get aggregations for threads.
events_by_id: A map of event_id to events to get aggregations for threads.
relations_by_id: A map of event_id to the relation type, if one exists
for that event.
user_id: The user requesting the bundled aggregations.
ignored_users: The users ignored by the requesting user.
Expand All @@ -270,6 +267,9 @@ async def get_threads_for_events(
"""
user = UserID.from_string(user_id)

# It is not valid to start a thread on an event which itself relates to another event.
event_ids = [eid for eid in events_by_id.keys() if eid not in relations_by_id]

# Fetch thread summaries.
summaries = await self._main_store.get_thread_summaries(event_ids)

Expand Down Expand Up @@ -340,7 +340,8 @@ async def get_threads_for_events(
count=thread_count,
# If there's a thread summary it must also exist in the
# participated dictionary.
current_user_participated=participated[event_id],
current_user_participated=events_by_id[event_id].sender == user_id
or participated[event_id],
)

return results
Expand Down Expand Up @@ -398,9 +399,9 @@ async def get_bundled_aggregations(
# events to be fetched. Thus, we check those first!

# Fetch thread summaries (but only for the directly requested events).
threads = await self.get_threads_for_events(
# It is not valid to start a thread on an event which itself relates to another event.
[eid for eid in events_by_id.keys() if eid not in relations_by_id],
threads = await self._get_threads_for_events(
events_by_id,
relations_by_id,
user_id,
ignored_users,
)
Expand Down
10 changes: 9 additions & 1 deletion tests/rest/client/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,13 +1072,21 @@ def assert_thread(bundled_aggregations: JsonDict) -> None:

return assert_thread

self._test_bundled_aggregations(RelationTypes.THREAD, _gen_assert(False), 10)
# A user which has sent the root event or replied has participated.
self._test_bundled_aggregations(RelationTypes.THREAD, _gen_assert(True), 10)
# Note that this re-uses some cached values, so the total number of
# queries is much smaller.
self._test_bundled_aggregations(
RelationTypes.THREAD, _gen_assert(True), 2, access_token=self.user2_token
)

# A user with no interactions with the thread has not participated.
user3_id, user3_token = self._create_user("charlie")
self.helper.join(self.room, user=user3_id, tok=user3_token)
self._test_bundled_aggregations(
RelationTypes.THREAD, _gen_assert(False), 2, access_token=user3_token
)

def test_thread_with_bundled_aggregations_for_latest(self) -> None:
"""
Bundled aggregations should get applied to the latest thread event.
Expand Down

0 comments on commit eaa7916

Please sign in to comment.