def feed_for_user(user, offset='top', direction='next', items_to_skip=set(), viewer=None): from drawquest.apps.quest_comments.models import add_viewer_has_starred_field if direction != 'next': raise ValueError("Feed only supports 'next' direction, i.e. scrolling in one direction.") earliest_timestamp_cutoff = None if offset == 'top' else offset items = _feed_items(user, earliest_timestamp_cutoff=earliest_timestamp_cutoff, items_to_skip=items_to_skip) # Pagination. items = items[:knobs.COMMENTS_PER_PAGE] try: next_offset = items[-1]['ts'] next_offset = '{:.18}'.format(next_offset) except IndexError: next_offset = None pagination = FakePaginator(items, offset=offset, next_offset=next_offset) details = QuestCommentGalleryDetails.from_ids([item['comment_id'] for item in items]) add_viewer_has_starred_field(details, viewer=viewer) details_by_id = dict((cmt.id, cmt) for cmt in details) items = [item for item in items if int(item['comment_id']) in details_by_id] for item in items: item['comment'] = details_by_id[int(item['comment_id'])] # 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) return items, pagination
def gallery_comments(quest, offset='top', direction='next', force_comment=None, viewer=None, include_reactions=True): """ Returns comments, pagination. Each comment is itself a dict. """ if force_comment is not None: newer_comments = QuestComment.objects.filter(parent_comment=quest, id__gt=force_comment.id).order_by('id').values_list('id', flat=True) try: offset = list(newer_comments[:knobs.COMMENTS_PER_PAGE / 2])[-1] except IndexError: offset = force_comment.id pagination = Paginator(_exclude_flagged(_all_gallery_comments(quest), viewer), knobs.COMMENTS_PER_PAGE, offset=offset, direction=direction) comments = pagination.items promoter = None if include_reactions else QuestCommentGalleryDetails comments = CachedCall.queryset_details(comments, promoter=promoter) add_viewer_has_starred_field(comments, viewer=viewer) if force_comment is not None and force_comment.id not in [cmt['id'] for cmt in comments]: if force_comment.visibility != Visibility.CURATED: raise Http404() comments.append(force_comment.details()) comments = sorted(comments, key=lambda cmt: -cmt['id']) if viewer is not None and viewer.is_authenticated(): following = viewer.following_ids() for comment in comments: comment.user.viewer_is_following = comment.user.id in following return comments, pagination
def explore_comment_details(viewer=None): comments = CachedCall.multicall([ QuestComment.details_by_id(id_, promoter=QuestCommentExploreDetails) for id_ in preloaded_explore_comment_ids() ]) add_viewer_has_starred_field(comments, viewer=viewer) return comments
def unstar_comment(request, comment_id): comment = get_object_or_404(QuestComment, id=comment_id) models.unstar(request.user, comment) Metrics.unstar.record(request, comment=comment.id) comment_details = comment.details() add_viewer_has_starred_field([comment_details], viewer=request.user) return {'comment': comment_details}
def playback_drawing(request, comment_id): comment = get_object_or_404(QuestComment, id=comment_id) if request.user.is_authenticated(): Playback.append(comment=comment, viewer=request.user) comment_details = comment.details() add_viewer_has_starred_field([comment_details], viewer=request.user) return {'comment': comment_details}
def star_comment(request, comment_id): comment = get_object_or_404(QuestComment, id=comment_id) check_star_rate_limit(request, comment) models.star(request.user, comment, ip=request.META['REMOTE_ADDR'], request=request) Metrics.star.record(request, comment=comment.id) comment_details = comment.details() add_viewer_has_starred_field([comment_details], viewer=request.user) return {'comment': comment_details}
def quest_comment(request, comment_id): comment = QuestCommentDetails.from_id(comment_id) quest = QuestDetails.from_id(comment.quest_id) if request.user.is_authenticated(): comment.user.viewer_is_following = comment.user.id in request.user.following_ids() add_viewer_has_starred_field([comment], viewer=request.user) return { 'comment': comment, 'quest': quest, }
def gallery_comments(quest, offset='top', direction='next', force_comment=None, viewer=None, include_reactions=True): """ Returns comments, pagination. Each comment is itself a dict. """ if force_comment is not None: newer_comments = QuestComment.objects.filter( parent_comment=quest, id__gt=force_comment.id).order_by('id').values_list('id', flat=True) try: offset = list(newer_comments[:knobs.COMMENTS_PER_PAGE / 2])[-1] except IndexError: offset = force_comment.id pagination = Paginator(_exclude_flagged(_all_gallery_comments(quest), viewer), knobs.COMMENTS_PER_PAGE, offset=offset, direction=direction) comments = pagination.items promoter = None if include_reactions else QuestCommentGalleryDetails comments = CachedCall.queryset_details(comments, promoter=promoter) add_viewer_has_starred_field(comments, viewer=viewer) if force_comment is not None and force_comment.id not in [ cmt['id'] for cmt in comments ]: if force_comment.visibility != Visibility.CURATED: raise Http404() comments.append(force_comment.details()) comments = sorted(comments, key=lambda cmt: -cmt['id']) if viewer is not None and viewer.is_authenticated(): following = viewer.following_ids() for comment in comments: comment.user.viewer_is_following = comment.user.id in following return comments, pagination
def top_gallery_comments(quest, viewer=None, include_reactions=False): comment_ids = top_gallery_comment_ids(quest) comments = QuestComment.objects.filter(id__in=comment_ids).order_by('-star_count') comments = _exclude_flagged(comments, viewer) promoter = None if include_reactions else QuestCommentGalleryDetails comments = CachedCall.queryset_details(comments, promoter=promoter) add_viewer_has_starred_field(comments, viewer=viewer) if viewer is not None and viewer.is_authenticated(): following = viewer.following_ids() for comment in comments: comment.user.viewer_is_following = comment.user.id in following return comments
def top_gallery_comments(quest, viewer=None, include_reactions=False): comment_ids = top_gallery_comment_ids(quest) comments = QuestComment.objects.filter( id__in=comment_ids).order_by('-star_count') comments = _exclude_flagged(comments, viewer) promoter = None if include_reactions else QuestCommentGalleryDetails comments = CachedCall.queryset_details(comments, promoter=promoter) add_viewer_has_starred_field(comments, viewer=viewer) if viewer is not None and viewer.is_authenticated(): following = viewer.following_ids() for comment in comments: comment.user.viewer_is_following = comment.user.id in following return comments
def explore_comment_details(viewer=None): comments = CachedCall.multicall([QuestComment.details_by_id(id_, promoter=QuestCommentExploreDetails) for id_ in preloaded_explore_comment_ids()]) add_viewer_has_starred_field(comments, viewer=viewer) return comments