Remove optional state of `ApplicationService.is_interested`'s `store` parameter (#11911)

This commit is contained in:
Andrew Morgan 2022-02-07 18:26:42 +00:00 committed by GitHub
parent 314ca4c86d
commit cf06783d54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 25 deletions

1
changelog.d/11911.misc Normal file
View File

@ -0,0 +1 @@
Various refactors to the application service notifier code.

View File

@ -165,23 +165,16 @@ class ApplicationService:
return namespace.exclusive return namespace.exclusive
return False return False
async def _matches_user( async def _matches_user(self, event: EventBase, store: "DataStore") -> bool:
self, event: Optional[EventBase], store: Optional["DataStore"] = None
) -> bool:
if not event:
return False
if self.is_interested_in_user(event.sender): if self.is_interested_in_user(event.sender):
return True return True
# also check m.room.member state key # also check m.room.member state key
if event.type == EventTypes.Member and self.is_interested_in_user( if event.type == EventTypes.Member and self.is_interested_in_user(
event.state_key event.state_key
): ):
return True return True
if not store:
return False
does_match = await self.matches_user_in_member_list(event.room_id, store) does_match = await self.matches_user_in_member_list(event.room_id, store)
return does_match return does_match
@ -216,21 +209,15 @@ class ApplicationService:
return self.is_interested_in_room(event.room_id) return self.is_interested_in_room(event.room_id)
return False return False
async def _matches_aliases( async def _matches_aliases(self, event: EventBase, store: "DataStore") -> bool:
self, event: EventBase, store: Optional["DataStore"] = None
) -> bool:
if not store or not event:
return False
alias_list = await store.get_aliases_for_room(event.room_id) alias_list = await store.get_aliases_for_room(event.room_id)
for alias in alias_list: for alias in alias_list:
if self.is_interested_in_alias(alias): if self.is_interested_in_alias(alias):
return True return True
return False return False
async def is_interested( async def is_interested(self, event: EventBase, store: "DataStore") -> bool:
self, event: EventBase, store: Optional["DataStore"] = None
) -> bool:
"""Check if this service is interested in this event. """Check if this service is interested in this event.
Args: Args:

View File

@ -649,7 +649,7 @@ class ApplicationServicesHandler:
"""Retrieve a list of application services interested in this event. """Retrieve a list of application services interested in this event.
Args: Args:
event: The event to check. Can be None if alias_list is not. event: The event to check.
Returns: Returns:
A list of services interested in this event based on the service regex. A list of services interested in this event based on the service regex.
""" """

View File

@ -40,13 +40,19 @@ class ApplicationServiceTestCase(unittest.TestCase):
) )
self.store = Mock() self.store = Mock()
self.store.get_aliases_for_room = simple_async_mock([])
self.store.get_users_in_room = simple_async_mock([])
@defer.inlineCallbacks @defer.inlineCallbacks
def test_regex_user_id_prefix_match(self): def test_regex_user_id_prefix_match(self):
self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*")) self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
self.event.sender = "@irc_foobar:matrix.org" self.event.sender = "@irc_foobar:matrix.org"
self.assertTrue( self.assertTrue(
(yield defer.ensureDeferred(self.service.is_interested(self.event))) (
yield defer.ensureDeferred(
self.service.is_interested(self.event, self.store)
)
)
) )
@defer.inlineCallbacks @defer.inlineCallbacks
@ -54,7 +60,11 @@ class ApplicationServiceTestCase(unittest.TestCase):
self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*")) self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
self.event.sender = "@someone_else:matrix.org" self.event.sender = "@someone_else:matrix.org"
self.assertFalse( self.assertFalse(
(yield defer.ensureDeferred(self.service.is_interested(self.event))) (
yield defer.ensureDeferred(
self.service.is_interested(self.event, self.store)
)
)
) )
@defer.inlineCallbacks @defer.inlineCallbacks
@ -64,7 +74,11 @@ class ApplicationServiceTestCase(unittest.TestCase):
self.event.type = "m.room.member" self.event.type = "m.room.member"
self.event.state_key = "@irc_foobar:matrix.org" self.event.state_key = "@irc_foobar:matrix.org"
self.assertTrue( self.assertTrue(
(yield defer.ensureDeferred(self.service.is_interested(self.event))) (
yield defer.ensureDeferred(
self.service.is_interested(self.event, self.store)
)
)
) )
@defer.inlineCallbacks @defer.inlineCallbacks
@ -74,7 +88,11 @@ class ApplicationServiceTestCase(unittest.TestCase):
) )
self.event.room_id = "!some_prefixs0m3th1nGsome_suffix:matrix.org" self.event.room_id = "!some_prefixs0m3th1nGsome_suffix:matrix.org"
self.assertTrue( self.assertTrue(
(yield defer.ensureDeferred(self.service.is_interested(self.event))) (
yield defer.ensureDeferred(
self.service.is_interested(self.event, self.store)
)
)
) )
@defer.inlineCallbacks @defer.inlineCallbacks
@ -84,7 +102,11 @@ class ApplicationServiceTestCase(unittest.TestCase):
) )
self.event.room_id = "!XqBunHwQIXUiqCaoxq:matrix.org" self.event.room_id = "!XqBunHwQIXUiqCaoxq:matrix.org"
self.assertFalse( self.assertFalse(
(yield defer.ensureDeferred(self.service.is_interested(self.event))) (
yield defer.ensureDeferred(
self.service.is_interested(self.event, self.store)
)
)
) )
@defer.inlineCallbacks @defer.inlineCallbacks
@ -183,7 +205,11 @@ class ApplicationServiceTestCase(unittest.TestCase):
self.event.content = {"membership": "invite"} self.event.content = {"membership": "invite"}
self.event.state_key = self.service.sender self.event.state_key = self.service.sender
self.assertTrue( self.assertTrue(
(yield defer.ensureDeferred(self.service.is_interested(self.event))) (
yield defer.ensureDeferred(
self.service.is_interested(self.event, self.store)
)
)
) )
@defer.inlineCallbacks @defer.inlineCallbacks