Exemplo n.º 1
0
def nested_comment_pre_save(sender, instance, **kwargs):
    if instance.pk:
        try:
            previous_mentions = MentionsService.get_mentions(
                sender.objects.get(pk=instance.pk).text)
            current_mentions = MentionsService.get_mentions(instance.text)
            mentions = [
                item for item in current_mentions
                if item not in previous_mentions
            ]
        except sender.DoesNotExist:
            mentions = []

        cache.set("user.%d.comment_pre_save_mentions" % instance.author.pk,
                  mentions, 2)
    else:
        insufficient_index = instance.author.userprofile.get_scores()['user_scores_index'] is not None and \
                             instance.author.userprofile.get_scores()['user_scores_index'] < 1.00
        free_account = is_free(instance.author)
        insufficient_previous_approvals = NestedComment.objects.filter(
            Q(author=instance.author)
            & ~Q(pending_moderation=True)).count() < 3

        is_content_owner = False
        ct = ContentType.objects.get_for_id(instance.content_type_id)
        if ct.model == 'image':
            is_content_owner = instance.author == instance.content_object.user

        if insufficient_index and \
                free_account and \
                insufficient_previous_approvals and \
                not is_content_owner and \
                not ModerationService.auto_approve(instance.author):
            instance.pending_moderation = True
Exemplo n.º 2
0
def nested_comment_pre_save(sender, instance, **kwargs):
    if instance.pk:
        try:
            previous_mentions = MentionsService.get_mentions(
                sender.objects.get(pk=instance.pk).text)
            current_mentions = MentionsService.get_mentions(instance.text)
            mentions = [
                item for item in current_mentions
                if item not in previous_mentions
            ]
        except sender.DoesNotExist:
            mentions = []

        cache.set("user.%d.comment_pre_save_mentions" % instance.author.pk,
                  mentions, 2)
    else:
        insufficient_index = instance.author.userprofile.get_scores(
        )['user_scores_index'] < 1.00
        free_account = is_free(instance.author)
        insufficient_previous_approvals = NestedComment.objects.filter(
            Q(author=instance.author)
            & ~Q(pending_moderation=True)).count() < 3

        if insufficient_index and free_account and insufficient_previous_approvals:
            instance.pending_moderation = True
Exemplo n.º 3
0
def forum_post_pre_save(sender, instance, **kwargs):
    if instance.pk:
        try:
            previous_mentions = MentionsService.get_mentions(
                sender.objects.get(pk=instance.pk).body)
            current_mentions = MentionsService.get_mentions(instance.body)
            mentions = [
                item for item in current_mentions
                if item not in previous_mentions
            ]
        except sender.DoesNotExist:
            mentions = []

        cache.set("user.%d.forum_post_pre_save_mentions" % instance.user.pk,
                  mentions, 2)

    for attribute in ['body', 'body_text', 'body_html']:
        for language in settings.LANGUAGES:
            with override(language[0]):
                for message in [
                        '*** Type your message here ***',
                        '*** Type your forum post here ***',
                        '*** Type your reply here ***',
                ]:
                    translated = gettext(message).decode('utf-8')
                    content = getattr(instance, attribute)
                    setattr(instance, attribute,
                            content.replace(translated, ''))

        content = getattr(instance, attribute)
        re.sub(r'\n+', '\n', content).strip()
        setattr(instance, attribute, content)
Exemplo n.º 4
0
def image_pre_save(sender, instance, **kwargs):
    if instance.uploader_in_progress:
        return

    if not instance.pk and not instance.is_wip:
        instance.published = datetime.datetime.now()

    try:
        image = sender.objects_including_wip.get(pk=instance.pk)
    except sender.DoesNotExist:
        # Image is being created.

        last_image: Image = Image.objects_including_wip.filter(
            user=instance.user).order_by('-pk').first()
        if last_image:
            instance.watermark = last_image.watermark
            instance.watermark_text = last_image.watermark_text
            instance.watermark_position = last_image.watermark_position
            instance.watermark_size = last_image.watermark_size
            instance.watermark_opacity = last_image.watermark_opacity

        user_scores_index = instance.user.userprofile.get_scores(
        )['user_scores_index'] or 0
        if user_scores_index >= 1.00 or \
                is_any_premium_subscription(instance.user) or \
                ModerationService.auto_approve(instance.user):
            instance.moderated_when = datetime.date.today()
            instance.moderator_decision = 1
    else:
        if image.moderator_decision != 1 and instance.moderator_decision == 1:
            # This image is being approved
            if not instance.is_wip:
                add_story(instance.user,
                          verb='VERB_UPLOADED_IMAGE',
                          action_object=instance)

        if not instance.is_wip and not instance.published:
            # This image is being published
            instance.published = datetime.datetime.now()

        previous_mentions = MentionsService.get_mentions(
            image.description_bbcode)
        current_mentions = MentionsService.get_mentions(
            instance.description_bbcode)
        mentions = [
            item for item in current_mentions if item not in previous_mentions
        ]
        cache.set("image.%d.image_pre_save_mentions" % instance.pk, mentions,
                  2)

        if (instance.watermark_text != image.watermark_text
                or instance.watermark != image.watermark
                or instance.watermark_position != image.watermark_position
                or instance.watermark_size != image.watermark_size
                or instance.watermark_opacity != image.watermark_opacity):
            ImageService(image).invalidate_all_thumbnails()
