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

Do not consider events by ignored users for bundled aggregations #12235

Merged
merged 25 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
75b1b5f
Filter out events from ignored users in /relations.
clokep Mar 14, 2022
3a51cfe
Add tests for ignored users in bundled aggregations.
clokep Mar 16, 2022
5e73a5e
Filter out ignored users for aggregation groups.
clokep Mar 16, 2022
56dd70a
Rename a variable.
clokep Mar 14, 2022
3357181
Filter out ignored users for threads.
clokep Mar 16, 2022
9eef5cb
Filter out ignored users for references.
clokep Mar 16, 2022
156ef7a
Add a note about edits.
clokep Mar 16, 2022
3bb8071
Newsfragment
clokep Mar 16, 2022
89c89df
Merge remote-tracking branch 'origin/develop' into clokep/ignored-use…
clokep Mar 24, 2022
fffa6ca
Merge remote-tracking branch 'origin/develop' into clokep/ignored-use…
clokep Mar 24, 2022
887fcb0
Merge remote-tracking branch 'origin/develop' into clokep/ignored-use…
clokep Mar 30, 2022
fc7d14b
Merge remote-tracking branch 'origin/develop' into clokep/ignored-use…
clokep Mar 31, 2022
ecae2ad
Do not cache on the ignored users parameter when fetching relations.
clokep Mar 24, 2022
f03a6a8
Newsfragment
clokep Mar 31, 2022
622b621
Do not cache on the ignored users parameter when fetching annotations.
clokep Mar 31, 2022
b60fded
Merge remote-tracking branch 'origin/develop' into clokep/ignored-use…
clokep Apr 5, 2022
30ce317
Add an intermediate method for threads.
clokep Apr 1, 2022
b0d4474
Do not cache on the ignored users parameter when fetching threads.
clokep Apr 6, 2022
1b2c9a1
Add missing docstrings.
clokep Apr 6, 2022
44f6975
Merge remote-tracking branch 'origin/develop' into clokep/ignored-use…
clokep Apr 6, 2022
96d9215
Revert unused changes.
clokep Apr 8, 2022
e2910a4
Docstring.
clokep Apr 8, 2022
7d6fa1e
Add logging.
clokep Apr 8, 2022
61c7526
Merge remote-tracking branch 'origin/develop' into clokep/ignored-use…
clokep Apr 8, 2022
e95bd07
Merge remote-tracking branch 'origin/develop' into clokep/ignored-use…
clokep Apr 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/12235.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a long-standing bug where events from ignored users were still considered for bundled aggregations.
1 change: 1 addition & 0 deletions changelog.d/12338.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a long-standing bug where events from ignored users were still considered for bundled aggregations.
1 change: 0 additions & 1 deletion changelog.d/12338.misc

This file was deleted.

244 changes: 214 additions & 30 deletions synapse/handlers/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from typing import TYPE_CHECKING, Dict, Iterable, Optional
from typing import (
TYPE_CHECKING,
Collection,
Dict,
FrozenSet,
Iterable,
List,
Optional,
Tuple,
)

import attr
from frozendict import frozendict

from synapse.api.constants import RelationTypes
from synapse.api.errors import SynapseError
from synapse.events import EventBase
from synapse.types import JsonDict, Requester, StreamToken
from synapse.storage.databases.main.relations import _RelatedEvent
from synapse.types import JsonDict, Requester, StreamToken, UserID
from synapse.visibility import filter_events_for_client

