comment_new.go: simplify state logic

Signed-off-by: Adhityaa Chandrasekar <adtac@adtac.in>
This commit is contained in:
Adhityaa Chandrasekar 2021-02-28 13:43:58 +05:30
parent 18612933f6
commit fc83eed221

View File

@ -82,61 +82,37 @@ func commentNewHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
// logic: (empty column indicates the value doesn't matter) var commenterHex, commenterName, commenterEmail, commenterLink string
// | anonymous | moderator | requireIdentification | requireModeration | moderateAllAnonymous | approved? | var isModerator bool
// |-----------+-----------+-----------------------+-------------------+----------------------+-----------|
// | yes | | | | no | yes |
// | yes | | | | yes | no |
// | no | yes | | | | yes |
// | no | no | | yes | | yes |
// | no | no | | no | | no |
var commenterHex string
var state string
if *x.CommenterToken == "anonymous" { if *x.CommenterToken == "anonymous" {
commenterHex = "anonymous" commenterHex, commenterName, commenterEmail, commenterLink = "anonymous", "Anonymous", "", ""
if isSpam(*x.Domain, getIp(r), getUserAgent(r), "Anonymous", "", "", *x.Markdown) {
state = "flagged"
} else {
if d.ModerateAllAnonymous || d.RequireModeration {
state = "unapproved"
} else {
state = "approved"
}
}
} else { } else {
c, err := commenterGetByCommenterToken(*x.CommenterToken) c, err := commenterGetByCommenterToken(*x.CommenterToken)
if err != nil { if err != nil {
bodyMarshal(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
commenterHex, commenterName, commenterEmail, commenterLink = c.CommenterHex, c.Name, c.Email, c.Link
// cheaper than a SQL query as we already have this information
isModerator := false
for _, mod := range d.Moderators { for _, mod := range d.Moderators {
if mod.Email == c.Email { if mod.Email == c.Email {
isModerator = true isModerator = true
break break
} }
} }
}
commenterHex = c.CommenterHex var state string
if isModerator { if isModerator {
state = "approved" state = "approved"
} else { } else if d.RequireModeration {
if isSpam(*x.Domain, getIp(r), getUserAgent(r), c.Name, c.Email, c.Link, *x.Markdown) { state = "unapproved"
} else if commenterHex == "anonymous" && d.ModerateAllAnonymous {
state = "unapproved"
} else if d.AutoSpamFilter && isSpam(*x.Domain, getIp(r), getUserAgent(r), commenterName, commenterEmail, commenterLink, *x.Markdown) {
state = "flagged" state = "flagged"
} else {
if d.RequireModeration {
state = "unapproved"
} else { } else {
state = "approved" state = "approved"
} }
}
}
}
commentHex, err := commentNew(commenterHex, domain, path, *x.ParentHex, *x.Markdown, state, time.Now().UTC()) commentHex, err := commentNew(commenterHex, domain, path, *x.ParentHex, *x.Markdown, state, time.Now().UTC())
if err != nil { if err != nil {