示例#1
0
    def _process_post_mentions(self):
        if not self.text:
            self.user_mentions.all().delete()
        else:
            usernames = extract_usernames_from_string(string=self.text)
            if not usernames:
                self.user_mentions.all().delete()
            else:
                existing_mention_usernames = []
                for existing_mention in self.user_mentions.only('id', 'user__username').all().iterator():
                    if existing_mention.user.username not in usernames:
                        existing_mention.delete()
                    else:
                        existing_mention_usernames.append(existing_mention.user.username)

                PostUserMention = get_post_user_mention_model()
                User = get_user_model()

                for username in usernames:
                    username = username.lower()
                    if username not in existing_mention_usernames:
                        try:
                            user = User.objects.only('id', 'username').get(username=username)
                            user_is_post_creator = user.pk == self.creator_id
                            if user.can_see_post(post=self) and not user_is_post_creator:
                                PostUserMention.create_post_user_mention(user=user, post=self)
                                existing_mention_usernames.append(username)
                        except User.DoesNotExist:
                            pass
示例#2
0
    def _process_post_comment_mentions(self):
        usernames = extract_usernames_from_string(string=self.text)

        if not usernames:
            self.user_mentions.all().delete()
        else:
            existing_mention_usernames = []
            for existing_mention in self.user_mentions.only(
                    'id', 'user__username').all().iterator():
                if existing_mention.user.username not in usernames:
                    existing_mention.delete()
                else:
                    existing_mention_usernames.append(
                        existing_mention.user.username)

            PostCommentUserMention = get_post_comment_user_mention_model()
            User = get_user_model()

            for username in usernames:
                username = username.lower()
                if username not in existing_mention_usernames:
                    try:
                        user = User.objects.only(
                            'id', 'username').get(username=username)
                        user_can_see_post_comment = user.can_see_post_comment(
                            post_comment=self)
                        user_is_commenter = user.pk == self.commenter_id

                        if not user_can_see_post_comment or user_is_commenter:
                            continue

                        if self.parent_comment:
                            user_is_parent_comment_creator = self.parent_comment.commenter_id == user.pk
                            user_has_replied_before = self.parent_comment.replies.filter(
                                commenter_id=user.pk).exists()

                            if user_has_replied_before or user_is_parent_comment_creator:
                                # Its a reply to a comment, if the user previously replied to the comment
                                # or if he's the creator of the parent comment he will already be alerted of the reply,
                                # no need for mention
                                continue
                        else:
                            user_has_commented_before = self.post.comments.filter(
                                commenter_id=user.pk).exists()
                            if user_has_commented_before:
                                # Its a comment to a post, if the user previously commented on the post
                                # he will already be alerted of the comment, no need for mention
                                continue

                        PostCommentUserMention.create_post_comment_user_mention(
                            user=user, post_comment=self)
                        existing_mention_usernames.append(username)
                    except User.DoesNotExist:
                        pass