boyenec
boyenec

Reputation: 1637

Django signals how to notify comment user on new reply?

Right now my I am triggering two type of signals.

type1: If someone comment on author post then it will notify to author.

type2 if someone add reply on any parent comment and parent user id didn't match with parent comment id then it will notify to parent user.

Now I am thinking to triggering signals for child object. Let you explain. User A comment "hello world" where hello world is an parent comment. Now user B add reply on user A comment then user C come and add reply on user B comment. Now I want to triggers signals for user B and notify to user B that an reply added from user C.

models.py

class BlogComment(models.Model):
      blog = models.ForeignKey(Blog,on_delete=models.CASCADE,null=True, blank=True,related_name="blogcomment_blog")
      parent = models.ForeignKey('self', on_delete=models.CASCADE,
                            null=True, blank=True, related_name='children')
      user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='user_comment',blank=True,null=True)
     #my others model fields....

@receiver(post_save,sender=BlogComment)
def NotiFicationSignals(instance,created,**kwargs):
    if created:
        if instance.user.id != instance.blog.author.id: #send notification to author 
               noti_to_author = Notifications.objects.create(duplicate_value="author",blog=instance.blog,blogcomment=instance,sender=instance.user,receiver=instance.blog.author,text_preview=f"Recived new comment from {instance.user}")
                        
        #Notify to parent commenter. This will prevent to create notification if parent owner add reply on his own parent commnet.  
        if instance.parent and instance.parent.user.id != instance.user.id:  
            noti_to_commenter = Notifications.objects.create(duplicate_value="commenter",blog=instance.blog,blogcomment=instance,sender=instance.user,receiver=instance.parent.user,text_preview=f"{instance.user.first_name} replied on your comment") 
              
 

type3 signals will notify to child user(user B)

Upvotes: 0

Views: 267

Answers (2)

Muneeb Shahid
Muneeb Shahid

Reputation: 333

Somehow, you need to keep track of parent comment (initial comment that can be replied). I can suggest one way based on your given model schema:

You can add parent_comment field in BlogComment model. This field can be empty for parent comment but you need to set this field for child or replied comments. This field will point to parent BlogComment model and you can extract user information from this field.

In this scenario your code will look like this:

class BlogComment(models.Model):
  blog = models.ForeignKey(Blog,on_delete=models.CASCADE,blank=True,null=True)
  user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,blank=True,null=True) 
  parent_comment = models.ForeignKey('self', on_delete=models.CASCADE,blank=True,null=True) 

@receiver(post_save,sender=BlogComment)
def NotiFicationSignals(instance,created,**kwargs):
    if created:
             if instance.user.id != instance.blog.author.id: #notify author 
                 noti_to_author = Notifications.objects.create(duplicate_value="author",sender=instance.user,receiver=instance.blog.author,text_preview=f"Recived new comment from {instance.user}")
             if instance.parent_comment: # notify to parent commenter 
                 noti_to_parent_commenter = Notifications.objects.create(duplicate_value="parent commenter",sender=instance.user,receiver=instance.parent_comment.user,text_preview=f"Recived new comment from {instance.user}")
      

Similar database structure is proposed in this thread Posts, comments, replies, and likes database schema

Upvotes: 1

Chymdy
Chymdy

Reputation: 660

add an optional reply_to foreign_key field to your blog comment like:

reply_to= models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="replies_to_me", blank=True,null=True) 

then do this in your signals

noti_to_commenter = Notifications.objects.create(duplicate_value="author",sender=instance.user,receiver=instance.reply_to,text_preview=f"Recived new comment from {instance.user}")

Upvotes: 1

Related Questions