# -*- 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 synapse.api.errors import StoreError

from ._base import SQLBaseStore


class ApplicationServiceStore(SQLBaseStore):

    def __init__(self, hs):
        super(ApplicationServiceStore, self).__init__(hs)

        self.clock = hs.get_clock()

    @defer.inlineCallbacks
    def get_app_service(self, as_token):
        """Get the application service with the given token.

        Args:
            token (str): The application service token.
        Raises:
            StoreError if there was a problem retrieving this.
        """
        row = self._simple_select_one(
            "application_services", {"token": as_token},
            ["url", "token"]
        )
        if not row:
            raise StoreError(400, "Bad application services token supplied.")
        defer.returnValue(row)

@@ -709,16 +709,1882 @@
 Store%0A%0A%0A
+# XXX: This feels like it should belong in a %22models%22 module, not storage.%0Aclass ApplicationService(object):%0A    %22%22%22Defines an application service.%0A%0A    Provides methods to check if this service is %22interested%22 in events.%0A    %22%22%22%0A%0A    def __init__(self, token, url=None, namespaces=None):%0A        self.token = token%0A        if url:%0A            self.url = url%0A        if namespaces:%0A            self.namespaces = namespaces%0A%0A    def is_interested(self, event):%0A        %22%22%22Check if this service is interested in this event.%0A%0A        Args:%0A            event(Event): The event to check.%0A        Returns:%0A            bool: True if this service would like to know about this event.%0A        %22%22%22%0A        # NB: This does not check room alias regex matches because that requires%0A        # more context that an Event can provide. Room alias matches are checked%0A        # in the ApplicationServiceHandler.%0A%0A        # TODO check if event.room_id regex matches%0A        # TODO check if event.user_id regex matches (or m.room.member state_key)%0A%0A        return True%0A%0A%0Aclass ApplicationServiceCache(object):%0A    %22%22%22Caches ApplicationServices and provides utility functions on top.%0A%0A    This class is designed to be invoked on incoming events in order to avoid%0A    hammering the database every time to extract a list of application service%0A    regexes.%0A    %22%22%22%0A%0A    def __init__(self):%0A        self.services = %5B%5D%0A%0A    def get_services_for_event(self, event):%0A        %22%22%22Retrieve a list of application services interested in this event.%0A%0A        Args:%0A            event(Event): The event to check.%0A        Returns:%0A            list%3CApplicationService%3E: A list of services interested in this%0A            event based on the service regex.%0A        %22%22%22%0A        interested_list = %5B%0A            s for s in self.services if s.is_event_claimed(event)%0A        %5D%0A        return interested_list%0A%0A%0A
 class Ap
@@ -2707,16 +2707,62 @@
 t__(hs)%0A
+        self.cache = ApplicationServiceCache()
 %0A       

Add AS specific classes with docstrings.