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
|
|
|
|
}
|
|
|
|
|
2021-02-28 16:13:58 +08:00
|
|
|
var commenterHex, commenterName, commenterEmail, commenterLink string
|
|
|
|
var isModerator bool
|
2018-06-20 11:29:55 +08:00
|
|
|
if *x.CommenterToken == "anonymous" {
|
2021-02-28 16:13:58 +08:00
|
|
|
commenterHex, commenterName, commenterEmail, commenterLink = "anonymous", "Anonymous", "", ""
|
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
|
|
|
|
}
|
2021-02-28 16:13:58 +08:00
|
|
|
commenterHex, commenterName, commenterEmail, commenterLink = c.CommenterHex, c.Name, c.Email, c.Link
|
2018-05-27 22:40:42 +08:00
|
|
|
for _, mod := range d.Moderators {
|
|
|
|
if mod.Email == c.Email {
|
|
|
|
isModerator = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-02-28 16:13:58 +08:00
|
|
|
}
|
2018-05-27 22:40:42 +08:00
|
|
|
|
2021-02-28 16:13:58 +08:00
|
|
|
var state string
|
|
|
|
if isModerator {
|
|
|
|
state = "approved"
|
|
|
|
} else if d.RequireModeration {
|
|
|
|
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"
|
|
|
|
} 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
|
|
|
}
|