mirror of
https://gitlab.com/commento/commento.git
synced 2025-06-29 22:56:37 -04:00
api,db: add page attributes and thread locking
This commit is contained in:

committed by
Adhityaa Chandrasekar

parent
0a03a2c6fc
commit
299649cea2
@ -111,6 +111,12 @@ func commentListHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
p, err := pageGet(domain, path)
|
||||
if err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
commenterHex := "anonymous"
|
||||
isModerator := false
|
||||
if *x.CommenterToken != "anonymous" {
|
||||
@ -151,6 +157,7 @@ func commentListHandler(w http.ResponseWriter, r *http.Request) {
|
||||
"requireIdentification": d.RequireIdentification,
|
||||
"isFrozen": d.State == "frozen",
|
||||
"isModerator": isModerator,
|
||||
"attributes": p,
|
||||
"configuredOauths": configuredOauths,
|
||||
})
|
||||
}
|
||||
|
@ -13,6 +13,16 @@ func commentNew(commenterHex string, domain string, path string, parentHex strin
|
||||
return "", errorMissingField
|
||||
}
|
||||
|
||||
p, err := pageGet(domain, path)
|
||||
if err != nil {
|
||||
logger.Errorf("cannot get page attributes: %v", err)
|
||||
return "", errorInternal
|
||||
}
|
||||
|
||||
if p.IsLocked {
|
||||
return "", errorThreadLocked
|
||||
}
|
||||
|
||||
commentHex, err := randomHex(32)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@ -31,6 +41,10 @@ func commentNew(commenterHex string, domain string, path string, parentHex strin
|
||||
return "", errorInternal
|
||||
}
|
||||
|
||||
if err = pageNew(domain, path); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return commentHex, nil
|
||||
}
|
||||
|
||||
|
@ -56,3 +56,18 @@ func TestCommentNewUpvoted(t *testing.T) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommentNewThreadLocked(t *testing.T) {
|
||||
failTestOnError(t, setupTestEnv())
|
||||
|
||||
pageNew("example.com", "/path.html")
|
||||
p, _ := pageGet("example.com", "/path.html")
|
||||
p.IsLocked = true
|
||||
pageUpdate(p)
|
||||
|
||||
_, err := commentNew("temp-commenter-hex", "example.com", "/path.html", "root", "**foo**", "approved", time.Now().UTC())
|
||||
if err == nil {
|
||||
t.Errorf("expected error not found creating a new comment on a locked thread")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -41,3 +41,4 @@ var errorSelfVote = errors.New("You cannot vote on your own comment.")
|
||||
var errorInvalidConfigFile = errors.New("Invalid config file.")
|
||||
var errorInvalidConfigValue = errors.New("Invalid config value.")
|
||||
var errorNewOwnerForbidden = errors.New("New user registrations are forbidden and closed.")
|
||||
var errorThreadLocked = errors.New("This thread is locked. You cannot add new comments.")
|
||||
|
9
api/page.go
Normal file
9
api/page.go
Normal file
@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import ()
|
||||
|
||||
type page struct {
|
||||
Domain string `json:"domain"`
|
||||
Path string `json:"path"`
|
||||
IsLocked bool `json:"isLocked"`
|
||||
}
|
34
api/page_get.go
Normal file
34
api/page_get.go
Normal file
@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
func pageGet(domain string, path string) (page, error) {
|
||||
// path can be empty
|
||||
if domain == "" {
|
||||
return page{}, errorMissingField
|
||||
}
|
||||
|
||||
statement := `
|
||||
SELECT isLocked
|
||||
FROM pages
|
||||
WHERE domain=$1 AND path=$2;
|
||||
`
|
||||
row := db.QueryRow(statement, domain, path)
|
||||
|
||||
p := page{Domain: domain, Path: path}
|
||||
if err := row.Scan(&p.IsLocked); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
// If there haven't been any comments, there won't be a record for this
|
||||
// page. The sane thing to do is return defaults.
|
||||
// TODO: the defaults are hard-coded in two places: here and the schema
|
||||
p.IsLocked = false
|
||||
} else {
|
||||
logger.Errorf("error scanning page: %v", err)
|
||||
return page{}, errorInternal
|
||||
}
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
43
api/page_get_test.go
Normal file
43
api/page_get_test.go
Normal file
@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPageGetBasics(t *testing.T) {
|
||||
failTestOnError(t, setupTestEnv())
|
||||
|
||||
pageNew("example.com", "/path.html")
|
||||
|
||||
p, err := pageGet("example.com", "/path.html")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error getting page: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if p.IsLocked != false {
|
||||
t.Errorf("expected p.IsLocked=false got %v", p.IsLocked)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := pageGet("example.com", "/path2.html"); err != nil {
|
||||
t.Errorf("unexpected error getting page with non-existant record: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageGetEmpty(t *testing.T) {
|
||||
failTestOnError(t, setupTestEnv())
|
||||
|
||||
pageNew("example.com", "")
|
||||
|
||||
if _, err := pageGet("example.com", ""); err != nil {
|
||||
t.Errorf("unexpected error getting page with empty path: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := pageGet("", "/path.html"); err == nil {
|
||||
t.Errorf("exepected error not found when getting page with empty domain")
|
||||
return
|
||||
}
|
||||
}
|
24
api/page_new.go
Normal file
24
api/page_new.go
Normal file
@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import ()
|
||||
|
||||
func pageNew(domain string, path string) error {
|
||||
// path can be empty
|
||||
if domain == "" {
|
||||
return errorMissingField
|
||||
}
|
||||
|
||||
statement := `
|
||||
INSERT INTO
|
||||
pages (domain, path)
|
||||
VALUES ($1, $2 )
|
||||
ON CONFLICT DO NOTHING;
|
||||
`
|
||||
_, err := db.Exec(statement, domain, path)
|
||||
if err != nil {
|
||||
logger.Errorf("error inserting new page: %v", err)
|
||||
return errorInternal
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
43
api/page_new_test.go
Normal file
43
api/page_new_test.go
Normal file
@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPageNewBasics(t *testing.T) {
|
||||
failTestOnError(t, setupTestEnv())
|
||||
|
||||
if err := pageNew("example.com", "/path.html"); err != nil {
|
||||
t.Errorf("unexpected error creating page: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageNewEmpty(t *testing.T) {
|
||||
failTestOnError(t, setupTestEnv())
|
||||
|
||||
if err := pageNew("example.com", ""); err != nil {
|
||||
t.Errorf("unexpected error creating page with empty path: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := pageNew("", "/path.html"); err == nil {
|
||||
t.Errorf("expected error not found creating page with empty domain")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageNewUnique(t *testing.T) {
|
||||
failTestOnError(t, setupTestEnv())
|
||||
|
||||
if err := pageNew("example.com", "/path.html"); err != nil {
|
||||
t.Errorf("unexpected error creating page: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// no error should be returned when trying to duplicate insert
|
||||
if err := pageNew("example.com", "/path.html"); err != nil {
|
||||
t.Errorf("unexpected error creating same page twice: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
70
api/page_update.go
Normal file
70
api/page_update.go
Normal file
@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func pageUpdate(p page) error {
|
||||
if p.Domain == "" {
|
||||
return errorMissingField
|
||||
}
|
||||
|
||||
statement := `
|
||||
INSERT INTO
|
||||
pages (domain, path, isLocked)
|
||||
VALUES ($1, $2, $3 )
|
||||
ON CONFLICT (domain, path) DO
|
||||
UPDATE SET isLocked = $3;
|
||||
`
|
||||
_, err := db.Exec(statement, p.Domain, p.Path, p.IsLocked)
|
||||
if err != nil {
|
||||
logger.Errorf("error setting page attributes: %v", err)
|
||||
return errorInternal
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func pageUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||
type request struct {
|
||||
CommenterToken *string `json:"commenterToken"`
|
||||
Domain *string `json:"domain"`
|
||||
Path *string `json:"path"`
|
||||
Attributes *page `json:"attributes"`
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c, err := commenterGetByCommenterToken(*x.CommenterToken)
|
||||
if err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
domain := domainStrip(*x.Domain)
|
||||
|
||||
isModerator, err := isDomainModerator(domain, c.Email)
|
||||
if err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if !isModerator {
|
||||
bodyMarshal(w, response{"success": false, "message": errorNotModerator.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
(*x.Attributes).Domain = *x.Domain
|
||||
(*x.Attributes).Path = *x.Path
|
||||
|
||||
if err = pageUpdate(*x.Attributes); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
bodyMarshal(w, response{"success": true})
|
||||
}
|
43
api/page_update_test.go
Normal file
43
api/page_update_test.go
Normal file
@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPageUpdateBasics(t *testing.T) {
|
||||
failTestOnError(t, setupTestEnv())
|
||||
|
||||
commenterHex, _ := commenterNew("test@example.com", "Test", "undefined", "https://example.com/photo.jpg", "google", "")
|
||||
|
||||
commentNew(commenterHex, "example.com", "/path.html", "root", "**foo**", "unapproved", time.Now().UTC())
|
||||
|
||||
p, _ := pageGet("example.com", "/path.html")
|
||||
if p.IsLocked != false {
|
||||
t.Errorf("expected IsLocked=false got %v", p.IsLocked)
|
||||
return
|
||||
}
|
||||
|
||||
p.IsLocked = true
|
||||
|
||||
if err := pageUpdate(p); err != nil {
|
||||
t.Errorf("unexpected error updating page: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
p, _ = pageGet("example.com", "/path.html")
|
||||
if p.IsLocked != true {
|
||||
t.Errorf("expected IsLocked=true got %v", p.IsLocked)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageUpdateEmpty(t *testing.T) {
|
||||
failTestOnError(t, setupTestEnv())
|
||||
|
||||
p := page{Domain: "", Path: "", IsLocked: false}
|
||||
if err := pageUpdate(p); err == nil {
|
||||
t.Errorf("expected error not found updating page with empty everything")
|
||||
return
|
||||
}
|
||||
}
|
@ -35,5 +35,7 @@ func apiRouterInit(router *mux.Router) error {
|
||||
router.HandleFunc("/api/comment/approve", commentApproveHandler).Methods("POST")
|
||||
router.HandleFunc("/api/comment/delete", commentDeleteHandler).Methods("POST")
|
||||
|
||||
router.HandleFunc("/api/page/update", pageUpdateHandler).Methods("POST")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user