2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Take `creationDate` as a param because comment import (from Disqus, for
|
|
|
|
// example) will require a custom time.
|
|
|
|
func commentNew(commenterHex string, domain string, path string, parentHex string, markdown string, state string, creationDate time.Time) (string, error) {
|
|
|
|
// path is allowed to be empty
|
|
|
|
if commenterHex == "" || domain == "" || parentHex == "" || markdown == "" || state == "" {
|
|
|
|
return "", errorMissingField
|
|
|
|
}
|
|
|
|
|
2018-07-05 13:06:52 +08:00
|
|
|
p, err := pageGet(domain, path)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot get page attributes: %v", err)
|
|
|
|
return "", errorInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.IsLocked {
|
|
|
|
return "", errorThreadLocked
|
|
|
|
}
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
commentHex, err := randomHex(32)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
html := markdownToHtml(markdown)
|
|
|
|
|
2019-05-09 13:28:20 +08:00
|
|
|
if err = pageNew(domain, path); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
statement := `
|
|
|
|
INSERT INTO
|
|
|
|
comments (commentHex, domain, path, commenterHex, parentHex, markdown, html, creationDate, state)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9 );
|
|
|
|
`
|
|
|
|
_, err = db.Exec(statement, commentHex, domain, path, commenterHex, parentHex, markdown, html, creationDate, state)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot insert comment: %v", err)
|
|
|
|
return "", errorInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
return commentHex, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func commentNewHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
type request struct {
|
2018-06-20 11:29:55 +08:00
|
|
|
CommenterToken *string `json:"commenterToken"`
|
2018-06-20 11:50:11 +08:00
|
|
|
Domain *string `json:"domain"`
|
|
|
|
Path *string `json:"path"`
|
|
|
|
ParentHex *string `json:"parentHex"`
|
|
|
|
Markdown *string `json:"markdown"`
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var x request
|
2018-07-24 14:58:43 +08:00
|
|
|
if err := bodyUnmarshal(r, &x); err != nil {
|
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
2018-05-27 22:40:42 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:00:45 +08:00
|
|
|
domain := domainStrip(*x.Domain)
|
2018-05-27 22:40:42 +08:00
|
|
|
path := *x.Path
|
|
|
|
|
|
|
|
d, err := domainGet(domain)
|
|
|
|
if err != nil {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
2018-05-27 22:40:42 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.State == "frozen" {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": errorDomainFrozen.Error()})
|
2018-05-27 22:40:42 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-19 00:07:16 +08:00
|
|
|
if d.RequireIdentification && *x.CommenterToken == "anonymous" {
|
|
|
|
bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
// logic: (empty column indicates the value doesn't matter)
|
2019-01-23 13:26:22 +08:00
|
|
|
// | anonymous | moderator | requireIdentification | requireModeration | moderateAllAnonymous | approved? |
|
|
|
|
// |-----------+-----------+-----------------------+-------------------+----------------------+-----------|
|
|
|
|
// | yes | | | | no | yes |
|
|
|
|
// | yes | | | | yes | no |
|
|
|
|
// | no | yes | | | | yes |
|
|
|
|
// | no | no | | yes | | yes |
|
|
|
|
// | no | no | | no | | no |
|
2018-05-27 22:40:42 +08:00
|
|
|
|
|
|
|
var commenterHex string
|
|
|
|
var state string
|
|
|
|
|
2018-06-20 11:29:55 +08:00
|
|
|
if *x.CommenterToken == "anonymous" {
|
2018-05-27 22:40:42 +08:00
|
|
|
commenterHex = "anonymous"
|
2018-12-20 11:57:02 +08:00
|
|
|
if isSpam(*x.Domain, getIp(r), getUserAgent(r), "Anonymous", "", "", *x.Markdown) {
|
|
|
|
state = "flagged"
|
|
|
|
} else {
|
2019-02-19 00:05:34 +08:00
|
|
|
if d.ModerateAllAnonymous || d.RequireModeration {
|
2019-01-23 13:26:22 +08:00
|
|
|
state = "unapproved"
|
|
|
|
} else {
|
|
|
|
state = "approved"
|
|
|
|
}
|
2018-12-20 11:57:02 +08:00
|
|
|
}
|
2018-05-27 22:40:42 +08:00
|
|
|
} else {
|
2018-06-20 11:29:55 +08:00
|
|
|
c, err := commenterGetByCommenterToken(*x.CommenterToken)
|
2018-05-27 22:40:42 +08:00
|
|
|
if err != nil {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
2018-05-27 22:40:42 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// cheaper than a SQL query as we already have this information
|
|
|
|
isModerator := false
|
|
|
|
for _, mod := range d.Moderators {
|
|
|
|
if mod.Email == c.Email {
|
|
|
|
isModerator = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
commenterHex = c.CommenterHex
|
|
|
|
|
|
|
|
if isModerator {
|
|
|
|
state = "approved"
|
|
|
|
} else {
|
2018-12-20 11:57:02 +08:00
|
|
|
if isSpam(*x.Domain, getIp(r), getUserAgent(r), c.Name, c.Email, c.Link, *x.Markdown) {
|
|
|
|
state = "flagged"
|
2018-05-27 22:40:42 +08:00
|
|
|
} else {
|
2018-12-20 11:57:02 +08:00
|
|
|
if d.RequireModeration {
|
|
|
|
state = "unapproved"
|
|
|
|
} else {
|
|
|
|
state = "approved"
|
|
|
|
}
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
commentHex, err := commentNew(commenterHex, domain, path, *x.ParentHex, *x.Markdown, state, time.Now().UTC())
|
|
|
|
if err != nil {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
2018-05-27 22:40:42 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-19 00:23:44 +08:00
|
|
|
// TODO: reuse html in commentNew and do only one markdown to HTML conversion?
|
|
|
|
html := markdownToHtml(*x.Markdown)
|
|
|
|
|
|
|
|
bodyMarshal(w, response{"success": true, "commentHex": commentHex, "state": state, "html": html})
|
|
|
|
if smtpConfigured {
|
2019-10-25 16:04:27 +08:00
|
|
|
go emailNotificationNew(d, path, commenterHex, commentHex, html, *x.ParentHex, state)
|
2019-02-19 00:23:44 +08:00
|
|
|
}
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|