示例#1
0
文件: notice.py 项目: fstcap/chat
 def delete(self):
     """Delete notice one by one
     Return:
         status_code: 0 or 1
     """
     redis = get_redis()
     redis.delete(self.notice_name.format(g.user['id']))
     return make_response({"message": "success"})
示例#2
0
 def wrapped_view(*args, **kwargs):
     redis = get_redis()
     sorted_set_name = f"application_{g.user['id']}"
     hash_name = "application_" + str(g.user['id']) + "_{}"
     if 'id' in request.form:
         id = int(request.form['id'])
         assert type(id) == int
         return view(redis, sorted_set_name, hash_name, id, *args, **kwargs)
     return view(redis, sorted_set_name, hash_name, *args, **kwargs)
示例#3
0
文件: notice.py 项目: fstcap/chat
 def get(self):
     """Get application notice len
     
     Return:
         notice_len: number of application len
     """
     redis = get_redis()
     notice_len = redis.llen(self.notice_name.format(g.user['id']))
     return make_response({"notice_len": notice_len})
示例#4
0
 def delete(self):
     """Method delete
     """
     redis = get_redis()
     if 'room_id' not in request.form or re.match(r"^\w+$", request.form['room_id']) is None:
         abort(400)
     room_id = request.form['room_id']
     redis.hdel(room_id, g.user['id'])
     redis.zrem(self.sorted_set_name.format(g.user['id']), room_id)
     if redis.hlen(room_id) <=1: redis.delete(room_id)
     return redirect(url_for('room.index'))
示例#5
0
 def get(self):
     """Method get
     """
     redis = get_redis()
     
     user_room_names = redis.zrevrange(self.sorted_set_name.format(g.user['id']), 0, -1)
     user_rooms = []
     for user_room_name in user_room_names:
         values = redis.hgetall(user_room_name).values()
         values = list(map(lambda x: json.loads(x), values))
         user_rooms.append({"room_id": user_room_name, "users": values})
     return render_template('room/index.html', user_rooms=user_rooms)
示例#6
0
文件: notice.py 项目: fstcap/chat
def notice_application(id, hash_name, json):
    """Notice application
    Args:
        id: table user id
        hash_name: application user hash name
        json: applicator name and message
    """
    redis = get_redis()
    notice_name = f"application_notice_{id}"
    redis.lpush(notice_name, hash_name)
    emit('application', json, 
        namespace='/notice',
        room=f"notice_application_{id}")
示例#7
0
    def post(self):
        """Mehtod post
        """
        redis = get_redis()
        if 'id' not in request.form or re.match(r"^\d+$", request.form['id']) is None:
            abort(400)
        id = int(request.form['id'])
        user_room_names = redis.zrevrange(self.sorted_set_name.format(g.user['id']), 0, -1)
        friend_room_names = redis.zrevrange(self.sorted_set_name.format(id), 0, -1)
        u_f_intersection = set(user_room_names).intersection(set(friend_room_names))

        room_name = None
        for name in u_f_intersection:
            if redis.hlen(name) == 2:
                room_name = name
                break
        created = datetime.datetime.now().timestamp()
        if room_name is None:
            dk = bytes(self.hash_name.format(g.user['id'], created), encoding='utf-8')
            dk = hashlib.pbkdf2_hmac('sha256', dk, os.urandom(16), 100000)
            room_name = dk.hex()
            
            db = get_db()
            user = db.execute(
                'SELECT username FROM user WHERE id=?',
                (g.user['id'],)
            ).fetchone()
            friend = db.execute(
                'SELECT username FROM user WHERE id=?',
                (id,)
            ).fetchone()

            members = {
                g.user['id']: json.dumps({'id': g.user['id'], 'username': user['username']}), 
                id: json.dumps({'id': id, 'username': friend['username']})}
            redis.hmset(room_name, members)
        
        redis.zadd(self.sorted_set_name.format(g.user['id']), {room_name: created})
        redis.zadd(self.sorted_set_name.format(id), {room_name: created}) 
        return redirect(location=url_for('room.index'))
示例#8
0
    def post(self):
        """Send aplication
        from search result send friend application

        Args:
            id: user id 
        Return: 
            redirect to friend index when send success
            friend send.html
        """
        if 'id' in request.form:
            id = int(request.form['id'])
            assert type(id) == int

        redis = get_redis()
        sorted_set_name = f"application_{id}"
        hash_name = f"application_{id}_{g.user['id']}"

        redis.zrem(sorted_set_name, g.user['id'])
        redis.delete(hash_name)

        auth = get_db().execute('SELECT username FROM user WHERE id=?',
                                (g.user['id'], )).fetchone()
        created = datetime.datetime.now().replace(microsecond=0).timestamp()
        auth = {
            "applicant_id": g.user['id'],
            "username": auth['username'],
            "created": created,
            "message": request.form['message']
        }
        redis.zadd(sorted_set_name, {g.user['id']: created})
        redis.hmset(hash_name, auth)

        notice_application(id, hash_name, {
            'username': auth['username'],
            'message': auth['message']
        })
        return redirect(url_for('friend.index'))