Browse Source

Notify thread subscribers with alert #94

Ralfp 12 years ago
parent
commit
dcca89e0c0

+ 15 - 1
misago/apps/threadtype/posting/base.py

@@ -43,21 +43,35 @@ class PostingBaseView(ViewBase):
     def after_form(self, form):
         pass
 
+    def email_watchers(self, notified_users):
+        pass
+
     def notify_users(self):
         try:
             post_content = self.md
         except AttributeError:
             post_content = False
 
+        notified_users = []
+
         if post_content:
             try:
-                if self.quote and self.quote.user_id:
+                if (self.quote and self.quote.user_id and
+                        self.quote.user.username_slug in post_content.mentions):
                     del post_content.mentions[self.quote.user.username_slug]
+                    if not self.quote.user in self.post.mentions.all():
+                        notified_users.append(self.quote.user)
+                        alert = self.quote.user.alert(ugettext_lazy("%(username)s has replied to your post in thread %(thread)s").message)
+                        alert.profile('username', self.request.user)
+                        alert.post('thread', self.type_prefix, self.thread, self.post)
+                        alert.save_all()
             except KeyError:
                 pass
             if post_content.mentions:
+                notified_users += [x for x in post_content.mentions.itervalues()]
                 self.post.notify_mentioned(self.request, self.type_prefix, post_content.mentions)
                 self.post.save(force_update=True)
+        self.email_watchers(notified_users)
 
     def watch_thread(self):
         if self.request.user.subscribe_start:

+ 16 - 9
misago/apps/threadtype/posting/newreply.py

@@ -132,14 +132,21 @@ class NewReplyBaseView(PostingBaseView):
             checkpoint_post.save(force_update=True)
             self.thread.save(force_update=True)
 
-        # Notify user we quoted?
-        if (self.quote and self.quote.user_id and not merged
+        # Mute quoted user?
+        if not (self.quote and self.quote.user_id and not merged
                 and self.quote.user_id != self.request.user.pk
                 and not self.quote.user.is_ignoring(self.request.user)):
-            alert = self.quote.user.alert(ugettext_lazy("%(username)s has replied to your post in thread %(thread)s").message)
-            alert.profile('username', self.request.user)
-            alert.post('thread', self.type_prefix, self.thread, self.post)
-            alert.save_all()
-
-        # E-mail users about new response
-        self.thread.email_watchers(self.request, self.type_prefix, self.post)
+            self.quote = None
+
+    # E-mail users about new response
+    def email_watchers(self, notified_users):
+        emailed = self.thread.email_watchers(self.request, self.type_prefix, self.post)
+        for user in emailed:
+            if not user in notified_users:
+                if user.pk == self.thread.start_poster_id:
+                    alert = user.alert(ugettext_lazy("%(username)s has replied to your thread %(thread)s").message)
+                else:
+                    alert = user.alert(ugettext_lazy("%(username)s has replied to thread %(thread)s that you are watching").message)
+                alert.profile('username', self.request.user)
+                alert.post('thread', self.type_prefix, self.thread, self.post)
+                alert.save_all()

+ 3 - 0
misago/models/threadmodel.py

@@ -152,6 +152,7 @@ class Thread(models.Model):
         from misago.acl.exceptions import ACLError403, ACLError404
         from misago.models import ThreadRead, WatchedThread
 
+        emailed = []
         for watch in WatchedThread.objects.filter(thread=self).filter(email=True).filter(last_read__gte=self.previous_last.date):
             user = watch.user
             if user.pk != request.user.pk:
@@ -167,8 +168,10 @@ class Thread(models.Model):
                                         _('New reply in thread "%(thread)s"') % {'thread': self.name},
                                         {'author': request.user, 'post': post, 'thread': self}
                                         )
+                        emailed.append(user)
                 except (ACLError403, ACLError404):
                     pass
+        return emailed
 
 
 def rename_user_handler(sender, **kwargs):