feat: added endpoint for priviledged roles to delete threads of a user (#37030)

* feat: added endpoint for priviledged roles to delete threads of a user

* chore: moved forum calls to django comment common app

* fix: fixed nits
This commit is contained in:
Muhammad Adeel Tajamul
2025-07-21 09:43:55 +05:00
committed by GitHub
parent e3d3eedd8b
commit 989ecfe5a0
7 changed files with 292 additions and 73 deletions

View File

@@ -6,6 +6,7 @@ from openedx.core.djangoapps.django_comment_common.comment_client import models,
from .thread import Thread
from .utils import CommentClientRequestError, get_course_key
from forum import api as forum_api
from forum.backends.mongodb.comments import Comment as ForumComment
class Comment(models.Model):
@@ -97,6 +98,37 @@ class Comment(models.Model):
soup = BeautifulSoup(self.body, 'html.parser')
return soup.get_text()
@classmethod
def get_user_comment_count(cls, user_id, course_ids):
"""
Returns comments and responses count of user in the given course_ids.
TODO: Add support for MySQL backend as well
"""
query_params = {
"course_id": {"$in": course_ids},
"author_id": str(user_id),
"_type": "Comment"
}
return ForumComment()._collection.count_documents(query_params) # pylint: disable=protected-access
@classmethod
def delete_user_comments(cls, user_id, course_ids):
"""
Deletes comments and responses of user in the given course_ids.
TODO: Add support for MySQL backend as well
"""
query_params = {
"course_id": {"$in": course_ids},
"author_id": str(user_id),
}
comments_deleted = 0
comments = ForumComment().get_list(**query_params)
for comment in comments:
comment_id = comment.get("_id")
if comment_id:
comments_deleted += ForumComment().delete(comment_id)
return comments_deleted
def _url_for_thread_comments(thread_id):
return f"{settings.PREFIX}/threads/{thread_id}/comments"

View File

@@ -9,6 +9,8 @@ from eventtracking import tracker
from . import models, settings, utils
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.backends.mongodb.threads import CommentThread
log = logging.getLogger(__name__)
@@ -229,6 +231,37 @@ class Thread(models.Model):
)
self._update_from_response(response)
@classmethod
def get_user_threads_count(cls, user_id, course_ids):
"""
Returns threads and responses count of user in the given course_ids.
TODO: Add support for MySQL backend as well
"""
query_params = {
"course_id": {"$in": course_ids},
"author_id": str(user_id),
"_type": "CommentThread"
}
return CommentThread()._collection.count_documents(query_params) # pylint: disable=protected-access
@classmethod
def delete_user_threads(cls, user_id, course_ids):
"""
Deletes threads of user in the given course_ids.
TODO: Add support for MySQL backend as well
"""
query_params = {
"course_id": {"$in": course_ids},
"author_id": str(user_id),
}
threads_deleted = 0
threads = CommentThread().get_list(**query_params)
for thread in threads:
thread_id = thread.get("_id")
if thread_id:
threads_deleted += CommentThread().delete(thread_id)
return threads_deleted
def _url_for_flag_abuse_thread(thread_id):
return f"{settings.PREFIX}/threads/{thread_id}/abuse_flag"