def create_friendship(user_id, target_id, state='pending'): if User.get(id=user_id, ignore=404) is None or User.get(id=target_id, ignore=404) is None: raise FaceduckError("001") if exists_friendship(user_id, target_id): raise FaceduckError("001") friendship = Friendship(meta={'id': uuid.uuid4()}, user_id=user_id, target_id=target_id, state=state) friendship.save() return friendship
def create_user(username, email, password, name, surname, birthday, gender): if username_already_exists(username): raise FaceduckError("002") if email_already_exists(email): raise FaceduckError("003") id = uuid.uuid4() password = generate_password_hash(password) user = User(meta={'id': id}, username=username, email=email, password=password, name=name, surname=surname, birthday=birthday, gender=gender) user.save() return user
def login_user(email, password, device, ip): user = get_user_by_email(email) if user is None: raise FaceduckError("004") if not check_password_hash(user.password, password): add_log(user, device, ip, False) raise FaceduckError("004") token = SessionKeeping.generate_jwt_token(user.meta.id) add_log(user, device, ip, True) return user, token
def get_user(user_id): try: user = User.get(id=user_id) except NotFoundError: raise FaceduckError("001") return user
def get_post(post_id, user_id): try: post = Post.get(id=post_id) except NotFoundError: raise FaceduckError("001") if post.visibility == "private" and post.author != user_id: raise FaceduckError("001") elif post.visibility == "friends": friends = get_full_friend_ids(post.author) + [user_id] if user_id not in friends: raise FaceduckError("001") elif post.visibility == "loggedin" and user_id is None: raise FaceduckError("001") return post
def create_post(text, author_id, image_url, visibility): id = uuid.uuid4() created_at = str(datetime.now().time()) split_post = text.split(' ') tags = [] for word in split_post: word = remove_punct(word) if len(word) > 0 and word[0] == '#': tags.append(word) if User.get(id=author_id, ignore=404) is None: raise FaceduckError("001") if image_url is None or len(image_url) == 0: image_url = social_card_for_post(text) post = Post(meta={'id': id}, text=text, created_at=created_at, author=author_id, image_url=image_url, tags=tags, visibility=visibility) post.save(refresh=True) return post
def get_full_friends(user_id): try: friends = Friendship.search().from_dict({ "query": { "bool": { "must": { "match": { "state": "friends" } }, "should": [ { "multi_match": { "query": user_id, "fields": ["user_id", "target_id"] } } ] } } }).doc_type(Friendship).execute() except NotFoundError: raise FaceduckError("001") return friends
def update_friendship(user_id, target_id, state): states = ['friends'] if state not in states: raise FaceduckError("001") if User.get(id=user_id, ignore=404) is None or User.get(id=target_id, ignore=404) is None: raise FaceduckError("001") friendship = exists_friendship(target_id, user_id) if friendship is False: raise FaceduckError("001") friendship.state = state friendship.save() return friendship
def get_post_by_id(post_id): try: post = Post.get(id=post_id) except NotFoundError: raise FaceduckError("001") return post