# Buggy code
def missedmessage_hook(user_profile_id, queue, last_for_client):
    # type: (int, ClientDescriptor, bool) -> None
    # Only process missedmessage hook when the last queue for a
    # client has been garbage collected
    if not last_for_client:
        return

    message_ids_to_notify = []  # type: List[Dict[str, Any]]
    for event in queue.event_queue.contents():
        if event["type"] != "message":
            continue
        assert "flags" in event

        flags = event["flags"]
        mentioned = "mentioned" in flags and "read" not in flags
        if mentioned:
            notify_info = dict(message_id=event["message"]["id"])

            if not event.get("push_notified", False):
                notify_info["send_push"] = True
            if not event.get("email_notified", False):
                notify_info["send_email"] = True
            message_ids_to_notify.append(notify_info)

    for notify_info in message_ids_to_notify:
        msg_id = notify_info["message_id"]
        notice = build_offline_notification(user_profile_id, msg_id)
        if notify_info.get("send_push", False):
            queue_json_publish(
                "missedmessage_mobile_notifications", notice, lambda notice: None
            )
        if notify_info.get("send_email", False):
            queue_json_publish("missedmessage_emails", notice, lambda notice: None)


# Traceback (KeyError)
2017-09-19 22:35:36,955 ERROR    Problem handling data on queue missedmessage_mobile_notifications
Traceback (most recent call last):
File "/home/zulip/deployments/2017-09-16-11-37-40/zerver/worker/queue_processors.py", line 120, in consume_wrapper
self.consume(data)
File "/home/zulip/deployments/2017-09-16-11-37-40/zerver/worker/queue_processors.py", line 262, in consume
handle_push_notification(data['user_profile_id'], data)
File "/home/zulip/deployments/2017-09-16-11-37-40/zerver/decorator.py", line 624, in wrapped_func
ret = func(*args, **kwargs)
File "/home/zulip/deployments/2017-09-16-11-37-40/zerver/lib/push_notifications.py", line 402, in handle_push_notification
triggers = missed_message['triggers']
KeyError: 'triggers'

# Fixed code
def missedmessage_hook(user_profile_id, queue, last_for_client):
    # type: (int, ClientDescriptor, bool) -> None
    # Only process missedmessage hook when the last queue for a
    # client has been garbage collected
    if not last_for_client:
        return

    for event in queue.event_queue.contents():
        if event["type"] != "message":
            continue
        assert "flags" in event

        flags = event["flags"]
        mentioned = "mentioned" in flags and "read" not in flags
        # TODO: These next variables should be extracted from the
        # event, but to match the historical effect of this function
        # in only supporting mentions, we've just hardcoded them to
        # False.  Fixing this will correct currently buggy behavior in
        # our handling of private message and users who've requested
        # stream push notifications.
        private_message = False
        stream_push_notify = False
        stream_name = None
        always_push_notify = False
        # Since we just GC'd the last event queue, the user is definitely idle.
        idle = True

        message_id = event["message"]["id"]
        maybe_enqueue_notifications(
            user_profile_id,
            message_id,
            private_message,
            mentioned,
            stream_push_notify,
            stream_name,
            always_push_notify,
            idle,
        )