Move new event boilerplate in room handlers into a method on a base clase.

This commit is contained in:
Mark Haines 2014-08-26 18:49:51 +01:00
parent 64e2a5d58e
commit a498df0428
2 changed files with 33 additions and 52 deletions

View File

@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from twisted.internet import defer
class BaseHandler(object): class BaseHandler(object):
@ -26,3 +26,21 @@ class BaseHandler(object):
self.state_handler = hs.get_state_handler() self.state_handler = hs.get_state_handler()
self.distributor = hs.get_distributor() self.distributor = hs.get_distributor()
self.hs = hs self.hs = hs
class BaseRoomHandler(BaseHandler):
@defer.inlineCallbacks
def _on_new_room_event(self, event, snapshot, extra_destinations=[]):
store_id = yield self.store.persist_event(event)
destinations = set(extra_destinations)
# Send a PDU to all hosts who have joined the room.
destinations.update((yield self.store.get_joined_hosts_for_room(
event.room_id
)))
event.destinations = list(destinations)
self.notifier.on_new_room_event(event, store_id)
yield self.hs.get_federation().handle_new_event(event, snapshot)

View File

@ -25,14 +25,14 @@ from synapse.api.events.room import (
from synapse.api.streams.event import EventStream, EventsStreamData from synapse.api.streams.event import EventStream, EventsStreamData
from synapse.handlers.presence import PresenceStreamData from synapse.handlers.presence import PresenceStreamData
from synapse.util import stringutils from synapse.util import stringutils
from ._base import BaseHandler from ._base import BaseRoomHandler
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class MessageHandler(BaseHandler): class MessageHandler(BaseRoomHandler):
def __init__(self, hs): def __init__(self, hs):
super(MessageHandler, self).__init__(hs) super(MessageHandler, self).__init__(hs)
@ -89,16 +89,7 @@ class MessageHandler(BaseHandler):
if not suppress_auth: if not suppress_auth:
yield self.auth.check(event, snapshot, raises=True) yield self.auth.check(event, snapshot, raises=True)
# store message in db yield self._on_new_room_event(event, snapshot)
store_id = yield self.store.persist_event(event)
event.destinations = yield self.store.get_joined_hosts_for_room(
event.room_id
)
self.notifier.on_new_room_event(event, store_id)
yield self.hs.get_federation().handle_new_event(event, snapshot)
@defer.inlineCallbacks @defer.inlineCallbacks
def get_messages(self, user_id=None, room_id=None, pagin_config=None, def get_messages(self, user_id=None, room_id=None, pagin_config=None,
@ -144,15 +135,7 @@ class MessageHandler(BaseHandler):
yield self.state_handler.handle_new_event(event) yield self.state_handler.handle_new_event(event)
# store in db yield self._on_new_room_event(event, snapshot)
store_id = yield self.store.persist_event(event)
event.destinations = yield self.store.get_joined_hosts_for_room(
event.room_id
)
self.notifier.on_new_room_event(event, store_id)
yield self.hs.get_federation().handle_new_event(event, snapshot)
@defer.inlineCallbacks @defer.inlineCallbacks
def get_room_data(self, user_id=None, room_id=None, def get_room_data(self, user_id=None, room_id=None,
@ -226,14 +209,7 @@ class MessageHandler(BaseHandler):
yield self.auth.check(event, snapshot, raises=True) yield self.auth.check(event, snapshot, raises=True)
# store message in db # store message in db
store_id = yield self.store.persist_event(event) yield self._on_new_room_event(event, snapshot)
event.destinations = yield self.store.get_joined_hosts_for_room(
event.room_id
)
yield self.hs.get_federation().handle_new_event(event, snapshot)
self.notifier.on_new_room_event(event, store_id)
@defer.inlineCallbacks @defer.inlineCallbacks
def snapshot_all_rooms(self, user_id=None, pagin_config=None, def snapshot_all_rooms(self, user_id=None, pagin_config=None,
@ -311,7 +287,7 @@ class MessageHandler(BaseHandler):
defer.returnValue(ret) defer.returnValue(ret)
class RoomCreationHandler(BaseHandler): class RoomCreationHandler(BaseRoomHandler):
@defer.inlineCallbacks @defer.inlineCallbacks
def create_room(self, user_id, room_id, config): def create_room(self, user_id, room_id, config):
@ -417,7 +393,7 @@ class RoomCreationHandler(BaseHandler):
defer.returnValue(result) defer.returnValue(result)
class RoomMemberHandler(BaseHandler): class RoomMemberHandler(BaseRoomHandler):
# TODO(paul): This handler currently contains a messy conflation of # TODO(paul): This handler currently contains a messy conflation of
# low-level API that works on UserID objects and so on, and REST-level # low-level API that works on UserID objects and so on, and REST-level
# API that takes ID strings and returns pagination chunks. These concerns # API that takes ID strings and returns pagination chunks. These concerns
@ -707,39 +683,26 @@ class RoomMemberHandler(BaseHandler):
defer.returnValue([r.room_id for r in rooms]) defer.returnValue([r.room_id for r in rooms])
@defer.inlineCallbacks
def _do_local_membership_update(self, event, membership, snapshot): def _do_local_membership_update(self, event, membership, snapshot):
# store membership destinations = []
store_id = yield self.store.persist_event(event)
# Send a PDU to all hosts who have joined the room.
destinations = yield self.store.get_joined_hosts_for_room(
event.room_id
)
# If we're inviting someone, then we should also send it to that # If we're inviting someone, then we should also send it to that
# HS. # HS.
target_user_id = event.state_key target_user_id = event.state_key
if membership == Membership.INVITE: if membership == Membership.INVITE:
host = UserID.from_string( host = UserID.from_string(target_user_id, self.hs).domain
target_user_id, self.hs
).domain
destinations.append(host) destinations.append(host)
# If we are joining a remote HS, include that. # If we are joining a remote HS, include that.
if membership == Membership.JOIN: if membership == Membership.JOIN:
host = UserID.from_string( host = UserID.from_string(target_user_id, self.hs).domain
target_user_id, self.hs
).domain
destinations.append(host) destinations.append(host)
event.destinations = list(set(destinations)) return self._on_new_room_event(
event, snapshot, extra_destinations=destinations
)
yield self.hs.get_federation().handle_new_event(event, snapshot) class RoomListHandler(BaseRoomHandler):
self.notifier.on_new_room_event(event, store_id)
class RoomListHandler(BaseHandler):
@defer.inlineCallbacks @defer.inlineCallbacks
def get_public_room_list(self): def get_public_room_list(self):