2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func commentList(commenterHex string, domain string, path string, includeUnapproved bool) ([]comment, map[string]commenter, error) {
|
2018-09-23 12:41:02 +08:00
|
|
|
// path can be empty
|
|
|
|
if commenterHex == "" || domain == "" {
|
2018-05-27 22:40:42 +08:00
|
|
|
return nil, nil, errorMissingField
|
|
|
|
}
|
|
|
|
|
|
|
|
statement := `
|
2019-05-02 06:45:14 +08:00
|
|
|
SELECT
|
|
|
|
commentHex,
|
|
|
|
commenterHex,
|
|
|
|
markdown,
|
|
|
|
html,
|
|
|
|
parentHex,
|
|
|
|
score,
|
|
|
|
state,
|
2019-09-14 09:13:38 +08:00
|
|
|
deleted,
|
2019-05-02 06:45:14 +08:00
|
|
|
creationDate
|
2018-05-27 22:40:42 +08:00
|
|
|
FROM comments
|
|
|
|
WHERE
|
2019-05-02 06:45:14 +08:00
|
|
|
comments.domain = $1 AND
|
|
|
|
comments.path = $2
|
|
|
|
`
|
2018-05-27 22:40:42 +08:00
|
|
|
|
|
|
|
if !includeUnapproved {
|
|
|
|
if commenterHex == "anonymous" {
|
2019-05-02 06:45:14 +08:00
|
|
|
statement += `AND state = 'approved'`
|
2018-05-27 22:40:42 +08:00
|
|
|
} else {
|
2019-05-02 06:45:14 +08:00
|
|
|
statement += `AND (state = 'approved' OR commenterHex = $3)`
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
statement += `;`
|
|
|
|
|
|
|
|
var rows *sql.Rows
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if !includeUnapproved && commenterHex != "anonymous" {
|
|
|
|
rows, err = db.Query(statement, domain, path, commenterHex)
|
|
|
|
} else {
|
|
|
|
rows, err = db.Query(statement, domain, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot get comments: %v", err)
|
|
|
|
return nil, nil, errorInternal
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
commenters := make(map[string]commenter)
|
|
|
|
commenters["anonymous"] = commenter{CommenterHex: "anonymous", Email: "undefined", Name: "Anonymous", Link: "undefined", Photo: "undefined", Provider: "undefined"}
|
|
|
|
|
|
|
|
comments := []comment{}
|
|
|
|
for rows.Next() {
|
|
|
|
c := comment{}
|
2019-05-02 06:45:14 +08:00
|
|
|
if err = rows.Scan(
|
|
|
|
&c.CommentHex,
|
|
|
|
&c.CommenterHex,
|
|
|
|
&c.Markdown,
|
|
|
|
&c.Html,
|
|
|
|
&c.ParentHex,
|
|
|
|
&c.Score,
|
|
|
|
&c.State,
|
2019-09-14 09:13:38 +08:00
|
|
|
&c.Deleted,
|
2019-05-02 06:45:14 +08:00
|
|
|
&c.CreationDate); err != nil {
|
2018-05-27 22:40:42 +08:00
|
|
|
return nil, nil, errorInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
if commenterHex != "anonymous" {
|
|
|
|
statement = `
|
2019-05-02 06:45:14 +08:00
|
|
|
SELECT direction
|
|
|
|
FROM votes
|
|
|
|
WHERE commentHex=$1 AND commenterHex=$2;
|
|
|
|
`
|
2018-05-27 22:40:42 +08:00
|
|
|
row := db.QueryRow(statement, c.CommentHex, commenterHex)
|
|
|
|
|
2018-06-07 15:39:53 +08:00
|
|
|
if err = row.Scan(&c.Direction); err != nil {
|
2018-05-27 22:40:42 +08:00
|
|
|
// TODO: is the only error here that there is no such entry?
|
2018-06-07 15:39:53 +08:00
|
|
|
c.Direction = 0
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 01:45:01 +08:00
|
|
|
if commenterHex != c.CommenterHex {
|
|
|
|
c.Markdown = ""
|
|
|
|
}
|
|
|
|
|
2018-06-07 15:40:15 +08:00
|
|
|
if !includeUnapproved {
|
|
|
|
c.State = ""
|
|
|
|
}
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
comments = append(comments, c)
|
|
|
|
|
|
|
|
if _, ok := commenters[c.CommenterHex]; !ok {
|
|
|
|
commenters[c.CommenterHex], err = commenterGetByHex(c.CommenterHex)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot retrieve commenter: %v", err)
|
|
|
|
return nil, nil, errorInternal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return comments, commenters, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func commentListHandler(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"`
|
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
|
|
|
|
}
|
|
|
|
|
2018-07-05 13:06:52 +08:00
|
|
|
p, err := pageGet(domain, path)
|
|
|
|
if err != nil {
|
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
commenterHex := "anonymous"
|
2019-05-02 06:53:33 +08:00
|
|
|
isModerator := false
|
|
|
|
modList := map[string]bool{}
|
|
|
|
|
|
|
|
if *x.CommenterToken != "anonymous" {
|
|
|
|
c, err := commenterGetByCommenterToken(*x.CommenterToken)
|
|
|
|
if err != nil {
|
|
|
|
if err == errorNoSuchToken {
|
|
|
|
commenterHex = "anonymous"
|
|
|
|
} else {
|
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
2018-05-27 22:40:42 +08:00
|
|
|
} else {
|
2019-05-02 06:53:33 +08:00
|
|
|
commenterHex = c.CommenterHex
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
2019-05-02 06:53:33 +08:00
|
|
|
for _, mod := range d.Moderators {
|
|
|
|
modList[mod.Email] = true
|
|
|
|
if mod.Email == c.Email {
|
|
|
|
isModerator = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, mod := range d.Moderators {
|
|
|
|
modList[mod.Email] = true
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
domainViewRecord(domain, commenterHex)
|
|
|
|
|
|
|
|
comments, commenters, err := commentList(commenterHex, domain, path, isModerator)
|
|
|
|
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-23 11:43:25 +08:00
|
|
|
_commenters := map[string]commenter{}
|
|
|
|
for commenterHex, cr := range commenters {
|
|
|
|
if _, ok := modList[cr.Email]; ok {
|
|
|
|
cr.IsModerator = true
|
|
|
|
}
|
|
|
|
cr.Email = ""
|
|
|
|
_commenters[commenterHex] = cr
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{
|
2018-05-27 22:40:42 +08:00
|
|
|
"success": true,
|
|
|
|
"domain": domain,
|
|
|
|
"comments": comments,
|
2019-02-23 11:43:25 +08:00
|
|
|
"commenters": _commenters,
|
2018-05-27 22:40:42 +08:00
|
|
|
"requireModeration": d.RequireModeration,
|
|
|
|
"requireIdentification": d.RequireIdentification,
|
|
|
|
"isFrozen": d.State == "frozen",
|
|
|
|
"isModerator": isModerator,
|
2019-12-05 10:50:50 +08:00
|
|
|
"defaultSortPolicy": d.DefaultSortPolicy,
|
2018-07-05 13:06:52 +08:00
|
|
|
"attributes": p,
|
2019-04-20 07:03:34 +08:00
|
|
|
"configuredOauths": map[string]bool{
|
|
|
|
"commento": d.CommentoProvider,
|
|
|
|
"google": googleConfigured && d.GoogleProvider,
|
|
|
|
"twitter": twitterConfigured && d.TwitterProvider,
|
|
|
|
"github": githubConfigured && d.GithubProvider,
|
2022-10-11 00:01:30 +08:00
|
|
|
"authentik": authentikConfigured && d.AuthentikProvider,
|
2019-04-20 07:03:34 +08:00
|
|
|
"gitlab": gitlabConfigured && d.GitlabProvider,
|
2019-04-21 08:34:25 +08:00
|
|
|
"sso": d.SsoProvider,
|
2019-04-20 07:03:34 +08:00
|
|
|
},
|
2018-05-27 22:40:42 +08:00
|
|
|
})
|
|
|
|
}
|