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

Commit 6324ed3

Browse files
anoadragon453David Robertson
authored andcommitted
Rename various ApplicationServices interested methods (#11915)
1 parent 916f081 commit 6324ed3

File tree

8 files changed

+175
-76
lines changed

8 files changed

+175
-76
lines changed

changelog.d/11915.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Simplify the `ApplicationService` class' set of public methods related to interest checking.

synapse/appservice/__init__.py

Lines changed: 91 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -175,27 +175,14 @@ def _is_exclusive(self, namespace_key: str, test_string: str) -> bool:
175175
return namespace.exclusive
176176
return False
177177

178-
async def _matches_user(self, event: EventBase, store: "DataStore") -> bool:
179-
if self.is_interested_in_user(event.sender):
180-
return True
181-
182-
# also check m.room.member state key
183-
if event.type == EventTypes.Member and self.is_interested_in_user(
184-
event.state_key
185-
):
186-
return True
187-
188-
does_match = await self.matches_user_in_member_list(event.room_id, store)
189-
return does_match
190-
191178
@cached(num_args=1, cache_context=True)
192-
async def matches_user_in_member_list(
179+
async def _matches_user_in_member_list(
193180
self,
194181
room_id: str,
195182
store: "DataStore",
196183
cache_context: _CacheContext,
197184
) -> bool:
198-
"""Check if this service is interested a room based upon it's membership
185+
"""Check if this service is interested a room based upon its membership
199186
200187
Args:
201188
room_id: The room to check.
@@ -214,47 +201,110 @@ async def matches_user_in_member_list(
214201
return True
215202
return False
216203

217-
def _matches_room_id(self, event: EventBase) -> bool:
218-
if hasattr(event, "room_id"):
219-
return self.is_interested_in_room(event.room_id)
220-
return False
204+
def is_interested_in_user(
205+
self,
206+
user_id: str,
207+
) -> bool:
208+
"""
209+
Returns whether the application is interested in a given user ID.
210+
211+
The appservice is considered to be interested in a user if either: the
212+
user ID is in the appservice's user namespace, or if the user is the
213+
appservice's configured sender_localpart.
214+
215+
Args:
216+
user_id: The ID of the user to check.
217+
218+
Returns:
219+
True if the application service is interested in the user, False if not.
220+
"""
221+
return (
222+
# User is the appservice's sender_localpart user
223+
user_id == self.sender
224+
# User is in the appservice's user namespace
225+
or self.is_user_in_namespace(user_id)
226+
)
227+
228+
@cached(num_args=1, cache_context=True)
229+
async def is_interested_in_room(
230+
self,
231+
room_id: str,
232+
store: "DataStore",
233+
cache_context: _CacheContext,
234+
) -> bool:
235+
"""
236+
Returns whether the application service is interested in a given room ID.
237+
238+
The appservice is considered to be interested in the room if either: the ID or one
239+
of the aliases of the room is in the appservice's room ID or alias namespace
240+
respectively, or if one of the members of the room fall into the appservice's user
241+
namespace.
221242
222-
async def _matches_aliases(self, event: EventBase, store: "DataStore") -> bool:
223-
alias_list = await store.get_aliases_for_room(event.room_id)
243+
Args:
244+
room_id: The ID of the room to check.
245+
store: The homeserver's datastore class.
246+
247+
Returns:
248+
True if the application service is interested in the room, False if not.
249+
"""
250+
# Check if we have interest in this room ID
251+
if self.is_room_id_in_namespace(room_id):
252+
return True
253+
254+
# likewise with the room's aliases (if it has any)
255+
alias_list = await store.get_aliases_for_room(room_id)
224256
for alias in alias_list:
225-
if self.is_interested_in_alias(alias):
257+
if self.is_room_alias_in_namespace(alias):
226258
return True
227259

228-
return False
260+
# And finally, perform an expensive check on whether any of the
261+
# users in the room match the appservice's user namespace
262+
return await self._matches_user_in_member_list(
263+
room_id, store, on_invalidate=cache_context.invalidate
264+
)
229265

230-
async def is_interested(self, event: EventBase, store: "DataStore") -> bool:
266+
@cached(num_args=1, cache_context=True)
267+
async def is_interested_in_event(
268+
self,
269+
event_id: str,
270+
event: EventBase,
271+
store: "DataStore",
272+
cache_context: _CacheContext,
273+
) -> bool:
231274
"""Check if this service is interested in this event.
232275
233276
Args:
277+
event_id: The ID of the event to check. This is purely used for simplifying the
278+
caching of calls to this method.
234279
event: The event to check.
235280
store: The datastore to query.
236281
237282
Returns:
238-
True if this service would like to know about this event.
283+
True if this service would like to know about this event, otherwise False.
239284
"""
240-
# Do cheap checks first
241-
if self._matches_room_id(event):
285+
# Check if we're interested in this event's sender by namespace (or if they're the
286+
# sender_localpart user)
287+
if self.is_interested_in_user(event.sender):
242288
return True
243289

244-
# This will check the namespaces first before
245-
# checking the store, so should be run before _matches_aliases
246-
if await self._matches_user(event, store):
290+
# additionally, if this is a membership event, perform the same checks on
291+
# the user it references
292+
if event.type == EventTypes.Member and self.is_interested_in_user(
293+
event.state_key
294+
):
247295
return True
248296

249-
# This will check the store, so should be run last
250-
if await self._matches_aliases(event, store):
297+
# This will check the datastore, so should be run last
298+
if await self.is_interested_in_room(
299+
event.room_id, store, on_invalidate=cache_context.invalidate
300+
):
251301
return True
252302

253303
return False
254304

255-
@cached(num_args=1)
305+
@cached(num_args=1, cache_context=True)
256306
async def is_interested_in_presence(
257-
self, user_id: UserID, store: "DataStore"
307+
self, user_id: UserID, store: "DataStore", cache_context: _CacheContext
258308
) -> bool:
259309
"""Check if this service is interested a user's presence
260310
@@ -272,20 +322,19 @@ async def is_interested_in_presence(
272322

273323
# Then find out if the appservice is interested in any of those rooms
274324
for room_id in room_ids:
275-
if await self.matches_user_in_member_list(room_id, store):
325+
if await self.is_interested_in_room(
326+
room_id, store, on_invalidate=cache_context.invalidate
327+
):
276328
return True
277329
return False
278330

279-
def is_interested_in_user(self, user_id: str) -> bool:
280-
return (
281-
bool(self._matches_regex(ApplicationService.NS_USERS, user_id))
282-
or user_id == self.sender
283-
)
331+
def is_user_in_namespace(self, user_id: str) -> bool:
332+
return bool(self._matches_regex(ApplicationService.NS_USERS, user_id))
284333

285-
def is_interested_in_alias(self, alias: str) -> bool:
334+
def is_room_alias_in_namespace(self, alias: str) -> bool:
286335
return bool(self._matches_regex(ApplicationService.NS_ALIASES, alias))
287336

288-
def is_interested_in_room(self, room_id: str) -> bool:
337+
def is_room_id_in_namespace(self, room_id: str) -> bool:
289338
return bool(self._matches_regex(ApplicationService.NS_ROOMS, room_id))
290339

291340
def is_exclusive_user(self, user_id: str) -> bool:

synapse/handlers/appservice.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ async def query_room_alias_exists(
571571
room_alias_str = room_alias.to_string()
572572
services = self.store.get_app_services()
573573
alias_query_services = [
574-
s for s in services if (s.is_interested_in_alias(room_alias_str))
574+
s for s in services if (s.is_room_alias_in_namespace(room_alias_str))
575575
]
576576
for alias_service in alias_query_services:
577577
is_known_alias = await self.appservice_api.query_alias(
@@ -660,7 +660,7 @@ async def _get_services_for_event(
660660
# inside of a list comprehension anymore.
661661
interested_list = []
662662
for s in services:
663-
if await s.is_interested(event, self.store):
663+
if await s.is_interested_in_event(event.event_id, event, self.store):
664664
interested_list.append(s)
665665

666666
return interested_list

synapse/handlers/directory.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ async def create_association(
119119

120120
service = requester.app_service
121121
if service:
122-
if not service.is_interested_in_alias(room_alias_str):
122+
if not service.is_room_alias_in_namespace(room_alias_str):
123123
raise SynapseError(
124124
400,
125125
"This application service has not reserved this kind of alias.",
@@ -221,7 +221,7 @@ async def delete_association(
221221
async def delete_appservice_association(
222222
self, service: ApplicationService, room_alias: RoomAlias
223223
) -> None:
224-
if not service.is_interested_in_alias(room_alias.to_string()):
224+
if not service.is_room_alias_in_namespace(room_alias.to_string()):
225225
raise SynapseError(
226226
400,
227227
"This application service has not reserved this kind of alias",
@@ -376,7 +376,7 @@ def can_modify_alias(self, alias: RoomAlias, user_id: Optional[str] = None) -> b
376376
# non-exclusive locks on the alias (or there are no interested services)
377377
services = self.store.get_app_services()
378378
interested_services = [
379-
s for s in services if s.is_interested_in_alias(alias.to_string())
379+
s for s in services if s.is_room_alias_in_namespace(alias.to_string())
380380
]
381381

382382
for service in interested_services:

synapse/handlers/receipts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ async def get_new_events_as(
269269
# Then filter down to rooms that the AS can read
270270
events = []
271271
for room_id, event in rooms_to_events.items():
272-
if not await service.matches_user_in_member_list(room_id, self.store):
272+
if not await service.is_interested_in_room(room_id, self.store):
273273
continue
274274

275275
events.append(event)

synapse/handlers/typing.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,7 @@ async def get_new_events_as(
486486
if handler._room_serials[room_id] <= from_key:
487487
continue
488488

489-
if not await service.matches_user_in_member_list(
490-
room_id, self._main_store
491-
):
489+
if not await service.is_interested_in_room(room_id, self._main_store):
492490
continue
493491

494492
events.append(self._make_event_for(room_id))

tests/appservice/test_appservice.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ def setUp(self):
3636
hostname="matrix.org", # only used by get_groups_for_user
3737
)
3838
self.event = Mock(
39-
type="m.something", room_id="!foo:bar", sender="@someone:somewhere"
39+
event_id="$abc:xyz",
40+
type="m.something",
41+
room_id="!foo:bar",
42+
sender="@someone:somewhere",
4043
)
4144

4245
self.store = Mock()
@@ -50,7 +53,9 @@ def test_regex_user_id_prefix_match(self):
5053
self.assertTrue(
5154
(
5255
yield defer.ensureDeferred(
53-
self.service.is_interested(self.event, self.store)
56+
self.service.is_interested_in_event(
57+
self.event.event_id, self.event, self.store
58+
)
5459
)
5560
)
5661
)
@@ -62,7 +67,9 @@ def test_regex_user_id_prefix_no_match(self):
6267
self.assertFalse(
6368
(
6469
yield defer.ensureDeferred(
65-
self.service.is_interested(self.event, self.store)
70+
self.service.is_interested_in_event(
71+
self.event.event_id, self.event, self.store
72+
)
6673
)
6774
)
6875
)
@@ -76,7 +83,9 @@ def test_regex_room_member_is_checked(self):
7683
self.assertTrue(
7784
(
7885
yield defer.ensureDeferred(
79-
self.service.is_interested(self.event, self.store)
86+
self.service.is_interested_in_event(
87+
self.event.event_id, self.event, self.store
88+
)
8089
)
8190
)
8291
)
@@ -90,7 +99,9 @@ def test_regex_room_id_match(self):
9099
self.assertTrue(
91100
(
92101
yield defer.ensureDeferred(
93-
self.service.is_interested(self.event, self.store)
102+
self.service.is_interested_in_event(
103+
self.event.event_id, self.event, self.store
104+
)
94105
)
95106
)
96107
)
@@ -104,7 +115,9 @@ def test_regex_room_id_no_match(self):
104115
self.assertFalse(
105116
(
106117
yield defer.ensureDeferred(
107-
self.service.is_interested(self.event, self.store)
118+
self.service.is_interested_in_event(
119+
self.event.event_id, self.event, self.store
120+
)
108121
)
109122
)
110123
)
@@ -121,7 +134,9 @@ def test_regex_alias_match(self):
121134
self.assertTrue(
122135
(
123136
yield defer.ensureDeferred(
124-
self.service.is_interested(self.event, self.store)
137+
self.service.is_interested_in_event(
138+
self.event.event_id, self.event, self.store
139+
)
125140
)
126141
)
127142
)
@@ -174,7 +189,9 @@ def test_regex_alias_no_match(self):
174189
self.assertFalse(
175190
(
176191
yield defer.ensureDeferred(
177-
self.service.is_interested(self.event, self.store)
192+
self.service.is_interested_in_event(
193+
self.event.event_id, self.event, self.store
194+
)
178195
)
179196
)
180197
)
@@ -191,7 +208,9 @@ def test_regex_multiple_matches(self):
191208
self.assertTrue(
192209
(
193210
yield defer.ensureDeferred(
194-
self.service.is_interested(self.event, self.store)
211+
self.service.is_interested_in_event(
212+
self.event.event_id, self.event, self.store
213+
)
195214
)
196215
)
197216
)
@@ -207,7 +226,9 @@ def test_interested_in_self(self):
207226
self.assertTrue(
208227
(
209228
yield defer.ensureDeferred(
210-
self.service.is_interested(self.event, self.store)
229+
self.service.is_interested_in_event(
230+
self.event.event_id, self.event, self.store
231+
)
211232
)
212233
)
213234
)
@@ -225,7 +246,9 @@ def test_member_list_match(self):
225246
self.assertTrue(
226247
(
227248
yield defer.ensureDeferred(
228-
self.service.is_interested(event=self.event, store=self.store)
249+
self.service.is_interested_in_event(
250+
self.event.event_id, self.event, self.store
251+
)
229252
)
230253
)
231254
)

0 commit comments

Comments
 (0)