def create_group(avatar, name): db = get_db() fs = GridFS(db, collection="images") filename, file_extension = os.path.splitext(avatar.filename) if not allowed_file(file_extension): return 'Image type is wrong!' data = avatar.read() sha1 = hashlib.sha1(data).hexdigest() filename = sha1 + file_extension image_obj = fs.find_one({"filename": filename}) if not image_obj: image_obj = fs.put(data, filename=filename, content_type=avatar.content_type) else: image_obj = image_obj._id # print(image_obj) gid = db.groups.insert({ 'name': name, 'avatar': image_obj, }) db.group_user.insert({ 'uid': ObjectId(session['uid']), 'gid': gid, }) return 'ok'
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 get_group_avatar(object_id): db = get_db() fs = GridFS(db, collection="images") image_obj = fs.find_one({"_id": ObjectId(object_id)}) # print(image_obj.content_type) response = make_response(image_obj.read()) content_type = image_obj.content_type if not content_type: content_type, _ = guess_type(image_obj.filename) response.mimetype = content_type return response
def join_group(gid): db = get_db() uid = ObjectId(session['uid']) gid = ObjectId(gid) group = db.groups.find_one({'_id': gid}) found = db.group_user.find_one({ 'uid': uid, 'gid': gid, }) if group and not found: db.group_user.insert({ 'uid': uid, 'gid': gid, }) return '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)