def get_events_backend(request, user_profile, handler = None, user_client = REQ(converter=get_client, default=None), last_event_id = REQ(converter=int, default=None), queue_id = REQ(default=None), apply_markdown = REQ(default=False, validator=check_bool), all_public_streams = REQ(default=False, validator=check_bool), event_types = REQ(default=None, validator=check_list(check_string)), dont_block = REQ(default=False, validator=check_bool), narrow = REQ(default=[], validator=check_list(None)), lifespan_secs = REQ(default=0, converter=int)): if user_client is None: user_client = request.client was_connected = False orig_queue_id = queue_id if queue_id is None: if dont_block: client = allocate_client_descriptor(user_profile.id, user_profile.realm.id, event_types, user_client, apply_markdown, all_public_streams, lifespan_secs, narrow=narrow) queue_id = client.event_queue.id else: return json_error("Missing 'queue_id' argument") else: if last_event_id is None: return json_error("Missing 'last_event_id' argument") client = get_client_descriptor(queue_id) if client is None: return json_error("Bad event queue id: %s" % (queue_id,)) if user_profile.id != client.user_profile_id: return json_error("You are not authorized to get events from this queue") client.event_queue.prune(last_event_id) was_connected = client.finish_current_handler() if not client.event_queue.empty() or dont_block: ret = {'events': client.event_queue.contents()} if orig_queue_id is None: ret['queue_id'] = queue_id request._log_data['extra'] = "[%s/%s]" % (queue_id, len(ret["events"])) if was_connected: request._log_data['extra'] += " [was connected]" return json_success(ret) handler._request = request if was_connected: logging.info("Disconnected handler for queue %s (%s/%s)" % (queue_id, user_profile.email, user_client.name)) client.connect_handler(handler) # runtornado recognizes this special return value. return RespondAsynchronously
def do_test(self, action, event_types=None): client = allocate_client_descriptor(self.user_profile.id, self.user_profile.realm.id, event_types, get_client("website"), True, False, 600, []) # hybrid_state = initial fetch state + re-applying events triggered by our action # normal_state = do action then fetch at the end (the "normal" code path) hybrid_state = fetch_initial_state_data(self.user_profile, event_types, "") action() events = client.event_queue.contents() self.assertTrue(len(events) > 0) apply_events(hybrid_state, events, self.user_profile) normal_state = fetch_initial_state_data(self.user_profile, event_types, "") self.match_states(hybrid_state, normal_state) return events
def do_test(self, action, event_types=None): client = allocate_client_descriptor(self.user_profile.id, self.user_profile.email, self.user_profile.realm.id, event_types, "website", True, False, 600, []) # hybrid_state = initial fetch state + re-applying events triggered by our action # normal_state = do action then fetch at the end (the "normal" code path) hybrid_state = fetch_initial_state_data(self.user_profile, event_types, "") action() events = client.event_queue.contents() self.assertTrue(len(events) > 0) apply_events(hybrid_state, events, self.user_profile) normal_state = fetch_initial_state_data(self.user_profile, event_types, "") self.match_states(hybrid_state, normal_state) return events
def do_test(self, action, event_types=None): client = allocate_client_descriptor( dict(user_profile_id = self.user_profile.id, user_profile_email = self.user_profile.email, realm_id = self.user_profile.realm.id, event_types = event_types, client_type_name = "website", apply_markdown = True, all_public_streams = False, queue_timeout = 600, last_connection_time = time.time(), narrow = []) ) # hybrid_state = initial fetch state + re-applying events triggered by our action # normal_state = do action then fetch at the end (the "normal" code path) hybrid_state = fetch_initial_state_data(self.user_profile, event_types, "") action() events = client.event_queue.contents() self.assertTrue(len(events) > 0) apply_events(hybrid_state, events, self.user_profile) normal_state = fetch_initial_state_data(self.user_profile, event_types, "") self.match_states(hybrid_state, normal_state) return events