mirror of
https://git.anonymousland.org/anonymousland/synapse-product.git
synced 2024-12-11 13:34:18 -05:00
Add namespace constants. Add restrict_to option to limit namespace checks.
This commit is contained in:
parent
3bd2841fdb
commit
a060b47b13
@ -52,11 +52,12 @@ class ApplicationServicesHandler(BaseHandler):
|
|||||||
logger.info("Unregister as_token=%s", token)
|
logger.info("Unregister as_token=%s", token)
|
||||||
yield self.store.unregister_app_service(token)
|
yield self.store.unregister_app_service(token)
|
||||||
|
|
||||||
def get_services_for_event(self, event):
|
def get_services_for_event(self, event, restrict_to=""):
|
||||||
"""Retrieve a list of application services interested in this event.
|
"""Retrieve a list of application services interested in this event.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
event(Event): The event to check.
|
event(Event): The event to check.
|
||||||
|
restrict_to(str): The namespace to restrict regex tests to.
|
||||||
Returns:
|
Returns:
|
||||||
list<ApplicationService>: A list of services interested in this
|
list<ApplicationService>: A list of services interested in this
|
||||||
event based on the service regex.
|
event based on the service regex.
|
||||||
@ -66,7 +67,7 @@ class ApplicationServicesHandler(BaseHandler):
|
|||||||
|
|
||||||
interested_list = [
|
interested_list = [
|
||||||
s for s in self.store.get_app_services() if (
|
s for s in self.store.get_app_services() if (
|
||||||
s.is_interested(event, alias_list)
|
s.is_interested(event, restrict_to, alias_list)
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
return interested_list
|
return interested_list
|
||||||
@ -80,11 +81,15 @@ class ApplicationServicesHandler(BaseHandler):
|
|||||||
Args:
|
Args:
|
||||||
event(Event): The event to push out to interested services.
|
event(Event): The event to push out to interested services.
|
||||||
"""
|
"""
|
||||||
# TODO: Gather interested services
|
# Gather interested services
|
||||||
# get_services_for_event(event) <-- room IDs and user IDs
|
services = self.get_services_for_event(event)
|
||||||
# Get a list of room aliases. Check regex.
|
if len(services) == 0:
|
||||||
# TODO: If unknown user: poke User Query API.
|
return # no services need notifying
|
||||||
# TODO: If unknown room alias: poke Room Alias Query API.
|
|
||||||
|
|
||||||
# TODO: Fork off pushes to these services - XXX First cut, best effort
|
# Do we know this user exists? If not, poke the user query API for
|
||||||
pass
|
# all services which match that user regex.
|
||||||
|
|
||||||
|
# Do we know this room alias exists? If not, poke the room alias query
|
||||||
|
# API for all services which match that room alias regex.
|
||||||
|
|
||||||
|
# Fork off pushes to these services - XXX First cut, best effort
|
||||||
|
@ -23,12 +23,6 @@ from ._base import SQLBaseStore
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
namespace_enum = [
|
|
||||||
"users", # 0
|
|
||||||
"aliases", # 1
|
|
||||||
"rooms" # 2
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
# XXX: This feels like it should belong in a "models" module, not storage.
|
# XXX: This feels like it should belong in a "models" module, not storage.
|
||||||
class ApplicationService(object):
|
class ApplicationService(object):
|
||||||
@ -36,6 +30,10 @@ class ApplicationService(object):
|
|||||||
|
|
||||||
Provides methods to check if this service is "interested" in events.
|
Provides methods to check if this service is "interested" in events.
|
||||||
"""
|
"""
|
||||||
|
NS_USERS = "users"
|
||||||
|
NS_ALIASES = "aliases"
|
||||||
|
NS_ROOMS = "rooms"
|
||||||
|
NS_LIST = [NS_USERS, NS_ALIASES, NS_ROOMS]
|
||||||
|
|
||||||
def __init__(self, token, url=None, namespaces=None):
|
def __init__(self, token, url=None, namespaces=None):
|
||||||
self.token = token
|
self.token = token
|
||||||
@ -52,7 +50,7 @@ class ApplicationService(object):
|
|||||||
if not namespaces:
|
if not namespaces:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
for ns in ["users", "rooms", "aliases"]:
|
for ns in ApplicationService.NS_LIST:
|
||||||
if type(namespaces[ns]) != list:
|
if type(namespaces[ns]) != list:
|
||||||
raise ValueError("Bad namespace value for '%s'", ns)
|
raise ValueError("Bad namespace value for '%s'", ns)
|
||||||
for regex in namespaces[ns]:
|
for regex in namespaces[ns]:
|
||||||
@ -68,31 +66,36 @@ class ApplicationService(object):
|
|||||||
|
|
||||||
def _matches_user(self, event):
|
def _matches_user(self, event):
|
||||||
if (hasattr(event, "user_id") and
|
if (hasattr(event, "user_id") and
|
||||||
self._matches_regex(event.user_id, "users")):
|
self._matches_regex(
|
||||||
|
event.user_id, ApplicationService.NS_USERS)):
|
||||||
return True
|
return True
|
||||||
# also check m.room.member state key
|
# also check m.room.member state key
|
||||||
if (hasattr(event, "type") and event.type == EventTypes.Member
|
if (hasattr(event, "type") and event.type == EventTypes.Member
|
||||||
and hasattr(event, "state_key")
|
and hasattr(event, "state_key")
|
||||||
and self._matches_regex(event.state_key, "users")):
|
and self._matches_regex(
|
||||||
|
event.state_key, ApplicationService.NS_USERS)):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _matches_room_id(self, event):
|
def _matches_room_id(self, event):
|
||||||
if hasattr(event, "room_id"):
|
if hasattr(event, "room_id"):
|
||||||
return self._matches_regex(event.room_id, "rooms")
|
return self._matches_regex(
|
||||||
|
event.room_id, ApplicationService.NS_ROOMS
|
||||||
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _matches_aliases(self, event, alias_list):
|
def _matches_aliases(self, event, alias_list):
|
||||||
for alias in alias_list:
|
for alias in alias_list:
|
||||||
if self._matches_regex(alias, "aliases"):
|
if self._matches_regex(alias, ApplicationService.NS_ALIASES):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def is_interested(self, event, aliases_for_event=None):
|
def is_interested(self, event, restrict_to=None, aliases_for_event=None):
|
||||||
"""Check if this service is interested in this event.
|
"""Check if this service is interested in this event.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
event(Event): The event to check.
|
event(Event): The event to check.
|
||||||
|
restrict_to(str): The namespace to restrict regex tests to.
|
||||||
aliases_for_event(list): A list of all the known room aliases for
|
aliases_for_event(list): A list of all the known room aliases for
|
||||||
this event.
|
this event.
|
||||||
Returns:
|
Returns:
|
||||||
@ -100,6 +103,9 @@ class ApplicationService(object):
|
|||||||
"""
|
"""
|
||||||
if aliases_for_event is None:
|
if aliases_for_event is None:
|
||||||
aliases_for_event = []
|
aliases_for_event = []
|
||||||
|
if restrict_to not in ApplicationService.NS_LIST:
|
||||||
|
# this is a programming error, so raise a general exception
|
||||||
|
raise Exception("Unexpected restrict_to value: %s". restrict_to)
|
||||||
|
|
||||||
return (self._matches_user(event)
|
return (self._matches_user(event)
|
||||||
or self._matches_aliases(event, aliases_for_event)
|
or self._matches_aliases(event, aliases_for_event)
|
||||||
@ -215,7 +221,7 @@ class ApplicationServiceStore(SQLBaseStore):
|
|||||||
"DELETE FROM application_services_regex WHERE as_id=?",
|
"DELETE FROM application_services_regex WHERE as_id=?",
|
||||||
(as_id,)
|
(as_id,)
|
||||||
)
|
)
|
||||||
for (ns_int, ns_str) in enumerate(namespace_enum):
|
for (ns_int, ns_str) in enumerate(ApplicationService.NS_LIST):
|
||||||
if ns_str in service.namespaces:
|
if ns_str in service.namespaces:
|
||||||
for regex in service.namespaces[ns_str]:
|
for regex in service.namespaces[ns_str]:
|
||||||
txn.execute(
|
txn.execute(
|
||||||
@ -285,9 +291,9 @@ class ApplicationServiceStore(SQLBaseStore):
|
|||||||
"url": res["url"],
|
"url": res["url"],
|
||||||
"token": as_token,
|
"token": as_token,
|
||||||
"namespaces": {
|
"namespaces": {
|
||||||
"users": [],
|
ApplicationService.NS_USERS: [],
|
||||||
"aliases": [],
|
ApplicationService.NS_ALIASES: [],
|
||||||
"rooms": []
|
ApplicationService.NS_ROOMS: []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
# add the namespace regex if one exists
|
# add the namespace regex if one exists
|
||||||
@ -295,7 +301,8 @@ class ApplicationServiceStore(SQLBaseStore):
|
|||||||
if ns_int is None:
|
if ns_int is None:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
services[as_token]["namespaces"][namespace_enum[ns_int]].append(
|
services[as_token]["namespaces"][
|
||||||
|
ApplicationService.NS_LIST[ns_int]].append(
|
||||||
res["regex"]
|
res["regex"]
|
||||||
)
|
)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
|
Loading…
Reference in New Issue
Block a user