fix: handle new_comment notification edge case (#33088)

This commit is contained in:
Muhammad Adeel Tajamul
2023-08-31 16:33:20 +05:00
committed by GitHub
parent c1b28c35f8
commit 96f7e9cd9a
3 changed files with 49 additions and 5 deletions

View File

@@ -240,7 +240,7 @@ class TestSendResponseNotifications(ForumsEnableMixin, CommentsServiceMockMixin,
expected_context = {
'replier_name': self.user_3.username,
'post_title': self.thread.title,
'author_name': 'dummy',
'author_name': 'dummy\'s',
'course_name': self.course.display_name,
}
self.assertDictEqual(args_comment.context, expected_context)
@@ -311,6 +311,36 @@ class TestSendResponseNotifications(ForumsEnableMixin, CommentsServiceMockMixin,
self.assertEqual(args_comment.app_name, 'discussion')
class TestSendCommentNotification(ForumsEnableMixin, CommentsServiceMockMixin, ModuleStoreTestCase):
"""
Test case to send new_comment notification
"""
def setUp(self):
super().setUp()
httpretty.reset()
httpretty.enable()
self.course = CourseFactory.create()
self.user_1 = UserFactory.create()
self.user_2 = UserFactory.create()
def test_new_comment_notification(self):
handler = Mock()
USER_NOTIFICATION_REQUESTED.connect(handler)
thread = ThreadMock(thread_id=1, creator=self.user_1, title='test thread')
response = ThreadMock(thread_id=2, creator=self.user_2, title='test response')
self.register_get_comment_response({
'id': response.id,
'thread_id': 'abc',
'user_id': response.user_id
})
send_response_notifications(thread, self.course, self.user_2, parent_id=response.id)
handler.assert_called_once()
context = handler.call_args[1]['notification_data'].context
self.assertEqual(context['author_name'], 'their')
@ddt.ddt
class TestBlackoutDates(ForumsEnableMixin, CommentsServiceMockMixin, ModuleStoreTestCase):
"""

View File

@@ -9,6 +9,7 @@ from django.conf import settings
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
from django.core.paginator import Paginator
from django.db.models.functions import Length
from django.utils.translation import gettext_lazy as _
from common.djangoapps.student.models import CourseEnrollment
from common.djangoapps.student.roles import CourseStaffRole, CourseInstructorRole
@@ -502,19 +503,32 @@ class DiscussionNotificationSender:
"""
return int(self.parent_response.user_id) == int(self.thread.user_id)
def _response_and_comment_has_same_creator(self):
return int(self.parent_response.attributes['user_id']) == self.creator.id
def send_new_comment_notification(self):
"""
Send notification to parent thread creator i.e. comment on the response.
"""
#
if (
self.parent_response and
self.creator.id != int(self.thread.user_id)
):
# use your if author of response is same as author of post.
author_name = "your" if self._response_and_thread_has_same_creator() else self.parent_response.username
# use 'their' if comment author is also response author.
author_name = (
# Translators: Replier commented on "your" response to your post
_("your")
if self._response_and_thread_has_same_creator()
else (
# Translators: Replier commented on "their" response to your post
_("their")
if self._response_and_comment_has_same_creator()
else f"{self.parent_response.username}'s"
)
)
context = {
"author_name": author_name,
"author_name": str(author_name),
}
self._send_notification([self.thread.user_id], "new_comment", extra_context=context)

View File

@@ -25,7 +25,7 @@ COURSE_NOTIFICATION_TYPES = {
'notification_app': 'discussion',
'name': 'new_comment',
'is_core': True,
'content_template': _('<{p}><{strong}>{replier_name}</{strong}> commented on <{strong}>{author_name}\'s'
'content_template': _('<{p}><{strong}>{replier_name}</{strong}> commented on <{strong}>{author_name}'
'</{strong}> response to your post <{strong}>{post_title}</{strong}></{p}>'),
'content_context': {
'post_title': 'Post title',