def publish_message(request, tutalk_message):
    """sends a message to tutalk via redis pub/sub

    not all 'Rimac' messages are valid 'TuTalk' messages. The message
    types in SHOULDNT_PUBLISH_MESSAGE_TYPES should merely be logged.

    also adds the uid and gid fields, so the client doesn't need to
    pass them

    FIXME? I suppose ideally, rimac system should only deal with
    'Rimac' messages, and there should be a rimac<->tutalk message
    translation layer.  rimac-event-listener partially does this for
    messages originating from tutalk, but it would probably be better
    if it instead translated them into rimac-specific message types
    rather than simply adding more information to the payload
    """
    msg_type = tutalk_message.get('type', None)
    if msg_type is None:
        raise exc.MsgMissingTypeException()

    try:
        add_uid_gid_fields(request, tutalk_message)
    except Exception:
        raise exc.NoUidInSessionException()

    db.log_message(request, tutalk_message)
    if should_publish_message(msg_type):
        if "logout" == msg_type:
            # we don't want to recieve any messages other than the
            # corresponding tutor-logout
            delete_mailbox(request)
        rc = get_redis_connection()
        tutalk_host = encode_tutalk_host(request.session['tutalk_host'])
        routing_key = '%s.%s.%s.%s' % (tutalk_host,
                                       request.session['experimenter'],
                                       request.session['scenario'],
                                       msg_type)
        msg = json.dumps(tutalk_message)
        LOGGER.debug("attempting to publish %s", routing_key)
        LOGGER.debug("message: %s", msg)
        success = rc.publish(routing_key, msg)
        if 0 == success:
            raise exc.ScenarioNotRunningException()
def logged_in_to_tutalk(request):
    """checks the redis database that the user is logged in.

    FIXME? should I be double-checking with tutalk itself?
    """
    found = False
    tutalk_host = encode_tutalk_host(request.session['tutalk_host'])
    experimenter = request.session['experimenter']
    scenario = request.session['scenario']
    top_level_key = '%s.scenarios' % tutalk_host
    groups_key = '%s.%s.groups' % (experimenter, scenario)
    rc = get_redis_connection()
    val = rc.hget(top_level_key, groups_key)
    if val is not None:
        groups = json.loads(val)
        for group in groups:
            for agent in group['agents']:
                if agent['uid'] == request.session['tutalk_uid']:
                    found = True
                    break
    return found