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

Commit

Permalink
Add an event ID arg to is_interested_in_event to cache with
Browse files Browse the repository at this point in the history
Caching using an EventBase is not quite sufficient while we have no defined way of
checking whether two instances are equal other than if they are the equivalent object.

Instead, we use the event ID here for now, which should be good enough in this case.
  • Loading branch information
anoadragon453 committed Mar 3, 2022
1 parent eb779e0 commit 453fdac
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
10 changes: 8 additions & 2 deletions synapse/appservice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,16 +265,22 @@ async def is_interested_in_room(

@cached(num_args=1, cache_context=True)
async def is_interested_in_event(
self, event: EventBase, store: "DataStore", cache_context: _CacheContext
self,
event_id: str,
event: EventBase,
store: "DataStore",
cache_context: _CacheContext,
) -> bool:
"""Check if this service is interested in this event.
Args:
event_id: The ID of the event to check. This is purely used for simplifying the
caching of calls to this method.
event: The event to check.
store: The datastore to query.
Returns:
True if this service would like to know about this event.
True if this service would like to know about this event, otherwise False.
"""
# Check if we're interested in this event's sender by namespace (or if they're the
# sender_localpart user)
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ async def _get_services_for_event(
# inside of a list comprehension anymore.
interested_list = []
for s in services:
if await s.is_interested_in_event(event, self.store):
if await s.is_interested_in_event(event.event_id, event, self.store):
interested_list.append(s)

return interested_list
Expand Down
44 changes: 32 additions & 12 deletions tests/appservice/test_appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ def setUp(self):
hostname="matrix.org", # only used by get_groups_for_user
)
self.event = Mock(
type="m.something", room_id="!foo:bar", sender="@someone:somewhere"
event_id="$abc:xyz",
type="m.something",
room_id="!foo:bar",
sender="@someone:somewhere",
)

self.store = Mock()
Expand All @@ -50,7 +53,9 @@ def test_regex_user_id_prefix_match(self):
self.assertTrue(
(
yield defer.ensureDeferred(
self.service.is_interested_in_event(self.event, self.store)
self.service.is_interested_in_event(
self.event.event_id, self.event, self.store
)
)
)
)
Expand All @@ -62,7 +67,9 @@ def test_regex_user_id_prefix_no_match(self):
self.assertFalse(
(
yield defer.ensureDeferred(
self.service.is_interested_in_event(self.event, self.store)
self.service.is_interested_in_event(
self.event.event_id, self.event, self.store
)
)
)
)
Expand All @@ -76,7 +83,9 @@ def test_regex_room_member_is_checked(self):
self.assertTrue(
(
yield defer.ensureDeferred(
self.service.is_interested_in_event(self.event, self.store)
self.service.is_interested_in_event(
self.event.event_id, self.event, self.store
)
)
)
)
Expand All @@ -90,8 +99,9 @@ def test_regex_room_id_match(self):
self.assertTrue(
(
yield defer.ensureDeferred(
# We need to provide the store here in order to carry out room checks
self.service.is_interested_in_event(self.event, self.store)
self.service.is_interested_in_event(
self.event.event_id, self.event, self.store
)
)
)
)
Expand All @@ -105,7 +115,9 @@ def test_regex_room_id_no_match(self):
self.assertFalse(
(
yield defer.ensureDeferred(
self.service.is_interested_in_event(self.event, self.store)
self.service.is_interested_in_event(
self.event.event_id, self.event, self.store
)
)
)
)
Expand All @@ -122,7 +134,9 @@ def test_regex_alias_match(self):
self.assertTrue(
(
yield defer.ensureDeferred(
self.service.is_interested_in_event(self.event, self.store)
self.service.is_interested_in_event(
self.event.event_id, self.event, self.store
)
)
)
)
Expand Down Expand Up @@ -175,7 +189,9 @@ def test_regex_alias_no_match(self):
self.assertFalse(
(
yield defer.ensureDeferred(
self.service.is_interested_in_event(self.event, self.store)
self.service.is_interested_in_event(
self.event.event_id, self.event, self.store
)
)
)
)
Expand All @@ -192,7 +208,9 @@ def test_regex_multiple_matches(self):
self.assertTrue(
(
yield defer.ensureDeferred(
self.service.is_interested_in_event(self.event, self.store)
self.service.is_interested_in_event(
self.event.event_id, self.event, self.store
)
)
)
)
Expand All @@ -208,7 +226,9 @@ def test_interested_in_self(self):
self.assertTrue(
(
yield defer.ensureDeferred(
self.service.is_interested_in_event(self.event, self.store)
self.service.is_interested_in_event(
self.event.event_id, self.event, self.store
)
)
)
)
Expand All @@ -227,7 +247,7 @@ def test_member_list_match(self):
(
yield defer.ensureDeferred(
self.service.is_interested_in_event(
event=self.event, store=self.store
self.event.event_id, self.event, self.store
)
)
)
Expand Down

0 comments on commit 453fdac

Please sign in to comment.