danirus / django-comments-xtd

A pluggable Django comments application with thread support, follow-up notifications, mail confirmation, like/dislike flags, moderation, a ReactJS plugin and Bootstrap 5.3.
https://django-comments-xtd.readthedocs.io
BSD 2-Clause "Simplified" License
595 stars 158 forks source link

Follow-up email notifications to object authors #340

Closed jdbit closed 2 years ago

jdbit commented 2 years ago

Hi, I added comments to an object (post) created by a user. Is there a way to send follow-up notifications about new comments to the user who created this post? Probably I can do this through signals, but just curious if it has been already implemented in django-comments-xtd.

danirus commented 2 years ago

Hi @jdbit, I am afraid not, it's not implemented.

I would do it connecting to comment_was_posted, and I would verify that the email address of the user that posted the object is not among the email addresses of the users that already have sent comments, to avoid sending more than one email to the same email address.

It would be a nice feature addition.

jdbit commented 2 years ago

I'll do it through signals then. Thank you!

jdbit commented 2 years ago

Hi @danirus, Could you please give me a hint on how to check if the email of the user that created the object is not among emails that already got an email notification? Is it as simple as a query request below or I miss something? followup = XtdComment.objects.filter(object_pk=comment.content_object.pk, user=comment.content_object.createdby, followup=True)

My signals look like this:

@receiver(comment_was_posted)
def comment_was_posted_handler(sender, comment, **kwargs):
    if comment.parent_id == 0:
        message = render_to_string('django_comments_xtd/email_followup_comment.html', {'comment': comment})
        send_mail(
            subject="Someone replied to your post",
            message=message,
            from_email=settings.SERVER_EMAIL,
            recipient_list=[comment.content_object.createdby.email],
        )

Also, could you please clarify, the followup checkbox means that the user gets notifications only to replies to their comments or notifications about all the comments posted under the post, even replies to other users? Let's say an article has 300 comments and all the users enabled followup checkbox, does it mean that when someone creates a new comment under the post, they will wait while the server sends all the 300 emails which might take a few seconds I guess? Or is there an email queue handled by Django so it sends all the emails in the background?

danirus commented 2 years ago

Hi @jdbit, yes you missed something about the first question. Comments may be sent by registered users but they eventually could send them when they are not logged in. Imagine you send a comment from someone else's computer, and you don't want to login there. In such a case you would be requested to confirm the comment by email, and your email would be recorded in the user_email field of the comment. It sounds less likely to happen but it could. So in summary you need to check whether the user=comment.content_object.createdby or user_email=comment.content_object.createdby.email.

About the 2nd question, a commenter that ticked the follow-up box would receive notifications on every new comment sent to the same object. No matter whether the comment is in the same comment's thread_id or not.

About the 3rd question, you can avoid blocking Django when sending mails in several ways:

  1. The first is changing the setting COMMENTS_XTD_THREADED_EMAILS to True. By doing so every email sent by django-comments-xtd will be handled in a separate thread.
  2. Threads are a better solution than blocking, but still if your site will send 300 emails at once and that may happen often, I would try to avoid creating 300 threads at once. I would rather look for an alternative to the default Django email backend. Something like django-celery-email would take over and send the actual emails without blocking.
  3. Still your app has to make the 300 send_email calls. To improve that so the 300 calls happen in a deferred way we would need to make improvements in the app. The simplest thing would be to define a new customizable setting to allow the user define a replacement for the django_comments_xtd.views.notify_comment_followers, so that the function runs non-blocking. The easiest thing would be to point to a user function that schedules a task to do that very same thing but within a celery worker.
jdbit commented 2 years ago

Thank you very much! Good point regarding the first question, I'll keep it in mind. So far I disabled the ability to comment for non-authorized users so it should not be an issue. Regarding the 3rd question, I'll take a look at the second and third approaches (maybe I can rewrite that function for my needs).