if TYPE_CHECKING:
Expand Down Expand Up @@ -115,6 +125,9 @@ async def get_relations(
if event is None:
raise SynapseError(404, "Unknown parent event.")

# Note that ignored users are not passed into get_relations_for_event
# below. Ignored users are handled in filter_events_for_client (and by
# not passing them in here we should get a better cache hit rate).
related_events, next_token = await self._main_store.get_relations_for_event(
event_id=event_id,
event=event,
Expand All @@ -128,7 +141,9 @@ async def get_relations(
to_token=to_token,
)

events = await self._main_store.get_events_as_list(related_events)
events = await self._main_store.get_events_as_list(
[e.event_id for e in related_events]
)

events = await filter_events_for_client(
self._storage, user_id, events, is_peeking=(member_event_id is None)
Expand Down Expand Up @@ -162,16 +177,95 @@ async def get_relations(

return return_value

async def get_relations_for_event(
self,
event_id: str,
event: EventBase,
room_id: str,
relation_type: str,
ignored_users: FrozenSet[str] = frozenset(),
) -> Tuple[List[_RelatedEvent], Optional[StreamToken]]:
"""Get a list of events which relate to an event, ordered by topological ordering.

Args:
event_id: Fetch events that relate to this event ID.
event: The matching EventBase to event_id.
room_id: The room the event belongs to.
relation_type: The type of relation.
ignored_users: The users ignored by the requesting user.

Returns:
List of event IDs that match relations requested. The rows are of
the form `{"event_id": "..."}`.
"""

# Call the underlying storage method, which is cached.
related_events, next_token = await self._main_store.get_relations_for_event(
event_id, event, room_id, relation_type, direction="f"
)

# Filter out ignored users and convert to the expected format.
related_events = [
event for event in related_events if event.sender not in ignored_users
]

return related_events, next_token

async def get_annotations_for_event(
self,
event_id: str,
room_id: str,
limit: int = 5,
ignored_users: FrozenSet[str] = frozenset(),
) -> List[JsonDict]:
"""Get a list of annotations on the event, grouped by event type and
aggregation key, sorted by count.

This is used e.g. to get the what and how many reactions have happend
on an event.

Args:
event_id: Fetch events that relate to this event ID.
room_id: The room the event belongs to.
limit: Only fetch the `limit` groups.
ignored_users: The users ignored by the requesting user.

Returns:
List of groups of annotations that match. Each row is a dict with
`type`, `key` and `count` fields.
"""
# Get the base results for all users.
full_results = await self._main_store.get_aggregation_groups_for_event(
event_id, room_id, limit
)

# Then subtract off the results for any ignored users.
ignored_results = await self._main_store.get_aggregation_groups_for_users(
event_id, room_id, limit, ignored_users
)

filtered_results = []
for result in full_results:
key = (result["type"], result["key"])
if key in ignored_results:
result = result.copy()
result["count"] -= ignored_results[key]
if result["count"] <= 0:
continue
filtered_results.append(result)

return filtered_results

async def _get_bundled_aggregation_for_event(
self, event: EventBase, user_id: str
self, event: EventBase, ignored_users: FrozenSet[str]
) -> Optional[BundledAggregations]:
"""Generate bundled aggregations for an event.

Note that this does not use a cache, but depends on cached methods.

Args:
event: The event to calculate bundled aggregations for.
user_id: The user requesting the bundled aggregations.
ignored_users: The users ignored by the requesting user.

Returns:
The bundled aggregations for an event, if bundled aggregations are
Expand All @@ -194,18 +288,22 @@ async def _get_bundled_aggregation_for_event(
# while others need more processing during serialization.
aggregations = BundledAggregations()

annotations = await self._main_store.get_aggregation_groups_for_event(
event_id, room_id
annotations = await self.get_annotations_for_event(
event_id, room_id, ignored_users=ignored_users
)
if annotations:
aggregations.annotations = {"chunk": annotations}

references, next_token = await self._main_store.get_relations_for_event(
event_id, event, room_id, RelationTypes.REFERENCE, direction="f"
references, next_token = await self.get_relations_for_event(
event_id,
event,
room_id,
RelationTypes.REFERENCE,
ignored_users=ignored_users,
)
if references:
aggregations.references = {
"chunk": [{"event_id": event_id} for event_id in references]
"chunk": [{"event_id": event.event_id} for event in references]
}

if next_token:
Expand All @@ -216,6 +314,99 @@ async def _get_bundled_aggregation_for_event(
# Store the bundled aggregations in the event metadata for later use.
return aggregations

async def get_threads_for_events(
self, event_ids: Collection[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.
user_id: The user requesting the bundled aggregations.
ignored_users: The users ignored by the requesting user.

Returns:
A dictionary mapping event ID to the thread information.

May not contain a value for all requested event IDs.
"""
user = UserID.from_string(user_id)

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

# Only fetch participated for a limited selection based on what had
# summaries.
thread_event_ids = [
event_id for event_id, summary in summaries.items() if summary
]
participated = await self._main_store.get_threads_participated(
thread_event_ids, user_id
)

# Then subtract off the results for any ignored users.
ignored_results = await self._main_store.get_threaded_messages_per_user(
thread_event_ids, ignored_users
)

# A map of event ID to the thread aggregation.
results = {}

for event_id, summary in summaries.items():
if summary:
thread_count, latest_thread_event, edit = summary

# Subtract off the count of any ignored users.
for ignored_user in ignored_users:
thread_count -= ignored_results.get((event_id, ignored_user), 0)

# This is gnarly, but if the latest event is from an ignored user,
# attempt to find one that isn't from an ignored user.
if latest_thread_event.sender in ignored_users:
room_id = latest_thread_event.room_id

# If the root event is not found, something went wrong, do
# not include a summary of the thread.
event = await self._event_handler.get_event(user, room_id, event_id)
if event is None:
continue

potential_events, _ = await self.get_relations_for_event(
event_id,
event,
room_id,
RelationTypes.THREAD,
ignored_users,
)

# If all found events are from ignored users, do not include
# a summary of the thread.
if not potential_events:
continue

# The *last* event returned is the one that is cared about.
event = await self._event_handler.get_event(
user, room_id, potential_events[-1].event_id
)
# It is unexpected that the event will not exist.
if event is None:
logger.warning(
"Unable to fetch latest event in a thread with event ID: %s",
potential_events[-1].event_id,
)
continue
clokep marked this conversation as resolved.
Show resolved Hide resolved
latest_thread_event = event

results[event_id] = _ThreadAggregation(
latest_event=latest_thread_event,
latest_edit=edit,
count=thread_count,
# If there's a thread summary it must also exist in the
# participated dictionary.
current_user_participated=participated[event_id],
)

return results

async def get_bundled_aggregations(
self, events: Iterable[EventBase], user_id: str
) -> Dict[str, BundledAggregations]:
Expand All @@ -239,13 +430,21 @@ async def get_bundled_aggregations(
# event ID -> bundled aggregation in non-serialized form.
results: Dict[str, BundledAggregations] = {}

# Fetch any ignored users of the requesting user.
ignored_users = await self._main_store.ignored_users(user_id)

# Fetch other relations per event.
for event in events_by_id.values():
event_result = await self._get_bundled_aggregation_for_event(event, user_id)
event_result = await self._get_bundled_aggregation_for_event(
event, ignored_users
)
if event_result:
results[event.event_id] = event_result

# Fetch any edits (but not for redacted events).
#
# Note that there is no use in limiting edits by ignored users since the
# parent event should be ignored in the first place if the user is ignored.
Comment on lines +446 to +447
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this sound like sound logic? I couldn't come up with a situation where we would need to check edits,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think it's probably fine. The only thing is if we decided in future to allow e.g. moderators to edit messages.

edits = await self._main_store.get_applicable_edits(
[
event_id
Expand All @@ -256,25 +455,10 @@ async def get_bundled_aggregations(
for event_id, edit in edits.items():
results.setdefault(event_id, BundledAggregations()).replace = edit

# Fetch thread summaries.
summaries = await self._main_store.get_thread_summaries(events_by_id.keys())
# Only fetch participated for a limited selection based on what had
# summaries.
participated = await self._main_store.get_threads_participated(
[event_id for event_id, summary in summaries.items() if summary], user_id
threads = await self.get_threads_for_events(
events_by_id.keys(), user_id, ignored_users
)
for event_id, summary in summaries.items():
if summary:
thread_count, latest_thread_event, edit = summary
results.setdefault(
event_id, BundledAggregations()
).thread = _ThreadAggregation(
latest_event=latest_thread_event,
latest_edit=edit,
count=thread_count,
# If there's a thread summary it must also exist in the
# participated dictionary.
current_user_participated=participated[event_id],
)
for event_id, thread in threads.items():
results.setdefault(event_id, BundledAggregations()).thread = thread

return results
Loading