Exemplo n.º 5
0
    def test_get_mentions_nested_quotes_only_mentions_outer(self):
        text = "[quote=\"foo\"][quote=\"bar\"]2[/quote]1[/quote]"
        mentions = MentionsService.get_mentions(text)
        self.assertTrue("foo" in mentions)
        self.assertFalse("bar" in mentions)

        text = "[quote=\"foo\"][quote=\"bar\"][quote=\"tar\"]3[/quote]2[/quote]1[/quote]"
        mentions = MentionsService.get_mentions(text)
        self.assertTrue("foo" in mentions)
        self.assertFalse("bar" in mentions)
        self.assertFalse("tar" in mentions)
Exemplo n.º 6
0
def forum_post_pre_save(sender, instance, **kwargs):
    if instance.pk:
        try:
            previous_mentions = MentionsService.get_mentions(
                sender.objects.get(pk=instance.pk).body)
            current_mentions = MentionsService.get_mentions(instance.body)
            mentions = [
                item for item in current_mentions
                if item not in previous_mentions
            ]
        except sender.DoesNotExist:
            mentions = []

        cache.set("user.%d.forum_post_pre_save_mentions" % instance.user.pk,
                  mentions, 2)
Exemplo n.º 7
0
def nested_comment_pre_save(sender, instance, **kwargs):
    if instance.pk:
        try:
            previous_mentions = MentionsService.get_mentions(
                sender.objects.get(pk=instance.pk).text)
            current_mentions = MentionsService.get_mentions(instance.text)
            mentions = [
                item for item in current_mentions
                if item not in previous_mentions
            ]
        except sender.DoesNotExist:
            mentions = []

        cache.set("user.%d.comment_pre_save_mentions" % instance.author.pk,
                  mentions, 2)
Exemplo n.º 8
0
def forum_post_post_save(sender, instance, created, **kwargs):
    if created:
        mentions = MentionsService.get_mentions(instance.body)
        if hasattr(instance.topic.forum, 'group'):
            instance.topic.forum.group.save()  # trigger date_updated update
    else:
        mentions = cache.get(
            "user.%d.forum_post_pre_save_mentions" % instance.user.pk, [])

    for username in mentions:
        try:
            user = User.objects.get(username=username)
            push_notification(
                [user], 'new_forum_post_mention', {
                    'url':
                    settings.BASE_URL + instance.get_absolute_url(),
                    'user':
                    instance.user.userprofile.get_display_name(),
                    'user_url':
                    settings.BASE_URL + reverse_url(
                        'user_page', kwargs={'username': instance.user}),
                    'post':
                    instance.topic.name,
                })
        except User.DoesNotExist:
            pass
Exemplo n.º 9
0
def nested_comment_post_save(sender, instance, created, **kwargs):
    if created:
        mentions = MentionsService.get_mentions(instance.text)

        CommentNotificationsService(instance).send_notifications()

        if hasattr(instance.content_object, "updated"):
            # This will trigger the auto_now fields in the content_object
            # We do it only if created, because the content_object needs to
            # only be updated if the number of comments changes.
            instance.content_object.save(keep_deleted=True)
    else:
        mentions = cache.get(
            "user.%d.comment_pre_save_mentions" % instance.author.pk, [])

    for username in mentions:
        try:
            user = User.objects.get(username=username)
            push_notification(
                [user], 'new_comment_mention', {
                    'url':
                    settings.BASE_URL + instance.get_absolute_url(),
                    'user':
                    instance.author.userprofile.get_display_name(),
                    'user_url':
                    settings.BASE_URL + reverse_url(
                        'user_page', kwargs={'username': instance.author}),
                })
        except User.DoesNotExist:
            pass
