From 50e850ba304395dd49b4a131659332e770d945a1 Mon Sep 17 00:00:00 2001 From: Ahtisham Shahid Date: Tue, 16 Apr 2024 13:28:03 +0500 Subject: [PATCH] fix: endorsed post notification audiance updated fix: updated endorsed notification logic fix: updated endorsed notification logic --- .../rest_api/discussions_notifications.py | 6 ++--- lms/djangoapps/discussion/rest_api/tasks.py | 22 ++++++++++++------- .../discussion/rest_api/tests/test_tasks.py | 21 +++++++++++------- lms/djangoapps/discussion/signals/handlers.py | 5 ++--- .../notifications/base_notification.py | 4 ++-- 5 files changed, 33 insertions(+), 25 deletions(-) diff --git a/lms/djangoapps/discussion/rest_api/discussions_notifications.py b/lms/djangoapps/discussion/rest_api/discussions_notifications.py index 988ea25c8754..f72271f3a60c 100644 --- a/lms/djangoapps/discussion/rest_api/discussions_notifications.py +++ b/lms/djangoapps/discussion/rest_api/discussions_notifications.py @@ -240,10 +240,8 @@ def send_response_endorsed_on_thread_notification(self): Sends a notification to the author of the thread response on his thread has been endorsed """ - context = { - "username": self.creator.username, - } - self._send_notification([self.thread.user_id], "response_endorsed_on_thread", context) + if self.creator.id != int(self.thread.user_id): + self._send_notification([self.thread.user_id], "response_endorsed_on_thread") def send_response_endorsed_notification(self): """ diff --git a/lms/djangoapps/discussion/rest_api/tasks.py b/lms/djangoapps/discussion/rest_api/tasks.py index bd41e1078cd3..628d93d15bc1 100644 --- a/lms/djangoapps/discussion/rest_api/tasks.py +++ b/lms/djangoapps/discussion/rest_api/tasks.py @@ -6,6 +6,7 @@ from edx_django_utils.monitoring import set_code_owner_attribute from opaque_keys.edx.locator import CourseKey from lms.djangoapps.courseware.courses import get_course_with_access +from openedx.core.djangoapps.django_comment_common.comment_client import Comment from openedx.core.djangoapps.django_comment_common.comment_client.thread import Thread from openedx.core.djangoapps.notifications.config.waffle import ENABLE_NOTIFICATIONS, ENABLE_COURSEWIDE_NOTIFICATIONS from lms.djangoapps.discussion.rest_api.discussions_notifications import DiscussionNotificationSender @@ -51,7 +52,7 @@ def send_response_notifications(thread_id, course_key_str, user_id, parent_id=No @shared_task @set_code_owner_attribute -def send_response_endorsed_notifications(thread_id, course_key_str, comment_author_id): +def send_response_endorsed_notifications(thread_id, response_id, course_key_str, endorsed_by): """ Send notifications when a response is marked answered/ endorsed """ @@ -59,10 +60,15 @@ def send_response_endorsed_notifications(thread_id, course_key_str, comment_auth if not ENABLE_NOTIFICATIONS.is_enabled(course_key): return thread = Thread(id=thread_id).retrieve() - comment_author = User.objects.get(id=comment_author_id) - course = get_course_with_access(comment_author, 'load', course_key, check_if_enrolled=True) - notification_sender = DiscussionNotificationSender(thread, course, comment_author) - #sends notification to author of thread - notification_sender.send_response_endorsed_on_thread_notification() - #sends notification to author of response - notification_sender.send_response_endorsed_notification() + creator = User.objects.get(id=endorsed_by) + course = get_course_with_access(creator, 'load', course_key, check_if_enrolled=True) + response = Comment(id=response_id).retrieve() + notification_sender = DiscussionNotificationSender(thread, course, creator) + # skip sending notification to author of thread if they are the same as the author of the response + if response.user_id != thread.user_id: + # sends notification to author of thread + notification_sender.send_response_endorsed_on_thread_notification() + # sends notification to author of response + if int(response.user_id) != creator.id: + notification_sender.creator = User.objects.get(id=response.user_id) + notification_sender.send_response_endorsed_notification() diff --git a/lms/djangoapps/discussion/rest_api/tests/test_tasks.py b/lms/djangoapps/discussion/rest_api/tests/test_tasks.py index 824bff2962c3..29e63524b2b2 100644 --- a/lms/djangoapps/discussion/rest_api/tests/test_tasks.py +++ b/lms/djangoapps/discussion/rest_api/tests/test_tasks.py @@ -549,7 +549,9 @@ def setUp(self): self.user_1 = UserFactory.create() CourseEnrollment.enroll(self.user_1, self.course.id) self.user_2 = UserFactory.create() + self.user_3 = UserFactory.create() CourseEnrollment.enroll(self.user_2, self.course.id) + CourseEnrollment.enroll(self.user_3, self.course.id) def test_basic(self): """ @@ -565,7 +567,6 @@ def test_response_endorsed_notifications(self): """ Tests response endorsed notifications """ - 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_thread_response({ @@ -578,34 +579,38 @@ def test_response_endorsed_notifications(self): "title": thread.title, }) self.register_get_comment_response({ - 'id': response.id, + 'id': 1, + 'thread_id': thread.id, + 'user_id': response.user_id + }) + self.register_get_comment_response({ + 'id': 2, 'thread_id': thread.id, 'user_id': response.user_id }) handler = mock.Mock() USER_NOTIFICATION_REQUESTED.connect(handler) - send_response_endorsed_notifications(thread.id, str(self.course.id), self.user_2.id) + send_response_endorsed_notifications(thread.id, response.id, str(self.course.id), self.user_3.id) self.assertEqual(handler.call_count, 2) - #Test response endorsed on thread notification + # Test response endorsed on thread notification notification_data = handler.call_args_list[0][1]['notification_data'] # Target only the thread author self.assertEqual([int(user_id) for user_id in notification_data.user_ids], [int(thread.user_id)]) self.assertEqual(notification_data.notification_type, 'response_endorsed_on_thread') expected_context = { - 'replier_name': response.username, + 'replier_name': self.user_3.username, 'post_title': 'test thread', 'course_name': self.course.display_name, - 'sender_id': int(response.user_id), - 'username': response.username, + 'sender_id': int(self.user_3.id), } self.assertDictEqual(notification_data.context, expected_context) self.assertEqual(notification_data.content_url, _get_mfe_url(self.course.id, thread.id)) self.assertEqual(notification_data.app_name, 'discussion') self.assertEqual('response_endorsed_on_thread', notification_data.notification_type) - #Test response endorsed notification + # Test response endorsed notification notification_data = handler.call_args_list[1][1]['notification_data'] # Target only the response author self.assertEqual([int(user_id) for user_id in notification_data.user_ids], [int(response.user_id)]) diff --git a/lms/djangoapps/discussion/signals/handlers.py b/lms/djangoapps/discussion/signals/handlers.py index 423bc5b2bc7e..8c4991cd0289 100644 --- a/lms/djangoapps/discussion/signals/handlers.py +++ b/lms/djangoapps/discussion/signals/handlers.py @@ -191,8 +191,7 @@ def create_response_endorsed_on_thread_notification(*args, **kwargs): and another notification for response author when response is endorsed """ comment = kwargs['post'] - comment_author_id = comment.attributes['user_id'] thread_id = comment.attributes['thread_id'] course_key_str = comment.attributes['course_id'] - - send_response_endorsed_notifications.apply_async(args=[thread_id, course_key_str, comment_author_id]) + endorsed_by = kwargs['user'].id + send_response_endorsed_notifications.apply_async(args=[thread_id, kwargs['post'].id, course_key_str, endorsed_by]) diff --git a/openedx/core/djangoapps/notifications/base_notification.py b/openedx/core/djangoapps/notifications/base_notification.py index 5f498a951d2e..4384a2e20779 100644 --- a/openedx/core/djangoapps/notifications/base_notification.py +++ b/openedx/core/djangoapps/notifications/base_notification.py @@ -145,11 +145,11 @@ 'is_core': True, 'info': '', 'non_editable': [], - 'content_template': _('<{p}><{strong}>{username} response has been endorsed in your post ' + 'content_template': _('<{p}><{strong}>{replier_name} response has been endorsed in your post ' '<{strong}>{post_title}'), 'content_context': { 'post_title': 'Post title', - 'username': 'Response author name', + 'replier_name': 'Endorsed by', }, 'email_template': '', 'filters': [FILTER_AUDIT_EXPIRED_USERS_WITH_NO_ROLE]