Пример #1
0
    def on_get(self, req, resp):
        user = authenticate(req, resp)

        if not user:
            return

        count = req.get_param_as_int('count', min_value=1, max_value=MAXIMUM_COUNT, default=10)
        offset = req.get_param_as_int('offset', min_value=0, default=0)

        user_obj = user.get_obj()

        total_count = user_obj[User.POSTED_POSTS_COUNT]

        all_posts = user_obj[User.POSTS]
        posts_with_offset = all_posts[offset:offset + count]

        count = len(posts_with_offset)

        posts = []

        for post_obj in posts_with_offset:
            posts.append(Post(companion=post_obj).get_public_obj())

        resp.status = falcon.HTTP_200
        resp.media = {'totalCount': total_count, 'count': count, 'data': posts}
Пример #2
0
    def on_get(self, req, resp):
        resp.set_header('Access-Control-Allow-Origin', '*')
        resp.set_header('Access-Control-Allow-Headers', '*')
        resp.set_header('Access-Control-Allow-Methods', 'GET')
        resp.set_header('Access-Control-Max-Age', 86400)  # One day

        token = req.get_header('token')

        if token != 'web':
            user = authenticate(req, resp)

            if not user:
                return

        post_id = req.get_param('id')

        post = POST_REPO.find_post(post_id)

        if not post:
            resp.status = falcon.HTTP_404  # Not found
            resp.media = {'message': 'Post not found.'}
            return

        resp.status = falcon.HTTP_200
        resp.media = post.get_public_obj()
Пример #3
0
    def on_get(self, req, resp):
        resp.set_header('Access-Control-Allow-Origin', '*')
        resp.set_header('Access-Control-Allow-Headers', '*')
        resp.set_header('Access-Control-Allow-Methods', 'GET')
        resp.set_header('Access-Control-Max-Age', 86400)  # One day

        token = req.get_header('token')

        user = None

        if True or token != 'web':
            user = authenticate(req, resp)

            if not user:
                return

        req_user_id = req.get_param('id', 'self')

        if req.req_user_id != 'self' and req_user_id != user.get_public_id():
            raw_req_user_id = bytes.fromhex(req_user_id)

            user = USER_REPO.find_user(raw_req_user_id)

        if not user:
            resp.status = falcon.HTTP_NOT_FOUND
            resp.media = {'message': 'User not found.'}
            return

        resp.status = falcon.HTTP_OK
        resp.media = user.get_public_obj()
Пример #4
0
    def on_post(self, req, resp):
        user = authenticate(req, resp)

        if not user:
            return

        post_content = req.media.get('post')
        language = req.media.get('language')
        country = req.media.get('country')
        community = req.media.get('community')

        if not validate(post_content, language, country, resp):
            return

        if not community:
            community = COMMUNITY_REPO.get_public_community_number()
        else:
            community = COMMUNITY_REPO.get_community_by_id_name(community)

            if not community:
                resp.status = falcon.HTTP_400  # Bad request
                resp.media = {'message': 'Community id name not found.'}
                return

            community = community.get_obj()[Community.NUMBER]

        post = POST_REPO.new_post(user, post_content, language, country,
                                  community)

        USER_REPO.on_new_post(user, post)

        sync = SYNC_REPO.new_post(post)

        resp.status = falcon.HTTP_200
        resp.media = post.get_public_obj()
Пример #5
0
    def on_post(self, req, resp):
        user = authenticate(req, resp)

        if not user:
            return

        post_id = req.media.get('postId')
        comment_id = req.media.get(
            'commentId'
        )  # Ignored because reporting is censorship in the first place.
        reason = req.media.get('reason')
        description = req.media.get('description')

        if not post_id:
            resp.status = falcon.HTTP_400  # Bad request
            resp.media = {'message': 'Post id is not given.'}
            return

        if not reason:
            resp.status = falcon.HTTP_400  # Bad request
            resp.media = {'message': 'Reason is not given.'}
            return

        try:
            reason = int(reason)
        except ValueError:
            resp.status = falcon.HTTP_400  # Bad request
            resp.media = {'message': 'Reason must be an integer.'}
            return

        post = POST_REPO.find_post(post_id)

        if not post:
            resp.status = falcon.HTTP_406  # Not acceptable
            resp.media = {'message': 'Post id does not exist.'}
            return

        if comment_id:
            resp.status = falcon.HTTP_200
            resp.media = {'message': 'Ignored'}
            return

        report = Report(reporting_user_id=user.get_public_id(),
                        reported_user_id=post.get_user_id(),
                        post_id=post_id,
                        reason=reason,
                        description=description)

        post = POST_REPO.on_new_report(post, report)

        #  These two commands can be aggregated into one command.
        USER_REPO.update_user_post(post)
        USER_REPO.on_report_on_user(post.get_user_id(), report)

        USER_REPO.on_new_report(user, report)

        resp.status = falcon.HTTP_200
        resp.media = report.get_obj()
