示例#1
0
    def test_new(self):
        ee = EventEmitter(new_listener=True)
        stack = []

        @ee.on("new_listener")
        def handler(func, event=None):
            stack.append((func, event))

        def newhandler():
            pass

        ee.on("new", newhandler)
        ee.on_any(newhandler)

        self.assertTrue(stack[-2] == (newhandler, "new"))
        self.assertTrue(stack[-1] == (newhandler, None))
示例#2
0
class AulaActor:
    def __init__(self, websocket_manager, rc_service, models):
        self.websocket_manager = websocket_manager
        self.rc_service = rc_service
        self.models = models
        self.event_emitter = EventEmitter()
        self.bind_events()

    def emit_event(self, event):
        self.event_emitter.emit(event.get("action"), event)

    def bind_events(self):
        self.event_emitter.on("aula.add_room", self.on_add_room)
        self.event_emitter.on("aula.move_student", self.on_move_student)
        self.event_emitter.on_any(func=self.broadcast_to_aula)

    def broadcast_to_aula(self, event):
        """ Adapts AulaService's messages to the WebsocketManager's broadcast interface"""
        active_session_id = event.get("active_session_id")
        aula_channel = f"aula-{active_session_id}"
        self.websocket_manager.broadcast(event, aula_channel)

    def on_add_room(self, event):
        room_name = event.get("data").get("room")
        slug = event.get("data").get("slug")
        channel_name = f"{room_name}-{slug}"
        res = self.rc_service.create_channel(channel_name)
        LOG.info(f"Result of adding room:", res)

    def on_move_student(self, event):
        data = event.get("data")
        to_channel = data.get("to_room")
        from_channel = data.get("from_room")
        student_id = data.get("student")
        slug = data.get("slug")
        from_room_name = f"{from_channel}-{slug}"
        to_room_name = f"{to_channel}-{slug}"
        user = self.models.User.query.filter_by(id=student_id).one()
        remove_res = self.rc_service.remove_user_from_channel(
            user.rocketchat_id, channel_name=from_room_name)
        add_res = self.rc_service.add_user_to_channel(
            user.rocketchat_id, channel_name=to_room_name)
        LOG.debug(f"User channel add result: {add_res}")