From ef7c8fcba2db132553a13554acc9060956f8aba6 Mon Sep 17 00:00:00 2001 From: Eugene Date: Fri, 22 Dec 2017 17:58:59 -0600 Subject: [PATCH] move admin rests to moderator --- app/rest/moderator.go | 45 +++++++++++++++++++++++++++++++++++++++++++ app/rest/server.go | 9 +++++++-- app/store/bolt.go | 16 +++++++++++++-- 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 app/rest/moderator.go diff --git a/app/rest/moderator.go b/app/rest/moderator.go new file mode 100644 index 00000000..b6462be5 --- /dev/null +++ b/app/rest/moderator.go @@ -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}) +} diff --git a/app/rest/server.go b/app/rest/server.go index 020e4d94..bf6c0707 100644 --- a/app/rest/server.go +++ b/app/rest/server.go @@ -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 } diff --git a/app/store/bolt.go b/app/store/bolt.go index e2be2ad8..59c755bd 100644 --- a/app/store/bolt.go +++ b/app/store/bolt.go @@ -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{}