Пример #6
0
    def on_post(self, req, resp):
        user = authenticate(req, resp)

        if not user:
            return

        post_id = req.media.get('postId')
        liked = req.media.get('like')

        if not post_id:
            resp.status = falcon.HTTP_400  # Bad request
            resp.media = {'message': 'Post id is not given.'}
            return

        if liked is None:
            resp.status = falcon.HTTP_400  # Bad request
            resp.media = {'message': '"like" boolean is not set.'}
            return

        if not isinstance(liked, bool):
            if liked.lower() == 'true':
                liked = True
            else:
                if liked.lower == 'false':
                    liked = False
                else:
                    resp.status = falcon.HTTP_400  # Bad request
                    resp.media = {'message': '"like" must either be true or false.'}
                    return

        like = Like(user_id=user.get_public_id(), post_id=post_id)

        (post, has_change) = POST_REPO.on_new_like(like, liked)

        if not post:
            resp.status = falcon.HTTP_406  # Not acceptable
            resp.media = {'message': 'Post id does not exist.'}
            return

        if not has_change:
            resp.status = falcon.HTTP_200
            resp.media = like.get_obj()
            return

        user_repo = USER_REPO

        user_repo.update_user_post(post)

        user_repo.on_new_like(user, like, liked)

        sync = SYNC_REPO.new_like(actor=user.get_public_id(), affected=post.get_user_id(), like=like, liked=liked)

        if liked:
            #  TODO Use sync to send a notification.
            pass

        resp.status = falcon.HTTP_200
        resp.media = like.get_obj()
Пример #7
0
    def on_get(self, req, resp):
        user = authenticate(req, resp)

        if not user:
            return

        update_token = req.get_param('updateToken', default=None)

        (syncs, new_update_token,
         has_more) = SYNC_REPO.sync(user, update_token, MAX_SYNC_OBJECT_COUNT)

        resp.status = falcon.HTTP_200
        resp.media = {
            'updateToken': new_update_token,
            'syncCompleted': not has_more,
            'changes': syncs
        }
Пример #8
0
    def on_post(self, req, resp):
        user = authenticate(req, resp)

        if not user:
            return

        token = req.media.get('token')

        if not token:
            resp.status = falcon.HTTP_400  # Bad request
            resp.media = {'message': 'Token is not given.'}
            return

        USER_REPO.set_fcm_token(user, token)

        resp.status = falcon.HTTP_200
        resp.media = {'message': 'Success!'}
Пример #9
0
    def on_get(self, req, resp):
        user = authenticate(req, resp)

        if not user:
            return

        count = req.get_param_as_int('count',
                                     min_value=1,
                                     max_value=MAXIMUM_RANDOM_POST_COUNT)
        language = req.get_param('language')
        country = req.get_param('country')
        community = req.get_param('community')

        if language and not ttm_util.validate_language(language):
            resp.status = falcon.HTTP_400  # Bad request
            resp.media = {'message': 'Invalid language code'}
            return

        if country and not ttm_util.validate_country(country):
            resp.status = falcon.HTTP_400  # Bad request
            resp.media = {'message': 'Invalid country code'}
            return

        if community is None or len(community) == 0:
            community = COMMUNITY_REPO.get_public_community_id()
        else:
            community = COMMUNITY_REPO.get_community_by_id_name(community)

            if community is not None:
                community = community.get_obj()[Community.ID]
            else:
                resp.status = falcon.HTTP_404  # Not found
                resp.media = {'message': 'Community not found'}
                return

        posts = POST_REPO.random_posts(count, language, country, community)

        if posts == COMMUNITY_DOES_NOT_EXIST:
            resp.status = falcon.HTTP_404  # Not found
            resp.media = {'message': 'Community not found'}
            return

        resp.status = falcon.HTTP_200
        resp.media = {'posts': posts}
Пример #10
0
    def on_post(self, req, resp):
        user = authenticate(req, resp)

        if not user:
            return

        post_id = req.media.get('postId')
        content = req.media.get('comment')

        if not post_id:
            resp.status = falcon.HTTP_400  # Bad request
            resp.media = {'message': 'Post id is not given.'}
            return

        if not validate(content, resp):
            return

        comment = Comment(user_id=user.get_public_id(),
                          post_id=post_id,
                          content=content)

        post = POST_REPO.on_new_comment(comment)

        if not post:
            resp.status = falcon.HTTP_406  # Not acceptable
            resp.media = {'message': 'Post id does not exist.'}
            return

        user_repo = USER_REPO

        receiving_user = user_repo.update_user_post(post)

        user_repo.on_new_comment(user, comment)

        sync = SYNC_REPO.new_comment(actor=user.get_public_id(),
                                     affected=post.get_user_id(),
                                     comment=comment)

        # Use sync to send a notification.
        gotification.send_notification(receiving_user, sync)

        resp.status = falcon.HTTP_200
        resp.media = comment.get_obj()
