|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | import logging |
| 6 | +import math |
6 | 7 | from typing import Any, Optional |
7 | 8 |
|
8 | 9 | from django.core.exceptions import ObjectDoesNotExist |
@@ -317,3 +318,57 @@ def get_course_id_by_comment(comment_id: str) -> str | None: |
317 | 318 | or MySQLBackend.get_course_id_by_comment_id(comment_id) |
318 | 319 | or None |
319 | 320 | ) |
| 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