Example #1
0
 def test_title_in_metadata(self):
     user = self.user
     title = "here's a title for this comment"
     cmt = create_comment(title=title)
     ctx = self._ctx(user, cmt)
     cmt = TemplateComment(cmt.details(), request_context=ctx)
     self.assertTrue(strip_template_chars(title) in cmt.shareable_metadata()['title'])
Example #2
0
 def _metadata(self, **cmt_kwargs):
     group = create_group()
     cmt = TemplateComment(
         self.post_comment(category=group.name, **cmt_kwargs).details())
     return (
         cmt,
         cmt.shareable_metadata(),
     )
Example #3
0
 def test_title_in_metadata(self):
     user = self.user
     title = "here's a title for this comment"
     cmt = create_comment(title=title)
     ctx = self._ctx(user, cmt)
     cmt = TemplateComment(cmt.details(), request_context=ctx)
     self.assertTrue(
         strip_template_chars(title) in cmt.shareable_metadata()['title'])
Example #4
0
    def thread_context(self):
        top_reply_ids = self.top_reply_ids(
            force_show=features.thread_new(self.request))

        ctx = {
            'short_id':
            self.short_id,
            'page':
            self.page,
            'gotoreply':
            self.gotoreply,
            'viewer_is_staff':
            self.request.user.is_authenticated()
            and self.request.user.is_staff,
            'viewer_is_thread_author':
            self.is_author,
            'root':
            '/p/',
            'op_content':
            self.op_content,
            'op_category':
            self.op_category,
            'page':
            self.page,
            'per_page':
            self.per_page,
            'num_replies':
            self.num_replies,
            'reply_ids':
            self.reply_ids,
            'pages':
            self.pages,
            'page_reply_ids':
            self.page_reply_ids,
            'page_current':
            self.page_current,
            'page_next':
            self.page_next,
            'page_last':
            self.page_last,
            'page_penultimate':
            self.page_penultimate,
            'explicit_page_view':
            self.explicit_page_view,

            # Recent replies.
            'recent_reply_ids':
            self.recent_reply_ids,
            'top_reply_ids':
            top_reply_ids,
        }

        # Get all the replies in one query, then create the appropriate lists.
        _all_replies = Comment.visible.in_bulk(self.page_reply_ids +
                                               self.recent_reply_ids +
                                               top_reply_ids)
        _recent_replies = [_all_replies[cid] for cid in self.recent_reply_ids]
        _top_replies = filter(bool,
                              [_all_replies.get(cid) for cid in top_reply_ids])
        _replies = [_all_replies[cid] for cid in self.page_reply_ids]

        replyable = [self._op_comment
                     ] + _replies + _recent_replies + _top_replies

        # Get all comment ids (ids so 0 queries) that any of these comments are replies to, that aren't in this
        # page, so we can render them on hover.
        replied_ids = ([
            reply.replied_comment_id for reply in replyable
            if (reply.replied_comment_id
                and reply.replied_comment_id not in [r.id for r in replyable])
        ])

        ctx.update({
            'replied_ids': replied_ids,
            'replyable': replyable,
        })

        recent_replies = [reply.details for reply in _recent_replies]
        replies = [Comment.details_by_id(reply.id) for reply in _replies]
        replied = [Comment.details_by_id(cid) for cid in replied_ids]
        top_replies = [reply.details for reply in _top_replies]

        if self.request.user.is_authenticated():
            ctx['user_infos'] = {
                'pinned': self.request.user.id in self._op_comment.pins()
            }
            if self.request.user.is_staff:
                ctx['admin_infos'] = {
                    self._op_comment.id: self._op_comment.admin_info
                }
                # For replies we only use the username, so grab those all in one query and put them in admin_infos.
                ctx['usernames'] = Comment.visible.filter(
                    id__in=_all_replies.keys()).values('id',
                                                       'author__username')
                for reply_dict in ctx['usernames']:
                    ctx['admin_infos'][reply_dict['id']] = {
                        'username': reply_dict['author__username']
                    }

        ctx['replies_channel'] = self._op_comment.replies_channel.sync()

        # Get relevant sidebar data
        remix_of, reply_to = [], []
        # Remix of
        if self._op_content and self._op_content.remix_of:
            op_remix_of_caption = self._op_content.remix_of.first_caption
            if op_remix_of_caption:
                remix_of = [op_remix_of_caption.details]
            ctx['op_remix_of_caption'] = op_remix_of_caption
        # Reply to
        if self._op_comment.parent_comment and self._op_comment.parent_comment.is_visible(
        ):
            reply_to = [self._op_comment.parent_comment.details]

        (
            (op_comment, ),
            (linked_comment, ),
            remix_of,
            reply_to,
            replies,
            recent_replies,
            top_replies,
            replied,
        ) = CachedCall.many_multicall(
            [self.op_comment],
            [self.linked_comment],
            remix_of,
            reply_to,
            replies,
            recent_replies,
            top_replies,
            replied,
        )

        op_comment.is_my_post = bool(
            self._op_comment.author == self.request.user)
        op_comment.moderated = op_comment.visibility not in Visibility.public_choices

        linked_comment = TemplateComment(
            linked_comment,
            is_op=(linked_comment.id == op_comment.id),
            request=self.request,
            title=op_comment.title)

        if self.page_current == 1 and op_comment.has_content():
            first_comment_with_content = op_comment
        else:
            first_comment_with_content = None
            for reply in replies:
                if reply.has_content():
                    first_comment_with_content = reply
                    break

        last_comment_with_content = None
        for reply in reversed(replies):
            if reply.has_content():
                last_comment_with_content = reply
                break

        comment_to_expand = first_comment_with_content
        if self.gotoreply:
            comment_to_expand = linked_comment

        ctx.update({
            'op_comment':
            op_comment,
            'linked_comment':
            linked_comment,
            'remix_of':
            remix_of,
            'reply_to':
            reply_to,
            'replies':
            replies,
            'recent_replies':
            recent_replies,
            'top_replies':
            top_replies,
            'top_remixes':
            [reply for reply in top_replies if reply.has_content()],
            'replied':
            replied,
            'linked_comment':
            linked_comment,
            'fb_metadata':
            linked_comment.shareable_metadata(),
            'large_thread_view':
            len(replies) >= 50,
            'title':
            getattr(op_comment, 'title', None),
            'first_comment_with_content':
            first_comment_with_content,
            'last_comment_with_content':
            last_comment_with_content,
            'comment_to_expand':
            comment_to_expand,
        })

        return ctx
