Skip to content

Commit

Permalink
feat: added thread_flagged and comment_flagged signals in legacy disc…
Browse files Browse the repository at this point in the history
…ussion
  • Loading branch information
adeel.tajamul committed Dec 20, 2023
1 parent 82f3cec commit 53c2b26
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
48 changes: 46 additions & 2 deletions lms/djangoapps/discussion/django_comment_client/base/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@ def test_vote(self, mock_request):
self._assert_json_response_contains_group_info(response)

def test_flag(self, mock_request):
response = self.call_view("flag_abuse_for_thread", mock_request)
self._assert_json_response_contains_group_info(response)
with mock.patch('openedx.core.djangoapps.django_comment_common.signals.thread_flagged.send') as signal_mock:
response = self.call_view("flag_abuse_for_thread", mock_request)
self._assert_json_response_contains_group_info(response)
self.assertEqual(signal_mock.call_count, 1)
response = self.call_view("un_flag_abuse_for_thread", mock_request)
self._assert_json_response_contains_group_info(response)

Expand Down Expand Up @@ -1349,6 +1351,48 @@ def _test_unicode_data(self, text, mock_request):
assert mock_request.call_args[1]['data']['body'] == text


@patch('openedx.core.djangoapps.django_comment_common.comment_client.utils.requests.request', autospec=True)
class CommentActionTestCase(
MockRequestSetupMixin,
CohortedTestCase,
GroupIdAssertionMixin
):
def call_view(
self,
view_name,
mock_request,
user=None,
post_params=None,
view_args=None
):
self._set_mock_request_data(
mock_request,
{
"user_id": str(self.student.id),
"group_id": self.student_cohort.id,
"closed": False,
"type": "thread",
"commentable_id": "non_team_dummy_id",
"body": "test body",
}
)
request = RequestFactory().post("dummy_url", post_params or {})
request.user = user or self.student
request.view_name = view_name

return getattr(views, view_name)(
request,
course_id=str(self.course.id),
comment_id="dummy",
**(view_args or {})
)

def test_flag(self, mock_request):
with mock.patch('openedx.core.djangoapps.django_comment_common.signals.comment_flagged.send') as signal_mock:
self.call_view("flag_abuse_for_comment", mock_request)
self.assertEqual(signal_mock.call_count, 1)


@disable_signal(views, 'comment_created')
class CreateSubCommentUnicodeTestCase(
ForumsEnableMixin,
Expand Down
4 changes: 4 additions & 0 deletions lms/djangoapps/discussion/django_comment_client/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@
comment_deleted,
comment_edited,
comment_endorsed,
comment_flagged,
comment_voted,
thread_created,
thread_deleted,
thread_edited,
thread_flagged,
thread_followed,
thread_unfollowed,
thread_voted
Expand Down Expand Up @@ -900,6 +902,7 @@ def flag_abuse_for_thread(request, course_id, thread_id):
thread = cc.Thread.find(thread_id)
thread.flagAbuse(user, thread)
track_discussion_reported_event(request, course, thread)
thread_flagged.send(sender='flag_abuse_for_thread', user=request.user, post=thread)
return JsonResponse(prepare_content(thread.to_dict(), course_key))


Expand Down Expand Up @@ -938,6 +941,7 @@ def flag_abuse_for_comment(request, course_id, comment_id):
comment = cc.Comment.find(comment_id)
comment.flagAbuse(user, comment)
track_discussion_reported_event(request, course, comment)
comment_flagged.send(sender='flag_abuse_for_comment', user=request.user, post=comment)
return JsonResponse(prepare_content(comment.to_dict(), course_key))


Expand Down
12 changes: 7 additions & 5 deletions lms/djangoapps/discussion/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ def send_ace_message_for_reported_content(context): # lint-amnesty, pylint: dis
context['course_name'] = modulestore().get_course(context['course_id']).display_name

moderators = get_users_with_moderator_roles(context)
context['site'] = Site.objects.get(id=context['site_id']
)
context['site'] = Site.objects.get(id=context['site_id'])
if not _is_content_still_reported(context):
log.info('Reported content is no longer in reported state. Email to moderators will not be sent.')
return
Expand Down Expand Up @@ -225,9 +224,10 @@ def _build_message_context(context): # lint-amnesty, pylint: disable=missing-fu
def _build_message_context_for_reported_content(context, moderator): # lint-amnesty, pylint: disable=missing-function-docstring
message_context = get_base_template_context(context['site'])
message_context.update(context)
use_mfe_url = ENABLE_DISCUSSIONS_MFE.is_enabled(context['course_id'])

message_context.update({
'post_link': _get_mfe_thread_url(context),
'post_link': _get_mfe_thread_url(context) if use_mfe_url else _get_thread_url(context, settings.LMS_BASE),
'moderator_email': moderator.email,
})
return message_context
Expand All @@ -242,9 +242,11 @@ def _get_mfe_thread_url(context):
return urljoin(forum_url, mfe_post_link)


def _get_thread_url(context): # lint-amnesty, pylint: disable=missing-function-docstring
def _get_thread_url(context, domain_url=None): # lint-amnesty, pylint: disable=missing-function-docstring
scheme = 'https' if settings.HTTPS == 'on' else 'http'
base_url = '{}://{}'.format(scheme, context['site'].domain)
if domain_url is None:
domain_url = context['site'].domain
base_url = '{}://{}'.format(scheme, domain_url)
thread_content = {
'type': 'thread',
'course_id': context['course_id'],
Expand Down

0 comments on commit 53c2b26

Please sign in to comment.