diff options
author | r <r@freesoftwareextremist.com> | 2020-04-17 17:19:11 +0000 |
---|---|---|
committer | r <r@freesoftwareextremist.com> | 2020-04-17 17:31:32 +0000 |
commit | 04af1b93dc95d761b4e05a448c9d86ac67623ff6 (patch) | |
tree | 75da80bfb467874b4fd94617f9c7c4cbd3cdbb85 /service | |
parent | ccdb5ef051bfcc9e96f4b0ce98a32735eb48d1c4 (diff) | |
download | bloat-04af1b93dc95d761b4e05a448c9d86ac67623ff6.tar.gz bloat-04af1b93dc95d761b4e05a448c9d86ac67623ff6.zip |
Add account {,un}subscribe
Diffstat (limited to 'service')
-rw-r--r-- | service/auth.go | 24 | ||||
-rw-r--r-- | service/logging.go | 16 | ||||
-rw-r--r-- | service/service.go | 12 | ||||
-rw-r--r-- | service/transport.go | 34 |
4 files changed, 86 insertions, 0 deletions
diff --git a/service/auth.go b/service/auth.go index 7fd238b..dd114b4 100644 --- a/service/auth.go +++ b/service/auth.go @@ -364,6 +364,30 @@ func (s *as) UnBlock(ctx context.Context, c *model.Client, id string) (err error return s.Service.UnBlock(ctx, c, id) } +func (s *as) Subscribe(ctx context.Context, c *model.Client, id string) (err error) { + err = s.authenticateClient(ctx, c) + if err != nil { + return + } + err = checkCSRF(ctx, c) + if err != nil { + return + } + return s.Service.Subscribe(ctx, c, id) +} + +func (s *as) UnSubscribe(ctx context.Context, c *model.Client, id string) (err error) { + err = s.authenticateClient(ctx, c) + if err != nil { + return + } + err = checkCSRF(ctx, c) + if err != nil { + return + } + return s.Service.UnSubscribe(ctx, c, id) +} + func (s *as) SaveSettings(ctx context.Context, c *model.Client, settings *model.Settings) (err error) { err = s.authenticateClient(ctx, c) if err != nil { diff --git a/service/logging.go b/service/logging.go index 2955959..c16cdb3 100644 --- a/service/logging.go +++ b/service/logging.go @@ -269,6 +269,22 @@ func (s *ls) UnBlock(ctx context.Context, c *model.Client, id string) (err error return s.Service.UnBlock(ctx, c, id) } +func (s *ls) Subscribe(ctx context.Context, c *model.Client, id string) (err error) { + defer func(begin time.Time) { + s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n", + "Subscribe", id, time.Since(begin), err) + }(time.Now()) + return s.Service.Subscribe(ctx, c, id) +} + +func (s *ls) UnSubscribe(ctx context.Context, c *model.Client, id string) (err error) { + defer func(begin time.Time) { + s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n", + "UnSubscribe", id, time.Since(begin), err) + }(time.Now()) + return s.Service.UnSubscribe(ctx, c, id) +} + func (s *ls) SaveSettings(ctx context.Context, c *model.Client, settings *model.Settings) (err error) { defer func(begin time.Time) { s.logger.Printf("method=%v, took=%v, err=%v\n", diff --git a/service/service.go b/service/service.go index b039849..3a48e77 100644 --- a/service/service.go +++ b/service/service.go @@ -52,6 +52,8 @@ type Service interface { UnMute(ctx context.Context, c *model.Client, id string) (err error) Block(ctx context.Context, c *model.Client, id string) (err error) UnBlock(ctx context.Context, c *model.Client, id string) (err error) + Subscribe(ctx context.Context, c *model.Client, id string) (err error) + UnSubscribe(ctx context.Context, c *model.Client, id string) (err error) SaveSettings(ctx context.Context, c *model.Client, settings *model.Settings) (err error) MuteConversation(ctx context.Context, c *model.Client, id string) (err error) UnMuteConversation(ctx context.Context, c *model.Client, id string) (err error) @@ -839,6 +841,16 @@ func (svc *service) UnBlock(ctx context.Context, c *model.Client, id string) (er return } +func (svc *service) Subscribe(ctx context.Context, c *model.Client, id string) (err error) { + _, err = c.Subscribe(ctx, id) + return +} + +func (svc *service) UnSubscribe(ctx context.Context, c *model.Client, id string) (err error) { + _, err = c.UnSubscribe(ctx, id) + return +} + func (svc *service) SaveSettings(ctx context.Context, c *model.Client, settings *model.Settings) (err error) { diff --git a/service/transport.go b/service/transport.go index 6540333..108cc18 100644 --- a/service/transport.go +++ b/service/transport.go @@ -549,6 +549,38 @@ func NewHandler(s Service, staticDir string) http.Handler { w.WriteHeader(http.StatusFound) } + subscribe := func(w http.ResponseWriter, req *http.Request) { + c := newClient(w) + ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token")) + id, _ := mux.Vars(req)["id"] + + err := s.Subscribe(ctx, c, id) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + s.ServeErrorPage(ctx, c, err) + return + } + + w.Header().Add("Location", req.Header.Get("Referer")) + w.WriteHeader(http.StatusFound) + } + + unSubscribe := func(w http.ResponseWriter, req *http.Request) { + c := newClient(w) + ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token")) + id, _ := mux.Vars(req)["id"] + + err := s.UnSubscribe(ctx, c, id) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + s.ServeErrorPage(ctx, c, err) + return + } + + w.Header().Add("Location", req.Header.Get("Referer")) + w.WriteHeader(http.StatusFound) + } + settings := func(w http.ResponseWriter, req *http.Request) { c := newClient(w) ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token")) @@ -762,6 +794,8 @@ func NewHandler(s Service, staticDir string) http.Handler { r.HandleFunc("/unmute/{id}", unMute).Methods(http.MethodPost) r.HandleFunc("/block/{id}", block).Methods(http.MethodPost) r.HandleFunc("/unblock/{id}", unBlock).Methods(http.MethodPost) + r.HandleFunc("/subscribe/{id}", subscribe).Methods(http.MethodPost) + r.HandleFunc("/unsubscribe/{id}", unSubscribe).Methods(http.MethodPost) r.HandleFunc("/settings", settings).Methods(http.MethodPost) r.HandleFunc("/muteconv/{id}", muteConversation).Methods(http.MethodPost) r.HandleFunc("/unmuteconv/{id}", unMuteConversation).Methods(http.MethodPost) |