Add fields to discussion thread list endpoint
This commit adds fields that are related to the requesting user's interaction with the thread (e.g. following).
This commit is contained in:
@@ -14,6 +14,7 @@ from django_comment_common.models import (
|
||||
Role,
|
||||
)
|
||||
from lms.lib.comment_client.thread import Thread
|
||||
from lms.lib.comment_client.user import User
|
||||
from openedx.core.djangoapps.course_groups.cohorts import get_cohort_id
|
||||
|
||||
|
||||
@@ -76,7 +77,7 @@ def get_course_topics(course, user):
|
||||
}
|
||||
|
||||
|
||||
def _cc_thread_to_api_thread(thread):
|
||||
def _cc_thread_to_api_thread(thread, cc_user):
|
||||
"""
|
||||
Convert a thread data dict from the comment_client format (which is a direct
|
||||
representation of the format returned by the comments service) to the format
|
||||
@@ -98,6 +99,10 @@ def _cc_thread_to_api_thread(thread):
|
||||
ret.update({
|
||||
"topic_id": thread["commentable_id"],
|
||||
"raw_body": thread["body"],
|
||||
"following": thread["id"] in cc_user["subscribed_thread_ids"],
|
||||
"abuse_flagged": cc_user["id"] in thread["abuse_flaggers"],
|
||||
"voted": thread["id"] in cc_user["upvoted_ids"],
|
||||
"vote_count": thread["votes"]["up_count"],
|
||||
"comment_count": thread["comments_count"],
|
||||
"unread_comment_count": thread["unread_comments_count"],
|
||||
})
|
||||
@@ -125,6 +130,7 @@ def get_thread_list(request, course_key, page, page_size):
|
||||
name__in=[FORUM_ROLE_ADMINISTRATOR, FORUM_ROLE_MODERATOR, FORUM_ROLE_COMMUNITY_TA],
|
||||
users=request.user
|
||||
).exists()
|
||||
cc_user = User.from_django_user(request.user).retrieve()
|
||||
threads, result_page, num_pages, _ = Thread.search({
|
||||
"course_id": unicode(course_key),
|
||||
"group_id": None if user_is_privileged else get_cohort_id(request.user, course_key),
|
||||
@@ -139,5 +145,5 @@ def get_thread_list(request, course_key, page, page_size):
|
||||
if result_page != page:
|
||||
raise Http404
|
||||
|
||||
results = [_cc_thread_to_api_thread(thread) for thread in threads]
|
||||
results = [_cc_thread_to_api_thread(thread, cc_user) for thread in threads]
|
||||
return get_paginated_data(request, results, page, num_pages)
|
||||
|
||||
Reference in New Issue
Block a user