Пример #1
0
def comment_is_pinned(comment_details, viewer):
    """
    `viewer` should be request.user - the user viewing the comment.

    Returns "pinned" if pinned, else None.
    """
    if not isinstance(comment_details, CommentDetails):
        comment_details = CommentDetails(comment_details)
    return 'pinned' if comment_details.is_pinned(viewer) else None
Пример #2
0
    def __init__(self, comment_details):
        """ `comment_details` can be either the parent comment, or any comment within the thread. """
        from canvas.details_models import CommentDetails

        if getattr(comment_details, 'parent_id', None):
            op = CommentDetails.from_id(comment_details.parent_id)
        else:
            op = comment_details
            if not isinstance(op, CommentDetails):
                op = CommentDetails(comment_details)
        self.op = op
Пример #3
0
 def handle(self, *args, **options):
     for user in User.objects.all():
         post_ids = user.redis.feed_source[:]
         for post_id in post_ids:
             comment_details = CommentDetails.from_id(post_id)
             if not visible_in_feed({'comment': comment_details, 'type': 'post'}):
                 user.redis.feed_source.remove(post_id)
Пример #4
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}
Пример #5
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}
Пример #6
0
def sticky_threads(user):
    from apps.sticky_threads.models import get_sticky_threads_from_cache

    hidden_comments = map(int, user.redis.hidden_comments.smembers())
    items = [{'type': 'sticky_thread', 'comment': CommentDetails.from_id(id_), 'comment_id': id_, 'text': text}
             for id_, text in get_sticky_threads_from_cache()
             if id_ not in hidden_comments]

    for item in items:
        _add_viewer_sticker_to_item(item, user)

    return items
 def _from_sticker(cls, comment_sticker):
     from canvas.details_models import CommentDetails
     comment_details = CommentDetails.from_id(comment_sticker.comment_id)
     data = {
         'comment_sticker_type_id': comment_sticker.type_id,
         'details_url': comment_details.linked_url,
     }
     if comment_details.reply_content:
         try:
             data['thumbnail_url'] = comment_details.reply_content.get_absolute_url_for_image_type('small_square')
         except KeyError:
             pass
     return data
Пример #8
0
 def _from_sticker(cls, comment_sticker):
     from canvas.details_models import CommentDetails
     comment_details = CommentDetails.from_id(comment_sticker.comment_id)
     data = {
         'comment_sticker_type_id': comment_sticker.type_id,
         'details_url': comment_details.linked_url,
     }
     if comment_details.reply_content:
         try:
             data[
                 'thumbnail_url'] = comment_details.reply_content.get_absolute_url_for_image_type(
                     'small_square')
         except KeyError:
             pass
     return data
Пример #9
0
 def invites(self):
     return CommentDetails.from_ids(self.smembers())
Пример #10
0
 def invites(self):
     return CommentDetails.from_ids(self.smembers())
Пример #11
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