Exemplo n.º 10
0
def nested_comment_pre_save(sender, instance, **kwargs):
    if instance.pk:
        try:
            previous_mentions = MentionsService.get_mentions(
                sender.objects.get(pk=instance.pk).text)
            current_mentions = MentionsService.get_mentions(instance.text)
            mentions = [
                item for item in current_mentions
                if item not in previous_mentions
            ]
        except sender.DoesNotExist:
            mentions = []

        cache.set("user.%d.comment_pre_save_mentions" % instance.author.pk,
                  mentions, 2)
    else:
        user_scores_index = instance.author.userprofile.get_scores(
        )['user_scores_index']
        if user_scores_index < 1.00 and is_free(instance.author):
            instance.pending_moderation = True
Exemplo n.º 11
0
def image_pre_save(sender, instance, **kwargs):
    if instance.uploader_in_progress:
        return

    if not instance.pk and not instance.is_wip:
        instance.published = datetime.datetime.now()

    try:
        image = sender.objects_including_wip.get(pk=instance.pk)
    except sender.DoesNotExist:
        user_scores_index = instance.user.userprofile.get_scores(
        )['user_scores_index'] or 0
        if user_scores_index >= 1.00 or \
                is_any_premium_subscription(instance.user) or \
                ModerationService.auto_approve(instance.user):
            instance.moderated_when = datetime.date.today()
            instance.moderator_decision = 1
    else:
        if image.moderator_decision != 1 and instance.moderator_decision == 1:
            # This image is being approved
            if not instance.is_wip:
                add_story(instance.user,
                          verb='VERB_UPLOADED_IMAGE',
                          action_object=instance)

        if not instance.is_wip and not instance.published:
            instance.published = datetime.datetime.now()

        previous_mentions = MentionsService.get_mentions(
            image.description_bbcode)
        current_mentions = MentionsService.get_mentions(
            instance.description_bbcode)
        mentions = [
            item for item in current_mentions if item not in previous_mentions
        ]
        cache.set("image.%d.image_pre_save_mentions" % instance.pk, mentions,
                  2)
Exemplo n.º 12
0
def nested_comment_post_save(sender, instance, created, **kwargs):
    if created:
        mentions = MentionsService.get_mentions(instance.text)

        if hasattr(instance.content_object, "updated"):
            # This will trigger the auto_now fields in the content_object
            # We do it only if created, because the content_object needs to
            # only be updated if the number of comments changes.
            save_kwargs = {}
            if issubclass(type(instance.content_object), SafeDeleteModel):
                save_kwargs['keep_deleted'] = True
            instance.content_object.save(**save_kwargs)

        if instance.pending_moderation:
            CommentNotificationsService(
                instance).send_moderation_required_notification()
        else:
            CommentNotificationsService(instance).send_notifications()
    else:
        mentions = cache.get(
            "user.%d.comment_pre_save_mentions" % instance.author.pk, [])

    if not instance.pending_moderation:
        for username in mentions:
            user = get_object_or_None(User, username=username)
            if not user:
                try:
                    profile = get_object_or_None(UserProfile,
                                                 real_name=username)
                    if profile:
                        user = profile.user
                except MultipleObjectsReturned:
                    user = None
            if user:
                push_notification(
                    [user], instance.author, 'new_comment_mention', {
                        'url':
                        build_notification_url(
                            settings.BASE_URL + instance.get_absolute_url(),
                            instance.author),
                        'user':
                        instance.author.userprofile.get_display_name(),
                        'user_url':
                        settings.BASE_URL + reverse_url(
                            'user_page', kwargs={'username': instance.author}),
                    })
Exemplo n.º 13
0
 def test_get_mentions_two_mentions_multiline(self):
     text = "Hello [url=https://www.astrobin.com/users/foo/]@Foo Smith[/url]\nHello [url=https://www.astrobin.com/users/bar/]@Bar Test[/url]"
     self.assertEquals(["foo", "bar"], MentionsService.get_mentions(text))
