def update_public_key(key): uid = session['uid'] db = get_db() db.users.update({'_id': uid}, {'$set': { 'publicKey': key, }}) return jsonify({'status': 'ok'})
def post_message(uid, gid, outercypher, noncestr, ephermeralpubkey): # print(uid, gid, outercypher, noncestr, ephermeralpubkey) db = get_db() db.messages.insert_one({ 'uid': ObjectId(uid), 'gid': ObjectId(gid), 'outercypher': outercypher, 'noncestr': noncestr, 'ephermeralpubkey': ephermeralpubkey, }) return jsonify({'status': "ok"})
def login(code, name, avatar, gender): params = { 'appid': app.config['APP_ID'], 'secret': app.config['APP_SECRET'], 'js_code': code, 'grant_type': 'authorization_code', } r = requests.get('https://api.weixin.qq.com/sns/jscode2session', params=params) data = json.loads(r.text) if data['openid']: db = get_db() row = db.users.find_one({'openid': data['openid']}) if not row: # register db.users.insert({ 'openid': data['openid'], 'name': name, 'avatar': avatar, 'gender': gender, 'publicKey': '', }) row = db.users.find_one({'openid': data['openid']}) elif name != row['name'] or avatar != row['avatar'] or gender != row[ 'gender']: # update user info db.users.update( {'_id': row['_id']}, {'$set': { 'name': name, 'avatar': avatar, 'gender': gender, }}) session['openid'] = data['openid'] session['session_key'] = data['session_key'] session['uid'] = row['_id'] print(row) print(session.sid) return jsonify({ 'sid': session.sid, 'uid': row['_id'], 'publicKey': row['publicKey'], 'avatar': row['avatar'], }) else: pass
def get_message(latest_message_id='{}'): latest_message_dict = json.loads(latest_message_id) db = get_db() uid = session['uid'] data = {} groups = db_get_user_groups(uid) for group in groups: gid = str(group['_id']) query = { 'gid': ObjectId(gid), 'uid': { '$ne': ObjectId(uid) }, } if gid in latest_message_dict: query['_id'] = {'$gt': ObjectId(latest_message_dict[gid])} projection = { 'uid': 0, 'gid': 0, } data[gid] = list(db.messages.find(query, projection)) return jsonify(data)
def get_group_users(gid): db = get_db() pipeline = [{ "$match": { "gid": ObjectId(gid) } }, { "$lookup": { "from": "users", "localField": "uid", "foreignField": "_id", "as": "user" } }, { "$group": { "_id": "$gid", "users": {"$push": {"$arrayElemAt": ["$user", 0]}} } }] data = list(db.group_user.aggregate(pipeline)) if data: data = data[0]["users"] # print(jsonify(data)) return jsonify(data)
def get_group(gid): db = get_db() data = db.groups.find_one({ '_id': ObjectId(gid) }) return jsonify(data)
def get_user(uid): db = get_db() data = db.users.find_one({ '_id': ObjectId(uid) }) return jsonify(data)
def get_user_groups(uid): uid = session['uid'] data = db_get_user_groups(uid) return jsonify(data)