def confirm_apply(cls, user_id, request_id, is_agree): updated = yield cls.collection.update({'request_id': request_id, 'user_id': user_id}, {'$set': {'is_agree':is_agree, 'is_read': True}}) if is_agree: yield Friends.make_friends(request_id, user_id) # notify user agreed_user = yield User.collection.find_one({'_id': user_id}) notify_data = {'receive_id': request_id, 'user': User.serialize(agreed_user)} defer.returnValue(notify_data) defer.returnValue(None)
def get_unread_apply(cls, user_id): unread_list = yield cls.collection.find({'user_id': user_id, 'is_read': False}) request_id_list = [i['request_id'] for i in unread_list] user_cursor = yield User.collection.find({'_id': {'$in': request_id_list}}) user_dict = {user['_id']: user for user in user_cursor} for unread_apply in unread_list: temp_request_id = unread_apply['request_id'] temp_user = user_dict.get(temp_request_id, None) unread_apply.update({'request_user': User.serialize(temp_user)}) unread_list = cls.serialize(unread_list) defer.returnValue(unread_list)
def apply_friend(cls, request_id, user_id, message): # 还需要检查是否是好友,或是否已发送过好友申请,如果是,则不作任何处理 friend_list = yield Friends.collection.find({'user_id': user_id, 'friend_id': request_id}) applyed = yield cls.collection.find_one({'user_id': user_id, 'request_id': request_id, 'is_read': False}) if friend_list or applyed: defer.returnValue(None) else: # 检查user_id与request_id是否都是合法的…… # 1. user_id不能与request_id相同 # 2. 数据库这两个用户要确实存在? insert_doc = cls.validate({'request_id': request_id, 'user_id': user_id, 'message': message}) yield cls.collection.insert(insert_doc) # notify user request_user = yield User.collection.find_one({'_id': request_id}) notify_data = {'message': message, 'request_user': User.serialize(request_user), 'receive_id': user_id } # 要通知给user的消息 defer.returnValue(notify_data)
def get_friend_list(cls, user_id): user_list = [] friend_cursor = yield cls.collection.find({'user_id': user_id}) friend_id_list = [friend['friend_id'] for friend in friend_cursor] if friend_id_list: result_dict = collections.defaultdict(list) user_list = yield User.collection.find({'_id': {'$in': friend_id_list}}) user_list = User.serialize(user_list) for user in user_list: letter = get_pinyin_first(user['nick']) result_dict[letter].append(user) result_list = [] for key, user_list in result_dict.items(): result_list.append({'index': key, 'user_list': user_list}) defer.returnValue(result_list) else: defer.returnValue(user_list)