Exemplo n.º 14
0
 def test_get_mentions_two_unique_quotes(self):
     text = "[quote=\"foo\"]Test message[/quote] OK [quote=\"bar\"]Test message[/quote]"
     mentions = MentionsService.get_mentions(text)
     self.assertTrue("foo" in mentions)
     self.assertTrue("bar" in mentions)
Exemplo n.º 15
0
 def test_get_mentions_two_quotes(self):
     text = "[quote=\"foo\"]Test message[/quote] OK [quote=\"foo\"]Test message[/quote]"
     self.assertEqual(["foo"], MentionsService.get_mentions(text))
Exemplo n.º 16
0
 def test_get_mentions_unique_mentions(self):
     text = "Hello [url=https://www.astrobin.com/users/foo/]@Foo Smith[/url] and " \
            "[url=https://www.astrobin.com/users/foo/]@Foo Smith[/url]"
     self.assertEqual(["foo"], MentionsService.get_mentions(text))
Exemplo n.º 17
0
 def test_get_mentions_two_mentions_multiline(self):
     text = "Hello [url=https://www.astrobin.com/users/foo/]@Foo Smith[/url]\nHello " \
            "[url=https://www.astrobin.com/users/bar/]@Bar Test[/url]"
     mentions = MentionsService.get_mentions(text)
     self.assertTrue("foo" in mentions)
     self.assertTrue("bar" in mentions)
Exemplo n.º 18
0
 def test_get_mentions_localhost_and_port(self):
     text = "Hello [url=http://localhost:8084/users/foo/]@Foo Bar[/url]"
     self.assertEqual(["foo"], MentionsService.get_mentions(text))
Exemplo n.º 19
0
 def test_get_mentions_no_mentions(self):
     self.assertEqual([], MentionsService.get_mentions("hello"))
Exemplo n.º 20
0
def nested_comment_post_save(sender, instance, created, **kwargs):
    if created:
        model_class = instance.content_type.model_class()
        obj = instance.content_type.get_object_for_this_type(
            id=instance.object_id)
        url = settings.BASE_URL + instance.get_absolute_url()
        mentions = MentionsService.get_mentions(instance.text)

        if model_class == Image:
            image = instance.content_type.get_object_for_this_type(
                id=instance.object_id)
            if image.is_wip:
                return

            if UserService(obj.user).shadow_bans(instance.author):
                log.info(
                    "Skipping notification for comment because %d shadow-bans %d"
                    % (obj.user.pk, instance.author.pk))
                return

            if instance.author != obj.user:
                push_notification(
                    [obj.user], 'new_comment', {
                        'url':
                        url,
                        'user':
                        instance.author.userprofile.get_display_name(),
                        'user_url':
                        settings.BASE_URL + reverse_url(
                            'user_page',
                            kwargs={'username': instance.author.username}),
                    })

            if instance.parent and instance.parent.author != instance.author:
                push_notification(
                    [instance.parent.author], 'new_comment_reply', {
                        'url':
                        url,
                        'user':
                        instance.author.userprofile.get_display_name(),
                        'user_url':
                        settings.BASE_URL + reverse_url(
                            'user_page',
                            kwargs={'username': instance.author.username}),
                    })

            add_story(instance.author,
                      verb='VERB_COMMENTED_IMAGE',
                      action_object=instance,
                      target=obj)

        elif model_class == Gear:
            if not instance.parent:
                gear, gear_type = get_correct_gear(obj.id)
                user_attr_lookup = {
                    'Telescope': 'telescopes',
                    'Camera': 'cameras',
                    'Mount': 'mounts',
                    'FocalReducer': 'focal_reducers',
                    'Software': 'software',
                    'Filter': 'filters',
                    'Accessory': 'accessories',
                }

                recipients = [
                    x.user for x in UserProfile.objects.filter(
                        **{user_attr_lookup[gear_type]: gear})
                ]
                notification = 'new_gear_discussion'
            else:
                notification = 'new_comment_reply'
                recipients = [instance.parent.author]

            push_notification(
                recipients, notification, {
                    'url':
                    url,
                    'user':
                    instance.author.userprofile.get_display_name(),
                    'user_url':
                    settings.BASE_URL +
                    reverse_url('user_page',
                                kwargs={'username': instance.author.username}),
                })

            add_story(instance.author,
                      verb='VERB_COMMENTED_GEAR',
                      action_object=instance,
                      target=gear)

        if hasattr(instance.content_object, "updated"):
            # This will trigger the auto_now fields in the content_object
            # We do it only if created, because the content_object needs to
            # only be updated if the number of comments changes.
            instance.content_object.save(keep_deleted=True)
    else:
        mentions = cache.get(
            "user.%d.comment_pre_save_mentions" % instance.author.pk, [])

    for username in mentions:
        try:
            user = User.objects.get(username=username)
            push_notification(
                [user], 'new_comment_mention', {
                    'url':
                    settings.BASE_URL + instance.get_absolute_url(),
                    'user':
                    instance.author.userprofile.get_display_name(),
                    'user_url':
                    settings.BASE_URL + reverse_url(
                        'user_page', kwargs={'username': instance.author}),
                })
        except User.DoesNotExist:
            pass
