From 6f556caab4f9c827b229ac8e0dde5bd02e8e0ed3 Mon Sep 17 00:00:00 2001 From: ayeshoali Date: Mon, 22 Jan 2024 14:01:41 +0500 Subject: [PATCH] feat: added notification when response is endorsed or answered --- .../rest_api/discussions_notifications.py | 7 +++++++ lms/djangoapps/discussion/rest_api/tasks.py | 17 +++++++++++++++++ lms/djangoapps/discussion/signals/handlers.py | 15 ++++++++++++++- .../notifications/base_notification.py | 17 +++++++++++++++++ openedx/core/djangoapps/notifications/models.py | 2 +- 5 files changed, 56 insertions(+), 2 deletions(-) diff --git a/lms/djangoapps/discussion/rest_api/discussions_notifications.py b/lms/djangoapps/discussion/rest_api/discussions_notifications.py index 31f158fbd742..229c3e878b12 100644 --- a/lms/djangoapps/discussion/rest_api/discussions_notifications.py +++ b/lms/djangoapps/discussion/rest_api/discussions_notifications.py @@ -234,6 +234,13 @@ def _create_cohort_course_audience(self): } return {} + def send_new_response_endorsed_notification(self): + """ + Sends a notification to the author of the thread + response on his thread has been endorsed + """ + self._send_notification([self.thread.user_id], "response_endorsed") + def send_new_thread_created_notification(self): """ Send notification based on notification_type diff --git a/lms/djangoapps/discussion/rest_api/tasks.py b/lms/djangoapps/discussion/rest_api/tasks.py index 385e2a1f46a0..f180d3c96b4f 100644 --- a/lms/djangoapps/discussion/rest_api/tasks.py +++ b/lms/djangoapps/discussion/rest_api/tasks.py @@ -47,3 +47,20 @@ def send_response_notifications(thread_id, course_key_str, user_id, parent_id=No notification_sender.send_new_response_notification() notification_sender.send_new_comment_on_response_notification() notification_sender.send_response_on_followed_post_notification() + + +@shared_task +@set_code_owner_attribute +def send_response_endorsed_notification(thread_id, course_key_str): + """ + Send notification 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() + user = User.objects.get(id=thread.user_id) + course = get_course_with_access(user, 'load', course_key, check_if_enrolled=True) + notification_sender = DiscussionNotificationSender(thread, course) + notification_sender.send_new_response_endorsed_notification() + \ No newline at end of file diff --git a/lms/djangoapps/discussion/signals/handlers.py b/lms/djangoapps/discussion/signals/handlers.py index 3e142aecd9c0..fd3cec51739e 100644 --- a/lms/djangoapps/discussion/signals/handlers.py +++ b/lms/djangoapps/discussion/signals/handlers.py @@ -12,7 +12,7 @@ from xmodule.modulestore.django import SignalHandler from lms.djangoapps.discussion import tasks -from lms.djangoapps.discussion.rest_api.tasks import send_response_notifications, send_thread_created_notification +from lms.djangoapps.discussion.rest_api.tasks import send_response_notifications, send_thread_created_notification, send_response_endorsed_notification from openedx.core.djangoapps.django_comment_common import signals from openedx.core.djangoapps.site_configuration.models import SiteConfiguration from openedx.core.djangoapps.theming.helpers import get_current_site @@ -157,3 +157,16 @@ def create_comment_created_notification(*args, **kwargs): parent_id = comment.attributes['parent_id'] course_key_str = comment.attributes['course_id'] send_response_notifications.apply_async(args=[thread_id, course_key_str, user.id, parent_id]) + + +@receiver(signals.comment_endorsed) +def create_response_endorsed_notification(*args, **kwargs): + """ + Creates a notification when new response is endorsed + """ + user = kwargs['user'] + comment = kwargs['post'] + thread_id = comment.attributes['thread_id'] + course_key_str = comment.attributes['course_id'] + + send_response_endorsed_notification.apply_async(args=[thread_id, course_key_str]) diff --git a/openedx/core/djangoapps/notifications/base_notification.py b/openedx/core/djangoapps/notifications/base_notification.py index 877b029ebc28..4d093532eb6c 100644 --- a/openedx/core/djangoapps/notifications/base_notification.py +++ b/openedx/core/djangoapps/notifications/base_notification.py @@ -113,6 +113,23 @@ 'email_template': '', 'filters': [FILTER_AUDIT_EXPIRED_USERS_WITH_NO_ROLE] }, + 'endorsed_post': { + 'notification_app': 'discussion', + 'name': 'endorsed_post', + 'is_core': False, + 'info': '', + 'web': True, + 'email': True, + 'push': True, + 'non_editable': [], + 'content_template': _('<{p}><{strong}>{username} response has been endorsed in your post <{strong}>{post_title}'), + 'content_context': { + 'post_title': 'Post title', + 'username': 'Post author name', + }, + 'email_template': '', + 'filters': [FILTER_AUDIT_EXPIRED_USERS_WITH_NO_ROLE] + }, } COURSE_NOTIFICATION_APPS = { diff --git a/openedx/core/djangoapps/notifications/models.py b/openedx/core/djangoapps/notifications/models.py index 55f6c11a7cac..c2c03f202352 100644 --- a/openedx/core/djangoapps/notifications/models.py +++ b/openedx/core/djangoapps/notifications/models.py @@ -21,7 +21,7 @@ NOTIFICATION_CHANNELS = ['web', 'push', 'email'] # Update this version when there is a change to any course specific notification type or app. -COURSE_NOTIFICATION_CONFIG_VERSION = 4 +COURSE_NOTIFICATION_CONFIG_VERSION = 5 def get_course_notification_preference_config():