feat: added comment_endorsed, thread_followed and thread_unfollowed signals in mfe api (#33944)

This commit is contained in:
Muhammad Adeel Tajamul
2023-12-29 11:29:31 +05:00
committed by GitHub
parent dd4a97d410
commit 30c029f119
2 changed files with 25 additions and 3 deletions

View File

@@ -68,6 +68,7 @@ from openedx.core.djangoapps.django_comment_common.models import (
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 @@ from openedx.core.djangoapps.django_comment_common.signals import (
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

View File

@@ -2752,7 +2752,12 @@ class UpdateThreadTest(
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 @@ class UpdateCommentTest(
[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 @@ class UpdateCommentTest(
)
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