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

Commit cf06783

Browse files
Remove optional state of ApplicationService.is_interested's store parameter (#11911)
1 parent 314ca4c commit cf06783

File tree

4 files changed

+39
-25
lines changed

4 files changed

+39
-25
lines changed

changelog.d/11911.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Various refactors to the application service notifier code.

synapse/appservice/__init__.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -165,23 +165,16 @@ def _is_exclusive(self, namespace_key: str, test_string: str) -> bool:
165165
return namespace.exclusive
166166
return False
167167

168-
async def _matches_user(
169-
self, event: Optional[EventBase], store: Optional["DataStore"] = None
170-
) -> bool:
171-
if not event:
172-
return False
173-
168+
async def _matches_user(self, event: EventBase, store: "DataStore") -> bool:
174169
if self.is_interested_in_user(event.sender):
175170
return True
171+
176172
# also check m.room.member state key
177173
if event.type == EventTypes.Member and self.is_interested_in_user(
178174
event.state_key
179175
):
180176
return True
181177

182-
if not store:
183-
return False
184-
185178
does_match = await self.matches_user_in_member_list(event.room_id, store)
186179
return does_match
187180

@@ -216,21 +209,15 @@ def _matches_room_id(self, event: EventBase) -> bool:
216209
return self.is_interested_in_room(event.room_id)
217210
return False
218211

219-
async def _matches_aliases(
220-
self, event: EventBase, store: Optional["DataStore"] = None
221-
) -> bool:
222-
if not store or not event:
223-
return False
224-
212+
async def _matches_aliases(self, event: EventBase, store: "DataStore") -> bool:
225213
alias_list = await store.get_aliases_for_room(event.room_id)
226214
for alias in alias_list:
227215
if self.is_interested_in_alias(alias):
228216
return True
217+
229218
return False
230219

231-
async def is_interested(
232-
self, event: EventBase, store: Optional["DataStore"] = None
233-
) -> bool:
220+
async def is_interested(self, event: EventBase, store: "DataStore") -> bool:
234221
"""Check if this service is interested in this event.
235222
236223
Args:

synapse/handlers/appservice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ async def _get_services_for_event(
649649
"""Retrieve a list of application services interested in this event.
650650
651651
Args:
652-
event: The event to check. Can be None if alias_list is not.
652+
event: The event to check.
653653
Returns:
654654
A list of services interested in this event based on the service regex.
655655
"""

tests/appservice/test_appservice.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,31 @@ def setUp(self):
4040
)
4141

4242
self.store = Mock()
43+
self.store.get_aliases_for_room = simple_async_mock([])
44+
self.store.get_users_in_room = simple_async_mock([])
4345

4446
@defer.inlineCallbacks
4547
def test_regex_user_id_prefix_match(self):
4648
self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
4749
self.event.sender = "@irc_foobar:matrix.org"
4850
self.assertTrue(
49-
(yield defer.ensureDeferred(self.service.is_interested(self.event)))
51+
(
52+
yield defer.ensureDeferred(
53+
self.service.is_interested(self.event, self.store)
54+
)
55+
)
5056
)
5157

5258
@defer.inlineCallbacks
5359
def test_regex_user_id_prefix_no_match(self):
5460
self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
5561
self.event.sender = "@someone_else:matrix.org"
5662
self.assertFalse(
57-
(yield defer.ensureDeferred(self.service.is_interested(self.event)))
63+
(
64+
yield defer.ensureDeferred(
65+
self.service.is_interested(self.event, self.store)
66+
)
67+
)
5868
)
5969

6070
@defer.inlineCallbacks
@@ -64,7 +74,11 @@ def test_regex_room_member_is_checked(self):
6474
self.event.type = "m.room.member"
6575
self.event.state_key = "@irc_foobar:matrix.org"
6676
self.assertTrue(
67-
(yield defer.ensureDeferred(self.service.is_interested(self.event)))
77+
(
78+
yield defer.ensureDeferred(
79+
self.service.is_interested(self.event, self.store)
80+
)
81+
)
6882
)
6983

7084
@defer.inlineCallbacks
@@ -74,7 +88,11 @@ def test_regex_room_id_match(self):
7488
)
7589
self.event.room_id = "!some_prefixs0m3th1nGsome_suffix:matrix.org"
7690
self.assertTrue(
77-
(yield defer.ensureDeferred(self.service.is_interested(self.event)))
91+
(
92+
yield defer.ensureDeferred(
93+
self.service.is_interested(self.event, self.store)
94+
)
95+
)
7896
)
7997

8098
@defer.inlineCallbacks
@@ -84,7 +102,11 @@ def test_regex_room_id_no_match(self):
84102
)
85103
self.event.room_id = "!XqBunHwQIXUiqCaoxq:matrix.org"
86104
self.assertFalse(
87-
(yield defer.ensureDeferred(self.service.is_interested(self.event)))
105+
(
106+
yield defer.ensureDeferred(
107+
self.service.is_interested(self.event, self.store)
108+
)
109+
)
88110
)
89111

90112
@defer.inlineCallbacks
@@ -183,7 +205,11 @@ def test_interested_in_self(self):
183205
self.event.content = {"membership": "invite"}
184206
self.event.state_key = self.service.sender
185207
self.assertTrue(
186-
(yield defer.ensureDeferred(self.service.is_interested(self.event)))
208+
(
209+
yield defer.ensureDeferred(
210+
self.service.is_interested(self.event, self.store)
211+
)
212+
)
187213
)
188214

189215
@defer.inlineCallbacks

0 commit comments

Comments
 (0)