Skip to content

feat: Check HTML comments in question, answer or comments #1202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2024
Merged
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
6 changes: 6 additions & 0 deletions i18n/en_US.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,17 @@ backend:
other: No permission to update.
question_closed_cannot_add:
other: Questions are closed and cannot be added.
content_cannot_empty:
other: Answer content cannot be empty.
comment:
edit_without_permission:
other: Comment are not allowed to edit.
not_found:
other: Comment not found.
cannot_edit_after_deadline:
other: The comment time has been too long to modify.
content_cannot_empty:
other: Comment content cannot be empty.
email:
duplicate:
other: Email already exists.
Expand Down Expand Up @@ -225,6 +229,8 @@ backend:
other: No permission to close.
cannot_update:
other: No permission to update.
content_cannot_empty:
other: Content cannot be empty.
rank:
fail_to_meet_the_condition:
other: Reputation rank fail to meet the condition.
Expand Down
6 changes: 6 additions & 0 deletions i18n/zh_CN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,17 @@ backend:
other: 没有更新权限。
question_closed_cannot_add:
other: 问题已关闭,无法添加。
content_cannot_empty:
other: 回答内容不能为空。
comment:
edit_without_permission:
other: 不允许编辑评论。
not_found:
other: 评论未找到。
cannot_edit_after_deadline:
other: 评论时间太久,无法修改。
content_cannot_empty:
other: 评论内容不能为空。
email:
duplicate:
other: 邮箱已存在。
Expand Down Expand Up @@ -224,6 +228,8 @@ backend:
other: 没有关闭权限。
cannot_update:
other: 没有更新权限。
content_cannot_empty:
other: 内容不能为空。
rank:
fail_to_meet_the_condition:
other: 声望值未达到要求。
Expand Down
3 changes: 3 additions & 0 deletions internal/base/reason/reason.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ const (
QuestionCannotUpdate = "error.question.cannot_update"
QuestionAlreadyDeleted = "error.question.already_deleted"
QuestionUnderReview = "error.question.under_review"
QuestionContentCannotEmpty = "error.question.content_cannot_empty"
AnswerNotFound = "error.answer.not_found"
AnswerCannotDeleted = "error.answer.cannot_deleted"
AnswerCannotUpdate = "error.answer.cannot_update"
AnswerCannotAddByClosedQuestion = "error.answer.question_closed_cannot_add"
AnswerRestrictAnswer = "error.answer.restrict_answer"
AnswerContentCannotEmpty = "error.answer.content_cannot_empty"
CommentEditWithoutPermission = "error.comment.edit_without_permission"
CommentContentCannotEmpty = "error.comment.content_cannot_empty"
DisallowVote = "error.object.disallow_vote"
DisallowFollow = "error.object.disallow_follow"
DisallowVoteYourSelf = "error.object.disallow_vote_your_self"
Expand Down
14 changes: 14 additions & 0 deletions internal/schema/answer_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
package schema

import (
"github.com/apache/incubator-answer/internal/base/reason"
"github.com/apache/incubator-answer/internal/base/validator"
"github.com/apache/incubator-answer/pkg/converter"
"github.com/segmentfault/pacman/errors"
)

// RemoveAnswerReq delete answer request
Expand Down Expand Up @@ -60,6 +62,12 @@ type AnswerAddReq struct {

func (req *AnswerAddReq) Check() (errFields []*validator.FormErrorField, err error) {
req.HTML = converter.Markdown2HTML(req.Content)
if req.HTML == "" {
return append(errFields, &validator.FormErrorField{
ErrorField: "content",
ErrorMsg: reason.AnswerContentCannotEmpty,
}), errors.BadRequest(reason.AnswerContentCannotEmpty)
}
return nil, nil
}

Expand All @@ -79,6 +87,12 @@ type AnswerUpdateReq struct {

func (req *AnswerUpdateReq) Check() (errFields []*validator.FormErrorField, err error) {
req.HTML = converter.Markdown2HTML(req.Content)
if req.HTML == "" {
return append(errFields, &validator.FormErrorField{
ErrorField: "content",
ErrorMsg: reason.AnswerContentCannotEmpty,
}), errors.BadRequest(reason.AnswerContentCannotEmpty)
}
return nil, nil
}

Expand Down
14 changes: 14 additions & 0 deletions internal/schema/comment_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
package schema

import (
"github.com/apache/incubator-answer/internal/base/reason"
"github.com/apache/incubator-answer/internal/base/validator"
"github.com/apache/incubator-answer/internal/entity"
"github.com/apache/incubator-answer/pkg/converter"
"github.com/jinzhu/copier"
"github.com/segmentfault/pacman/errors"
)

// AddCommentReq add comment request
Expand Down Expand Up @@ -53,6 +55,12 @@ type AddCommentReq struct {

func (req *AddCommentReq) Check() (errFields []*validator.FormErrorField, err error) {
req.ParsedText = converter.Markdown2HTML(req.OriginalText)
if req.ParsedText == "" {
return append(errFields, &validator.FormErrorField{
ErrorField: "original_text",
ErrorMsg: reason.CommentContentCannotEmpty,
}), errors.BadRequest(reason.CommentContentCannotEmpty)
}
return nil, nil
}

Expand Down Expand Up @@ -88,6 +96,12 @@ type UpdateCommentReq struct {

func (req *UpdateCommentReq) Check() (errFields []*validator.FormErrorField, err error) {
req.ParsedText = converter.Markdown2HTML(req.OriginalText)
if req.ParsedText == "" {
return append(errFields, &validator.FormErrorField{
ErrorField: "original_text",
ErrorMsg: reason.CommentContentCannotEmpty,
}), errors.BadRequest(reason.CommentContentCannotEmpty)
}
return nil, nil
}

Expand Down
29 changes: 29 additions & 0 deletions internal/schema/question_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package schema

import (
"github.com/apache/incubator-answer/internal/base/reason"
"github.com/segmentfault/pacman/errors"
"strings"
"time"

Expand Down Expand Up @@ -97,6 +99,12 @@ func (req *QuestionAdd) Check() (errFields []*validator.FormErrorField, err erro
tag.ParsedText = converter.Markdown2HTML(tag.OriginalText)
}
}
if req.HTML == "" {
return append(errFields, &validator.FormErrorField{
ErrorField: "content",
ErrorMsg: reason.QuestionContentCannotEmpty,
}), errors.BadRequest(reason.QuestionContentCannotEmpty)
}
return nil, nil
}

Expand Down Expand Up @@ -129,6 +137,21 @@ func (req *QuestionAddByAnswer) Check() (errFields []*validator.FormErrorField,
tag.ParsedText = converter.Markdown2HTML(tag.OriginalText)
}
}
if req.HTML == "" {
errFields = append(errFields, &validator.FormErrorField{
ErrorField: "content",
ErrorMsg: reason.QuestionContentCannotEmpty,
})
}
if req.AnswerHTML == "" {
errFields = append(errFields, &validator.FormErrorField{
ErrorField: "answer_content",
ErrorMsg: reason.AnswerContentCannotEmpty,
})
}
if req.HTML == "" || req.AnswerHTML == "" {
return errFields, errors.BadRequest(reason.QuestionContentCannotEmpty)
}
return nil, nil
}

Expand Down Expand Up @@ -203,6 +226,12 @@ type QuestionUpdateInviteUser struct {

func (req *QuestionUpdate) Check() (errFields []*validator.FormErrorField, err error) {
req.HTML = converter.Markdown2HTML(req.Content)
if req.HTML == "" {
return append(errFields, &validator.FormErrorField{
ErrorField: "content",
ErrorMsg: reason.QuestionContentCannotEmpty,
}), errors.BadRequest(reason.QuestionContentCannotEmpty)
}
return nil, nil
}

Expand Down