Exemplo n.º 21
0
def image_post_save(sender, instance, created, **kwargs):
    # type: (object, Image, bool, object) -> None

    if created:
        instance.user.userprofile.premium_counter += 1
        instance.user.userprofile.save(keep_deleted=True)

        if not instance.is_wip:
            if not instance.skip_notifications:
                push_notification_for_new_image.apply_async(args=(
                    instance.user.pk,
                    instance.pk,
                ))
            if instance.moderator_decision == 1:
                add_story(instance.user,
                          verb='VERB_UPLOADED_IMAGE',
                          action_object=instance)

        if Image.all_objects.filter(user=instance.user).count() == 1:
            push_notification(
                [instance.user], None, 'congratulations_for_your_first_image',
                {
                    'BASE_URL': settings.BASE_URL,
                    'PREMIUM_MAX_IMAGES_FREE':
                    settings.PREMIUM_MAX_IMAGES_FREE,
                    'url': reverse_url('image_detail',
                                       args=(instance.get_id(), ))
                })

        mentions = MentionsService.get_mentions(instance.description_bbcode)
    else:
        mentions = cache.get("image.%d.image_pre_save_mentions" % instance.pk,
                             [])

    for username in mentions:
        user = get_object_or_None(User, username=username)
        if not user:
            try:
                profile = get_object_or_None(UserProfile, real_name=username)
                if profile:
                    user = profile.user
            except MultipleObjectsReturned:
                user = None
        if user and user != instance.user:
            thumb = instance.thumbnail_raw('gallery', None, sync=True)
            push_notification(
                [user], instance.user, 'new_image_description_mention', {
                    'image':
                    instance,
                    'image_thumbnail':
                    thumb.url if thumb else None,
                    'url':
                    build_notification_url(
                        settings.BASE_URL + instance.get_absolute_url(),
                        instance.user),
                    'user':
                    instance.user.userprofile.get_display_name(),
                    'user_url':
                    settings.BASE_URL + reverse_url(
                        'user_page', kwargs={'username': instance.user}),
                })

    if not instance.uploader_in_progress:
        groups = instance.user.joined_group_set.filter(autosubmission=True)
        for group in groups:
            if instance.is_wip:
                group.images.remove(instance)
            else:
                group.images.add(instance)

        if instance.user.userprofile.updated < datetime.datetime.now(
        ) - datetime.timedelta(minutes=5):
            instance.user.save()
            try:
                instance.user.userprofile.save(keep_deleted=True)
            except UserProfile.DoesNotExist:
                pass

        UserService(instance.user).clear_gallery_image_list_cache()

        if instance.user.userprofile.auto_submit_to_iotd_tp_process:
            IotdService.submit_to_iotd_tp_process(instance.user, instance,
                                                  False)
Exemplo n.º 22
0
 def test_get_mentions_empty_string(self):
     self.assertEqual([], MentionsService.get_mentions(""))
