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
5 changes: 4 additions & 1 deletion api/files/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class BaseFileSerializer(JSONAPISerializer):
FileRelationshipField(
related_view='nodes:node-comments',
related_view_kwargs={'node_id': '<target._id>'},
related_meta={'unread': 'get_unread_comments_count'},
related_meta={'unread': 'get_unread_comments_count', 'comment_count': 'get_comments_count'},
filter={'target': 'get_file_guid'},
),
)
Expand Down Expand Up @@ -337,6 +337,9 @@ def get_unread_comments_count(self, obj):
return 0
return Comment.find_n_unread(user=user, node=obj.target, page='files', root_id=obj.get_guid()._id)

def get_comments_count(self, obj):
return Comment.find_count(node=obj.target, page='files', root_id=obj.get_guid()._id)

def user_id(self, obj):
# NOTE: obj is the user here, the meta field for
# Hyperlinks is weird
Expand Down
9 changes: 8 additions & 1 deletion api/nodes/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ class NodeSerializer(TaxonomizableSerializerMixin, JSONAPISerializer):
comments = RelationshipField(
related_view='nodes:node-comments',
related_view_kwargs={'node_id': '<_id>'},
related_meta={'unread': 'get_unread_comments_count'},
related_meta={'unread': 'get_unread_comments_count', 'comment_count': 'get_comments_count'},
filter={'target': '<_id>'},
)

Expand Down Expand Up @@ -724,6 +724,13 @@ def get_unread_comments_count(self, obj):
'node': node_comments,
}

def get_comments_count(self, obj):
node_comment_count = Comment.find_count(node=obj, page='node')

return {
'node_comment_count': node_comment_count,
}

def get_region_id(self, obj):
try:
# use the annotated value if possible
Expand Down
3 changes: 2 additions & 1 deletion api/registrations/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ class RegistrationSerializer(NodeSerializer):
related_meta={
'unread': 'get_unread_comments_count',
'count': 'get_total_comments_count',
'comment_count': 'get_comments_count',
},
filter={'target': '<_id>'},
),
Expand Down Expand Up @@ -948,7 +949,7 @@ class RegistrationFileSerializer(OsfStorageFileSerializer):
comments = FileRelationshipField(
related_view='registrations:registration-comments',
related_view_kwargs={'node_id': '<target._id>'},
related_meta={'unread': 'get_unread_comments_count'},
related_meta={'unread': 'get_unread_comments_count', 'comment_count': 'get_comments_count'},
filter={'target': 'get_file_guid'},
)

Expand Down
4 changes: 2 additions & 2 deletions api/wikis/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class NodeWikiSerializer(WikiSerializer):
comments = RelationshipField(
related_view='nodes:node-comments',
related_view_kwargs={'node_id': '<node._id>'},
related_meta={'unread': 'get_unread_comments_count'},
related_meta={'unread': 'get_unread_comments_count', 'comment_count': 'get_comments_count'},
filter={'target': '<_id>'},
)

Expand Down Expand Up @@ -158,7 +158,7 @@ class RegistrationWikiSerializer(WikiSerializer):
comments = RelationshipField(
related_view='registrations:registration-comments',
related_view_kwargs={'node_id': '<node._id>'},
related_meta={'unread': 'get_unread_comments_count'},
related_meta={'unread': 'get_unread_comments_count', 'comment_count': 'get_comments_count'},
filter={'target': '<_id>'},
)

Expand Down
15 changes: 15 additions & 0 deletions osf/models/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ def find_n_unread(cls, user, node, page, root_id=None):

return 0

@classmethod
def find_count(cls, node, page, root_id=None):
if page == Comment.OVERVIEW:
root_target = Guid.load(node._id)
elif page == Comment.FILES or page == Comment.WIKI:
root_target = Guid.load(root_id)
else:
raise ValueError('Invalid page')

return cls.objects.filter(
Q(node=node) &
Q(is_deleted=False) &
Q(root_target=root_target)
).count()

@classmethod
def create(cls, auth, **kwargs):
comment = cls(**kwargs)
Expand Down
31 changes: 31 additions & 0 deletions osf_tests/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,37 @@ def test_find_unread_does_not_include_deleted_comments(self):
n_unread = Comment.find_n_unread(user=user, node=project, page='node')
assert n_unread == 0

#Comment count tests
def test_find_count_is_zero_when_no_comments(self):
n_count = Comment.find_count(node=ProjectFactory(), page='node')
assert n_count == 0

def test_find_count_new_comments(self):
project = ProjectFactory()
user = UserFactory()
project.add_contributor(user, save=True)
CommentFactory(node=project, user=project.creator)
n_count = Comment.find_count(node=project, page='node')
assert n_count == 1

def test_find_count_includes_comment_replies(self):
project = ProjectFactory()
user = UserFactory()
project.add_contributor(user, save=True)
comment = CommentFactory(node=project, user=user)
CommentFactory(node=project, target=Guid.load(comment._id), user=project.creator)
n_count = Comment.find_count(node=project, page='node')
assert n_count == 1

def test_find_count_does_not_include_deleted_comments(self):
project = ProjectFactory()
user = AuthUserFactory()
project.add_contributor(user)
project.save()
CommentFactory(node=project, user=project.creator, is_deleted=True)
n_count = Comment.find_count(node=project, page='node')
assert n_count == 0


# copied from tests/test_comments.py
class FileCommentMoveRenameTestMixin:
Expand Down
6 changes: 5 additions & 1 deletion website/static/css/commentpane.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@

.comment-handle-icon {
padding-top: 2px;
color: #428bca;
color: #333333;
cursor: pointer;
position: relative;
}

.comment-handle-icon.has-comments {
color: #428bca;
}

.cp-bar {
width: 1px; /* Note: Should be the same as .cp-sidebar:margin-left */
top: 88px;
Expand Down
12 changes: 12 additions & 0 deletions website/static/js/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ BaseComment.prototype.fetchNext = function(url, comments, setUnread) {
self.$root.unreadComments(response.links.meta.unread);
setUnread = false;
}
if (response.links.meta.comment_count) {
self.$root.totalComments(response.links.meta.comment_count);
}
comments.forEach(function(comment) {
self.comments.push(
new CommentModel(comment, self, self.$root)
Expand Down Expand Up @@ -780,6 +783,15 @@ var CommentListModel = function(options) {
}
});

self.totalComments = ko.observable(0);
self.hasComments = ko.pureComputed(function() {
if (self.totalComments() !== 0) {
return true;
} else {
return false;
}
});

/* Removes number of unread comments from tab when comments pane is opened */
self.removeCount = function() {
self.unreadComments(0);
Expand Down
3 changes: 2 additions & 1 deletion website/templates/include/comment_pane_template.mako
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
<span data-bind="if: unreadComments() !== 0">
<span data-bind="text: displayCount" class="badge unread-comments-count"></span>
</span>
<i class="fa fa-comments-o fa-2x comment-handle-icon"></i>
<i class="fa fa-comments-o fa-2x comment-handle-icon"
data-bind="css: { 'has-comments': hasComments }"></i>
</div>
<div class="cp-bar"></div>

Expand Down