8
"github.com/go-chi/chi/v5"
9
"xelbot.com/reprogl/api/backend"
10
"xelbot.com/reprogl/api/telegram"
11
"xelbot.com/reprogl/container"
12
"xelbot.com/reprogl/models"
13
"xelbot.com/reprogl/models/repositories"
14
"xelbot.com/reprogl/session"
15
"xelbot.com/reprogl/views"
18
type addCommentResponse struct {
19
Valid bool `json:"valid"`
20
Errors []backend.FormError `json:"errors,omitempty"`
23
func AddCommentDummy(w http.ResponseWriter, r *http.Request) {
24
w.Header().Set("Content-Type", "text/plain")
25
w.Write([]byte("Silence is gold"))
28
func AddComment(app *container.Application) http.HandlerFunc {
29
return func(w http.ResponseWriter, r *http.Request) {
30
topicId, err := strconv.Atoi(r.PostFormValue("topicId"))
32
app.ClientError(w, http.StatusBadRequest)
36
parentId, err := strconv.Atoi(r.PostFormValue("parentId"))
38
app.ClientError(w, http.StatusBadRequest)
42
repo := repositories.ArticleRepository{DB: app.DB}
43
article, err := repo.GetByIdForComment(topicId)
45
if errors.Is(err, models.RecordNotFound) {
48
app.ServerError(w, err)
54
var commentator *backend.CommentatorDTO
55
var user *backend.CommentUserDTO
56
if identity, hasIdentity := session.GetIdentity(r.Context()); hasIdentity {
57
user = &backend.CommentUserDTO{
61
commentator = &backend.CommentatorDTO{
62
Name: r.PostFormValue("name"),
63
Email: r.PostFormValue("mail"),
64
Website: r.PostFormValue("website"),
68
commentData := backend.CommentDTO{
69
Commentator: commentator,
71
Text: r.PostFormValue("comment_text"),
74
UserAgent: r.UserAgent(),
75
IP: container.RealRemoteAddress(r),
79
statusCode := http.StatusCreated
81
apiResponse, err := backend.SendComment(commentData)
83
if errors.Is(err, backend.NotAllowedComment) {
84
statusCode = http.StatusOK
85
responseData = addCommentResponse{
87
Errors: []backend.FormError{
90
Message: "Добавление комментариев тут отключено 😐",
96
statusCode = http.StatusBadRequest
100
if apiResponse != nil {
101
result := addCommentResponse{
103
Errors: apiResponse.Violations,
106
if apiResponse.Violations != nil && len(apiResponse.Violations) > 0 {
109
if apiResponse.Comment != nil {
110
go afterCommentHook(app, apiResponse.Comment, article)
114
responseData = result
117
jsonResponse(w, statusCode, responseData)
121
func CommentsFragment(app *container.Application) http.HandlerFunc {
122
return func(w http.ResponseWriter, r *http.Request) {
124
articleId, err := strconv.Atoi(chi.URLParam(r, "article_id"))
126
app.ServerError(w, err)
131
repo := repositories.CommentRepository{DB: app.DB}
133
var comments models.CommentList
134
identity, found := session.GetIdentity(r.Context())
135
if found && identity.IsAdmin() {
136
comments, err = repo.GetCollectionWithExtraDataByArticleId(articleId)
138
comments, err = repo.GetCollectionByArticleId(articleId)
141
app.ServerError(w, err)
146
templateData := &views.FragmentCommentsData{
148
EnabledComments: chi.URLParam(r, "disabled_flag") == models.EnabledComments,
151
cacheControl(w, container.DefaultEsiTTL)
152
err = views.WriteTemplateWithContext(r.Context(), w, "comments.gohtml", templateData)
154
app.ServerError(w, err)
159
func afterCommentHook(
160
app *container.Application,
161
comment *backend.CreatedCommentDTO,
162
article *models.ArticleForComment,
164
var updatedComment *backend.CreatedCommentDTO
166
backend.PingGeolocation()
167
refreshedComment, err := backend.RefreshComment(comment.ID)
169
updatedComment = comment
171
updatedComment = refreshedComment
174
telegram.SendNotification(app, updatedComment, article)