Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion forum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Openedx forum app.
"""

__version__ = "0.6.1"
__version__ = "0.6.2"
83 changes: 78 additions & 5 deletions forum/api/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Native Python Threads APIs.
"""

# pylint: disable=import-outside-toplevel,cyclic-import

import logging
from typing import Any, Optional

Expand Down Expand Up @@ -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


Expand All @@ -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

Expand Down
5 changes: 3 additions & 2 deletions tests/test_views/test_threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
Loading