示例#1
0
文件: app.py 项目: vltanh/narupedia
def request_chat():
    request_data = request.get_json()
    from_user = request_data.get('from_user', '')
    to_user = request_data.get('to_user', '')
    to_user_channel = "private-notification_user_%s" % (to_user)
    from_user_channel = "private-notification_user_%s" % (from_user)

    try:
        bot = User.query.filter(User.id == to_user).one()
    except NoResultFound:
        print('Error! No bot (yet).')
    except MultipleResultsFound:
        print('Error! Wait what?')

    try:
        yolo.get_model(bot.username)
    except:
        print('Error! Cannot load model!')

    # check if there is a channel that already exists between this two user
    channel = Channel.query.filter( Channel.from_user.in_([from_user, to_user]) ) \
                            .filter( Channel.to_user.in_([from_user, to_user]) ) \
                            .first()
    if not channel:
        # Generate a channel...
        chat_channel = "private-chat_%s_%s" % (from_user, to_user)

        new_channel = Channel()
        new_channel.from_user = from_user
        new_channel.to_user = to_user
        new_channel.name = chat_channel
        db_session.add(new_channel)
        db_session.commit()
    else:
        # Use the channel name stored on the database
        chat_channel = channel.name

    data = {
        "from_user": from_user,
        "to_user": to_user,
        "from_user_notification_channel": from_user_channel,
        "to_user_notification_channel": to_user_channel,
        "channel_name": chat_channel,
    }

    # Trigger an event to the other user
    pusher.trigger(to_user_channel, 'new_chat', data)

    return jsonify(data)
示例#2
0
    def post(self):
        request_data = request.get_json()
        from_user = request_data.get('from_user', '')
        to_user = request_data.get('to_user', '')
    #to_user_channel = "private-notification_user_%s" % (to_user)
    #from_user_channel = "private-notification_user_%s" % (from_user)

    # check if there is a channel that already exists between this two user
        channel = Channel.query.filter(Channel.from_user.in_([from_user, to_user])) \
                           .filter(Channel.to_user.in_([from_user, to_user])) \
                           .first()
        if not channel:
        # Generate a channel...
           chat_channel = "private-chat_%s_%s" % (from_user, to_user)

           new_channel = Channel()
           new_channel.from_user = from_user
           new_channel.to_user = to_user
           new_channel.name = chat_channel
           db_session.add(new_channel)
           db_session.commit()
           db_session.remove()
        else:
        # Use the channel name stored on the database
           chat_channel = channel.id

        data = {
        "from_user": from_user,
        "to_user": to_user,
        #"from_user_notification_channel": from_user_channel,
        #"to_user_notification_channel": to_user_channel,
        "channel_name": chat_channel,
         }

    # Trigger an event to the other user
    #pusher.trigger(to_user_channel, 'new_chat', data)

        return make_response(jsonify(data))
示例#3
0
def request_chat():
    request_data = request.get_json()
    from_user = request_data.get('from_user', '')
    to_user = request_data.get('to_user', '')
    to_user_channel = "private-notification_user_%s" % (to_user)
    from_user_channel = "private-notification_user_%s" % (from_user)

    # check if a channel exists between the two users
    channel = Channel.query.filter(Channel.from_user.in_([from_user, to_user])) \
        .filter(Channel.to_user.in_([from_user, to_user])) \
        .first()
    if not channel:
        # generate new channel
        chat_channel = "private-chat_%s_%s" % (from_user, to_user)

        new_channel = Channel()
        new_channel.from_user = from_user
        new_channel.to_user = to_user
        new_channel.name = chat_channel
        db_session.add(new_channel)
        db_session.commit()
    else:
        # Use existing channel
        chat_channel = channel.name

    data = {
        "from_user": from_user,
        "to_user": to_user,
        "from_user_notification_channel": from_user_channel,
        "to_user_notification_channel": to_user_channel,
        "channel_name": chat_channel,
    }

    # an event is triggered to the other use
    pusher.trigger(to_user_channel, 'new_chat', data)

    return jsonify(data)