From aff8a3709c380871bf61ec6640aa12bb61be30d1 Mon Sep 17 00:00:00 2001 From: Maurits van Rees Date: Wed, 15 Dec 2010 23:41:57 +0000 Subject: [PATCH] Avoid sending multiple notification emails to the same person when he has commented multiple times. svn path=/plone.app.discussion/trunk/; revision=46365 --- CHANGES.txt | 4 + plone/app/discussion/comment.py | 80 +++++++++++-------- .../discussion/tests/test_notifications.py | 34 ++++++++ 3 files changed, 83 insertions(+), 35 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 9768b9d..1dd08b7 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -4,6 +4,10 @@ Changelog 1.0RC1 (unreleased) ------------------- +- Avoid sending multiple notification emails to the same person when + he has commented multiple times. + [maurits] + - Disable discussion by default. [timo] diff --git a/plone/app/discussion/comment.py b/plone/app/discussion/comment.py index ba398ba..1a73ca4 100644 --- a/plone/app/discussion/comment.py +++ b/plone/app/discussion/comment.py @@ -219,42 +219,52 @@ def notify_user(obj, event): # conversation and enabled user_notification. conversation = aq_parent(obj) content_object = aq_parent(conversation) - + + # Avoid sending multiple notification emails to the same person + # when he has commented multiple times. + emails = set() for comment in conversation.getComments(): - if obj != comment and \ - comment.user_notification and comment.author_email: - subject = translate(_(u"A comment has been posted."), - context=obj.REQUEST) - message = translate(Message(MAIL_NOTIFICATION_MESSAGE, - mapping={'title': content_object.title, - 'link': content_object.absolute_url()}), - context=obj.REQUEST) - - # Send email - if PLONE_4: - try: - mail_host.send(message, - comment.author_email, - sender, - subject, - charset='utf-8') - except SMTPException: - logger.error('SMTP exception while trying to send an ' + - 'email from %s to %s', - sender, - comment.author_email) - else: # pragma: no cover - try: - mail_host.secureSend(message, - comment.author_email, - sender, - subject=subject, - charset='utf-8') - except SMTPException: - logger.error('SMTP exception while trying to send an ' + - 'email from %s to %s', - sender, - comment.author_email) + if (obj != comment and + comment.user_notification and comment.author_email): + emails.add(comment.author_email) + + if not emails: + return + + subject = translate(_(u"A comment has been posted."), + context=obj.REQUEST) + message = translate(Message( + MAIL_NOTIFICATION_MESSAGE, + mapping={'title': content_object.title, + 'link': content_object.absolute_url()}), + context=obj.REQUEST) + for email in emails: + # Send email + if PLONE_4: + try: + mail_host.send(message, + email, + sender, + subject, + charset='utf-8') + except SMTPException: + logger.error('SMTP exception while trying to send an ' + + 'email from %s to %s', + sender, + email) + else: # pragma: no cover + try: + mail_host.secureSend(message, + email, + sender, + subject=subject, + charset='utf-8') + except SMTPException: + logger.error('SMTP exception while trying to send an ' + + 'email from %s to %s', + sender, + email) + def notify_moderator(obj, event): """Tell the moderator when a comment needs attention. diff --git a/plone/app/discussion/tests/test_notifications.py b/plone/app/discussion/tests/test_notifications.py index baa00e3..312ad13 100644 --- a/plone/app/discussion/tests/test_notifications.py +++ b/plone/app/discussion/tests/test_notifications.py @@ -129,6 +129,40 @@ class TestUserNotificationUnit(PloneTestCase): self.assertEquals(len(self.mailhost.messages), 0) + def test_notify_only_once(self): + # When a user has added two comments in a conversation and has + # both times requested email notification, do not send him two + # emails when another comment has been added. + + # Comment 1 + comment = createObject('plone.Comment') + comment.text = 'Comment text' + comment.user_notification = True + comment.author_email = "john@plone.test" + self.conversation.addComment(comment) + + # Comment 2 + comment = createObject('plone.Comment') + comment.text = 'Comment text' + comment.user_notification = True + comment.author_email = "john@plone.test" + self.conversation.addComment(comment) + # Note that we might want to get rid of this message, as the + # new comment is added by the same user. + self.assertEquals(len(self.mailhost.messages), 1) + self.mailhost.reset() + self.assertEquals(len(self.mailhost.messages), 0) + + # Comment 3 + comment = createObject('plone.Comment') + comment.text = 'Comment text' + self.conversation.addComment(comment) + self.assertEquals(len(self.mailhost.messages), 1) + self.failUnless(self.mailhost.messages[0]) + msg = str(self.mailhost.messages[0]) + self.failUnless('To: john@plone.test' in msg) + self.failUnless('From: portal@plone.test' in msg) + class TestModeratorNotificationUnit(PloneTestCase):