Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: endorsed post notification audiance updated #34513

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
22 changes: 14 additions & 8 deletions lms/djangoapps/discussion/rest_api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,18 +52,23 @@ 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
"""
course_key = CourseKey.from_string(course_key_str)
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()
21 changes: 13 additions & 8 deletions lms/djangoapps/discussion/rest_api/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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({
Expand All @@ -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)])
Expand Down
5 changes: 2 additions & 3 deletions lms/djangoapps/discussion/signals/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
4 changes: 2 additions & 2 deletions openedx/core/djangoapps/notifications/base_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@
'is_core': True,
'info': '',
'non_editable': [],
'content_template': _('<{p}><{strong}>{username}</{strong}> response has been endorsed in your post '
'content_template': _('<{p}><{strong}>{replier_name}</{strong}> response has been endorsed in your post '
'<{strong}>{post_title}</{strong}></{p}>'),
'content_context': {
'post_title': 'Post title',
'username': 'Response author name',
'replier_name': 'Endorsed by',
},
'email_template': '',
'filters': [FILTER_AUDIT_EXPIRED_USERS_WITH_NO_ROLE]
Expand Down
Loading