def accept_friend(): if not request.data: return INVALID_PARAM() req = json.loads(request.data) request_id = req.get('request_id') friend_uid = req.get('uid') if not request_id or not friend_uid: return INVALID_PARAM() friend_req = Friend.get_friend_request(g._db, request_id) if not friend_req: return INVALID_PARAM() if friend_req['uid'] != friend_uid or \ friend_req['friend_uid'] != request.uid: return INVALID_PARAM() #添加双向的好友关系 Friend.add_friend_relation(g._db, friend_req['uid'], friend_req['friend_uid']) sys_msg = {"friend":{"type":"accept", "uid":request.uid}} gobelieve.send_system_message(friend_req['uid'], json.dumps(sys_msg)) return make_response(200, {"success":True})
def post(self, user_id): """ Accept the friend request Add the user into friend list Remove the request from friend request """ args = friendsParser.parse_args() profile_id = args['profile_id'] if profile_id is None: abort(400) success = Request.objects(user=user_id).only( 'requests_list').update_one(pull__requests_list=profile_id) if success is 0: abort(400) success = Friend.objects(user=user_id).only( 'friends_list').update_one(add_to_set__friends_list=profile_id) if success is 0: friends = Friend(user=user_id, friends_list=[profile_id]) friends.save() return {'status': 'success', 'message': 'The user has been added to your friend list'}
def post(self, user_id): """ Add a specific user to friend list, and send a friend request to that user """ args = friendsParser.parse_args() profile_id = args['profile_id'] if profile_id is None: abort(400) friend_profile = Profile.objects(id=profile_id).only('user').first() if friend_profile is None: abort(400) # add the user to friend list success = Friend.objects(user=user_id).only( 'friends_list').update_one(add_to_set__friends_list=profile_id) if success is 0: friends = Friend(user=user_id, friends_list=[profile_id]) friends.save() # put the friend request to the user's request list user_profile = Profile.objects(user=user_id).only('id').first() success = Request.objects(user=friend_profile.user).update_one( add_to_set__requests_list=user_profile) if success is 0: friend_request = Request( user=friend_profile.user, type='friends', requests_list=[user_profile]) friend_request.save() return {'status': 'success', 'message': 'The user has been added to your friend list'}
def update_match(match_id, is_accepted): match = Match.query.get(match_id) if not match: response_object = { 'status': 'fail', 'message': 'Match no encontrado' } return response_object, 404 match.status_request = 1 if is_accepted else -1 update() if is_accepted: friend1 = Friend(user_id=match.user_from_id, friend_id=match.user_to_id) friend2 = Friend(user_id=match.user_to_id, friend_id=match.user_from_id) save_changes(friend1) save_changes(friend2) response_object = { 'status': 'success', 'message': 'Solicitud actualizada correctamente' } return response_object, 200
def delete_friend(friend_uid): uid = request.uid Friend.delete_friend_relation(g._db, uid, friend_uid) sys_msg = {"friend":{"type":"delete", "uid":uid}} gobelieve.send_system_message(friend_uid, json.dumps(sys_msg)) return make_response(200, {"success":True})
def get(self, user_id): """ Get the user's friend list """ friends = Friend.objects(user=user_id).only('friends_list').first() if friends is None: return abort(400) return friends_list_serialize(friends.friends_list)
def request_friend(): uid = request.uid if not request.data: return INVALID_PARAM() req = json.loads(request.data) friend_uid = req.get('friend_uid') req_id = Friend.add_friend_request(g._db, uid, friend_uid) sys_msg = {"friend":{"request_id":req_id, "uid":uid, "type":"request"}} gobelieve.send_system_message(friend_uid, json.dumps(sys_msg)) resp = {"request_id":req_id} return make_response(200, resp)
def delete(self, user_id): """ Delete a specific user from the friend list """ args = friendsParser.parse_args() profile_id = args['profile_id'] if profile_id is None: abort(400) success = Friend.objects(user=user_id).only( 'friends_list').update_one(pull__friends_list=profile_id) if success is 0: abort(400) return {'status': 'success', 'message': 'The user has been delete from your friend list'}
def get(self): user_id = self.request.get('user_id') user = User.get_by_key_name(user_id) template_values = {} template_values['friends'] = [] for friend in Friend.all().ancestor(user).order( "-compatibility_score"): template_values['friends'].append({ 'genre_scores': friend.user.genre_scores, 'id': friend.user.key().name(), 'name': friend.user.name, 'avatar': friend.user.avatar, 'compatibility_score': friend.compatibility_score }) path = 'templates/show_friends.html' self.response.out.write(template.render(path, template_values))
def create(profile, user): steamid = profile['steamid'] name = profile['personaname'] avatar = profile['avatar'] recently_played_games = user_data.recently_played_games(steamid) genres = [] for id in recently_played_games: genres.extend(game_factory.get_or_create(id).genres) friend_genre_scores = score.genre_scores(genres) user_genre_scores = json.loads(user.genre_scores) compatibility_score = score.compatibility_percentages( user_genre_scores, friend_genre_scores) friend_user = User.get_or_insert( key_name=steamid, games=recently_played_games, name=name, avatar=avatar, genre_scores=json.dumps(friend_genre_scores)) Friend(key_name=steamid, parent=user, user=friend_user, compatibility_score=compatibility_score).put()