From 3d2f05db1cbd0d9ea10cfab1437d6f34c894038a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=86=D0=B2=D0=B0=D0=BD=20=D0=9D=D1=94=D0=B4=D1=94=D0=BB?= =?UTF-8?q?=D1=8C=D0=BD=D1=96=D1=86=D0=B5=D0=B2?= Date: Fri, 26 Apr 2024 12:45:16 +0300 Subject: [PATCH 1/2] feat: [AXM-271] Add push notification event to discussions --- lms/djangoapps/discussion/tasks.py | 2 ++ .../discussion/edx_ace/responsenotification/push/body.txt | 8 ++++++++ .../edx_ace/responsenotification/push/subject.txt | 3 +++ 3 files changed, 13 insertions(+) create mode 100644 lms/djangoapps/discussion/templates/discussion/edx_ace/responsenotification/push/body.txt create mode 100644 lms/djangoapps/discussion/templates/discussion/edx_ace/responsenotification/push/subject.txt diff --git a/lms/djangoapps/discussion/tasks.py b/lms/djangoapps/discussion/tasks.py index d483a82dbd66..cfa18dfdbb86 100644 --- a/lms/djangoapps/discussion/tasks.py +++ b/lms/djangoapps/discussion/tasks.py @@ -218,6 +218,8 @@ def _build_message_context(context): # lint-amnesty, pylint: disable=missing-fu 'thread_username': thread_author.username, 'comment_username': comment_author.username, 'post_link': post_link, + 'click_action': post_link, + 'send_push_notification': True, 'comment_created_at': date.deserialize(context['comment_created_at']), 'thread_created_at': date.deserialize(context['thread_created_at']) }) diff --git a/lms/djangoapps/discussion/templates/discussion/edx_ace/responsenotification/push/body.txt b/lms/djangoapps/discussion/templates/discussion/edx_ace/responsenotification/push/body.txt new file mode 100644 index 000000000000..87440702954d --- /dev/null +++ b/lms/djangoapps/discussion/templates/discussion/edx_ace/responsenotification/push/body.txt @@ -0,0 +1,8 @@ +{% load i18n %} + +{% block content %} +{% blocktrans trimmed %} + {{ comment_username }} replied to {{ thread_title }}: +{% endblocktrans %} +{{ comment_body }} +{% endblock %} diff --git a/lms/djangoapps/discussion/templates/discussion/edx_ace/responsenotification/push/subject.txt b/lms/djangoapps/discussion/templates/discussion/edx_ace/responsenotification/push/subject.txt new file mode 100644 index 000000000000..a49eb5dec1c2 --- /dev/null +++ b/lms/djangoapps/discussion/templates/discussion/edx_ace/responsenotification/push/subject.txt @@ -0,0 +1,3 @@ +{% load i18n %} + +{% blocktrans %}Response to {{ thread_title }}{% endblocktrans %} \ No newline at end of file From c26c26e1ce769e14b2a54dd8ca6fc416b3c86ced Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=86=D0=B2=D0=B0=D0=BD=20=D0=9D=D1=94=D0=B4=D1=94=D0=BB?= =?UTF-8?q?=D1=8C=D0=BD=D1=96=D1=86=D0=B5=D0=B2?= Date: Mon, 29 Apr 2024 18:08:34 +0300 Subject: [PATCH 2/2] refactor: [AXM-252] add extra context to push notification --- lms/djangoapps/discussion/signals/handlers.py | 1 + lms/djangoapps/discussion/tasks.py | 7 +++++-- .../edx_ace/responsenotification/push/body.txt | 9 ++------- lms/djangoapps/discussion/tests/test_tasks.py | 8 +++++++- .../django_comment_common/comment_client/comment.py | 10 +++++++++- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lms/djangoapps/discussion/signals/handlers.py b/lms/djangoapps/discussion/signals/handlers.py index 8c4991cd0289..ead8128a0fe0 100644 --- a/lms/djangoapps/discussion/signals/handlers.py +++ b/lms/djangoapps/discussion/signals/handlers.py @@ -113,6 +113,7 @@ def create_message_context(comment, site): 'course_id': str(thread.course_id), 'comment_id': comment.id, 'comment_body': comment.body, + 'comment_body_text': comment.body_text, 'comment_author_id': comment.user_id, 'comment_created_at': comment.created_at, # comment_client models dates are already serialized 'thread_id': thread.id, diff --git a/lms/djangoapps/discussion/tasks.py b/lms/djangoapps/discussion/tasks.py index cfa18dfdbb86..7d19cbb9cd63 100644 --- a/lms/djangoapps/discussion/tasks.py +++ b/lms/djangoapps/discussion/tasks.py @@ -218,8 +218,11 @@ def _build_message_context(context): # lint-amnesty, pylint: disable=missing-fu 'thread_username': thread_author.username, 'comment_username': comment_author.username, 'post_link': post_link, - 'click_action': post_link, - 'send_push_notification': True, + 'push_notification_extra_context': { + 'notification_type': 'forum_comment', + 'thread_id': context['thread_id'], + 'comment_id': context['comment_id'], + }, 'comment_created_at': date.deserialize(context['comment_created_at']), 'thread_created_at': date.deserialize(context['thread_created_at']) }) diff --git a/lms/djangoapps/discussion/templates/discussion/edx_ace/responsenotification/push/body.txt b/lms/djangoapps/discussion/templates/discussion/edx_ace/responsenotification/push/body.txt index 87440702954d..145d8344d3a1 100644 --- a/lms/djangoapps/discussion/templates/discussion/edx_ace/responsenotification/push/body.txt +++ b/lms/djangoapps/discussion/templates/discussion/edx_ace/responsenotification/push/body.txt @@ -1,8 +1,3 @@ {% load i18n %} - -{% block content %} -{% blocktrans trimmed %} - {{ comment_username }} replied to {{ thread_title }}: -{% endblocktrans %} -{{ comment_body }} -{% endblock %} +{% blocktrans trimmed %}{{ comment_username }} replied to {{ thread_title }}:{% endblocktrans %} +{{ comment_body_text }} diff --git a/lms/djangoapps/discussion/tests/test_tasks.py b/lms/djangoapps/discussion/tests/test_tasks.py index f6cce4437546..d3d3edb1f9e2 100644 --- a/lms/djangoapps/discussion/tests/test_tasks.py +++ b/lms/djangoapps/discussion/tests/test_tasks.py @@ -271,6 +271,7 @@ def test_send_discussion_email_notification(self, user_subscribed): expected_message_context.update({ 'comment_author_id': self.comment_author.id, 'comment_body': comment['body'], + 'comment_body_text': comment.body_text, 'comment_created_at': ONE_HOUR_AGO, 'comment_id': comment['id'], 'comment_username': self.comment_author.username, @@ -283,7 +284,12 @@ def test_send_discussion_email_notification(self, user_subscribed): 'thread_commentable_id': thread['commentable_id'], 'post_link': f'https://{site.domain}{self.mock_permalink.return_value}', 'site': site, - 'site_id': site.id + 'site_id': site.id, + 'push_notification_extra_context': { + 'notification_type': 'forum_comment', + 'thread_id': thread['id'], + 'comment_id': comment['id'], + }, }) expected_recipient = Recipient(self.thread_author.id, self.thread_author.email) actual_message = self.mock_ace_send.call_args_list[0][0][0] diff --git a/openedx/core/djangoapps/django_comment_common/comment_client/comment.py b/openedx/core/djangoapps/django_comment_common/comment_client/comment.py index 0b7a695a1c3e..c86f7eb40515 100644 --- a/openedx/core/djangoapps/django_comment_common/comment_client/comment.py +++ b/openedx/core/djangoapps/django_comment_common/comment_client/comment.py @@ -1,5 +1,5 @@ # pylint: disable=missing-docstring,protected-access - +from bs4 import BeautifulSoup from openedx.core.djangoapps.django_comment_common.comment_client import models, settings @@ -99,6 +99,14 @@ def unFlagAbuse(self, user, voteable, removeAll): ) voteable._update_from_response(response) + @property + def body_text(self): + """ + Return the text content of the comment html body. + """ + soup = BeautifulSoup(self.body, 'html.parser') + return soup.get_text() + def _url_for_thread_comments(thread_id): return f"{settings.PREFIX}/threads/{thread_id}/comments"