2015-01-27 10:50:28 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2015 OpenMarket Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
|
|
|
from ._base import BaseHandler
|
2015-01-28 06:59:38 -05:00
|
|
|
from synapse.api.errors import Codes, StoreError, SynapseError
|
2015-02-03 09:44:16 -05:00
|
|
|
from synapse.appservice import ApplicationService
|
2015-02-04 06:19:18 -05:00
|
|
|
from synapse.appservice.api import ApplicationServiceApi
|
2015-01-27 10:50:28 -05:00
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2015-02-04 07:24:20 -05:00
|
|
|
# NB: Purposefully not inheriting BaseHandler since that contains way too much
|
|
|
|
# setup code which this handler does not need or use. This makes testing a lot
|
|
|
|
# easier.
|
|
|
|
class ApplicationServicesHandler(object):
|
2015-01-27 10:50:28 -05:00
|
|
|
|
|
|
|
def __init__(self, hs):
|
2015-02-04 07:24:20 -05:00
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.hs = hs
|
2015-02-04 06:19:18 -05:00
|
|
|
self.appservice_api = ApplicationServiceApi(hs)
|
2015-01-27 10:50:28 -05:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2015-01-27 12:15:06 -05:00
|
|
|
def register(self, app_service):
|
2015-01-27 12:34:40 -05:00
|
|
|
logger.info("Register -> %s", app_service)
|
2015-01-27 10:50:28 -05:00
|
|
|
# check the token is recognised
|
|
|
|
try:
|
2015-02-03 06:26:33 -05:00
|
|
|
stored_service = yield self.store.get_app_service_by_token(
|
|
|
|
app_service.token
|
|
|
|
)
|
2015-01-27 12:15:06 -05:00
|
|
|
if not stored_service:
|
2015-02-03 06:26:33 -05:00
|
|
|
raise StoreError(404, "Application service not found")
|
2015-01-27 10:50:28 -05:00
|
|
|
except StoreError:
|
|
|
|
raise SynapseError(
|
|
|
|
403, "Unrecognised application services token. "
|
2015-01-28 06:59:38 -05:00
|
|
|
"Consult the home server admin.",
|
|
|
|
errcode=Codes.FORBIDDEN
|
2015-01-27 10:50:28 -05:00
|
|
|
)
|
2015-02-02 12:39:41 -05:00
|
|
|
logger.info("Updating application service info...")
|
|
|
|
yield self.store.update_app_service(app_service)
|
2015-01-27 11:53:59 -05:00
|
|
|
|
|
|
|
def unregister(self, token):
|
2015-01-27 12:34:40 -05:00
|
|
|
logger.info("Unregister as_token=%s", token)
|
2015-01-27 11:53:59 -05:00
|
|
|
yield self.store.unregister_app_service(token)
|
|
|
|
|
2015-02-03 08:17:28 -05:00
|
|
|
def get_services_for_event(self, event, restrict_to=""):
|
2015-02-03 06:26:33 -05:00
|
|
|
"""Retrieve a list of application services interested in this event.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
event(Event): The event to check.
|
2015-02-03 08:17:28 -05:00
|
|
|
restrict_to(str): The namespace to restrict regex tests to.
|
2015-02-03 06:26:33 -05:00
|
|
|
Returns:
|
|
|
|
list<ApplicationService>: A list of services interested in this
|
|
|
|
event based on the service regex.
|
|
|
|
"""
|
|
|
|
# We need to know the aliases associated with this event.room_id, if any
|
|
|
|
alias_list = [] # TODO
|
|
|
|
|
|
|
|
interested_list = [
|
|
|
|
s for s in self.store.get_app_services() if (
|
2015-02-03 08:17:28 -05:00
|
|
|
s.is_interested(event, restrict_to, alias_list)
|
2015-02-03 06:26:33 -05:00
|
|
|
)
|
|
|
|
]
|
|
|
|
return interested_list
|
|
|
|
|
2015-01-27 11:53:59 -05:00
|
|
|
def notify_interested_services(self, event):
|
|
|
|
"""Notifies (pushes) all application services interested in this event.
|
|
|
|
|
|
|
|
Pushing is done asynchronously, so this method won't block for any
|
|
|
|
prolonged length of time.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
event(Event): The event to push out to interested services.
|
|
|
|
"""
|
2015-02-03 08:17:28 -05:00
|
|
|
# Gather interested services
|
|
|
|
services = self.get_services_for_event(event)
|
|
|
|
if len(services) == 0:
|
|
|
|
return # no services need notifying
|
|
|
|
|
|
|
|
# Do we know this user exists? If not, poke the user query API for
|
|
|
|
# all services which match that user regex.
|
2015-02-03 08:29:27 -05:00
|
|
|
unknown_user = False # TODO check
|
|
|
|
if unknown_user:
|
|
|
|
user_query_services = self.get_services_for_event(
|
|
|
|
event=event,
|
|
|
|
restrict_to=ApplicationService.NS_USERS
|
|
|
|
)
|
|
|
|
for user_service in user_query_services:
|
2015-02-03 09:44:16 -05:00
|
|
|
# this needs to block XXX: Need to feed response back to caller
|
2015-02-04 06:19:18 -05:00
|
|
|
is_known_user = self.appservice_api.query_user(
|
|
|
|
user_service, event
|
|
|
|
)
|
|
|
|
if is_known_user:
|
|
|
|
# the user exists now,so don't query more ASes.
|
|
|
|
break
|
2015-02-03 08:17:28 -05:00
|
|
|
|
|
|
|
# Do we know this room alias exists? If not, poke the room alias query
|
|
|
|
# API for all services which match that room alias regex.
|
2015-02-03 08:29:27 -05:00
|
|
|
unknown_room_alias = False # TODO check
|
|
|
|
if unknown_room_alias:
|
|
|
|
alias_query_services = self.get_services_for_event(
|
|
|
|
event=event,
|
|
|
|
restrict_to=ApplicationService.NS_ALIASES
|
|
|
|
)
|
|
|
|
for alias_service in alias_query_services:
|
2015-02-03 09:44:16 -05:00
|
|
|
# this needs to block XXX: Need to feed response back to caller
|
2015-02-04 06:19:18 -05:00
|
|
|
is_known_alias = self.appservice_api.query_alias(
|
|
|
|
alias_service, event
|
|
|
|
)
|
|
|
|
if is_known_alias:
|
|
|
|
# the alias exists now so don't query more ASes.
|
|
|
|
break
|
2015-02-03 08:17:28 -05:00
|
|
|
|
|
|
|
# Fork off pushes to these services - XXX First cut, best effort
|
2015-02-03 08:29:27 -05:00
|
|
|
for service in services:
|
2015-02-04 06:19:18 -05:00
|
|
|
self.appservice_api.push(service, event)
|