def publish_on_home_page(publisher_id, post_id): try: list_of_followings = mongo_client_obj.fetch_data(FollowingRelation, {'follower': publisher_id}) for follow_relation in list_of_followings: following = follow_relation["following"] mongo_client_obj.update_data(HomePage, {'owner': following}, {"$push": {"inclusive_posts": post_id}}, upsert=True) return True except Exception as e: logger.error("publish_on_home_page/base : " + str(e)) raise Exception(str(e))
def unlike_post(author_id, post_id): try: author_obj = mongo_client_obj.fetch_one_data(Profile, {'_id': ObjectId(author_id)}) unlike_obj = mongo_client_obj.delete_one_data(Like, {"$and": [{'author': {'username': author_obj.username, 'picture': author_obj.picture}}, {'post_id': ObjectId(post_id)}]}) mongo_client_obj.update_data(Post, {'_id': ObjectId(post_id)}, {"$inc": {'likes': -1}}, upsert=False) return "unlike" except Exception as e: logger.error("unlike_post/base" + str(e)) raise Exception(str(e))
def update_profile(profile_id, username, first_name, last_name, picture, picture_id, private): try: data = { 'username': username, 'first_name': first_name, 'last_name': last_name, 'picture': picture, 'picture_id': ObjectId(picture_id), 'private': private } old_profile_obj = mongo_client_obj.fetch_one_data(Profile, {'_id': ObjectId(profile_id)}) # delete previous picture if old_profile_obj.picture_id != picture_id: delete_picture(old_profile_obj.picture_id) # update whole object profile_obj = mongo_client_obj.update_data(Profile, {'_id': ObjectId(profile_id)}, {"$set": data}, upsert=False) profile_dict = make_dict_embedded_profile(profile_obj) output_profile_obj = OutputProfile(**profile_dict) return output_profile_obj except Exception as e: logger.error("update_profile/base : " + str(e)) raise Exception(str(e))
def like_post(author_id, post_id): try: author_obj = mongo_client_obj.fetch_one_data(Profile, {'_id': ObjectId(author_id)}) like_obj = mongo_client_obj.insert_one_data(Like, {'author': { 'username': author_obj.username, 'picture': author_obj.picture}, 'post_id': ObjectId(post_id)}) # increase the number of post's like mongo_client_obj.update_data(Post, {'_id': ObjectId(post_id)}, {"$inc": {'likes': 1}}, upsert=False) return "like" except Exception as e: logger.error("like_post/base" + str(e)) raise Exception(str(e))
def follow(following_id, follower_id): following_id = ObjectId(following_id) follower_id = ObjectId(follower_id) mongo_client_obj.insert_one_data(FollowingRelation, {'following': following_id, 'follower': follower_id}) # increase the number of follower and following follower_obj = mongo_client_obj.update_data(Profile, {'_id': following_id}, {"$inc": {'number_of_follower': 1}}, upsert=False) following_obj = mongo_client_obj.update_data(Profile, {'_id': follower_id}, {"$inc": {'number_of_following': 1}}, upsert=False) return "followed"
def stop_to_follow(following_id, follower_id): try: following_id = ObjectId(following_id) follower_id = ObjectId(follower_id) mongo_client_obj.delete_one_data(FollowingRelation, {'following': following_id, 'follower': follower_id}) # decrease the number of followers and followings follower_obj = mongo_client_obj.update_data(Profile, {'_id': following_id}, {"$inc": {'number_of_follower': -1}}, upsert=False) following_obj = mongo_client_obj.update_data(Profile, {'_id': follower_id}, {"$inc": {'number_of_following': -1}}, upsert=False) return "stop" except Exception as e: logger.error("stop_to_follow/base : " + str(e)) raise Exception(str(e))
def delete_following(auth, following_id): try: auth = ObjectId(auth) following_id = ObjectId(following_id) deleted_count = mongo_client_obj.delete_one_data(FollowingRelation, {'following': following_id, 'follower': auth}) following_obj = mongo_client_obj.update_data(Profile, {'_id': auth}, {"$inc": {'number_of_following': -1}}, upsert=False) follower_obj = mongo_client_obj.update_data(Profile, {'_id': following_id}, {"$inc": {'number_of_follower': -1}}, upsert=False) return "stop" except Exception as e: logger.error("delete_following/base : " + str(e)) raise Exception(str(e))
def create_post(data): try: post_obj = mongo_client_obj.insert_one_data(Post, data) profile_obj = mongo_client_obj.update_data(Profile, {"_id": post_obj.publisher}, {"$inc": {"number_of_posts": 1}}, upsert=False) publish_on_home_page(post_obj.publisher, post_obj._id) post_dict = make_dict_embedded_post(post_obj) profile_dict = make_dict_embedded_profile(profile_obj) output_create_post_obj = OutputPost(**{'publisher': profile_dict, 'post': post_dict}) return output_create_post_obj except Exception as e: logger.error("create_post/base :" + str(e)) raise Exception(str(e))
def block_or_unblock_following(action, auth, following_id): if action == "block": try: # blocking = mongo_client_obj.update_data(FollowingRelation, # {"$and": [ # {'following': ObjectId(following_id)}, # {'follower': ObjectId(auth)}]}, # {"$set": {'block': True}}, # upsert=False) # find profile of blocked user blocked_profile = mongo_client_obj.fetch_one_data(Profile, {'_id': ObjectId(following_id)}) blocked_username = blocked_profile.username post_list_of_blocker = mongo_client_obj.fetch_data(Post, {'publisher': ObjectId(auth)}) post_ids = [] for post in post_list_of_blocker: post_ids.append(post["_id"]) print("post:ids : ", post_ids) # TODO : make the error correct remove_comments_of_blocked_user.delay(blocked_username=blocked_username, post_ids=2) # remove_likes_of_blocked_user.delay(blocked_username, post_ids) return "blocked" except Exception as e: logger.error("block/base : " + str(e)) raise Exception(str(e)) elif action == "unblock": try: blocking = mongo_client_obj.update_data(FollowingRelation, {"$and": [ {'following': ObjectId(following_id)}, {'follower': ObjectId(auth)}]}, {"$set": {'block': False}}, upsert=False) return "unblocked" except Exception as e: logger.error("unblock/base : " + str(e)) raise Exception(str(e))