Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added comment_endorsed, thread_followed and thread_unfollowed signals in new discussion experience #33944

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lms/djangoapps/discussion/rest_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,17 @@
from openedx.core.djangoapps.django_comment_common.signals import (
comment_created,
comment_deleted,
comment_endorsed,
comment_edited,
comment_flagged,
comment_voted,
thread_created,
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
Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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


Expand Down
11 changes: 9 additions & 2 deletions lms/djangoapps/discussion/rest_api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))},
Expand All @@ -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
Expand Down
Loading