2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func commentVote(commenterHex string, commentHex string, direction int) error {
|
|
|
|
if commentHex == "" || commenterHex == "" {
|
|
|
|
return errorMissingField
|
|
|
|
}
|
|
|
|
|
|
|
|
statement := `
|
2018-06-14 17:10:19 +08:00
|
|
|
SELECT commenterHex
|
|
|
|
FROM comments
|
|
|
|
WHERE commentHex = $1;
|
|
|
|
`
|
|
|
|
row := db.QueryRow(statement, commentHex)
|
|
|
|
|
|
|
|
var authorHex string
|
|
|
|
if err := row.Scan(&authorHex); err != nil {
|
2019-11-24 17:41:24 +08:00
|
|
|
logger.Errorf("error selecting authorHex for vote")
|
2018-06-14 17:10:19 +08:00
|
|
|
return errorInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
if authorHex == commenterHex {
|
|
|
|
return errorSelfVote
|
|
|
|
}
|
|
|
|
|
|
|
|
statement = `
|
2019-12-28 09:27:47 +08:00
|
|
|
INSERT INTO
|
|
|
|
votes (commentHex, commenterHex, direction, voteDate)
|
|
|
|
VALUES ($1, $2, $3, $4 )
|
|
|
|
ON CONFLICT (commentHex, commenterHex) DO
|
|
|
|
UPDATE SET direction = $3;
|
|
|
|
`
|
2018-05-27 22:40:42 +08:00
|
|
|
_, err := db.Exec(statement, commentHex, commenterHex, direction, time.Now().UTC())
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("error inserting/updating votes: %v", err)
|
|
|
|
return errorInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func commentVoteHandler(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
|
|
|
CommentHex *string `json:"commentHex"`
|
|
|
|
Direction *int `json:"direction"`
|
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-06-20 11:29:55 +08:00
|
|
|
if *x.CommenterToken == "anonymous" {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": errorUnauthorisedVote.Error()})
|
2018-05-27 22:40:42 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
direction := 0
|
|
|
|
if *x.Direction > 0 {
|
|
|
|
direction = 1
|
|
|
|
} else if *x.Direction < 0 {
|
|
|
|
direction = -1
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := commentVote(c.CommenterHex, *x.CommentHex, direction); 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-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": true})
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|