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:
parent
9eb172023a
commit
aff8a3709c
@ -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]
|
||||||
|
|
||||||
|
@ -219,42 +219,52 @@ def notify_user(obj, event):
|
|||||||
# conversation and enabled user_notification.
|
# conversation and enabled user_notification.
|
||||||
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)
|
||||||
# Send email
|
message = translate(Message(
|
||||||
if PLONE_4:
|
MAIL_NOTIFICATION_MESSAGE,
|
||||||
try:
|
mapping={'title': content_object.title,
|
||||||
mail_host.send(message,
|
'link': content_object.absolute_url()}),
|
||||||
comment.author_email,
|
context=obj.REQUEST)
|
||||||
sender,
|
for email in emails:
|
||||||
subject,
|
# Send email
|
||||||
charset='utf-8')
|
if PLONE_4:
|
||||||
except SMTPException:
|
try:
|
||||||
logger.error('SMTP exception while trying to send an ' +
|
mail_host.send(message,
|
||||||
'email from %s to %s',
|
email,
|
||||||
sender,
|
sender,
|
||||||
comment.author_email)
|
subject,
|
||||||
else: # pragma: no cover
|
charset='utf-8')
|
||||||
try:
|
except SMTPException:
|
||||||
mail_host.secureSend(message,
|
logger.error('SMTP exception while trying to send an ' +
|
||||||
comment.author_email,
|
'email from %s to %s',
|
||||||
sender,
|
sender,
|
||||||
subject=subject,
|
email)
|
||||||
charset='utf-8')
|
else: # pragma: no cover
|
||||||
except SMTPException:
|
try:
|
||||||
logger.error('SMTP exception while trying to send an ' +
|
mail_host.secureSend(message,
|
||||||
'email from %s to %s',
|
email,
|
||||||
sender,
|
sender,
|
||||||
comment.author_email)
|
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):
|
def notify_moderator(obj, event):
|
||||||
"""Tell the moderator when a comment needs attention.
|
"""Tell the moderator when a comment needs attention.
|
||||||
|
@ -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):
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user