Example #1
0
def cast_vote(user, thing, direction, **data):
    """Register a vote and queue it for processing."""
    update_vote_lookups(user, thing, direction)

    vote_data = {
        "user_id": user._id,
        "thing_fullname": thing._fullname,
        "direction": direction,
        "date": int(epoch_timestamp(datetime.now(g.tz))),
    }

    data['ip'] = getattr(request, "ip", None)
    if data['ip'] is not None:
        data['org'] = organization_by_ips(data['ip'])
    vote_data['data'] = data

    hooks.get_hook("vote.get_vote_data").call(
        data=vote_data["data"],
        user=user,
        thing=thing,
        request=request,
        context=c,
    )

    # The vote event will actually be sent from an async queue processor, so
    # we need to pull out the context data at this point
    if not g.running_as_script:
        vote_data["event_data"] = {
            "context": Event.get_context_data(request, c),
            "sensitive": Event.get_sensitive_context_data(request, c),
        }

    amqp.add_item(thing.vote_queue_name, json.dumps(vote_data))
Example #2
0
def cast_vote(user, thing, direction, **data):
    """Register a vote and queue it for processing."""
    update_vote_lookups(user, thing, direction)

    vote_data = {
        "user_id": user._id,
        "thing_fullname": thing._fullname,
        "direction": direction,
        "date": int(epoch_timestamp(datetime.now(g.tz))),
    }

    data['ip'] = getattr(request, "ip", None)
    if data['ip'] is not None:
        data['org'] = organization_by_ips(data['ip'])
    vote_data['data'] = data

    hooks.get_hook("vote.get_vote_data").call(
        data=vote_data["data"],
        user=user,
        thing=thing,
        request=request,
        context=c,
    )

    # The vote event will actually be sent from an async queue processor, so
    # we need to pull out the context data at this point
    if not g.running_as_script:
        vote_data["event_data"] = {
            "context": Event.get_context_data(request, c),
            "sensitive": Event.get_sensitive_context_data(request, c),
        }

    amqp.add_item(thing.vote_queue_name, json.dumps(vote_data))
Example #3
0
def cast_vote(user, thing, direction, **data):
    """Register a vote and queue it for processing."""
    if not isinstance(thing, (Link, Comment)):
        return

    update_vote_lookups(user, thing, direction)

    vote_data = {
        "user_id": user._id,
        "thing_fullname": thing._fullname,
        "direction": direction,
        "date": int(epoch_timestamp(datetime.now(g.tz))),
    }

    data['ip'] = getattr(request, "ip", None)
    if data['ip'] is not None:
        data['org'] = organization_by_ips(data['ip'])
    vote_data['data'] = data

    hooks.get_hook("vote.get_vote_data").call(
        data=vote_data["data"],
        user=user,
        thing=thing,
        request=request,
        context=c,
    )

    # The vote event will actually be sent from an async queue processor, so
    # we need to pull out the context data at this point
    if not g.running_as_script:
        vote_data["event_data"] = {
            "context": Event.get_context_data(request, c),
            "sensitive": Event.get_sensitive_context_data(request, c),
        }

    try:
        vote_dump = json.dumps(vote_data)
    except UnicodeDecodeError:
        g.log.error("Got weird unicode in the vote data: %r", vote_data)
        return

    if isinstance(thing, Link):
        queue = "vote_link_q"
    elif isinstance(thing, Comment):
        queue = "vote_comment_q"

    amqp.add_item(queue, vote_dump)
Example #4
0
def cast_vote(user, thing, direction, **data):
    """Register a vote and queue it for processing."""
    if not isinstance(thing, (Link, Comment)):
        return

    update_vote_lookups(user, thing, direction)

    vote_data = {
        "user_id": user._id,
        "thing_fullname": thing._fullname,
        "direction": direction,
        "date": int(epoch_timestamp(datetime.now(g.tz))),
    }

    data['ip'] = getattr(request, "ip", None)
    if data['ip'] is not None:
        data['org'] = organization_by_ips(data['ip'])
    vote_data['data'] = data

    hooks.get_hook("vote.get_vote_data").call(
        data=vote_data["data"],
        user=user,
        thing=thing,
        request=request,
        context=c,
    )

    # The vote event will actually be sent from an async queue processor, so
    # we need to pull out the context data at this point
    if not g.running_as_script:
        vote_data["event_data"] = {
            "context": Event.get_context_data(request, c),
            "sensitive": Event.get_sensitive_context_data(request, c),
        }

    try:
        vote_dump = json.dumps(vote_data)
    except UnicodeDecodeError:
        g.log.error("Got weird unicode in the vote data: %r", vote_data)
        return

    if isinstance(thing, Link):
        queue = "vote_link_q"
    elif isinstance(thing, Comment):
        queue = "vote_comment_q"

    amqp.add_item(queue, vote_dump)
Example #5
0
def place_pixel(x, y, color):
    event = Event(
        topic="place_events",
        event_type="ss.place_pixel",
        time=datetime.datetime.now().replace(tzinfo=pytz.UTC),
        request=request,
        context=c,
        data={
            'x_coordinate': x,
            'y_coordinate': y,
            'color': color,
        },
    )
    g.events.save_event(event)
