Beispiel #1
0
    def update_user(self, user, name=IGNORE, about=IGNORE, contact=IGNORE):
        user_obj = user.get_obj()

        changes = {}

        if name != IGNORE:
            user_obj[User.NAME] = name
            changes[User.NAME] = name

            POST_REPO.on_user_name_changed(user.get_public_id(), name)

            posts = user_obj[User.POSTS]

            for post in posts:
                post[Post.USER_NAME] = name

            changes[User.POSTS] = posts

        if about != IGNORE:
            user_obj[User.ABOUT] = about
            changes[User.ABOUT] = about

        if contact != IGNORE:
            user_obj[User.CONTACT] = contact
            changes[User.CONTACT] = contact

        if len(changes) == 0:
            return

        user.on_updated()

        self.db.update_one({User.MONGO_ID: user_obj[User.MONGO_ID]},
                           {"$set": changes})
Beispiel #2
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()
Beispiel #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')

        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()
Beispiel #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()
Beispiel #5
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()
Beispiel #6
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}
Beispiel #7
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()
Beispiel #8
0
    def on_get(self, req, resp):
        count = req.get_param_as_int('count',
                                     min_value=1,
                                     max_value=MAXIMUM_RANDOM_POST_COUNT)
        day = req.get_param_as_int('day', min_value=0)
        week = req.get_param_as_int('week', min_value=0)
        month = req.get_param_as_int('month', min_value=0)
        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 = bytes.fromhex(community)

        posts = POST_REPO.exhibition_posts(count, language, country, community,
                                           day, week, month)

        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}
Beispiel #9
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}