From 04af1b93dc95d761b4e05a448c9d86ac67623ff6 Mon Sep 17 00:00:00 2001 From: r Date: Fri, 17 Apr 2020 17:19:11 +0000 Subject: Add account {,un}subscribe --- service/transport.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'service/transport.go') 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) -- cgit v1.2.3