1
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:
Adhityaa
2018-07-05 10:36:52 +05:30
committed by Adhityaa Chandrasekar
parent 0a03a2c6fc
commit 299649cea2
16 changed files with 427 additions and 4 deletions

70
api/page_update.go Normal file
View 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})
}