Skip to content
Open
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.4.4"
__version__ = "0.4.5"
4 changes: 3 additions & 1 deletion forum/backends/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ def get_commentables_counts_based_on_type(course_id: str) -> dict[str, Any]:
raise NotImplementedError

@classmethod
def get_user_voted_ids(cls, user_id: str, vote: str) -> list[str]:
def get_user_voted_ids(
cls, user_id: str, vote: str, course_id: Optional[str] = None
) -> list[str]:
"""Get user voted ids."""
raise NotImplementedError

Expand Down
19 changes: 15 additions & 4 deletions forum/backends/mongodb/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,13 +1159,20 @@ def get_commentables_counts_based_on_type(course_id: str) -> dict[str, Any]:
return commentable_counts

@classmethod
def get_user_voted_ids(cls, user_id: str, vote: str) -> list[str]:
def get_user_voted_ids(
cls, user_id: str, vote: str, course_id: Optional[str] = None
) -> list[str]:
"""Get the IDs of the posts voted by a user."""
if vote not in ["up", "down"]:
raise ValueError("Invalid vote type")

content_model = Contents()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add test coverage for these changes? (Not sure if this repo is set up with a mock DB for unit tests.)

contents = content_model.get_list()
content_query: dict[str, Any] = {}
if course_id:
content_query["course_id"] = str(course_id)
content_query[f"votes.{vote}"] = {"$in": [user_id, str(user_id)]}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you help me understand why this is [user_id, str(user_id)]? I see that user_id is already a string so it looks like this would be equivalent to [user_id, userId].

My understanding of MongoDB query syntax is a little hazy, but it seems like we could simplify this:

Suggested change
content_query[f"votes.{vote}"] = {"$in": [user_id, str(user_id)]}
content_query[f"votes.{vote}"] = user_id

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me check it in that manner.


contents = content_model.get_list(**content_query)
voted_ids = []
for content in contents:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can any of this be removed now that the query is more refined?

votes = content["votes"][vote]
Expand Down Expand Up @@ -1207,8 +1214,12 @@ def user_to_hash(

if params.get("complete"):
subscribed_thread_ids = cls.find_subscribed_threads(user["external_id"])
upvoted_ids = cls.get_user_voted_ids(user["external_id"], "up")
downvoted_ids = cls.get_user_voted_ids(user["external_id"], "down")
upvoted_ids = cls.get_user_voted_ids(
user["external_id"], "up", params.get("course_id")
)
downvoted_ids = cls.get_user_voted_ids(
user["external_id"], "down", params.get("course_id")
)
hash_data.update(
{
"subscribed_thread_ids": subscribed_thread_ids,
Expand Down
4 changes: 3 additions & 1 deletion forum/backends/mysql/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,9 @@ def get_threads(
return threads

@classmethod
def get_user_voted_ids(cls, user_id: str, vote: str) -> list[str]:
def get_user_voted_ids(
cls, user_id: str, vote: str, course_id: Optional[str] = None
) -> list[str]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a plan or ticket to optimize this by including course ID in the query?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(To be clear, I'm not saying we have to do this. If 2U isn't using the MySQL backend, we could just pass this info along to someone who is.)

"""Get the IDs of the posts voted by a user."""
if vote not in ["up", "down"]:
raise ValueError("Invalid vote type")
Expand Down
7 changes: 6 additions & 1 deletion tests/e2e/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,12 @@ def test_update_user_stats(api_client: APIClient, patched_get_backend: Any) -> N
# Sort the data for expected result (threads, responses, replies)
expected_result = sorted(
expected_data.values(),
key=lambda val: (val["threads"], val["responses"], val["replies"]),
key=lambda val: (
val["threads"],
val["responses"],
val["replies"],
val["username"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this the unrelated test change? I'm guessing this was a flaky test that you're fixing here. If so, can you ensure this ends up in the squash commit's message?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this was unrelated test-case. Yes, will make sure to add it in squash commit message.

),
reverse=True,
)

Expand Down
Loading