def add_comment(post_id): try: user_id = current_user.meta.id text = request.json["text"] response = core.add_comment(post_id, user_id, text) except KeyError: return client_error("001") except FaceduckError as e: return client_error(e.id) return jsonify(comment_mapper(response))
def remove_comment(post_id): try: user_id = current_user.meta.id comment_id = request.json["comment_id"] core.remove_comment(post_id, comment_id, user_id) except KeyError: return client_error("001") except FaceduckError as e: return client_error(e.id) return ("", 204)
def add_reactions(post_id): try: user_id = current_user.meta.id reaction = request.json["reaction"] core.set_reaction(post_id, user_id, reaction) response = get_post(post_id) except KeyError: return client_error("001") except FaceduckError as e: return client_error(e.id) return response
def update_friendship(): try: user_id = current_user.meta.id target_id = request.json["target_id"] state = request.json["state"] except KeyError: return client_error("001") try: friendship = core.update_friendship(user_id, target_id, state=state) return jsonify(friendship_mapper(friendship)) except FaceduckError as e: return client_error(e.id)
def login(): req = request.get_json() device = request.headers.get('User-Agent') ip = request.remote_addr try: email = req['email'] password = req['password'] except KeyError: return client_error("001") try: user, token = core.login_user(email, password, device, ip) return jsonify({'user': user_mapper(user), 'access-token': token}) except FaceduckError as e: return client_error(e.id)
def upload_media(): file = None if 'file' in request.files: file = request.files['file'] if file is None: return client_error("001") if file.filename == '': return client_error("001") media = core.upload_media(file, current_app.config['UPLOAD_FOLDER'], request.host_url) return jsonify(media)
def delete_reactions(post_id): try: user_id = current_user.meta.id core.delete_reaction(post_id, user_id) except FaceduckError as e: return client_error(e.id) return ("", 204)
def get_post(post_id): try: user_id = (current_user or None) and current_user.meta.id post = core.get_post(post_id, user_id) response = post_mapper(post) except FaceduckError as e: return client_error(e.id) return jsonify(response)
def get_login_logs(): user_id = current_user.meta.id try: logs = core.get_login_logs(user_id) except FaceduckError as e: return client_error(e.id) return jsonify([log_mapper(p) for p in logs])
def delete_friendship(): try: user_id = current_user.meta.id target_id = request.json["target_id"] except KeyError: return client_error("001") core.delete_friendship(user_id, target_id) return ("", 204)
def get_comments(post_id): try: user_id = (current_user or None) and current_user.meta.id core.get_post(post_id, user_id) cmts = core.get_comments(post_id, None) return jsonify([comment_mapper(c) for c in cmts]) except FaceduckError as e: return client_error(e.id)
def search_users(): content = request.get_json() try: query = content['query'] except KeyError: return client_error("001") users = core.search_users(query) return jsonify([user_mapper(u) for u in users])
def search_posts(): content = request.get_json() user_id = current_user.meta.id if "query" in content.keys(): query = content["query"] posts = core.search_posts(query, user_id) elif "author-id" in content.keys(): author_id = content["author-id"] posts = core.search_posts_by_author(author_id, user_id) elif "tag" in content.keys(): tag = content["tag"] posts = core.search_posts_by_tag(tag, user_id) else: return client_error("001") try: return jsonify([post_mapper(p) for p in posts]) except FaceduckError as e: return client_error(e.id)
def create_post(): if not request.is_json: return client_error("001") author_id = current_user.meta.id try: text = request.json["text"] image_url = request.json.get("image-url", None) visibility = request.json.get("visibility", "public") except KeyError: return client_error("001") try: post = core.create_post(text, author_id, image_url, visibility) response = post_mapper(post) except FaceduckError as e: return client_error(e.id) return jsonify(response)
def change_user_role(group_id): if "user_id" in request.get_json().keys(): user_id = request.json["user_id"] else: user_id = current_user.meta.id try: admin = request.json["admin"] except KeyError: return client_error("001") core.change_user_role(group_id, user_id, admin) return ("", 204)
def get_friendship(user_id, target_id): friendship = core.exists_friendship(user_id, target_id) try: if not friendship: return jsonify(user_id=user_id, target_id=target_id, state="not-friends") else: return jsonify(friendship_mapper(friendship)) except FaceduckError as e: return client_error(e.id)
def create_group_post(group_id): user_id = current_user.meta.id try: text = request.json["text"] except KeyError: return client_error("001") if "image-url" in request.get_json().keys(): image_url = request.json["image-url"] else: image_url = "" post = core.create_group_post(group_id, user_id, text, image_url) return jsonify(post_mapper(post))
def create_group(): user_id = current_user.meta.id try: name = request.json["name"] except KeyError: return client_error("001") if "image-url" in request.get_json().keys(): image_url = request.json["image-url"] else: image_url = "" group = core.create_group(name, image_url, user_id) return jsonify(group_mapper(group))
def signup(): if not request.is_json: return client_error("001") try: username = request.json['username'] email = request.json['email'] password = request.json['password'] name = request.json['name'] surname = request.json['surname'] birthday = request.json['birthday'] gender = request.json['gender'] except KeyError: return client_error("001") try: user = core.create_user(username, email, password, name, surname, birthday, gender) except FaceduckError as e: return client_error(e.id) return ("", 204)