Skip to content

Commit

Permalink
feat: Populate notification context with post, comment and response i…
Browse files Browse the repository at this point in the history
…ds (#36008)

* feat: Populate notification context with post, comment and response ids
  • Loading branch information
jawad-khan authored Dec 20, 2024
1 parent edbcada commit 76cceaa
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 15 deletions.
33 changes: 25 additions & 8 deletions lms/djangoapps/discussion/rest_api/discussions_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def send_new_response_notification(self):
context = {
'email_content': clean_thread_html_body(self.comment.body),
}
self._populate_context_with_ids_for_mobile(context)
self._send_notification([self.thread.user_id], "new_response", extra_context=context)

def _response_and_thread_has_same_creator(self) -> bool:
Expand Down Expand Up @@ -156,6 +157,7 @@ def send_new_comment_notification(self):
"author_pronoun": str(author_pronoun),
"email_content": clean_thread_html_body(self.comment.body),
}
self._populate_context_with_ids_for_mobile(context)
self._send_notification([self.thread.user_id], "new_comment", extra_context=context)

def send_new_comment_on_response_notification(self):
Expand All @@ -171,6 +173,7 @@ def send_new_comment_on_response_notification(self):
context = {
"email_content": clean_thread_html_body(self.comment.body),
}
self._populate_context_with_ids_for_mobile(context)
self._send_notification(
[self.parent_response.user_id],
"new_comment_on_response",
Expand Down Expand Up @@ -216,12 +219,15 @@ def send_response_on_followed_post_notification(self):
# Remove duplicate users from the list of users to send notification
users = list(set(users))
if not self.parent_id:
context = {
"email_content": clean_thread_html_body(self.comment.body),
}
self._populate_context_with_ids_for_mobile(context)
self._send_notification(
users,
"response_on_followed_post",
extra_context={
"email_content": clean_thread_html_body(self.comment.body),
})
extra_context=context
)
else:
author_name = f"{self.parent_response.username}'s"
# use 'their' if comment author is also response author.
Expand All @@ -231,14 +237,16 @@ def send_response_on_followed_post_notification(self):
if self._response_and_comment_has_same_creator()
else f"{self.parent_response.username}'s"
)
context = {
"author_name": str(author_name),
"author_pronoun": str(author_pronoun),
"email_content": clean_thread_html_body(self.comment.body),
}
self._populate_context_with_ids_for_mobile(context)
self._send_notification(
users,
"comment_on_followed_post",
extra_context={
"author_name": str(author_name),
"author_pronoun": str(author_pronoun),
"email_content": clean_thread_html_body(self.comment.body),
}
extra_context=context
)

def _create_cohort_course_audience(self):
Expand Down Expand Up @@ -290,6 +298,7 @@ def send_response_endorsed_on_thread_notification(self):
context = {
"email_content": clean_thread_html_body(self.comment.body)
}
self._populate_context_with_ids_for_mobile(context)
self._send_notification([self.thread.user_id], "response_endorsed_on_thread", extra_context=context)

def send_response_endorsed_notification(self):
Expand All @@ -299,6 +308,7 @@ def send_response_endorsed_notification(self):
context = {
"email_content": clean_thread_html_body(self.comment.body)
}
self._populate_context_with_ids_for_mobile(context)
self._send_notification([self.creator.id], "response_endorsed", extra_context=context)

def send_new_thread_created_notification(self):
Expand Down Expand Up @@ -330,6 +340,7 @@ def send_new_thread_created_notification(self):
'post_title': self.thread.title,
"email_content": clean_thread_html_body(self.thread.body),
}
self._populate_context_with_ids_for_mobile(context)
self._send_course_wide_notification(notification_type, audience_filters, context)

def send_reported_content_notification(self):
Expand Down Expand Up @@ -362,6 +373,12 @@ def send_reported_content_notification(self):
]}
self._send_course_wide_notification("content_reported", audience_filters, context)

def _populate_context_with_ids_for_mobile(self, context):
context['thread_id'] = self.thread.id
context['topic_id'] = self.thread.commentable_id
context['comment_id'] = self.comment_id
context['parent_id'] = self.parent_id


