diff --git a/lms/djangoapps/discussion_api/serializers.py b/lms/djangoapps/discussion_api/serializers.py index 7a53d1a713..ac71e89c8a 100644 --- a/lms/djangoapps/discussion_api/serializers.py +++ b/lms/djangoapps/discussion_api/serializers.py @@ -190,8 +190,8 @@ class ThreadSerializer(_ContentSerializer): pinned = serializers.SerializerMethodField(read_only=True) closed = serializers.BooleanField(read_only=True) following = serializers.SerializerMethodField() - comment_count = serializers.IntegerField(source="comments_count", read_only=True) - unread_comment_count = serializers.IntegerField(source="unread_comments_count", read_only=True) + comment_count = serializers.SerializerMethodField(read_only=True) + unread_comment_count = serializers.SerializerMethodField(read_only=True) comment_list_url = serializers.SerializerMethodField() endorsed_comment_list_url = serializers.SerializerMethodField() non_endorsed_comment_list_url = serializers.SerializerMethodField() @@ -252,6 +252,18 @@ class ThreadSerializer(_ContentSerializer): """Returns the URL to retrieve the thread's non-endorsed comments.""" return self.get_comment_list_url(obj, endorsed=False) + def get_comment_count(self, obj): + """Increments comment count to include post and returns total count of + contributions (i.e. post + responses + comments) for the thread""" + return obj["comments_count"] + 1 + + def get_unread_comment_count(self, obj): + """Increments comment count to include post if thread is unread and returns + total count of unread contributions (i.e. post + responses + comments) for the thread""" + if not obj["read"]: + return obj["unread_comments_count"] + 1 + return obj["unread_comments_count"] + def create(self, validated_data): thread = Thread(user_id=self.context["cc_requester"]["id"], **validated_data) thread.save() diff --git a/lms/djangoapps/discussion_api/tests/test_api.py b/lms/djangoapps/discussion_api/tests/test_api.py index c07e5abfac..62e6c4306a 100644 --- a/lms/djangoapps/discussion_api/tests/test_api.py +++ b/lms/djangoapps/discussion_api/tests/test_api.py @@ -644,7 +644,7 @@ class GetThreadListTest(CommentsServiceMockMixin, UrlResetMixin, SharedModuleSto "abuse_flagged": False, "voted": False, "vote_count": 4, - "comment_count": 5, + "comment_count": 6, "unread_comment_count": 3, "comment_list_url": "http://testserver/api/discussion/v1/comments/?thread_id=test_thread_id_0", "endorsed_comment_list_url": None, @@ -673,8 +673,8 @@ class GetThreadListTest(CommentsServiceMockMixin, UrlResetMixin, SharedModuleSto "abuse_flagged": False, "voted": False, "vote_count": 9, - "comment_count": 18, - "unread_comment_count": 0, + "comment_count": 19, + "unread_comment_count": 1, "comment_list_url": None, "endorsed_comment_list_url": ( "http://testserver/api/discussion/v1/comments/?thread_id=test_thread_id_1&endorsed=True" @@ -1397,8 +1397,8 @@ class CreateThreadTest( "abuse_flagged": False, "voted": False, "vote_count": 0, - "comment_count": 0, - "unread_comment_count": 0, + "comment_count": 1, + "unread_comment_count": 1, "comment_list_url": "http://testserver/api/discussion/v1/comments/?thread_id=test_id", "endorsed_comment_list_url": None, "non_endorsed_comment_list_url": None, @@ -1943,7 +1943,7 @@ class UpdateThreadTest( "abuse_flagged": False, "voted": False, "vote_count": 0, - "comment_count": 0, + "comment_count": 1, "unread_comment_count": 0, "comment_list_url": "http://testserver/api/discussion/v1/comments/?thread_id=test_thread", "endorsed_comment_list_url": None, @@ -2999,8 +2999,8 @@ class RetrieveThreadTest( "pinned": False, "closed": False, "following": False, - "comment_count": 0, - "unread_comment_count": 0, + "comment_count": 1, + "unread_comment_count": 1, "comment_list_url": "http://testserver/api/discussion/v1/comments/?thread_id=test_thread", "endorsed_comment_list_url": None, "non_endorsed_comment_list_url": None, @@ -3039,8 +3039,8 @@ class RetrieveThreadTest( "pinned": False, "closed": False, "following": False, - "comment_count": 0, - "unread_comment_count": 0, + "comment_count": 1, + "unread_comment_count": 1, "comment_list_url": "http://testserver/api/discussion/v1/comments/?thread_id=test_thread", "endorsed_comment_list_url": None, "non_endorsed_comment_list_url": None, diff --git a/lms/djangoapps/discussion_api/tests/test_serializers.py b/lms/djangoapps/discussion_api/tests/test_serializers.py index ae2c53e7b4..2ffe52317d 100644 --- a/lms/djangoapps/discussion_api/tests/test_serializers.py +++ b/lms/djangoapps/discussion_api/tests/test_serializers.py @@ -203,8 +203,8 @@ class ThreadSerializerSerializationTest(SerializerTestMixin, SharedModuleStoreTe "abuse_flagged": False, "voted": False, "vote_count": 4, - "comment_count": 5, - "unread_comment_count": 3, + "comment_count": 6, + "unread_comment_count": 4, "comment_list_url": "http://testserver/api/discussion/v1/comments/?thread_id=test_thread", "endorsed_comment_list_url": None, "non_endorsed_comment_list_url": None, diff --git a/lms/djangoapps/discussion_api/tests/test_views.py b/lms/djangoapps/discussion_api/tests/test_views.py index 536d86ad2c..fdd3309601 100644 --- a/lms/djangoapps/discussion_api/tests/test_views.py +++ b/lms/djangoapps/discussion_api/tests/test_views.py @@ -296,8 +296,8 @@ class ThreadViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): "abuse_flagged": False, "voted": True, "vote_count": 4, - "comment_count": 5, - "unread_comment_count": 3, + "comment_count": 6, + "unread_comment_count": 4, "comment_list_url": "http://testserver/api/discussion/v1/comments/?thread_id=test_thread", "endorsed_comment_list_url": None, "non_endorsed_comment_list_url": None, @@ -519,8 +519,8 @@ class ThreadViewSetCreateTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): "abuse_flagged": False, "voted": False, "vote_count": 0, - "comment_count": 0, - "unread_comment_count": 0, + "comment_count": 1, + "unread_comment_count": 1, "comment_list_url": "http://testserver/api/discussion/v1/comments/?thread_id=test_thread", "endorsed_comment_list_url": None, "non_endorsed_comment_list_url": None, @@ -634,6 +634,7 @@ class ThreadViewSetPartialUpdateTest(DiscussionAPIViewTestMixin, ModuleStoreTest ], "created_at": "Test Created Date", "updated_at": "Test Updated Date", + "comment_count": 1, }) ) self.assertEqual( @@ -684,6 +685,8 @@ class ThreadViewSetPartialUpdateTest(DiscussionAPIViewTestMixin, ModuleStoreTest "closed": True, "abuse_flagged": value, "editable_fields": ["abuse_flagged", "read"], + "comment_count": 1, + "unread_comment_count": 1, }) ) @@ -1154,8 +1157,8 @@ class ThreadViewSetRetrieveTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase) "pinned": False, "closed": False, "following": False, - "comment_count": 0, - "unread_comment_count": 0, + "comment_count": 1, + "unread_comment_count": 1, "comment_list_url": "http://testserver/api/discussion/v1/comments/?thread_id=test_thread", "endorsed_comment_list_url": None, "non_endorsed_comment_list_url": None,