From 7f5d29b954cf4068d5b76ed0540aea76552093e2 Mon Sep 17 00:00:00 2001 From: Naincy Chourasia Date: Fri, 15 May 2026 05:23:12 +0000 Subject: [PATCH] fix: include user replied posts in My Posts tab --- forum/__init__.py | 2 +- forum/api/threads.py | 83 ++++++++++++++++++++++++++++++-- tests/test_views/test_threads.py | 5 +- 3 files changed, 82 insertions(+), 8 deletions(-) diff --git a/forum/__init__.py b/forum/__init__.py index af155f75..b180362d 100644 --- a/forum/__init__.py +++ b/forum/__init__.py @@ -2,4 +2,4 @@ Openedx forum app. """ -__version__ = "0.6.1" +__version__ = "0.6.2" diff --git a/forum/api/threads.py b/forum/api/threads.py index 6e3889f0..a27b0389 100644 --- a/forum/api/threads.py +++ b/forum/api/threads.py @@ -2,6 +2,8 @@ Native Python Threads APIs. """ +# pylint: disable=import-outside-toplevel,cyclic-import + import logging from typing import Any, Optional @@ -437,11 +439,83 @@ def get_user_threads( params = {k: v for k, v in params.items() if v is not None} backend.validate_params(params) - thread_filter = backend.get_user_thread_filter(course_id) - filtered_threads = backend.get_filtered_threads(thread_filter, ids_only=True) - thread_ids = [thread["_id"] for thread in filtered_threads] - threads = backend.get_threads(params, user_id or "", ThreadSerializer, thread_ids) + # For "My Posts": get threads where user authored OR commented (non-anonymous) + if author_id and user_id and str(author_id) == str(user_id): + from forum.backends.mysql.api import MySQLBackend + + if isinstance(backend, MySQLBackend): + from forum.backends.mysql.models import Comment, CommentThread + + author_pk = int(author_id) + authored = CommentThread.objects.filter( + course_id=course_id, + author__pk=author_pk, + anonymous=False, + anonymous_to_peers=False, + ).values_list("pk", flat=True) + commented = ( + Comment.objects.filter( + course_id=course_id, + author__pk=author_pk, + anonymous=False, + anonymous_to_peers=False, + ) + .values_list("comment_thread__pk", flat=True) + .distinct() + ) + thread_ids = [str(tid) for tid in set(authored) | set(commented)] + else: + from forum.backends.mongodb.comments import Comment # type: ignore[assignment] + from forum.backends.mongodb.threads import CommentThread # type: ignore[assignment] + + query = { + "course_id": course_id, + "author_id": str(author_id), + "anonymous": False, + "anonymous_to_peers": False, + } + authored = CommentThread().distinct("_id", query) # type: ignore[attr-defined] + commented = Comment().distinct("comment_thread_id", query) # type: ignore[attr-defined] + thread_ids = [str(tid) for tid in set(authored) | set(commented)] + params.pop("author_id", None) + elif author_id: + # Viewing someone else's posts: show only their authored threads (not commented) + from forum.backends.mysql.api import MySQLBackend + + if isinstance(backend, MySQLBackend): + from forum.backends.mysql.models import CommentThread + + thread_ids = [ + str(tid) + for tid in CommentThread.objects.filter( + course_id=course_id, + author__pk=int(author_id), + anonymous=False, + anonymous_to_peers=False, + ).values_list("pk", flat=True) + ] + else: + from forum.backends.mongodb.threads import CommentThread # type: ignore[assignment] + + thread_ids = [ + str(tid) + for tid in CommentThread().distinct( # type: ignore[attr-defined] + "_id", + { + "course_id": course_id, + "author_id": str(author_id), + "anonymous": False, + "anonymous_to_peers": False, + }, + ) + ] + params.pop("author_id", None) + else: + thread_filter = backend.get_user_thread_filter(course_id) + filtered_threads = backend.get_filtered_threads(thread_filter, ids_only=True) + thread_ids = [thread["_id"] for thread in filtered_threads] + threads = backend.get_threads(params, user_id or "", ThreadSerializer, thread_ids) return threads @@ -450,7 +524,6 @@ def get_course_id_by_thread(thread_id: str) -> str | None: Return course_id for the matching thread. It searches for thread_id both in mongodb and mysql. """ - # pylint: disable=C0415 from forum.backends.mongodb.api import MongoBackend from forum.backends.mysql.api import MySQLBackend diff --git a/tests/test_views/test_threads.py b/tests/test_views/test_threads.py index da858c85..1350c2be 100644 --- a/tests/test_views/test_threads.py +++ b/tests/test_views/test_threads.py @@ -388,7 +388,7 @@ def test_filter_by_author(api_client: APIClient, patched_get_backend: Any) -> No def test_anonymous_threads(api_client: APIClient, patched_get_backend: Any) -> None: - """Test Anonymus threads are only visible to Authors""" + """Test Anonymous threads are excluded from both own posts and other users' posts""" backend = patched_get_backend course_id = "course-1" author_id = "1" @@ -431,7 +431,8 @@ def test_anonymous_threads(api_client: APIClient, patched_get_backend: Any) -> N response = api_client.get_json("/api/v2/threads", params) assert response.status_code == 200 result = response.json()["collection"] - assert len(result) == 2 + # Anonymous threads are now excluded from "My Posts" tab as well + assert len(result) == 1 def test_unresponded_filter(api_client: APIClient, patched_get_backend: Any) -> None: