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
|
import datetime
from szurubooru import search
from szurubooru.api.base_api import BaseApi
from szurubooru.func import auth, comments, posts, scores, util
def _serialize(ctx, comment, **kwargs):
return comments.serialize_comment(
comment,
ctx.user,
options=util.get_serialization_options(ctx), **kwargs)
class CommentListApi(BaseApi):
def __init__(self):
super().__init__()
self._search_executor = search.Executor(
search.configs.CommentSearchConfig())
def get(self, ctx):
auth.verify_privilege(ctx.user, 'comments:list')
return self._search_executor.execute_and_serialize(
ctx,
lambda comment: _serialize(ctx, comment))
def post(self, ctx):
auth.verify_privilege(ctx.user, 'comments:create')
text = ctx.get_param_as_string('text', required=True)
post_id = ctx.get_param_as_int('postId', required=True)
post = posts.get_post_by_id(post_id)
comment = comments.create_comment(ctx.user, post, text)
ctx.session.add(comment)
ctx.session.commit()
return _serialize(ctx, comment)
class CommentDetailApi(BaseApi):
def get(self, ctx, comment_id):
auth.verify_privilege(ctx.user, 'comments:view')
comment = comments.get_comment_by_id(comment_id)
return _serialize(ctx, comment)
def put(self, ctx, comment_id):
comment = comments.get_comment_by_id(comment_id)
infix = 'own' if ctx.user.user_id == comment.user_id else 'any'
text = ctx.get_param_as_string('text', required=True)
auth.verify_privilege(ctx.user, 'comments:edit:%s' % infix)
comment.last_edit_time = datetime.datetime.utcnow()
comments.update_comment_text(comment, text)
ctx.session.commit()
return _serialize(ctx, comment)
def delete(self, ctx, comment_id):
comment = comments.get_comment_by_id(comment_id)
infix = 'own' if ctx.user.user_id == comment.user_id else 'any'
auth.verify_privilege(ctx.user, 'comments:delete:%s' % infix)
ctx.session.delete(comment)
ctx.session.commit()
return {}
class CommentScoreApi(BaseApi):
def put(self, ctx, comment_id):
auth.verify_privilege(ctx.user, 'comments:score')
score = ctx.get_param_as_int('score', required=True)
comment = comments.get_comment_by_id(comment_id)
scores.set_score(comment, ctx.user, score)
ctx.session.commit()
return _serialize(ctx, comment)
def delete(self, ctx, comment_id):
auth.verify_privilege(ctx.user, 'comments:score')
comment = comments.get_comment_by_id(comment_id)
scores.delete_score(comment, ctx.user)
ctx.session.commit()
return _serialize(ctx, comment)
|