Пример #1
0
def all_completed_mobile_monsters(request):
    """ Returns all completed mobile monsters for ones the logged-in user began. """
    from apps.monster.models import MONSTER_MOBILE_GROUP

    comment_details = []
    cids = Comment.objects.filter(category__name=MONSTER_MOBILE_GROUP,
                                  parent_comment__author=request.user).values_list('id', flat=True)
    if cids:
        comment_details = CommentDetails.from_ids(cids)
    return {'bottoms': comment_details}
Пример #2
0
def all_completed_mobile_monsters(request):
    """ Returns all completed mobile monsters for ones the logged-in user began. """
    from apps.monster.models import MONSTER_MOBILE_GROUP

    comment_details = []
    cids = Comment.objects.filter(
        category__name=MONSTER_MOBILE_GROUP,
        parent_comment__author=request.user).values_list('id', flat=True)
    if cids:
        comment_details = CommentDetails.from_ids(cids)
    return {'bottoms': comment_details}
Пример #3
0
def feed_for_user(user,
                  earliest_timestamp_cutoff=None,
                  per_page=knobs.FEED_ITEMS_PER_PAGE,
                  items_to_skip=set()):
    following_ids = user.redis.following.smembers()
    followed_thread_ids = user.redis.followed_threads.smembers()

    if not following_ids and not followed_thread_ids:
        return []

    feed_source_keys = [
        UserFeedSourceBuffer.get_key(user_id) for user_id in following_ids
    ]
    feed_thread_source_keys = [
        ThreadFeedSourceBuffer.get_key(thread_id)
        for thread_id in followed_thread_ids
    ]

    thread_posts = [{
        'type': 'thread',
        'comment_id': id_,
        'ts': score
    } for id_, score in redis.zunion(
        feed_thread_source_keys,
        withscores=True,
        transaction=False,
        max_score=_tighten_earliest_timestamp_cutoff(
            earliest_timestamp_cutoff))]

    posts = [{
        'type': 'post',
        'comment_id': id_,
        'ts': score
    } for id_, score in redis.zunion(
        feed_source_keys,
        withscores=True,
        transaction=False,
        max_score=_tighten_earliest_timestamp_cutoff(
            earliest_timestamp_cutoff))]

    posts += thread_posts

    promotions = promoted_comments(
        user,
        earliest_timestamp_cutoff=earliest_timestamp_cutoff,
        comments_to_skip=set(post['comment_id'] for post in posts))

    # Sort by recency.
    items = sorted(itertools.chain(posts, promotions),
                   key=lambda e: float(e['ts']),
                   reverse=True)

    # Skip items as requested and skip comments the user has hidden.
    hidden_comments = user.redis.hidden_comments.smembers()
    comments_to_skip = set(str(item['comment_id'])
                           for item in items_to_skip) | hidden_comments

    # Remove dupes.
    items = OrderedDict(
        (
            str(item['comment_id']),
            item,
        ) for item in items
        if str(item['comment_id']) not in comments_to_skip).values()

    # Pagination.
    items = items[:per_page]

    # Promote comment_id to CommentDetails.
    details = CommentDetails.from_ids([item['comment_id'] for item in items])
    for i, item in enumerate(items):
        item['comment'] = details[i]

    # Hide hidden threads.
    items = user.redis.hidden_threads.filter_comments(
        items, comment_key=lambda item: item['comment'])

    # Prune items that shouldn't show up in this feed.
    items = filter(
        partial(visible_in_feed,
                earliest_timestamp_cutoff=earliest_timestamp_cutoff), items)
    items = filter(partial(not_self_authored, username=user.username), items)

    # Determine whether each item is dismissable by user.
    def item_is_dismissable(item):
        if item['type'] == 'promotion' and item['username'].lower(
        ) == 'canvas':
            return False
        return item['type'] != 'sticky_thread' and is_dismissable(
            item['comment'], user)

    for item in items:
        item['is_dismissable'] = item_is_dismissable(item)

    # Add viewer_sticker to items.
    for item in items:
        _add_viewer_sticker_to_item(item, user)

    return items
Пример #4
0
 def invites(self):
     return CommentDetails.from_ids(self.smembers())
Пример #5
0
 def invites(self):
     return CommentDetails.from_ids(self.smembers())
Пример #6
0
def feed_for_user(user, earliest_timestamp_cutoff=None, per_page=knobs.FEED_ITEMS_PER_PAGE, items_to_skip=set()):
    following_ids = user.redis.following.smembers()
    followed_thread_ids = user.redis.followed_threads.smembers()

    if not following_ids and not followed_thread_ids:
        return []

    feed_source_keys = [UserFeedSourceBuffer.get_key(user_id) for user_id in following_ids]
    feed_thread_source_keys = [ThreadFeedSourceBuffer.get_key(thread_id) for thread_id in followed_thread_ids]

    thread_posts = [{'type': 'thread', 'comment_id': id_, 'ts': score}
             for id_, score in
             redis.zunion(feed_thread_source_keys,
                          withscores=True, transaction=False,
                          max_score=_tighten_earliest_timestamp_cutoff(earliest_timestamp_cutoff))]

    posts = [{'type': 'post', 'comment_id': id_, 'ts': score}
             for id_, score in
             redis.zunion(feed_source_keys,
                          withscores=True, transaction=False,
                          max_score=_tighten_earliest_timestamp_cutoff(earliest_timestamp_cutoff))]

    posts += thread_posts

    promotions = promoted_comments(user,
                                   earliest_timestamp_cutoff=earliest_timestamp_cutoff,
                                   comments_to_skip=set(post['comment_id'] for post in posts))

    # Sort by recency.
    items = sorted(itertools.chain(posts, promotions), key=lambda e: float(e['ts']), reverse=True)

    # Skip items as requested and skip comments the user has hidden.
    hidden_comments = user.redis.hidden_comments.smembers()
    comments_to_skip = set(str(item['comment_id']) for item in items_to_skip) | hidden_comments

    # Remove dupes.
    items = OrderedDict((str(item['comment_id']), item,) for item in items
                        if str(item['comment_id']) not in comments_to_skip).values()

    # Pagination.
    items = items[:per_page]

    # Promote comment_id to CommentDetails.
    details = CommentDetails.from_ids([item['comment_id'] for item in items])
    for i, item in enumerate(items):
        item['comment'] = details[i]

    # Hide hidden threads.
    items = user.redis.hidden_threads.filter_comments(items, comment_key=lambda item: item['comment'])

    # Prune items that shouldn't show up in this feed.
    items = filter(partial(visible_in_feed, earliest_timestamp_cutoff=earliest_timestamp_cutoff), items)
    items = filter(partial(not_self_authored, username=user.username), items)

    # Determine whether each item is dismissable by user.
    def item_is_dismissable(item):
        if item['type'] == 'promotion' and item['username'].lower() == 'canvas':
            return False
        return item['type'] != 'sticky_thread' and is_dismissable(item['comment'], user)

    for item in items:
        item['is_dismissable'] = item_is_dismissable(item)

    # Add viewer_sticker to items.
    for item in items:
        _add_viewer_sticker_to_item(item, user)

    return items