diff --git a/lms/djangoapps/discussion_api/serializers.py b/lms/djangoapps/discussion_api/serializers.py index 7a53d1a713b3..ac71e89c8a9f 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 @@ def get_non_endorsed_comment_list_url(self, obj): """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 c07e5abfac66..62e6c4306ad6 100644 --- a/lms/djangoapps/discussion_api/tests/test_api.py +++ b/lms/djangoapps/discussion_api/tests/test_api.py @@ -644,7 +644,7 @@ def test_thread_content(self): "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 @@ def test_thread_content(self): "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 @@ def test_basic(self, mock_emit): "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 @@ def test_basic(self): "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 @@ def test_basic(self): "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 @@ def test_nonauthor_enrolled_in_course(self): "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 ae2c53e7b47b..2ffe52317da5 100644 --- a/lms/djangoapps/discussion_api/tests/test_serializers.py +++ b/lms/djangoapps/discussion_api/tests/test_serializers.py @@ -203,8 +203,8 @@ def test_basic(self): "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 536d86ad2cbc..fdd330960128 100644 --- a/lms/djangoapps/discussion_api/tests/test_views.py +++ b/lms/djangoapps/discussion_api/tests/test_views.py @@ -296,8 +296,8 @@ def test_basic(self): "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 @@ def test_basic(self): "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 @@ def test_basic(self): ], "created_at": "Test Created Date", "updated_at": "Test Updated Date", + "comment_count": 1, }) ) self.assertEqual( @@ -684,6 +685,8 @@ def test_closed_thread(self, field, value): "closed": True, "abuse_flagged": value, "editable_fields": ["abuse_flagged", "read"], + "comment_count": 1, + "unread_comment_count": 1, }) ) @@ -1154,8 +1157,8 @@ def test_basic(self): "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,