0

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)

boyenec
  • 1,405
  • 5
  • 29

2 Answers2

1

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}")
Chymdy
  • 662
  • 4
  • 14
  • Chymdy will not work. Because Sender and Receiver will be the same person. – boyenec Jan 26 '22 at 00:39
  • Assume commerterXYZ posted on commenterABC post then I want commerterXYZ will be sender and commenterABC will be receiver. does it make sense? – boyenec Jan 26 '22 at 00:42
  • when it is a normal comment, reply_to can be empty. in that case, the signal is triggered but nobody receives it. however when it is a reply to a comment, it should a have a reply_to field. – Chymdy Jan 26 '22 at 01:01
  • 1
    so A makes post, B comments and C replies B. B gets notified of C's reply – Chymdy Jan 26 '22 at 01:03
  • Yes exactly. How to notify C? – boyenec Jan 26 '22 at 07:37
  • I'm sorry, Why do you want to notify C of his own reply? – Chymdy Jan 26 '22 at 14:10
  • you misunderstood me. Assume A post then B commented on post of A then C add reply on B comment. I want to notify B that he got reply from C. – boyenec Jan 26 '22 at 15:15
  • Exactly what I'm saying too. That's what my answer intends to do. – Chymdy Jan 26 '22 at 22:13
1

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

Muneeb Shahid
  • 223
  • 4
  • 13
  • right now your solution only notify to parent user but what will be for child user. See my updated question. – boyenec Jan 26 '22 at 10:33
  • I think, same approach should work in that scenario as well. In this case parent of user B comment will be user A comment and parent of user C comment will be user B comment. You just need to track parent comment and set it when you are saving BlogComment model. – Muneeb Shahid Jan 26 '22 at 15:03