From f2b1de794038c379877bc2dc79045fc50c2693ba Mon Sep 17 00:00:00 2001 From: Zev Benjamin Date: Fri, 22 Mar 2013 13:21:50 -0400 Subject: [PATCH] Apply events in register() (imported from commit 4e30e52d968ceec596ecbabe71d4d9378cc8f7d9) --- zephyr/lib/event_queue.py | 16 +++++++++++++++- zephyr/views.py | 20 ++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/zephyr/lib/event_queue.py b/zephyr/lib/event_queue.py index 82cf7a60e0..9fc1d6f03e 100644 --- a/zephyr/lib/event_queue.py +++ b/zephyr/lib/event_queue.py @@ -172,7 +172,8 @@ def setup_event_queue(io_loop): send_restart_events() -# Called from Django +# The following functions are called from Django + def request_event_queue(user_profile, apply_markdown): if settings.TORNADO_SERVER: req = {'dont_block' : 'true', @@ -188,3 +189,16 @@ def request_event_queue(user_profile, apply_markdown): return resp.json['queue_id'] return None + +def get_user_events(user_profile, queue_id, last_event_id): + if settings.TORNADO_SERVER: + resp = requests.get(settings.TORNADO_SERVER + '/api/v1/events', + auth=requests.auth.HTTPBasicAuth(user_profile.user.email, + user_profile.api_key), + params={'queue_id' : queue_id, + 'last_event_id': last_event_id, + 'client' : 'internal'}) + + resp.raise_for_status() + + return resp.json['events'] diff --git a/zephyr/views.py b/zephyr/views.py index ca05cc1d9e..62f18f71bb 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -43,7 +43,7 @@ from zephyr.lib.response import json_success, json_error, json_response, json_me from zephyr.lib.timestamp import timestamp_to_datetime, datetime_to_timestamp from zephyr.lib.cache import cache_with_key from zephyr.lib.unminify import SourceMap -from zephyr.lib.event_queue import request_event_queue +from zephyr.lib.event_queue import request_event_queue, get_user_events from zephyr import tornado_callbacks from confirmation.models import Confirmation @@ -1423,7 +1423,11 @@ def events_register_backend(request, user_profile, apply_markdown=True, ret = {'queue_id': queue_id} event_types = set(event_types) + # Fetch initial data if "message" in event_types: + # The client should use get_old_messages() to fetch messages + # starting with the max_message_id. They will get messages + # newer than that ID via get_events() messages = Message.objects.filter(usermessage__user_profile=user_profile).order_by('-id')[:1] if messages: ret['max_message_id'] = messages[0].id @@ -1432,5 +1436,17 @@ def events_register_backend(request, user_profile, apply_markdown=True, if "pointer" in event_types: ret['pointer'] = user_profile.pointer - ret['last_event_id'] = -1 + # Apply events that came in while we were fetching initial data + events = get_user_events(user_profile, queue_id, -1) + for event in events: + if event['type'] == "message": + ret['max_message_id'] = max(ret['max_message_id'], event['message']['id']) + elif event['type'] == "pointer": + ret['pointer'] = max(ret['pointer'], event['pointer']) + + if events: + ret['last_event_id'] = events[-1:]['id'] + else: + ret['last_event_id'] = -1 + return json_success(ret)