def sync_chatbox(request, target):
    """
    Depending on the target argument value, the view either returns
    new chat requests for the logged in user or a list of online users,
    provided by the useractivity app.
    """
    if target == "chat_requests":
        now = datetime.now()

        # Checking for the timestamp in session data, explanations
        # are below.
        timestamp = request.session.get("im:chat_requests:sync", now)
        chat_requests = ChatRequest.objects.incoming(request.user, timestamp)
        data = map(
            lambda chat_request: render_to(request, "im/chat_request.html", {"chat_request": chat_request}),
            chat_requests,
        )

        # Saving last check timestamp in session data, so we can later
        # determine which requests were already sent to the browser.
        request.session["im:chat_requests:sync"] = now
    elif target == "online_users":
        friends = friend_set_for(request.user)
        online_users = get_online_users()
        if request.user in online_users:
            online_users.remove(request.user)
        online_friends = online_users & friends
        online_others = online_users - friends

        data = render_to(request, "im/userlist.html", {"friends": online_friends, "others": online_others})

    return json_response(data)
def sync_chatbox(request, target):
    """
    Depending on the target argument value, the view either returns
    new chat requests for the logged in user or a list of online users,
    provided by the useractivity app.
    """
    if target == "chat_requests":
        now = datetime.now()

        # Checking for the timestamp in session data, explanations
        # are below.
        timestamp = request.session.get("im:chat_requests:sync", now)
        chat_requests = ChatRequest.objects.incoming(
            request.user,
            timestamp,
        )
        data = map(
            lambda chat_request: render_to(request, "im/chat_request.html",
                                           {"chat_request": chat_request}),
            chat_requests)

        # Saving last check timestamp in session data, so we can later
        # determine which requests were already sent to the browser.
        request.session["im:chat_requests:sync"] = now
    elif target == "online_users":
        friends = friend_set_for(request.user)
        online_users = get_online_users()
        if request.user in online_users:
            online_users.remove(request.user)
        online_friends = online_users & friends
        online_others = online_users - friends

        data = render_to(request, "im/userlist.html", {
            "friends": online_friends,
            "others": online_others
        })

    return json_response(data)
def online_users(request):
    """Returns context variable with a set of users online."""
    if request.user.is_authenticated():
        return {"ONLINE_USERS": get_online_users()}
    return {}
def online_users(request):
    """Returns context variable with a set of users online."""
    if request.user.is_authenticated():
        return {"ONLINE_USERS": get_online_users()}
    return {}