-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathcomment.py
More file actions
279 lines (240 loc) · 10 KB
/
comment.py
File metadata and controls
279 lines (240 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import pytz
from django.db import models
from django.db.models import Q
from django.utils import timezone
from .node import Node
from .nodelog import NodeLog
from .base import GuidMixin, Guid, BaseModel
from .mixins import CommentableMixin
from .spam import SpamMixin
from .validators import CommentMaxLength, string_required
from osf.utils.fields import NonNaiveDateTimeField
from framework.exceptions import PermissionsError
from website import settings
from website.util import api_v2_url
from website.project import signals as project_signals
from website.project.model import get_valid_mentioned_users_guids
class Comment(GuidMixin, SpamMixin, CommentableMixin, BaseModel):
__guid_min_length__ = 12
OVERVIEW = 'node'
FILES = 'files'
WIKI = 'wiki'
SPAM_CHECK_FIELDS = {'content'}
user = models.ForeignKey('OSFUser', null=True, on_delete=models.CASCADE)
# the node that the comment belongs to
node = models.ForeignKey('AbstractNode', null=True, on_delete=models.CASCADE)
# The file or project overview page that the comment is for
root_target = models.ForeignKey(Guid, on_delete=models.SET_NULL,
related_name='comments',
null=True, blank=True)
# the direct 'parent' of the comment (e.g. the target of a comment reply is another comment)
target = models.ForeignKey(Guid, on_delete=models.SET_NULL,
related_name='child_comments',
null=True, blank=True)
edited = models.BooleanField(default=False)
is_deleted = models.BooleanField(default=False)
deleted = NonNaiveDateTimeField(blank=True, null=True)
# The type of root_target: node/files
page = models.CharField(max_length=255, blank=True)
content = models.TextField(
validators=[
CommentMaxLength(settings.COMMENT_MAXLENGTH),
string_required,
]
)
# The mentioned users
ever_mentioned = models.ManyToManyField(blank=True, related_name='mentioned_in', to='OSFUser')
@property
def url(self):
return f'/{self._id}/'
@property
def absolute_api_v2_url(self):
path = f'/comments/{self._id}/'
return api_v2_url(path)
@property
def target_type(self):
"""The object "type" used in the OSF v2 API."""
return 'comments'
@property
def root_target_page(self):
"""The page type associated with the object/Comment.root_target."""
return None
def belongs_to_node(self, node_id):
"""Check whether the comment is attached to the specified node."""
return self.node._id == node_id
# used by django and DRF
def get_absolute_url(self):
return self.absolute_api_v2_url
def get_comment_page_url(self):
if isinstance(self.root_target.referent, Node):
return self.node.absolute_url
return settings.DOMAIN + str(self.root_target._id) + '/'
def get_content(self, auth):
""" Returns the comment content if the user is allowed to see it. Deleted comments
can only be viewed by the user who created the comment."""
if not auth and not self.node.is_public:
raise PermissionsError
if self.is_deleted and ((not auth or auth.user.is_anonymous) or
(auth and not auth.user.is_anonymous and self.user._id != auth.user._id)):
return None
return self.content
def get_comment_page_title(self):
if self.page == Comment.FILES:
return self.root_target.referent.name
elif self.page == Comment.WIKI:
return self.root_target.referent.page_name
return ''
def get_comment_page_type(self):
if self.page == Comment.FILES:
return 'file'
elif self.page == Comment.WIKI:
return 'wiki'
return self.node.project_or_component
@classmethod
def find_n_unread(cls, user, node, page, root_id=None):
if node.is_contributor_or_group_member(user):
if page == Comment.OVERVIEW:
view_timestamp = user.get_node_comment_timestamps(target_id=node._id)
root_target = Guid.load(node._id)
elif page == Comment.FILES or page == Comment.WIKI:
view_timestamp = user.get_node_comment_timestamps(target_id=root_id)
root_target = Guid.load(root_id)
else:
raise ValueError('Invalid page')
if not view_timestamp.tzinfo:
view_timestamp = view_timestamp.replace(tzinfo=pytz.utc)
return cls.objects.filter(
Q(node=node) & ~Q(user=user) & Q(is_deleted=False) &
(Q(created__gt=view_timestamp) | Q(modified__gt=view_timestamp)) &
Q(root_target=root_target)
).count()
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)
if not comment.node.can_comment(auth):
raise PermissionsError(f'{auth.user!r} does not have permission to comment on this node')
log_dict = {
'project': comment.node.parent_id,
'node': comment.node._id,
'user': comment.user._id,
'comment': comment._id,
}
if isinstance(comment.target.referent, Comment):
comment.root_target = comment.target.referent.root_target
else:
comment.root_target = comment.target
page = getattr(comment.root_target.referent, 'root_target_page', None)
if not page:
raise ValueError('Invalid root target.')
comment.page = page
log_dict.update(comment.root_target.referent.get_extra_log_params(comment))
new_mentions = []
if comment.content:
if not comment.id:
# must have id before accessing M2M
comment.save()
new_mentions = get_valid_mentioned_users_guids(comment, comment.node.contributors_and_group_members)
if new_mentions:
project_signals.mention_added.send(comment, new_mentions=new_mentions, auth=auth)
comment.ever_mentioned.add(*comment.node.contributors.filter(guids___id__in=new_mentions))
comment.save()
comment.node.add_log(
NodeLog.COMMENT_ADDED,
log_dict,
auth=auth,
save=False,
)
comment.node.save()
project_signals.comment_added.send(comment, auth=auth, new_mentions=new_mentions)
return comment
def edit(self, content, auth, save=False):
if not self.node.can_comment(auth) or self.user._id != auth.user._id:
raise PermissionsError(f'{auth.user!r} does not have permission to edit this comment')
log_dict = {
'project': self.node.parent_id,
'node': self.node._id,
'user': self.user._id,
'comment': self._id,
}
log_dict.update(self.root_target.referent.get_extra_log_params(self))
self.content = content
self.edited = True
self.modified = timezone.now()
new_mentions = get_valid_mentioned_users_guids(self, self.node.contributors_and_group_members)
if save:
if new_mentions:
project_signals.mention_added.send(self, new_mentions=new_mentions, auth=auth)
self.ever_mentioned.add(*self.node.contributors.filter(guids___id__in=new_mentions))
self.save()
self.node.add_log(
NodeLog.COMMENT_UPDATED,
log_dict,
auth=auth,
save=False,
)
self.node.save()
def delete(self, auth, save=False):
if not self.node.can_comment(auth) or self.user._id != auth.user._id:
raise PermissionsError(f'{auth.user!r} does not have permission to comment on this node')
log_dict = {
'project': self.node.parent_id,
'node': self.node._id,
'user': self.user._id,
'comment': self._id,
}
self.is_deleted = True
current_time = timezone.now()
self.deleted = current_time
log_dict.update(self.root_target.referent.get_extra_log_params(self))
self.modified = current_time
if save:
self.save()
self.node.add_log(
NodeLog.COMMENT_REMOVED,
log_dict,
auth=auth,
save=False,
)
self.node.save()
def undelete(self, auth, save=False):
if not self.node.can_comment(auth) or self.user._id != auth.user._id:
raise PermissionsError(f'{auth.user!r} does not have permission to comment on this node')
self.is_deleted = False
self.deleted = None
log_dict = {
'project': self.node.parent_id,
'node': self.node._id,
'user': self.user._id,
'comment': self._id,
}
log_dict.update(self.root_target.referent.get_extra_log_params(self))
self.modified = timezone.now()
if save:
self.save()
self.node.add_log(
NodeLog.COMMENT_RESTORED,
log_dict,
auth=auth,
save=False,
)
self.node.save()
def _get_spam_content(self, *unused_args, **unused_kwargs):
'''For compatibility with the domain extraction management command.'''
content = [getattr(self, field, []) for field in self.SPAM_CHECK_FIELDS]
if not content:
return None
return ' '.join(content)