Skip to content

Commit 4254215

Browse files
taimoor-ahmed-1Taimoor  Ahmed
andauthored
feat: Add backward compatibility for retrieve_all comments (#238)
This PR adds get_user_comments api that retrieves all of the user comments as it was also supported in the cs_comments_service backend. Co-authored-by: Taimoor Ahmed <taimoor.ahmed@A006-01711.local>
1 parent fd33b4d commit 4254215

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

forum/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
Openedx forum app.
33
"""
44

5-
__version__ = "0.3.6"
5+
__version__ = "0.3.7"

forum/api/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
delete_comment,
1010
get_course_id_by_comment,
1111
get_parent_comment,
12+
get_user_comments,
1213
update_comment,
1314
)
1415
from .flags import (
@@ -68,6 +69,7 @@
6869
"get_thread_subscriptions",
6970
"get_user",
7071
"get_user_active_threads",
72+
"get_user_comments",
7173
"get_user_course_stats",
7274
"get_user_subscriptions",
7375
"get_user_threads",

forum/api/comments.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import logging
6+
import math
67
from typing import Any, Optional
78

89
from django.core.exceptions import ObjectDoesNotExist
@@ -317,3 +318,57 @@ def get_course_id_by_comment(comment_id: str) -> str | None:
317318
or MySQLBackend.get_course_id_by_comment_id(comment_id)
318319
or None
319320
)
321+
322+
323+
def get_user_comments(
324+
user_id: str,
325+
course_id: str,
326+
flagged: Optional[bool] = False,
327+
page: int = 1,
328+
per_page: int = 10,
329+
) -> dict[str, Any]:
330+
"""
331+
Get all comments made by a user in a specific course.
332+
333+
Args:
334+
user_id: The ID of the user
335+
course_id: The ID of the course
336+
flagged: Filter for flagged comments
337+
page: Page number for pagination
338+
per_page: Number of items per page
339+
340+
Returns:
341+
A dictionary containing paginated comment results
342+
"""
343+
344+
backend = get_backend(course_id)()
345+
346+
# Build query parameters
347+
query_params: dict[str, Any] = {
348+
"author_id": str(user_id),
349+
"course_id": course_id,
350+
}
351+
352+
if flagged:
353+
query_params["abuse_flaggers"] = {"$ne": [], "$exists": True}
354+
355+
# Get total count
356+
comment_count = backend.get_comments_count(**query_params)
357+
358+
# Calculate pagination
359+
num_pages = max(1, math.ceil(comment_count / per_page))
360+
skip = (page - 1) * per_page
361+
362+
# Get paginated comments
363+
query_params["resp_skip"] = skip
364+
query_params["resp_limit"] = per_page
365+
query_params["sort"] = -1 # Sort by newest first
366+
367+
comments = backend.get_comments(**query_params)
368+
369+
return {
370+
"collection": comments,
371+
"comment_count": comment_count,
372+
"num_pages": num_pages,
373+
"page": page,
374+
}

0 commit comments

Comments
 (0)