diff --git a/openedx/core/djangoapps/django_comment_common/comment_client/thread.py b/openedx/core/djangoapps/django_comment_common/comment_client/thread.py index af2424a5a0..bcee3145ea 100644 --- a/openedx/core/djangoapps/django_comment_common/comment_client/thread.py +++ b/openedx/core/djangoapps/django_comment_common/comment_client/thread.py @@ -7,10 +7,16 @@ import typing as t from eventtracking import tracker -from . import models, settings, utils +from django.core.exceptions import ObjectDoesNotExist from forum import api as forum_api -from openedx.core.djangoapps.discussions.config.waffle import is_forum_v2_enabled, is_forum_v2_disabled_globally +from forum.api.threads import prepare_thread_api_response +from forum.backend import get_backend from forum.backends.mongodb.threads import CommentThread +from forum.utils import ForumV2RequestError +from rest_framework.serializers import ValidationError + +from openedx.core.djangoapps.discussions.config.waffle import is_forum_v2_enabled, is_forum_v2_disabled_globally +from . import models, settings, utils log = logging.getLogger(__name__) @@ -245,6 +251,50 @@ class Thread(models.Model): } return CommentThread()._collection.count_documents(query_params) # pylint: disable=protected-access + @classmethod + def _delete_thread(cls, thread_id, course_id=None): + """ + Deletes a thread + """ + prefix = "<>" + backend = get_backend(course_id)() + try: + start_time = time.perf_counter() + thread = backend.validate_object("CommentThread", thread_id) + log.info(f"{prefix} Thread fetch {time.perf_counter() - start_time} sec") + except ObjectDoesNotExist as exc: + log.error("Forumv2RequestError for delete thread request.") + raise ForumV2RequestError( + f"Thread does not exist with Id: {thread_id}" + ) from exc + + start_time = time.perf_counter() + backend.delete_comments_of_a_thread(thread_id) + log.info(f"{prefix} Delete comments of thread {time.perf_counter() - start_time} sec") + + try: + start_time = time.perf_counter() + serialized_data = prepare_thread_api_response(thread, backend) + log.info(f"{prefix} Prepare response {time.perf_counter() - start_time} sec") + except ValidationError as error: + log.error(f"Validation error in get_thread: {error}") + raise ForumV2RequestError("Failed to prepare thread API response") from error + + start_time = time.perf_counter() + backend.delete_subscriptions_of_a_thread(thread_id) + log.info(f"{prefix} Delete subscriptions {time.perf_counter() - start_time} sec") + + start_time = time.perf_counter() + result = backend.delete_thread(thread_id) + log.info(f"{prefix} Delete thread {time.perf_counter() - start_time} sec") + if result and not (thread["anonymous"] or thread["anonymous_to_peers"]): + start_time = time.perf_counter() + backend.update_stats_for_course( + thread["author_id"], thread["course_id"], threads=-1 + ) + log.info(f"{prefix} Update stats {time.perf_counter() - start_time} sec") + return serialized_data + @classmethod def delete_user_threads(cls, user_id, course_ids): """ @@ -264,7 +314,7 @@ class Thread(models.Model): thread_id = thread.get("_id") course_id = thread.get("course_id") if thread_id: - forum_api.delete_thread(thread_id, course_id=course_id) + cls._delete_thread(thread_id, course_id=course_id) threads_deleted += 1 log.info(f"<> Deleted thread {thread_id} in {time.time() - start_time} seconds." f" Thread Found: {thread_id is not None}")