move export to admin

This commit is contained in:
Umputun
2017-12-24 18:20:03 -06:00
parent bfed1ef547
commit bda663f0d1
3 changed files with 27 additions and 25 deletions
+3 -3
View File
@@ -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_
+21 -10
View File
@@ -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)
}
+3 -12
View File
@@ -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})