Пример #11
0
    def on_get(self, req, resp):
        user = authenticate(req, resp)

        if not user:
            return

        user_id = req.get_param('id')
        count = req.get_param_as_int('count',
                                     min_value=1,
                                     max_value=MAXIMUM_COUNT,
                                     default=10)
        offset = req.get_param_as_int('offset', min_value=0, default=0)

        raw_user_id = bytes.fromhex(user_id)
        user = USER_REPO.find_user(raw_user_id)

        if not user:
            resp.status = falcon.HTTP_NOT_FOUND
            resp.media = {'message': 'User id not found.'}
            return

        user_obj = user.get_obj()

        total_count = user_obj[User.POSTED_POSTS_COUNT]

        all_posts = user_obj[User.POSTS]
        posts_with_offset = all_posts[offset:offset + count]

        count = len(posts_with_offset)

        posts = []

        for post_obj in posts_with_offset:
            posts.append(Post(companion=post_obj).get_public_obj())

        resp.status = falcon.HTTP_200
        resp.media = {'totalCount': total_count, 'count': count, 'data': posts}
Пример #12
0
    def on_put(self, req, resp):
        user = authenticate(req, resp)

        if not user:
            return

        name = req.media.get('name') if 'name' in req.media else IGNORE
        about = req.media.get('about') if 'about' in req.media else IGNORE
        contact = req.media.get(
            'contact') if 'contact' in req.media else IGNORE

        name = _.prepare_arg(name)
        about = _.prepare_arg(about)
        contact = _.prepare_arg(contact)

        if type(name) == str and (len(name) < NAME_MIN_LENGTH
                                  or len(name) > NAME_MAX_LENGTH):
            resp.status = falcon.HTTP_BAD_REQUEST
            resp.media = {'message': 'Invalid name length.'}
            return

        if type(about) == str and (len(about) < ABOUT_MIN_LENGTH
                                   or len(about) > ABOUT_MAX_LENGTH):
            resp.status = falcon.HTTP_BAD_REQUEST
            resp.media = {'message': 'Invalid about length.'}
            return

        if type(contact) == str and (len(contact) < CONTACT_MIN_LENGTH
                                     or len(contact) > CONTACT_MAX_LENGTH):
            resp.status = falcon.HTTP_BAD_REQUEST
            resp.media = {'message': 'Invalid contact length.'}
            return

        USER_REPO.update_user(user, name, about, contact)

        resp.status = falcon.HTTP_ACCEPTED
        resp.media = user.get_public_obj()
Пример #13
0
    def on_post(self, req, resp):
        user = authenticate(req, resp)

        if not user:
            return

        post_ids = req.media.get('postIds')

        if not post_ids:
            resp.status = falcon.HTTP_400  # Bad request
            resp.media = {'message': 'Require post ids.'}
            return

        if not isinstance(post_ids, list):
            resp.status = falcon.HTTP_400  # Bad request
            resp.media = {'message': 'postIds must be an array of post ids.'}
            return

        USER_REPO.on_viewed_posts(user, len(post_ids))

        # We can cache the list of ids to process them later in batch. Not by every request.
        # The logic in app also tries to send the view notices in batch.

        # The algorithm here aggregates twice:
        # First time on post ids, to avoid retrieving the same post multiple times.
        # Second time on user ids, to avoid retrieving the same user multiple times.

        post_ids = sorted(post_ids)

        aggregated_affected_users = {}

        processed_post_ids = []

        for post_id, values in groupby(post_ids):
            view_count = len(list(values))

            post = POST_REPO.on_post_viewed(post_id, view_count)

            if not post:
                continue

            processed_post_ids.append(post_id)

            affected_user = post.get_user_id()

            if affected_user:
                affected_posts = aggregated_affected_users.get(
                    affected_user, {})

                affected_posts[post.get_mongo_id()] = post.get_view_count()

                aggregated_affected_users[affected_user] = affected_posts

            SYNC_REPO.new_view(actor=user.get_public_id(),
                               affected=post.get_user_id(),
                               post_id=post.get_public_id(),
                               views=view_count)

        for user_id in aggregated_affected_users:
            affected_posts = aggregated_affected_users[user_id]

            USER_REPO.set_view_on_posts(user_id, affected_posts)

        resp.status = falcon.HTTP_200
        resp.media = {'postIds': processed_post_ids}