From b6455f337c90df86cee5808a879a0d6489bb0890 Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 12 Mar 2018 18:41:16 -0500 Subject: [PATCH] remove notifier --- app/main.go | 7 ++--- app/notifier/notifier.go | 66 ---------------------------------------- app/rest/api/rest.go | 45 --------------------------- 3 files changed, 2 insertions(+), 116 deletions(-) delete mode 100644 app/notifier/notifier.go diff --git a/app/main.go b/app/main.go index 7acdad95..74d75bdd 100644 --- a/app/main.go +++ b/app/main.go @@ -8,15 +8,13 @@ import ( "strings" "time" - "github.com/umputun/remark/app/notifier" - "github.com/umputun/remark/app/rest" - "github.com/gorilla/sessions" "github.com/hashicorp/logutils" "github.com/jessevdk/go-flags" "github.com/pkg/errors" "github.com/umputun/remark/app/migrator" + "github.com/umputun/remark/app/rest" "github.com/umputun/remark/app/rest/api" "github.com/umputun/remark/app/rest/auth" "github.com/umputun/remark/app/store" @@ -126,8 +124,7 @@ func main() { DevEnabled: opts.DevPasswd != "", DevPasswd: opts.DevPasswd, }, - Cache: rest.NewLoadingCache(4*time.Hour, 15*time.Minute, postFlushFn), - Notifier: notifier.NewNoOperation(), + Cache: rest.NewLoadingCache(4*time.Hour, 15*time.Minute, postFlushFn), } srv.Run(opts.ServerCommand.Port) } diff --git a/app/notifier/notifier.go b/app/notifier/notifier.go deleted file mode 100644 index 99f9eaec..00000000 --- a/app/notifier/notifier.go +++ /dev/null @@ -1,66 +0,0 @@ -// Package notifier handles update notification as well as subscriptions to notification -package notifier - -import ( - "fmt" - "log" - "sync" - - "github.com/umputun/remark/app/store" -) - -// Interface defines notifier, sending messages triggered by topic/reply updates -type Interface interface { - Subscribe(user store.User) error - UnSubscribe(user store.User) error - OnUpdate(comment store.Comment) error - Status(user store.User) (bool, error) -} - -// NoOperation implements Interface doing nothing but logging -type NoOperation struct { - sync.RWMutex - status map[string]struct{} -} - -// NewNoOperation makes NoOperation fake notifier -func NewNoOperation() *NoOperation { - res := NoOperation{status: map[string]struct{}{}} - return &res -} - -// Subscribe is a fake, just logging attempt -func (n *NoOperation) Subscribe(user store.User) error { - n.Lock() - n.status[user.ID] = struct{}{} - n.Unlock() - log.Printf("[DEBUG] user %+v subscribed to updates", user) - return nil -} - -// UnSubscribe is a fake, just logging attempt -func (n *NoOperation) UnSubscribe(user store.User) error { - n.Lock() - delete(n.status, user.ID) - n.Unlock() - log.Printf("[DEBUG] user %+v unsubscribed from updates", user) - return nil -} - -// OnUpdate is a fake, just logging event -func (n *NoOperation) OnUpdate(comment store.Comment) error { - log.Printf("[DEBUG] update for %+v", comment) - return nil -} - -// Status returns from in-memory map -func (n *NoOperation) Status(user store.User) (bool, error) { - n.RLock() - defer n.RUnlock() - _, found := n.status[user.ID] - return found, nil -} - -func (n *NoOperation) key(locator store.Locator, user store.User) string { - return fmt.Sprintf("%+v-%s", locator, user.ID) -} diff --git a/app/rest/api/rest.go b/app/rest/api/rest.go index f505bab6..cee0339a 100644 --- a/app/rest/api/rest.go +++ b/app/rest/api/rest.go @@ -23,7 +23,6 @@ import ( "gopkg.in/russross/blackfriday.v2" "github.com/umputun/remark/app/migrator" - "github.com/umputun/remark/app/notifier" "github.com/umputun/remark/app/rest" "github.com/umputun/remark/app/rest/auth" "github.com/umputun/remark/app/store" @@ -37,7 +36,6 @@ type Rest struct { Authenticator auth.Authenticator Exporter migrator.Exporter Cache rest.LoadingCache - Notifier notifier.Interface httpServer *http.Server mod admin @@ -95,8 +93,6 @@ func (s *Rest) Run(port int) { rauth.Put("/comment/{id}", s.updateCommentCtrl) rauth.Get("/user", s.userInfoCtrl) rauth.Put("/vote/{id}", s.voteCtrl) - rauth.Put("/notify", s.notifyActionCtrl) - rauth.Get("/notify", s.notifyStatusCtrl) // admin routes, admin users only s.mod = admin{ dataService: s.DataService, @@ -152,10 +148,6 @@ func (s *Rest) createCommentCtrl(w http.ResponseWriter, r *http.Request) { return } - if err = s.Notifier.OnUpdate(comment); err != nil { - log.Printf("[WARN] can't send notify event for %+v, %s", comment.Locator, err) - } - s.Cache.Flush() // reset all caches render.Status(r, http.StatusCreated) render.JSON(w, r, JSON{"id": id, "locator": comment.Locator}) @@ -450,43 +442,6 @@ func (s *Rest) voteCtrl(w http.ResponseWriter, r *http.Request) { render.JSON(w, r, JSON{"id": comment.ID, "score": comment.Score}) } -// PUT /notify?site=siteID&action=1 - subscribe/unsubscribe to notification -func (s *Rest) notifyActionCtrl(w http.ResponseWriter, r *http.Request) { - user, err := rest.GetUserInfo(r) - if err != nil { - rest.SendErrorJSON(w, r, http.StatusUnauthorized, err, "can't get user info") - return - } - action := "unknown" - switch r.URL.Query().Get("action") { - case "1": - err = s.Notifier.Subscribe(user) - action = "subscribe" - case "0": - err = s.Notifier.UnSubscribe(user) - action = "unsubscribe" - } - if err != nil { - rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't subscribe/unsubscribe") - return - } - render.JSON(w, r, JSON{"user": user.ID, "action": action}) -} - -// GET /notify?site=siteID - get notification status for user -func (s *Rest) notifyStatusCtrl(w http.ResponseWriter, r *http.Request) { - user, err := rest.GetUserInfo(r) - if err != nil { - rest.SendErrorJSON(w, r, http.StatusUnauthorized, err, "can't get user info") - return - } - status := "not subscribed" - if st, err := s.Notifier.Status(user); err == nil && st { - status = "subscribed" - } - render.JSON(w, r, JSON{"user": user.ID, "status": status}) -} - // serves static files from /web func addFileServer(r chi.Router, path string, root http.FileSystem) { log.Printf("[INFO] run file server for %s, path %s", root, path)