-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_blog_article_comment.go
More file actions
126 lines (99 loc) · 3.95 KB
/
edit_blog_article_comment.go
File metadata and controls
126 lines (99 loc) · 3.95 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
package endpoints
import (
"fantlab/core/converters"
"fantlab/core/db"
"fantlab/core/helpers"
"fantlab/pb"
"net/http"
"strconv"
"google.golang.org/protobuf/proto"
)
func (api *API) EditBlogArticleComment(r *http.Request) (int, proto.Message) {
var params struct {
// id комментария
CommentId uint64 `http:"id,path"`
// текст комментария (непустой)
Comment string `http:"comment,form"`
}
api.bindParams(¶ms, r)
if params.CommentId == 0 {
return api.badParam("id")
}
comment, err := api.services.DB().FetchBlogTopicComment(r.Context(), params.CommentId)
if err != nil {
if db.IsNotFoundError(err) {
return http.StatusNotFound, &pb.Error_Response{
Status: pb.Error_NOT_FOUND,
Context: strconv.FormatUint(params.CommentId, 10),
}
}
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
article, err := api.services.DB().FetchBlogTopic(r.Context(), comment.TopicId)
if err != nil {
if db.IsNotFoundError(err) {
return http.StatusNotFound, &pb.Error_Response{
Status: pb.Error_NOT_FOUND,
Context: "Статья не существует",
}
}
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
blog, err := api.services.DB().FetchBlog(r.Context(), article.BlogId)
if err != nil {
if db.IsNotFoundError(err) {
return http.StatusNotFound, &pb.Error_Response{
Status: pb.Error_NOT_FOUND,
Context: "Блог не существует",
}
}
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
if blog.IsClose == 1 {
return http.StatusForbidden, &pb.Error_Response{
Status: pb.Error_ACTION_FORBIDDEN,
Context: "Блог закрыт",
}
}
userId := api.getUserId(r)
// NOTE Пропущен весь хардкод касательно id отдельных юзеров, обработка is_referee (заданы в Auth.pm) и
// can_link_blogarticle_to_work (из main.cfg). Все они считаются модераторами любых блогов.
userIsCommunityModerator, err := api.services.DB().FetchUserIsCommunityModerator(r.Context(), userId, blog.BlogId, article.TopicId)
if err != nil {
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
// В отличие от форума, нет ограничения на время редактирования сообщения
if !(comment.UserId == userId || blog.UserId == userId || userIsCommunityModerator) {
return http.StatusForbidden, &pb.Error_Response{
Status: pb.Error_ACTION_FORBIDDEN,
Context: "Вы не можете отредактировать данный комментарий",
}
}
// В отличие от форума, здесь нет предварительного форматирования. Это позволяет не только навтыкать массу пробельных
// символов (мелочь), но и написать модераторское сообщение, будучи самым обычным пользователем (достаточно заключить
// текст в теги `moder`). https://github.com/parserpro/fantlab/issues/976
commentLength := uint64(len(params.Comment))
if commentLength == 0 {
return http.StatusForbidden, &pb.Error_Response{
Status: pb.Error_ACTION_FORBIDDEN,
Context: "Текст комментария пустой",
}
}
dbComment, err := api.services.DB().UpdateBlogTopicComment(r.Context(), comment.MessageId, params.Comment)
if err != nil {
return http.StatusInternalServerError, &pb.Error_Response{
Status: pb.Error_SOMETHING_WENT_WRONG,
}
}
helpers.DeleteBlogCommentTextCache(comment.MessageId)
commentResponse := converters.GetBlogArticleComment(dbComment, api.services.AppConfig())
return http.StatusOK, commentResponse
}