示例#1
0
def private_messages_get():
    channel_hash_id = request.args.get('channel_id')
    channel = Channel.get(channel_hash_id)
    l = []
    for msg in channel.messages:
        if msg.sender_id == current_user.id:
            if msg.unread:
                status = 'sent'
            else:
                status = 'read'
        else:
            msg.unread = False
            db.session.commit()
            status = 'received'
        recipient = User.query.get(msg.recipient_id)
        sender = User.query.get(msg.sender_id)
        d = {
            'position'    : msg.sender_id == current_user.id and 'right' or 'left',
            'date'        : msg.timestamp.isoformat() + 'Z',
            'status'      : status,
            'text'        : msg.body,
            'type'        : 'text',
            'id'          : msg.hash_id,
            'recipient_id': recipient.hash_id,
            'sender_id'   : sender.hash_id,
            'channel_id'  : msg.channel.hash_id
        }
        l.append(d)
    return jsonify(l)
示例#2
0
def send_private_message():
    user_hash_id = request.json.get('user_id')
    uuid = request.json.get('uuid', '')
    other = User.get_or_404(user_hash_id)
    content = request.json.get('content', '')
    if current_user.id != other.id:
        channel_d = Channel.private_channel_get(current_user, other)
        channel_obj = Channel.get(channel_d['id'])
        msg_obj = channel_obj.send_private_message(current_user, other, content)
        db.session.commit()
        recipient = User.query.get(msg_obj.recipient_id)
        sender = User.query.get(msg_obj.sender_id)
        new_message = {
            'position'    : msg_obj.sender_id == current_user.id and 'right' or 'left',
            'date'        : msg_obj.timestamp.isoformat() + 'Z',
            'text'        : msg_obj.body,
            'type'        : 'text',
            'id'          : msg_obj.hash_id,
            'recipient_id': recipient.hash_id,
            'sender_id'   : sender.hash_id,
            'uuid'        : uuid,
            'channel_id'  : msg_obj.channel.hash_id
        }
        return jsonify(new_message)