Example #6
0
def cast_vote(user, thing, direction, **data):
    """Register a vote and queue it for processing."""
    if not isinstance(thing, (Link, Comment)):
        return

    # CUSTOM: voting model, validate direction
    if direction not in (Vote.DIRECTIONS.up, Vote.DIRECTIONS.down, Vote.DIRECTIONS.unup, Vote.DIRECTIONS.undown):
        g.log.warning("!!! cast_vote() discarding vote with dir: %s" % direction)
        return

    # CUSTOM: voting model, use direction as state
    # NOTE: vote_direction is tracked in addition to direction for easy updating of _likes, _dislikes, and karma in Vote._commit()
    vote_direction = direction
    previous_vote = VoteDetailsByThing.get_vote(user, thing)
    if previous_vote:
        if direction == Vote.DIRECTIONS.up: # interesting/liked
            if previous_vote.is_offonvote:
                direction = Vote.DIRECTIONS.onon
            elif previous_vote.is_offoffvote:
                direction = Vote.DIRECTIONS.onoff
            # backward compatibility
            elif previous_vote.is_downvote:
                direction = Vote.DIRECTIONS.onon
            else:
                g.log.warning("!!! cast_vote() up, discarding vote with dir: %s prev dir: %s" % (direction, previous_vote.direction))
                return
        elif direction == Vote.DIRECTIONS.down: # funny/disliked
            if previous_vote.is_onoffvote:
                direction = Vote.DIRECTIONS.onon
            elif previous_vote.is_offoffvote:
                direction = Vote.DIRECTIONS.offon
            elif previous_vote.is_offoffvote:
                direction = Vote.DIRECTIONS.offon
            # backward compatibility
            elif previous_vote.is_upvote:
                direction = Vote.DIRECTIONS.onon
            else:
                g.log.warning("!!! cast_vote() down, discarding vote with dir: %s prev dir: %s" % (direction, previous_vote.direction))
                return
        elif direction == Vote.DIRECTIONS.unup: # un-interesting / unliked
            if previous_vote.is_ononvote:
                direction = Vote.DIRECTIONS.offon
            elif previous_vote.is_onoffvote:
                direction = Vote.DIRECTIONS.offoff
            # backward compatibility
            elif previous_vote.is_upvote:
                direction = Vote.DIRECTIONS.offoff
            else:
                g.log.warning("!!! cast_vote() unup, discarding vote with dir: %s prev dir: %s" % (direction, previous_vote.direction))
                return
        elif direction == Vote.DIRECTIONS.undown: # un-funny / undisliked
            if previous_vote.is_ononvote:
                direction = Vote.DIRECTIONS.onoff
            elif previous_vote.is_offonvote:
                direction = Vote.DIRECTIONS.offoff
            # backward compatibility
            elif previous_vote.is_downvote:
                direction = Vote.DIRECTIONS.offoff
            else:
                g.log.warning("!!! cast_vote() undown, discarding vote with dir: %s prev dir: %s" % (direction, previous_vote.direction))
                return
    # first vote
    else:
        if direction == Vote.DIRECTIONS.up:
            direction = Vote.DIRECTIONS.onoff
        elif direction == Vote.DIRECTIONS.down:
            direction = Vote.DIRECTIONS.offon
        else:
            return

    # g.log.warning("!!! cast_vote() new Vote with dir: %s vote_dir: %s" % (direction, vote_direction))

    update_vote_lookups(user, thing, direction)

    vote_data = {
        "user_id": user._id,
        "thing_fullname": thing._fullname,
        "direction": direction,
        "date": int(epoch_timestamp(datetime.now(g.tz))),
        # CUSTOM: voting model
        "vote_direction": vote_direction,
    }

    data['ip'] = getattr(request, "ip", None)
    if data['ip'] is not None:
        data['org'] = organization_by_ips(data['ip'])
    vote_data['data'] = data

    hooks.get_hook("vote.get_vote_data").call(
        data=vote_data["data"],
        user=user,
        thing=thing,
        request=request,
        context=c,
    )

    # The vote event will actually be sent from an async queue processor, so
    # we need to pull out the context data at this point
    if not g.running_as_script:
        vote_data["event_data"] = {
            "context": Event.get_context_data(request, c),
            "sensitive": Event.get_sensitive_context_data(request, c),
        }

    try:
        vote_dump = json.dumps(vote_data)
    except UnicodeDecodeError:
        g.log.error("Got weird unicode in the vote data: %r", vote_data)
        return

    if isinstance(thing, Link):
        queue = "vote_link_q"
    elif isinstance(thing, Comment):
        queue = "vote_comment_q"

    amqp.add_item(queue, vote_dump)
Example #7
0
def vote(room, vote, sent_dt, request=None, context=None):
    """Create and save a 'vote' event.

    room: a RobinRoom object
    vote: A string, one of "INCREASE", "CONTINUE", or "ABANDON"
    send_dt: A datetime object, representing when the vote was sent
    request, context: pylons.request & pylons.c respectively

    """
    event = Event(
        topic=EVENT_TOPIC,
        event_type="ss.robin_vote",
        time=sent_dt,
        request=request,
        context=context,
    )

    event.add("room_id", room.id)
    event.add("room_name", room.name)
    event.add("room_age", _age_in_ms(room))
    event.add("room_level", room.level)
    event.add("process_notes", vote)

    g.events.save_event(event)
Example #8
0
def message(room, message, sent_dt, request=None, context=None):
    """Create and save a 'message' event.

    room: a RobinRoom object
    message: A string, <= 140 characters, representing the message sent
    send_dt: A datetime object, representing when the message was sent
    request, context: pylons.request & pylons.c respectively

    """
    event = Event(
        topic=EVENT_TOPIC,
        event_type="ss.robin_message",
        time=sent_dt,
        request=request,
        context=context,
    )

    event.add("room_id", room.id)
    event.add("room_name", room.name)
    event.add("room_age", _age_in_ms(room))
    event.add("room_level", room.level)
    event.add_text("message_body", message)

    g.events.save_event(event)