move admin rests to moderator
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/render"
|
||||
"github.com/umputun/remark/app/store"
|
||||
)
|
||||
|
||||
type moderator struct {
|
||||
dataStore store.Interface
|
||||
}
|
||||
|
||||
func (m *moderator) routes() chi.Router {
|
||||
router := chi.NewRouter()
|
||||
router.Use(AdminOnly)
|
||||
router.Delete("/comment/{id}", m.deleteCommentCtrl)
|
||||
return router
|
||||
}
|
||||
|
||||
// DELETE /comment/{id}?url=post-url
|
||||
func (m *moderator) deleteCommentCtrl(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
log.Printf("[WARN] bad id %s", chi.URLParam(r, "id"))
|
||||
httpError(w, r, http.StatusBadRequest, err, "can't parse id")
|
||||
}
|
||||
|
||||
log.Printf("[INFO] delete comment %d", id)
|
||||
|
||||
url := r.URL.Query().Get("url")
|
||||
err = m.dataStore.Delete(store.Locator{URL: url}, id)
|
||||
if err != nil {
|
||||
log.Printf("[WARN] can't delete comment, %s", err)
|
||||
httpError(w, r, http.StatusInternalServerError, err, "can't delete comment")
|
||||
return
|
||||
}
|
||||
|
||||
render.Status(r, http.StatusOK)
|
||||
render.JSON(w, r, JSON{"id": id, "url": url})
|
||||
}
|
||||
+7
-2
@@ -53,7 +53,12 @@ func (s *Server) Run() {
|
||||
rauth.Post("/comment", s.createCommentCtrl)
|
||||
rauth.Get("/user", s.getUserInfo)
|
||||
rauth.Put("/vote/{id}", s.voteCtrl)
|
||||
rauth.With(AdminOnly).Delete("/comment/{id}", s.deleteCommentCtrl)
|
||||
//rauth.With(AdminOnly).Delete("/comment/{id}", s.deleteCommentCtrl)
|
||||
})
|
||||
|
||||
rapi.With(Auth(s.SessionStore, s.Admins, s.DevMode)).Group(func(rmoder chi.Router) {
|
||||
mod := moderator{dataStore: s.Store}
|
||||
rmoder.Mount("/moderate", mod.routes())
|
||||
})
|
||||
})
|
||||
|
||||
@@ -244,7 +249,7 @@ func (s *Server) voteCtrl(w http.ResponseWriter, r *http.Request) {
|
||||
comment, err := s.Store.Vote(store.Locator{URL: url}, id, user.ID, vote)
|
||||
if err != nil {
|
||||
log.Printf("[WARN] vote rejected for %s - %d, %s", user.ID, id, err)
|
||||
httpError(w, r, http.StatusInternalServerError, err, "can't delete comment")
|
||||
httpError(w, r, http.StatusBadRequest, err, "can't vote for comment")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
+14
-2
@@ -70,7 +70,7 @@ func (b *BoltDB) Create(comment Comment) (int64, error) {
|
||||
}
|
||||
|
||||
rv := refFromComment(comment)
|
||||
e = bucket.Put([]byte(fmt.Sprintf("%d", time.Now().UnixNano())), []byte(rv.value()))
|
||||
e = bucket.Put(b.keyFromValue(comment.ID), []byte(rv.value()))
|
||||
if e != nil {
|
||||
return errors.Wrapf(e, "can't put reference %s to %s", rv.value(), lastBucketName)
|
||||
}
|
||||
@@ -93,6 +93,17 @@ func (b *BoltDB) Delete(locator Locator, id int64) error {
|
||||
if err := bucket.Delete(key); err != nil {
|
||||
return errors.Wrapf(err, "can't delete key %s from bucket %s", key, locator.URL)
|
||||
}
|
||||
|
||||
// delete from "last" bucket
|
||||
bucket = tx.Bucket([]byte(lastBucketName))
|
||||
if bucket == nil {
|
||||
return errors.Errorf("no bucket %s in store", lastBucketName)
|
||||
}
|
||||
|
||||
if err := bucket.Delete(key); err != nil {
|
||||
return errors.Wrapf(err, "can't delete key %s from bucket %s", key, lastBucketName)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -179,7 +190,8 @@ func (b *BoltDB) Last(locator Locator, max int) (result []Comment, err error) {
|
||||
}
|
||||
commentVal := urlBucket.Get(b.keyFromValue(id))
|
||||
if commentVal == nil {
|
||||
return errors.Errorf("no comment for %d in store %s", id, url)
|
||||
log.Printf("[WARN] no comment for %d in store %s", id, url)
|
||||
continue
|
||||
}
|
||||
|
||||
comment := Comment{}
|
||||
|
||||
Reference in New Issue
Block a user