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,60 +82,36 @@ 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 if d.RequireModeration {
} else { state = "unapproved"
if isSpam(*x.Domain, getIp(r), getUserAgent(r), c.Name, c.Email, c.Link, *x.Markdown) { } else if commenterHex == "anonymous" && d.ModerateAllAnonymous {
state = "flagged" state = "unapproved"
} else { } else if d.AutoSpamFilter && isSpam(*x.Domain, getIp(r), getUserAgent(r), commenterName, commenterEmail, commenterLink, *x.Markdown) {
if d.RequireModeration { state = "flagged"
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())