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

Rename various ApplicationServices interested methods #11915

Merged
merged 10 commits into from
Mar 3, 2022
Prev Previous commit
Next Next commit
Add 'is_interested_in_user' and 'is_interested_in_room'
...that make use of the simpler helper methods we just renamed.

This commit adds the equivalent methods of is_interested_in_event for
both users and rooms. These implementations include more comprehensive
checks then the older methods with the same names.
  • Loading branch information
anoadragon453 committed Mar 3, 2022
commit fe178639c088e841f5cd937a225a2eb379bdadc7
79 changes: 72 additions & 7 deletions synapse/appservice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,68 @@ async def matches_user_in_member_list(
return True
return False

def is_interested_in_user(
self,
user_id: str,
) -> bool:
"""
Returns whether the application is interested in a given user ID.

The appservice is considered to be interested in a user if either: the
user ID is in the appservice's user namespace, or if the user is the
appservice's configured sender_localpart.

Args:
user_id: The ID of the user to check.

Returns:
True if the application service is interested in the user, False if not.
"""
return (
# User is the appservice's sender_localpart user
user_id == self.sender
# User is in the appservice's user namespace
or self.is_user_in_namespace(user_id)
)

@cached(num_args=1, cache_context=True)
async def is_interested_in_room(
self,
room_id: str,
store: "DataStore",
cache_context: _CacheContext,
) -> bool:
"""
Returns whether the application service is interested in a given room ID.

The appservice is considered to be interested in the room if either: the ID or one
of the aliases of the room is in the appservice's room ID or alias namespace
respectively, or if one of the members of the room fall into the appservice's user
namespace.

Args:
room_id: The ID of the room to check.
store: The homeserver's datastore class.

Returns:
True if the application service is interested in the room, False if not.
"""
# Check if this room ID matches the appservice's room ID namespace
if self.is_room_id_in_namespace(room_id):
return True

# likewise with the room's aliases (if it has any)
alias_list = await store.get_aliases_for_room(room_id)
for alias in alias_list:
if self.is_room_alias_in_namespace(alias):
return True

# And finally, perform an expensive check on whether any of the
# users in the room match the appservice's user namespace
return await self.matches_user_in_member_list(
room_id, store, on_invalidate=cache_context.invalidate
)

def _matches_room_id(self, event: EventBase) -> bool:
if hasattr(event, "room_id"):
return self.is_room_id_in_namespace(event.room_id)
Expand All @@ -239,17 +301,20 @@ async def is_interested_in_event(
Returns:
True if this service would like to know about this event.
"""
# Do cheap checks first
if self._matches_room_id(event):
# Check if we're interested in this event's sender by namespace (or if they're the
# sender_localpart user)
if self.is_interested_in_user(event.sender):
return True

# This will check the namespaces first before
# checking the store, so should be run before _matches_aliases
if await self._matches_user(event, store):
# additionally, if this is a membership event, perform the same checks on
# the user it references
if event.type == EventTypes.Member and self.is_interested_in_user(
event.state_key
):
return True

# This will check the store, so should be run last
if await self._matches_aliases(event, store):
# This will check the datastore, so should be run last
if await self.is_interested_in_room(event.room_id, store):
return True

return False
Expand Down