Exemplo n.º 23
0
    def send_notifications(self, force=False):
        if self.comment.pending_moderation and not force:
            return

        instance = self.comment

        model_class = instance.content_type.model_class()
        obj = instance.content_type.get_object_for_this_type(id=instance.object_id)
        object_owner = None
        notification = None
        mentions = MentionsService.get_mentions(instance.text)
        url = None

        if model_class == Image:
            object_owner = obj.user
            notification = 'new_comment'
            url = settings.BASE_URL + instance.get_absolute_url()
        elif hasattr(model_class, 'edit_proposal_by'):
            object_owner = obj.edit_proposal_by
            notification = 'new_comment_to_edit_proposal'
            url = instance.get_absolute_url()
        elif model_class == Iotd:
            object_owner = obj.judge
            notification = 'new_comment_to_scheduled_iotd'
            url = AppRedirectionService.redirect(f'/iotd/judgement-queue#comments-{obj.pk}-{instance.pk}')

        if UserService(object_owner).shadow_bans(instance.author):
            log.info("Skipping notification for comment because %d shadow-bans %d" % (
                object_owner.pk, instance.author.pk))
            return

        exclude = MentionsService.get_mentioned_users_with_notification_enabled(mentions, 'new_comment_mention')

        if instance.parent and \
                instance.parent.author != instance.author and \
                not instance.pending_moderation:
            recipients = [x for x in [instance.parent.author] if x not in exclude]
            if recipients:
                push_notification(
                    recipients, instance.author, 'new_comment_reply',
                    {
                        'url': build_notification_url(url, instance.author),
                        'user': instance.author.userprofile.get_display_name(),
                        'user_url': settings.BASE_URL + reverse(
                            'user_page', kwargs={'username': instance.author.username}
                        ),
                    }
                )

        if model_class == Image:
            if (force or not instance.pending_moderation) and not obj.is_wip:
                add_story(instance.author,
                          verb='VERB_COMMENTED_IMAGE',
                          action_object=instance,
                          target=obj)

        if object_owner and notification:
            if instance.author != object_owner and \
                    (instance.parent is None or instance.parent.author != object_owner) and \
                    not instance.pending_moderation:
                recipients = [x for x in [object_owner] if x not in exclude]
                if recipients:
                    push_notification(
                        recipients, instance.author, notification,
                        {
                            'url': build_notification_url(url, instance.author),
                            'user': instance.author.userprofile.get_display_name(),
                            'user_url': settings.BASE_URL + reverse(
                                'user_page', kwargs={'username': instance.author.username}
                            ),
                        }
                    )
Exemplo n.º 24
0
 def test_get_mentions_quote_without_user(self):
     text = "[quote]Test message[/url]"
     self.assertEqual([], MentionsService.get_mentions(text))
Exemplo n.º 25
0
 def test_get_mentions_no_www(self):
     text = "Hello [url=https://astrobin.com/users/foo/]@Foo Bar[/url]"
     self.assertEqual(["foo"], MentionsService.get_mentions(text))
Exemplo n.º 26
0
 def test_get_mentions_quote_and_mention_same_user(self):
     text = "[quote=\"foo\"]Test message[/quote] Hello [url=https://www.astrobin.com/users/foo/]@Foo Bar[/url]"
     self.assertEqual(["foo"], MentionsService.get_mentions(text))
Exemplo n.º 27
0
def forum_post_post_save(sender, instance, created, **kwargs):
    def notify_subscribers(mentions):
        # type: (List[str]) -> None
        recipients = list(
            instance.topic.subscribers.exclude(pk__in=list(
                set([instance.user.pk] + [
                    x.pk for x in MentionsService.
                    get_mentioned_users_with_notification_enabled(
                        mentions, 'new_forum_post_mention')
                ]))))

        if recipients:
            push_notification(
                recipients, instance.user, 'new_forum_reply', {
                    'user':
                    instance.user.userprofile.get_display_name(),
                    'user_url':
                    settings.BASE_URL + reverse_url(
                        'user_page', kwargs={'username': instance.user}),
                    'post_url':
                    build_notification_url(
                        settings.BASE_URL + instance.get_absolute_url(),
                        instance.user),
                    'topic_url':
                    build_notification_url(
                        settings.BASE_URL + instance.topic.get_absolute_url(),
                        instance.user),
                    'topic_name':
                    instance.topic.name,
                    'unsubscribe_url':
                    build_notification_url(
                        settings.BASE_URL +
                        reverse_url('pybb:delete_subscription',
                                    args=[instance.topic.id]), instance.user)
                })

    def notify_mentioned(mentions):
        # type: (List[str]) -> None
        for username in mentions:
            user = get_object_or_None(User, username=username)
            if user is None:
                try:
                    profile = get_object_or_None(UserProfile,
                                                 real_name=username)
                    if profile:
                        user = profile.user
                except MultipleObjectsReturned:
                    user = None
            if user:
                push_notification(
                    [user], instance.user, 'new_forum_post_mention', {
                        'url':
                        build_notification_url(
                            settings.BASE_URL + instance.get_absolute_url(),
                            instance.user),
                        'user':
                        instance.user.userprofile.get_display_name(),
                        'user_url':
                        settings.BASE_URL + reverse_url(
                            'user_page', kwargs={'username': instance.user}),
                        'post':
                        instance.topic.name,
                    })

    if created:
        if hasattr(instance.topic.forum, 'group'):
            instance.topic.forum.group.save()  # trigger date_updated update

        if get_pybb_profile(instance.user).autosubscribe and \
                perms.may_subscribe_topic(instance.user, instance.topic):
            instance.topic.subscribers.add(instance.user)

        mentions = MentionsService.get_mentions(instance.body)
        if not instance.on_moderation:
            notify_subscribers(mentions)
            notify_mentioned(mentions)
        else:
            NotificationsService.email_superusers(
                'New forum post needs moderation',
                '%s%s' % (settings.BASE_URL, instance.get_absolute_url()))

    else:
        mentions = cache.get(
            "post.%d.forum_post_pre_save_mentions" % instance.pk, [])
        cache.delete("post.%d.forum_post_pre_save_mentions" % instance.pk)
        if cache.get("post.%d.forum_post_pre_save_approved" % instance.pk):
            push_notification(
                [instance.user], None, 'forum_post_approved', {
                    'url':
                    build_notification_url(settings.BASE_URL +
                                           instance.get_absolute_url())
                })
            notify_subscribers(mentions)
            notify_mentioned(mentions)
            cache.delete("post.%d.forum_post_pre_save_approved" % instance.pk)

    cache_key = make_template_fragment_key(
        'home_page_latest_from_forums',
        (instance.user.pk, instance.user.userprofile.language))
    cache.delete(cache_key)
