def delete_user(cls, user_id, token): cls.logger().info(f"Deleting videos uploaded by user {user_id}...") from daos.videos_dao import VideoDAO VideoDAO.delete_all_user_videos(user_id) cls.logger().info(f"Deleting comments made by user {user_id}...") from daos.comments_dao import CommentDAO CommentDAO.delete_all_user_comments(user_id) cls.logger().info(f"Deleting reactions given by user {user_id}...") from daos.reactions_dao import ReactionDAO ReactionDAO.delete_all_user_reactions(user_id) cls.logger().info(f"Deleting chats posted by user {user_id}...") from daos.chats_dao import ChatsDAO ChatsDAO.delete_all_user_chats(user_id) cls.logger().info( f"Deleting friends, friend requests and push token for user {user_id}..." ) user = cls.get_raw(user_id) friends = user.friends for f in friends: user.delete_friendship(f) user.sent_requests = [] user.push_token = None db.session.delete(user) db.session.commit() cls.logger().info(f"Deleting user {user_id} from AuthSv...") from services.authsender import AuthSender AuthSender.delete_user(user_id, token)
def delete(self, user_id, friend_id): parser = reqparse.RequestParser() parser.add_argument("x-access-token", location='headers') args = parser.parse_args() viewer_uuid = AuthSender.get_uuid_from_token(args["x-access-token"]) if not AuthSender.has_permission(user_id, viewer_uuid): raise BadRequestError( f"You don't have permission to delete other users' friends") UsersDAO.delete_friendship(user_id, friend_id) return {"msg": "OK"}, 200
def get(self, vid_id): parser = reqparse.RequestParser() parser.add_argument("x-access-token", location='headers') args = parser.parse_args() self.logger.debug("getting uuid from token, via authsv") uuid = AuthSender.get_uuid_from_token(args["x-access-token"]) self.logger.debug("getting single vid from videoDAO") vid = VideoDAO.get(vid_id, uuid) vid["author"] = AuthSender.get_author_name(vid["uuid"], args["x-access-token"]) self.logger.info(f"Retrieved single video, id {vid_id}, info: {vid}. RESPONSECODE:200") return vid, 200
def _add_info_and_popularity(cls, all_vids, viewer_uuid, token, sort_by_pop=False): final_vids = [] for v in all_vids: res = v.serialize() if cls._cant_view(res["is_private"], res["uuid"], viewer_uuid): continue cls.add_extra_info(res, viewer_uuid) res["author"] = AuthSender.get_author_name(res["uuid"], token) if sort_by_pop: res["popularity"] = cls._calculate_popularity( v, viewer_uuid, res["timestamp"]) final_vids.append(res) if sort_by_pop: final_vids = sorted(final_vids, key=lambda k: k['popularity'], reverse=True) return final_vids
def patch(self, user_id): parser = reqparse.RequestParser() parser.add_argument("display_name", location="json", required=False, type=str) parser.add_argument("email", location="json", required=False, type=str) parser.add_argument("phone_number", location="json", required=False, type=str) parser.add_argument("image_location", location="json", required=False, type=str) parser.add_argument("x-access-token", location='headers', required=True, help='Missing user token!') parser.add_argument("password", location="json", required=False, default="", type=str) args_dict = parser.parse_args() args_dict = {k: v for k, v in args_dict.items() if v is not None} msg, code = AuthSender.modify_user(user_id, args_dict) self.logger.info( f"User {user_id} info edited: {msg}. RESPONSECODE:{code}") return msg, code
def get(self): parser = reqparse.RequestParser() parser.add_argument("name", type=str, required=False, location='args') parser.add_argument("email", type=str, required=False, location='args') parser.add_argument("phone", type=str, required=False, location='args') parser.add_argument('per_page', type=int, required=False, location='args') parser.add_argument('page', type=int, required=False, location='args') parser.add_argument("x-access-token", location='headers', required=True, help='Missing user token!') args = parser.parse_args() msg, code = AuthSender.find_user(args["x-access-token"], args["name"], args["email"], args["phone"], args["per_page"], args["page"]) self.logger.info( f"Executed user search with args name={args['name']}, email={args['email']}, phone={args['phone']}, per_page={args['per_page']}, page={args['page']}. RESPONSECODE:{code}" ) return msg, code
def post(self): parser = reqparse.RequestParser() parser.add_argument("email", location='json', type=str, required=True, help="Missing email address to recover") parser.add_argument("reset_code", location='json', type=str, required=True, help="Missing reset code") parser.add_argument("password", location='json', type=str, required=True, help="Missing new password") args = parser.parse_args() msg, code = AuthSender.send_new_password(args["email"], args["reset_code"], args["password"]) self.logger.info( f"User with email {args['email']} changed password. RESPONSECODE:{code}" ) return msg, code
def post(self, vid_id): args = postcmnt_parser.parse_args() uuid = AuthSender.get_uuid_from_token(args["x-access-token"]) new_cmnt = CommentDAO.add_cmnt(vid_id, uuid=uuid, text=args['text'], time=args["vid_time"], tkn=args["x-access-token"]) self.logger.info(f"Posted new comment to video {vid_id}. Comment: {new_cmnt}. RESPONSECODE:201") return new_cmnt, 201
def delete(self, user_id): parser = reqparse.RequestParser() parser.add_argument("x-access-token", location='headers', required=True, help='Missing user token!') args = parser.parse_args() viewer_uuid = AuthSender.get_uuid_from_token(args["x-access-token"]) if not AuthSender.has_permission(user_id, viewer_uuid): self.logger.info( f"User {viewer_uuid} attempted to delete user's {user_id} account. Access Denied." ) raise BadRequestError(f"You can't delete other users profiles!") UsersDAO.delete_user(user_id, args["x-access-token"]) return {"message": "OK"}, 200
def get(self, user_id): parser = reqparse.RequestParser() parser.add_argument("x-access-token", location='headers', required=True, help='Missing user token!') args = parser.parse_args() msg, code = AuthSender.get_user_info(user_id, args['x-access-token']) if code == 200: viewer_id = AuthSender.get_uuid_from_token(args["x-access-token"]) msg["friendship_status"] = UsersDAO.get_friendship_status( user_id, viewer_id) self.logger.info( f"User {user_id} info collected: {msg}. RESPONSECODE:{code}") return msg, code
def post(self, other_user_id): parser = reqparse.RequestParser() parser.add_argument("x-access-token", location='headers', required=True, help='Missing user token!') parser.add_argument("text", location='json', type=str, required=True, help='You must include the text to be sent!') args = parser.parse_args() if args["text"] == "": raise BadRequestError("Invalid message length") sender_uuid = AuthSender.get_uuid_from_token(args['x-access-token']) author_name = AuthSender.get_author_name(sender_uuid, args["x-access-token"]) if not UsersDAO.are_friends(sender_uuid, other_user_id): raise BadRequestError("You are not friends with this user") msg = ChatsDAO.send_message(sender_uuid, other_user_id, args["text"], author_name) self.logger.info(f"Succesfully sent message from user {sender_uuid} to {other_user_id}. RESPONSECODE:200") return msg, 201
def decorated(*args, **kwargs): token = None if 'x-access-token' in request.headers: token = request.headers['x-access-token'] if not token: return make_response({'message': 'Missing user token!'}, 400) if not AuthSender.is_valid_token(token): return make_response({'message': 'Token is invalid!'}, 401) return f(*args, **kwargs)
def delete(self, vid_id): parser = reqparse.RequestParser() parser.add_argument("x-access-token", location='headers') args = parser.parse_args() uuid = AuthSender.get_uuid_from_token(args["x-access-token"]) self.logger.debug(f"Deleting video {vid_id}") VideoDAO.delete(vid_id, uuid) self.logger.info(f"Deleted video with id {vid_id}. RESPONSECODE:200") return {"message":"OK"}, 200
def get(self, user_id): parser = reqparse.RequestParser() parser.add_argument("x-access-token", location='headers') args = parser.parse_args() viewer_uuid = AuthSender.get_uuid_from_token(args['x-access-token']) vids = VideoDAO.get_videos_by(user_id, viewer_uuid, args["x-access-token"]) self.logger.info( f"{len(vids)} vids collected by user {user_id}. RESPONSECODE:200") return vids, 200
def post(self): parser = reqparse.RequestParser() parser.add_argument("x-access-token", location='headers') parser.add_argument( "to", type=int, required=True, help="You must specify who you are sending this request to", location='json') args = parser.parse_args() sender_uuid = AuthSender.get_uuid_from_token(args["x-access-token"]) author_name = AuthSender.get_author_name(sender_uuid, args["x-access-token"]) msg = UsersDAO.send_request(sender_uuid, args["to"], author_name) self.logger.info( f"Succesfully sent request from user {sender_uuid} to {args['to']}. RESPONSECODE:201" ) return msg, 201
def delete(cls, vid_id, actioner_uuid): vid = cls.get_raw(vid_id) if not AuthSender.has_permission(vid.uuid, actioner_uuid): raise BadRequestError("Only the author can delete their video!") vid.comments = [] vid.reactions = [] db.session.delete(vid) db.session.commit() MediaSender.delete_vid(vid_id)
def get(self): parser = reqparse.RequestParser() parser.add_argument("x-access-token", location='headers', required=True, help='Missing user token!') args_dict = parser.parse_args() id = AuthSender.get_uuid_from_token(args_dict["x-access-token"]) self.logger.info( f"Returning push token for user {id}. RESPONSECODE:200") return {"push_token": UsersDAO.get_tkn(id)}, 200
def get(self, other_user_id): parser = reqparse.RequestParser() parser.add_argument("x-access-token", location='headers', required=True, help='Missing user token!') args = parser.parse_args() uuid1 = AuthSender.get_uuid_from_token(args['x-access-token']) UsersDAO.check_exists(uuid1) UsersDAO.check_exists(other_user_id) msgs = ChatsDAO.get_messages_between(uuid1, other_user_id) self.logger.info(f"Found {len(msgs)} messages between users {uuid1, other_user_id}. RESPONSECODE:200") return msgs, 200
def post(self): args = post_parser.parse_args() uuid = AuthSender.get_uuid_from_token(args["x-access-token"]) # add to local db new_vid_with_url = VideoDAO.add_vid(title=args['title'], description=args['description'], uuid=uuid, location=args['location'], is_private=args['is_private'], thumbnail_url=args['thumbnail_url']) # upload to mediasv new_vid_with_url['firebase_url'], new_vid_with_url['timestamp'] = MediaSender.send_url(new_vid_with_url['video_id'],args['firebase_url']) self.logger.info(f"New video uploaded, video info: {new_vid_with_url}. RESPONSECODE:201") return new_vid_with_url, 201
def get(self): parser = reqparse.RequestParser() parser.add_argument("x-access-token", location='headers') args = parser.parse_args() viewer_uuid = AuthSender.get_uuid_from_token(args["x-access-token"]) reqs = UsersDAO.view_pending_reqs(viewer_uuid) self.logger.info( f"Found {len(reqs['pending_reqs'])} pending requests for user {viewer_uuid}. RESPONSECODE:200" ) return reqs, 200
def post(self): parser = reqparse.RequestParser() parser.add_argument("display_name", location="json", required=True, help="Missing user's full name.", type=str) parser.add_argument("email", location="json", required=True, help="Missing user's email.", type=str) parser.add_argument("phone_number", location="json", required=False, default="", type=str) parser.add_argument("image_location", location="json", required=False, default="", type=str) parser.add_argument("password", location="json", required=False, default="", type=str) parser.add_argument("x-access-token", location='headers', required=True, help='Missing user token!') args = parser.parse_args() msg, code = AuthSender.register_user(fullname=args["display_name"], email=args['email'], phone=args['phone_number'], avatar=args['image_location'], token=args['x-access-token'], password=args["password"]) if code == 201: self.logger.info( f"New user created with info: {msg}. RESPONSECODE:{code}") UsersDAO.add_user_to_db(msg['id']) else: self.logger.error( f"Failed to create user with info: {msg}. RESPONSECODE:{code}") return msg, code
def get(self): parser = reqparse.RequestParser() parser.add_argument("x-access-token", location='headers', required=True, help='Missing user token!') args = parser.parse_args() msg, code = AuthSender.is_admin(args["x-access-token"]) self.logger.info( f"Executed GET on /users/admin. Result: {msg['admin']} RESPONSECODE:{code}" ) return msg, code
def patch(self, vid_id): edit_parser = reqparse.RequestParser() edit_parser.add_argument("x-access-token", location='headers') edit_parser.add_argument('description', type = str, location = 'json') edit_parser.add_argument('location', type = str, location = 'json') edit_parser.add_argument('title', type = str, location = 'json') edit_parser.add_argument('is_private', type = bool, location = 'json') args = edit_parser.parse_args() uuid = AuthSender.get_uuid_from_token(args["x-access-token"]) self.logger.debug("editing single vid from videoDAO") vid = VideoDAO.edit(vid_id, args, uuid) self.logger.info(f"Edited video info, id {vid_id}, info: {vid}. RESPONSECODE:200") return vid, 200
def edit(cls, vid_id, args, uuid): vid = cls.get_raw(vid_id) if not AuthSender.has_permission(vid.uuid, uuid): raise BadRequestError(f"Only the author can edit their video!") if "description" in args: vid.description = args["description"] if "location" in args: vid.location = args["location"] if "title" in args: vid.title = args["title"] if "is_private" in args: vid.is_private = args["is_private"] db.session.commit() return vid.serialize()
def get(self): parser = reqparse.RequestParser() parser.add_argument("x-access-token", location='headers') parser.add_argument("search", type=str, required=False, location='args', default=None) parser.add_argument("page", type=int, required=False, location='args', default=1) parser.add_argument("per_page", type=int, required=False, location='args', default=50) args = parser.parse_args() uuid = AuthSender.get_uuid_from_token(args["x-access-token"]) if args["search"]: videos = VideoDAO.get_from_search(uuid, args["x-access-token"], args["search"], page=args["page"], per_page=args["per_page"]) else: videos = VideoDAO.get_recommendations(uuid, args["x-access-token"], page=args["page"], per_page=args["per_page"]) # videos = VideoDAO.get_all(uuid, args["x-access-token"]) self.logger.info(f"Executed /videos search, found {len(videos['videos'])} videos. RESPONSECODE:200") return videos, 200
def post(self): parser = reqparse.RequestParser() parser.add_argument("x-access-token", location='headers', required=True, help='Missing user token!') parser.add_argument("push_token", location='json', type=str, required=True, help="Missing Expo Push Token") args_dict = parser.parse_args() id = AuthSender.get_uuid_from_token(args_dict["x-access-token"]) UsersDAO.set_tkn(id, args_dict["push_token"]) self.logger.info( f"Added new push token for user {id}. RESPONSECODE:200") return {"message": "OK"}, 200
def post(self, sender_id): parser = reqparse.RequestParser() parser.add_argument("x-access-token", location='headers') parser.add_argument( "accept", location='json', type=bool, required=True, help='You must specify if you either accept or reject the request') args = parser.parse_args() viewer_uuid = AuthSender.get_uuid_from_token(args["x-access-token"]) msg = UsersDAO.respond_request(viewer_uuid, sender_id, accept=args["accept"]) self.logger.info( f"User {viewer_uuid} responded request from {sender_id}. Accepted: {args['accept']}. RESPONSECODE:200" ) return msg, 200
def add_cmnt(cls, vid_id, uuid, text, time, tkn): new_cmnt = Comment(uuid=uuid, text=text, vid_time=time) vid = VideoDAO.get_raw(vid_id) new_cmnt.video = vid db.session.add(new_cmnt) db.session.commit() cls.logger().info( f"Succesfully appended comment to video {vid_id}. Comment: {new_cmnt.serialize()}" ) vid.update_relevance() # Notification build w/tkn... cmnt_author = AuthSender.get_author_name(uuid, tkn) if uuid != vid.uuid: NotificationsCreator.notify_new_comment(vid.uuid, vid_id, vid.title, text, cmnt_author) return new_cmnt.serialize()
def get_videos_by(cls, user_id, viewer_uuid, token): cls.logger().info(f"Grabbing all videos by user {user_id}") UsersDAO.check_exists(user_id) videos = [ v.serialize() for v in Video.query.filter(Video.uuid == user_id) ] cls.logger().info( f"Filtering by viewable videos for viewer {viewer_uuid}") filtered = [ f for f in videos if not cls._cant_view(f["is_private"], viewer_uuid, user_id) ] for f in filtered: cls.add_extra_info(f, viewer_uuid) f["author"] = AuthSender.get_author_name(f["uuid"], token) cls.logger().info( f"Found {len(filtered)} viewable videos uploaded by user {user_id}" ) return filtered
def _cant_view(cls, is_private, user1_id, user2_id): return is_private and not AuthSender.has_permission( user1_id, user2_id) and not UsersDAO.are_friends( user1_id, user2_id)