def post(post_id: str): """ 1. get post from secrets and format to json to return """ queried_post: Post = db.posts.get_post(post_id) return success(queried_post) if queried_post else failure( "post id %s does not exist" % post_id)
def explore(): """ 1. list random posts """ queried_posts = db_utils.grab_range_from_db(None, db.posts.all_posts) random.shuffle(queried_posts) return success(queried_posts)
def user_posts(username: str): """ request body: <none> 1. list n of user's posts """ queried_posts = db.posts.user_posts_default(username) return success(queried_posts)
def feed(sort_by: str = "mostRecent", first: int = 0): """ 1. list n most recent posts from people user follows """ queried_posts = db_utils.grab_range_from_db(None, db.posts.feed_posts, username=cognito.current_user) return success(queried_posts)
def followers(username: str): """ 1. list n of user's followers """ queried_followers = db_utils.grab_range_from_db(None, db.users.followers, username=username) return success(queried_followers)
def delete_current_user(): user = db.users.get_user(cognito.current_user) # delete profile pic from aws_tools pic_utils.delete_profile_picture(user.profile_picture) # update secrets db.users.delete_user(cognito.current_user) return success({})
def edit_profile(): """ request body: firstName, lastName, bio """ user_data = server.data.users.User( display_name=request.json["displayName"], bio=request.json["bio"]) edited_user = db.users.edit_user(cognito.current_user, user_data) return success(edited_user)
def user(username: str): """ request body: <none> 1. list user data """ queried_user = db.users.get_user(username) return success(queried_user) if queried_user else failure( "user %s does not exist" % username)
def create_post(): """ request body: text 1. upload image to aws_tools 2. add new post to request.database """ # update secrets with new post new_post = db.posts.create_post(cognito.current_user, request.json["text"]) return success(new_post)
def delete_post(post_id: str): """ :param post_id: :return: """ queried_post: Post = db.posts.get_post(post_id) if not queried_post or cognito.current_user != queried_post.username: return failure( f"{cognito.current_user} does not own this post, cannot delete") db.posts.delete_post(post_id) return success({})
def update_post_picture(post_id: str): """ file: pictureFile :param post_id: :return: """ # todo check duplicates (highly unlikely) upload_info = pic_utils.upload_post_picture(request, str(uuid4())) if upload_info.upload_state == UploadState.failure: return failure("failed to upload image") if upload_info.upload_state == UploadState.no_upload: return failure("no picture uploaded") db.posts.update_post_picture(post_id, upload_info.filename) return success(f"uploaded post image for {post_id}")
def search(query: str): """ request body: <none> 1. search users and posts by tag and text """ queried_posts = db.posts.search_posts_default(query) #queried_users = db_utils.grab_range_from_db(request.json, db.users.search_users, username=query) #response_data = { # "queriedPosts": queried_posts, # "queriedUsers": queried_users #} return success(queried_posts)
def upload_profile_picture(): """ file: profilePicture :return: """ upload_info = pic_utils.upload_profile_picture(request, cognito.current_user) if upload_info.upload_state == UploadState.success: db.users.update_profile_pic_filename(cognito.current_user, upload_info.filename) return success(f"updated profile picture for {cognito.current_user}") if upload_info.upload_state == UploadState.no_upload: return failure("no picture uploaded") return failure("picture upload failure")
def edit_post(post_id: str): """ request body: text 1. check if user owns post 2. get post 3. update request.data in post (see create_post) """ # check if user owns post queried_post = db.posts.get_post(post_id) if not queried_post or cognito.current_user != queried_post.username: return failure(f"{cognito.current_user} does not own this post") # update secrets edited_post = db.posts.edit_post(post_id, request.json["text"]) return success(edited_post)
def create_user(): """ request body: username, birthday, displayName, bio files: profilePicture """ if "username" not in request.json or "birthday" not in request.json: return failure("username and birthday required to create a new user") user_data = server.data.users.User( username=request.json["username"], birthday=datetime.datetime.strptime(request.json["birthday"], "%Y-%M-%d").date(), display_name=request.json["displayName"], bio=request.json["bio"]) new_user = db.users.create_user(user_data) return success(new_user)
def ajax(): return success("ajax successful!")
def secret_post_test(): post_data = request.json["secretData"] return success(f"here's the secret post data: {post_data}")
def secret_test(): if cognito.current_user: return success(cognito.current_user) return failure("not logged in")
def unfollow(user_to_unfollow: str): return success(db.users.unfollow(cognito.current_user, user_to_unfollow))