Exemplo n.º 28
0
 def test_get_mentions_quote_and_mention_unique_users(self):
     text = "[quote=\"bar\"]Test message[/quote] Hello [url=https://www.astrobin.com/users/foo/]@Foo Bar[/url]"
     mentions = MentionsService.get_mentions(text)
     self.assertTrue("foo" in mentions)
     self.assertTrue("bar" in mentions)
Exemplo n.º 29
0
def forum_post_post_save(sender, instance, created, **kwargs):
    def notify_subscribers():
        push_notification(
            list(instance.topic.subscribers.exclude(pk=instance.user.pk)),
            instance.user, 'new_forum_reply', {
                'user':
                instance.user.userprofile.get_display_name(),
                'user_url':
                settings.BASE_URL +
                reverse_url('user_page', kwargs={'username': instance.user}),
                'post_url':
                build_notification_url(
                    settings.BASE_URL + instance.get_absolute_url(),
                    instance.user),
                'topic_url':
                build_notification_url(
                    settings.BASE_URL + instance.topic.get_absolute_url(),
                    instance.user),
                'topic_name':
                instance.topic.name,
                'unsubscribe_url':
                build_notification_url(
                    settings.BASE_URL + reverse_url('pybb:delete_subscription',
                                                    args=[instance.topic.id]),
                    instance.user)
            })

    if created:
        mentions = MentionsService.get_mentions(instance.body)

        if hasattr(instance.topic.forum, 'group'):
            instance.topic.forum.group.save()  # trigger date_updated update

        if get_pybb_profile(instance.user).autosubscribe and \
                perms.may_subscribe_topic(instance.user, instance.topic):
            instance.topic.subscribers.add(instance.user)

        if not instance.on_moderation:
            notify_subscribers()
        else:
            NotificationsService.email_superusers(
                'New forum post needs moderation',
                '%s%s' % (settings.BASE_URL, instance.get_absolute_url()))

    else:
        mentions = cache.get(
            "user.%d.forum_post_pre_save_mentions" % instance.user.pk, [])

        if cache.get("user.%d.forum_post_pre_save_approved" %
                     instance.user.pk):
            notify_subscribers()

    for username in mentions:
        try:
            user = User.objects.get(username=username)
            push_notification(
                [user], instance.user, 'new_forum_post_mention', {
                    'url':
                    build_notification_url(
                        settings.BASE_URL + instance.get_absolute_url(),
                        instance.user),
                    'user':
                    instance.user.userprofile.get_display_name(),
                    'user_url':
                    settings.BASE_URL + reverse_url(
                        'user_page', kwargs={'username': instance.user}),
                    'post':
                    instance.topic.name,
                })
        except User.DoesNotExist:
            pass

    cache_key = make_template_fragment_key(
        'home_page_latest_from_forums',
        (instance.user.pk, instance.user.userprofile.language))
    cache.delete(cache_key)