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

Commit eaa7916

Browse files
committed
Consider the root event when calculating if the current user has participated in a thread.
1 parent a7086cb commit eaa7916

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

changelog.d/12766.bugfix

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Implement [MSC3816](https://github.com/matrix-org/matrix-spec-proposals/pull/3816): sending the root event in a thread
2+
should count as "participated" in it.

synapse/handlers/relations.py

+18-17
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import logging
15-
from typing import (
16-
TYPE_CHECKING,
17-
Collection,
18-
Dict,
19-
FrozenSet,
20-
Iterable,
21-
List,
22-
Optional,
23-
Tuple,
24-
)
15+
from typing import TYPE_CHECKING, Dict, FrozenSet, Iterable, List, Optional, Tuple
2516

2617
import attr
2718

@@ -253,13 +244,19 @@ async def get_annotations_for_event(
253244

254245
return filtered_results
255246

256-
async def get_threads_for_events(
257-
self, event_ids: Collection[str], user_id: str, ignored_users: FrozenSet[str]
247+
async def _get_threads_for_events(
248+
self,
249+
events_by_id: Dict[str, EventBase],
250+
relations_by_id: Dict[str, str],
251+
user_id: str,
252+
ignored_users: FrozenSet[str],
258253
) -> Dict[str, _ThreadAggregation]:
259254
"""Get the bundled aggregations for threads for the requested events.
260255
261256
Args:
262-
event_ids: Events to get aggregations for threads.
257+
events_by_id: A map of event_id to events to get aggregations for threads.
258+
relations_by_id: A map of event_id to the relation type, if one exists
259+
for that event.
263260
user_id: The user requesting the bundled aggregations.
264261
ignored_users: The users ignored by the requesting user.
265262
@@ -270,6 +267,9 @@ async def get_threads_for_events(
270267
"""
271268
user = UserID.from_string(user_id)
272269

270+
# It is not valid to start a thread on an event which itself relates to another event.
271+
event_ids = [eid for eid in events_by_id.keys() if eid not in relations_by_id]
272+
273273
# Fetch thread summaries.
274274
summaries = await self._main_store.get_thread_summaries(event_ids)
275275

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

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

400401
# Fetch thread summaries (but only for the directly requested events).
401-
threads = await self.get_threads_for_events(
402-
# It is not valid to start a thread on an event which itself relates to another event.
403-
[eid for eid in events_by_id.keys() if eid not in relations_by_id],
402+
threads = await self._get_threads_for_events(
403+
events_by_id,
404+
relations_by_id,
404405
user_id,
405406
ignored_users,
406407
)

tests/rest/client/test_relations.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1072,13 +1072,21 @@ def assert_thread(bundled_aggregations: JsonDict) -> None:
10721072

10731073
return assert_thread
10741074

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

1083+
# A user with no interactions with the thread has not participated.
1084+
user3_id, user3_token = self._create_user("charlie")
1085+
self.helper.join(self.room, user=user3_id, tok=user3_token)
1086+
self._test_bundled_aggregations(
1087+
RelationTypes.THREAD, _gen_assert(False), 2, access_token=user3_token
1088+
)
1089+
10821090
def test_thread_with_bundled_aggregations_for_latest(self) -> None:
10831091
"""
10841092
Bundled aggregations should get applied to the latest thread event.

0 commit comments

Comments
 (0)