From bda663f0d10ed8ff0d10131a5c8b5dbd1fc3003f Mon Sep 17 00:00:00 2001 From: Umputun Date: Sun, 24 Dec 2017 18:20:03 -0600 Subject: [PATCH] move export to admin --- README.md | 6 +++--- app/rest/{moderator.go => admin.go} | 31 +++++++++++++++++++---------- app/rest/server.go | 15 +++----------- 3 files changed, 27 insertions(+), 25 deletions(-) rename app/rest/{moderator.go => admin.go} (51%) diff --git a/README.md b/README.md index e078c1d7..eb1f5cc1 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,6 @@ type Locator struct { - `GET /api/v1/id/{id}` - get comment by `id` - `GET /api/v1/count?url=post-url` - get comment's count for `{url}` - `PUT /api/v1/vote/{id}?url=post-url&vote=1` - vote for comment. `vote`=1 will increase score, -1 decreases. _auth required_ -- `DELETE /api/v1/moderate/comment/{id}?url=post-url` - delete comment by `id`. _auth and admin required_ -- `PUT /api/v1/moderate/user/{userid}?site=side-id&block=1` - block or unblock user. _auth and admin required_ -- `GET /api/v1/export?site=side-id&block=1` - export all comments. _auth and admin required_ +- `DELETE /api/v1/admin/comment/{id}?url=post-url` - delete comment by `id`. _auth and admin required_ +- `PUT /api/v1/admin/user/{userid}?site=side-id&block=1` - block or unblock user. _auth and admin required_ +- `GET /api/v1/admin/export?site=side-id&block=1` - export all comments. _auth and admin required_ diff --git a/app/rest/moderator.go b/app/rest/admin.go similarity index 51% rename from app/rest/moderator.go rename to app/rest/admin.go index 745bddc9..b39671a9 100644 --- a/app/rest/moderator.go +++ b/app/rest/admin.go @@ -4,32 +4,36 @@ import ( "log" "net/http" + "github.com/umputun/remark/app/migrator" + "github.com/go-chi/chi" "github.com/go-chi/render" "github.com/umputun/remark/app/store" ) -type moderator struct { +type admin struct { dataStore store.Interface + exporter migrator.Exporter } -func (m *moderator) routes() chi.Router { +func (a *admin) routes() chi.Router { router := chi.NewRouter() router.Use(AdminOnly) - router.Delete("/comment/{id}", m.deleteCommentCtrl) - router.Put("/user/{userid}", m.setBlockCtrl) + router.Delete("/comment/{id}", a.deleteCommentCtrl) + router.Put("/user/{userid}", a.setBlockCtrl) + router.Get("/export", a.exportCtrl) return router } // DELETE /comment/{id}?url=post-url -func (m *moderator) deleteCommentCtrl(w http.ResponseWriter, r *http.Request) { +func (a *admin) deleteCommentCtrl(w http.ResponseWriter, r *http.Request) { id := chi.URLParam(r, "id") log.Printf("[INFO] delete comment %s", id) url := r.URL.Query().Get("url") - err := m.dataStore.Delete(store.Locator{URL: url}, id) + err := a.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") @@ -41,12 +45,12 @@ func (m *moderator) deleteCommentCtrl(w http.ResponseWriter, r *http.Request) { } // PUT /user/{userid}?site=side-id&block=1 -func (m *moderator) setBlockCtrl(w http.ResponseWriter, r *http.Request) { +func (a *admin) setBlockCtrl(w http.ResponseWriter, r *http.Request) { userID := chi.URLParam(r, "userid") siteID := r.URL.Query().Get("site") blockStatus := r.URL.Query().Get("block") == "1" - if err := m.dataStore.SetBlock(store.Locator{SiteID: siteID}, userID, blockStatus); err != nil { + if err := a.dataStore.SetBlock(store.Locator{SiteID: siteID}, userID, blockStatus); err != nil { httpError(w, r, http.StatusBadRequest, err, "can't set blocking status") return } @@ -54,6 +58,13 @@ func (m *moderator) setBlockCtrl(w http.ResponseWriter, r *http.Request) { render.JSON(w, r, JSON{"user_id": userID, "site_id": siteID, "block": blockStatus}) } -func (m *moderator) checkBlocked(locator store.Locator, user store.User) bool { - return m.dataStore.IsBlocked(store.Locator{}, user.ID) +// GET /export?site=site-id +func (a *admin) exportCtrl(w http.ResponseWriter, r *http.Request) { + siteID := r.URL.Query().Get("site") + if err := a.exporter.Export(w, siteID); err != nil { + httpError(w, r, http.StatusInternalServerError, err, "export failed") + } +} +func (a *admin) checkBlocked(locator store.Locator, user store.User) bool { + return a.dataStore.IsBlocked(store.Locator{}, user.ID) } diff --git a/app/rest/server.go b/app/rest/server.go index 678d15ce..470433aa 100644 --- a/app/rest/server.go +++ b/app/rest/server.go @@ -32,7 +32,7 @@ type Server struct { Exporter migrator.Exporter DevMode bool - mod moderator + mod admin } // Run the lister and request's router, activate rest server @@ -62,9 +62,8 @@ func (s *Server) Run() { }) rapi.With(Auth(s.SessionStore, s.Admins, s.DevMode)).Group(func(radmin chi.Router) { - s.mod = moderator{dataStore: s.Store} - radmin.Get("/export", s.exportCtrl) - radmin.Mount("/moderate", s.mod.routes()) + s.mod = admin{dataStore: s.Store, exporter: s.Exporter} + radmin.Mount("/admin", s.mod.routes()) }) }) @@ -252,14 +251,6 @@ func (s *Server) voteCtrl(w http.ResponseWriter, r *http.Request) { render.JSON(w, r, comment) } -// GET /export?site=site-id -func (s *Server) exportCtrl(w http.ResponseWriter, r *http.Request) { - siteID := r.URL.Query().Get("site") - if err := s.Exporter.Export(w, siteID); err != nil { - httpError(w, r, http.StatusInternalServerError, err, "export failed") - } -} - func httpError(w http.ResponseWriter, r *http.Request, code int, err error, details string) { render.Status(r, code) render.JSON(w, r, JSON{"error": err.Error(), "details": details})