Example #5
0
 def _metadata(self, **cmt_kwargs):
     group = create_group()
     cmt = TemplateComment(self.post_comment(category=group.name, **cmt_kwargs).details())
     return (cmt, cmt.shareable_metadata(),)
Example #6
0
    def thread_context(self):
        top_reply_ids = self.top_reply_ids(force_show=features.thread_new(self.request))

        ctx = {
            'short_id': self.short_id,
            'page': self.page,
            'gotoreply': self.gotoreply,

            'viewer_is_staff': self.request.user.is_authenticated() and self.request.user.is_staff,
            'viewer_is_thread_author': self.is_author,
            'root': '/p/',

            'op_content': self.op_content,
            'op_category': self.op_category,
            'page': self.page,
            'per_page': self.per_page,
            'num_replies': self.num_replies,
            'reply_ids': self.reply_ids,
            'pages': self.pages,
            'page_reply_ids': self.page_reply_ids,
            'page_current': self.page_current,
            'page_next': self.page_next,
            'page_last': self.page_last,
            'page_penultimate': self.page_penultimate,
            'explicit_page_view': self.explicit_page_view,

            # Recent replies.
            'recent_reply_ids': self.recent_reply_ids,

            'top_reply_ids': top_reply_ids,
        }

        # Get all the replies in one query, then create the appropriate lists.
        _all_replies = Comment.visible.in_bulk(self.page_reply_ids + self.recent_reply_ids + top_reply_ids)
        _recent_replies = [_all_replies[cid] for cid in self.recent_reply_ids]
        _top_replies = filter(bool, [_all_replies.get(cid) for cid in top_reply_ids])
        _replies = [_all_replies[cid] for cid in self.page_reply_ids]

        replyable = [self._op_comment] + _replies + _recent_replies + _top_replies

        # Get all comment ids (ids so 0 queries) that any of these comments are replies to, that aren't in this
        # page, so we can render them on hover.
        replied_ids = ([reply.replied_comment_id for reply in replyable
                        if (reply.replied_comment_id
                            and reply.replied_comment_id not in [r.id for r in replyable])])

        ctx.update({
            'replied_ids': replied_ids,
            'replyable': replyable,
        })

        recent_replies = [reply.details for reply in _recent_replies]
        replies = [Comment.details_by_id(reply.id) for reply in _replies]
        replied = [Comment.details_by_id(cid) for cid in replied_ids]
        top_replies = [reply.details for reply in _top_replies]

        if self.request.user.is_authenticated():
            ctx['user_infos'] = {'pinned': self.request.user.id in self._op_comment.pins()}
            if self.request.user.is_staff:
                ctx['admin_infos'] = {self._op_comment.id: self._op_comment.admin_info}
                # For replies we only use the username, so grab those all in one query and put them in admin_infos.
                ctx['usernames'] = Comment.visible.filter(id__in=_all_replies.keys()).values('id', 'author__username')
                for reply_dict in ctx['usernames']:
                    ctx['admin_infos'][reply_dict['id']] = {'username': reply_dict['author__username']}

        ctx['replies_channel'] = self._op_comment.replies_channel.sync()

        # Get relevant sidebar data
        remix_of, reply_to = [], []
        # Remix of
        if self._op_content and self._op_content.remix_of:
            op_remix_of_caption = self._op_content.remix_of.first_caption
            if op_remix_of_caption:
                remix_of = [op_remix_of_caption.details]
            ctx['op_remix_of_caption'] = op_remix_of_caption
        # Reply to
        if self._op_comment.parent_comment and self._op_comment.parent_comment.is_visible():
            reply_to = [self._op_comment.parent_comment.details]

        (
            (op_comment,),
            (linked_comment,),
            remix_of,
            reply_to,
            replies,
            recent_replies,
            top_replies,
            replied,
        ) = CachedCall.many_multicall(
            [self.op_comment],
            [self.linked_comment],
            remix_of,
            reply_to,
            replies,
            recent_replies,
            top_replies,
            replied,
        )

        op_comment.is_my_post = bool(self._op_comment.author == self.request.user)
        op_comment.moderated = op_comment.visibility not in Visibility.public_choices

        linked_comment = TemplateComment(linked_comment, is_op=(linked_comment.id == op_comment.id),
                                        request=self.request, title=op_comment.title)

        if self.page_current == 1 and op_comment.has_content():
            first_comment_with_content = op_comment
        else:
            first_comment_with_content = None
            for reply in replies:
                if reply.has_content():
                    first_comment_with_content = reply
                    break

        last_comment_with_content = None
        for reply in reversed(replies):
            if reply.has_content():
                last_comment_with_content = reply
                break

        comment_to_expand = first_comment_with_content
        if self.gotoreply:
            comment_to_expand = linked_comment

        ctx.update({
            'op_comment': op_comment,
            'linked_comment': linked_comment,
            'remix_of': remix_of,
            'reply_to': reply_to,
            'replies': replies,
            'recent_replies': recent_replies,
            'top_replies': top_replies,
            'top_remixes': [reply for reply in top_replies if reply.has_content()],
            'replied': replied,

            'linked_comment': linked_comment,
            'fb_metadata': linked_comment.shareable_metadata(),
            'large_thread_view': len(replies) >= 50,
            'title': getattr(op_comment, 'title', None),

            'first_comment_with_content': first_comment_with_content,
            'last_comment_with_content': last_comment_with_content,
            'comment_to_expand': comment_to_expand,
        })

        return ctx