Avoid sending multiple notification emails to the same person when

he has commented multiple times.

svn path=/plone.app.discussion/trunk/; revision=46365
This commit is contained in:
Maurits van Rees 2010-12-15 23:41:57 +00:00
parent 9eb172023a
commit aff8a3709c
3 changed files with 83 additions and 35 deletions

View File

@ -4,6 +4,10 @@ Changelog
1.0RC1 (unreleased) 1.0RC1 (unreleased)
------------------- -------------------
- Avoid sending multiple notification emails to the same person when
he has commented multiple times.
[maurits]
- Disable discussion by default. - Disable discussion by default.
[timo] [timo]

View File

@ -220,41 +220,51 @@ def notify_user(obj, event):
conversation = aq_parent(obj) conversation = aq_parent(obj)
content_object = aq_parent(conversation) 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(): for comment in conversation.getComments():
if obj != comment and \ if (obj != comment and
comment.user_notification and comment.author_email: comment.user_notification and comment.author_email):
subject = translate(_(u"A comment has been posted."), emails.add(comment.author_email)
context=obj.REQUEST)
message = translate(Message(MAIL_NOTIFICATION_MESSAGE, if not emails:
mapping={'title': content_object.title, return
'link': content_object.absolute_url()}),
context=obj.REQUEST) 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)
# 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)
def notify_moderator(obj, event): def notify_moderator(obj, event):
"""Tell the moderator when a comment needs attention. """Tell the moderator when a comment needs attention.

View File

@ -129,6 +129,40 @@ class TestUserNotificationUnit(PloneTestCase):
self.assertEquals(len(self.mailhost.messages), 0) 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): class TestModeratorNotificationUnit(PloneTestCase):