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
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})
|
||||
}
|
Reference in New Issue
Block a user