def is_discussion_cohorted(course_key_str):
"""
Expand Down
45 changes: 39 additions & 6 deletions lms/djangoapps/discussion/rest_api/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ def setUp(self):
"username": thread.username,
"thread_type": 'discussion',
"title": thread.title,
"commentable_id": thread.commentable_id,

})
self._register_subscriptions_endpoint()

Expand Down Expand Up @@ -354,7 +356,11 @@ def test_send_notification_to_thread_creator(self):
'post_title': 'test thread',
'email_content': self.comment.body,
'course_name': self.course.display_name,
'sender_id': self.user_2.id
'sender_id': self.user_2.id,
'parent_id': None,
'topic_id': None,
'thread_id': 1,
'comment_id': 4,
}
self.assertDictEqual(args.context, expected_context)
self.assertEqual(
Expand Down Expand Up @@ -400,7 +406,11 @@ def test_send_notification_to_parent_threads(self):
'author_name': 'dummy\'s',
'author_pronoun': 'dummy\'s',
'course_name': self.course.display_name,
'sender_id': self.user_3.id
'sender_id': self.user_3.id,
'parent_id': 2,
'topic_id': None,
'thread_id': 1,
'comment_id': 4,
}
self.assertDictEqual(args_comment.context, expected_context)
self.assertEqual(
Expand All @@ -417,7 +427,11 @@ def test_send_notification_to_parent_threads(self):
'post_title': self.thread.title,
'email_content': self.comment.body,
'course_name': self.course.display_name,
'sender_id': self.user_3.id
'sender_id': self.user_3.id,
'parent_id': 2,
'topic_id': None,
'thread_id': 1,
'comment_id': 4,
}
self.assertDictEqual(args_comment_on_response.context, expected_context)
self.assertEqual(
Expand Down Expand Up @@ -477,7 +491,11 @@ def test_comment_creators_own_response(self):
'author_pronoun': 'your',
'course_name': self.course.display_name,
'sender_id': self.user_3.id,
'email_content': self.comment.body
'email_content': self.comment.body,
'parent_id': 2,
'topic_id': None,
'thread_id': 1,
'comment_id': 4,
}
self.assertDictEqual(args_comment.context, expected_context)
self.assertEqual(
Expand Down Expand Up @@ -522,6 +540,10 @@ def test_send_notification_to_followers(self, parent_id, notification_type):
'email_content': self.comment.body,
'course_name': self.course.display_name,
'sender_id': self.user_2.id,
'parent_id': parent_id,
'topic_id': None,
'thread_id': 1,
'comment_id': 4,
}
if parent_id:
expected_context['author_name'] = 'dummy\'s'
Expand Down Expand Up @@ -624,6 +646,8 @@ def test_new_comment_notification(self):
"username": thread.username,
"thread_type": 'discussion',
"title": thread.title,
"commentable_id": thread.commentable_id,

})
self.register_get_comment_response({
'id': response.id,
Expand Down Expand Up @@ -707,6 +731,7 @@ def test_response_endorsed_notifications(self):
"username": thread.username,
"thread_type": 'discussion',
"title": thread.title,
"commentable_id": thread.commentable_id,
})
self.register_get_comment_response({
'id': 1,
Expand Down Expand Up @@ -734,7 +759,11 @@ def test_response_endorsed_notifications(self):
'post_title': 'test thread',
'course_name': self.course.display_name,
'sender_id': int(self.user_2.id),
'email_content': 'dummy'
'email_content': 'dummy',
'parent_id': None,
'topic_id': None,
'thread_id': 1,
'comment_id': 2,
}
self.assertDictEqual(notification_data.context, expected_context)
self.assertEqual(notification_data.content_url, _get_mfe_url(self.course.id, thread.id))
Expand All @@ -752,7 +781,11 @@ def test_response_endorsed_notifications(self):
'post_title': 'test thread',
'course_name': self.course.display_name,
'sender_id': int(response.user_id),
'email_content': 'dummy'
'email_content': 'dummy',
'parent_id': None,
'topic_id': None,
'thread_id': 1,
'comment_id': 2,
}
self.assertDictEqual(notification_data.context, expected_context)
self.assertEqual(notification_data.content_url, _get_mfe_url(self.course.id, thread.id))
Expand Down
3 changes: 2 additions & 1 deletion lms/djangoapps/discussion/rest_api/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,13 +675,14 @@ class ThreadMock(object):
A mock thread object
"""

def __init__(self, thread_id, creator, title, parent_id=None, body=''):
def __init__(self, thread_id, creator, title, parent_id=None, body='', commentable_id=None):
self.id = thread_id
self.user_id = str(creator.id)
self.username = creator.username
self.title = title
self.parent_id = parent_id
self.body = body
self.commentable_id = commentable_id

def url_with_id(self, params):
return f"http://example.com/{params['id']}"

0 comments on commit 76cceaa

Please sign in to comment.