From 6dbab3a7e8bf00bc30eb25583eb0f721c5cb1dd0 Mon Sep 17 00:00:00 2001 From: "adeel.tajamul" Date: Mon, 18 Dec 2023 13:23:07 +0500 Subject: [PATCH] feat: added comment_endorsed, thread_followed and thread_unfollowed signals in mfe api --- lms/djangoapps/discussion/rest_api/api.py | 17 ++++++++++++++++- .../discussion/rest_api/tests/test_api.py | 11 +++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lms/djangoapps/discussion/rest_api/api.py b/lms/djangoapps/discussion/rest_api/api.py index 61877f57f06a..459b9cbc24f4 100644 --- a/lms/djangoapps/discussion/rest_api/api.py +++ b/lms/djangoapps/discussion/rest_api/api.py @@ -68,6 +68,7 @@ from openedx.core.djangoapps.django_comment_common.signals import ( comment_created, comment_deleted, + comment_endorsed, comment_edited, comment_flagged, comment_voted, @@ -75,7 +76,9 @@ thread_deleted, thread_edited, thread_flagged, - thread_voted + thread_followed, + thread_voted, + thread_unfollowed ) from openedx.core.djangoapps.user_api.accounts.api import get_account_settings from openedx.core.lib.exceptions import CourseNotFoundError, DiscussionNotFoundError, PageNotFoundError @@ -1352,6 +1355,8 @@ def _handle_following_field(form_value, user, cc_content, request): user.follow(cc_content) else: user.unfollow(cc_content) + signal = thread_followed if form_value else thread_unfollowed + signal.send(sender=None, user=user, post=cc_content) track_thread_followed_event(request, course, cc_content, form_value) @@ -1416,6 +1421,15 @@ def _handle_pinned_field(pin_thread: bool, cc_content: Thread, user: User): cc_content.un_pin(user, cc_content.id) +def _handle_comment_signals(update_data, comment, user, sender=None): + """ + Send signals depending upon the the patch (update_data) + """ + for key, value in update_data.items(): + if key == "endorsed" and value is True: + comment_endorsed.send(sender=sender, user=user, post=comment) + + def create_thread(request, thread_data): """ Create a thread. @@ -1597,6 +1611,7 @@ def update_comment(request, comment_id, update_data): comment_edited.send(sender=None, user=request.user, post=cc_comment) api_comment = serializer.data _do_extra_actions(api_comment, cc_comment, list(update_data.keys()), actions_form, context, request) + _handle_comment_signals(update_data, cc_comment, request.user) return api_comment diff --git a/lms/djangoapps/discussion/rest_api/tests/test_api.py b/lms/djangoapps/discussion/rest_api/tests/test_api.py index bf744738b0a2..bb9eccd1c641 100644 --- a/lms/djangoapps/discussion/rest_api/tests/test_api.py +++ b/lms/djangoapps/discussion/rest_api/tests/test_api.py @@ -2752,7 +2752,12 @@ def test_following(self, old_following, new_following, mock_emit): self.register_subscription_response(self.user) self.register_thread() data = {"following": new_following} - result = update_thread(self.request, "test_thread", data) + signal_name = "thread_followed" if new_following else "thread_unfollowed" + mock_path = f"openedx.core.djangoapps.django_comment_common.signals.{signal_name}.send" + with mock.patch(mock_path) as signal_patch: + result = update_thread(self.request, "test_thread", data) + if old_following != new_following: + self.assertEqual(signal_patch.call_count, 1) assert result['following'] == new_following last_request_path = urlparse(httpretty.last_request().path).path # lint-amnesty, pylint: disable=no-member subscription_url = f"/api/v1/users/{self.user.id}/subscriptions" @@ -3333,7 +3338,8 @@ def test_raw_body_access(self, role_name, is_thread_author, is_comment_author): [True, False], )) @ddt.unpack - def test_endorsed_access(self, role_name, is_thread_author, thread_type, is_comment_author): + @mock.patch('openedx.core.djangoapps.django_comment_common.signals.comment_endorsed.send') + def test_endorsed_access(self, role_name, is_thread_author, thread_type, is_comment_author, endorsed_mock): _assign_role_to_user(user=self.user, course_id=self.course.id, role=role_name) self.register_comment( {"user_id": str(self.user.id if is_comment_author else (self.user.id + 1))}, @@ -3348,6 +3354,7 @@ def test_endorsed_access(self, role_name, is_thread_author, thread_type, is_comm ) try: update_comment(self.request, "test_comment", {"endorsed": True}) + self.assertEqual(endorsed_mock.call_count, 1) assert not expected_error except ValidationError as err: assert expected_error