From 1e44d5d3d50c850505065ef16bc513a207c0656c Mon Sep 17 00:00:00 2001 From: r Date: Sat, 8 Feb 2020 10:49:06 +0000 Subject: [PATCH] Add account muting and blocking --- service/auth.go | 48 +++++++++++++++++++++++++++++++ service/logging.go | 32 +++++++++++++++++++++ service/service.go | 26 ++++++++++++++++- service/transport.go | 68 ++++++++++++++++++++++++++++++++++++++++++++ templates/user.tmpl | 33 ++++++++++++++++++--- 5 files changed, 202 insertions(+), 5 deletions(-) diff --git a/service/auth.go b/service/auth.go index dac0338..6c71439 100644 --- a/service/auth.go +++ b/service/auth.go @@ -274,6 +274,54 @@ func (s *as) UnFollow(ctx context.Context, c *model.Client, id string) (err erro return s.Service.UnFollow(ctx, c, id) } +func (s *as) Mute(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.Mute(ctx, c, id) +} + +func (s *as) UnMute(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.UnMute(ctx, c, id) +} + +func (s *as) Block(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.Block(ctx, c, id) +} + +func (s *as) UnBlock(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.UnBlock(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 86aa1cb..e429fac 100644 --- a/service/logging.go +++ b/service/logging.go @@ -205,6 +205,38 @@ func (s *ls) UnFollow(ctx context.Context, c *model.Client, id string) (err erro return s.Service.UnFollow(ctx, c, id) } +func (s *ls) Mute(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", + "Mute", id, time.Since(begin), err) + }(time.Now()) + return s.Service.Mute(ctx, c, id) +} + +func (s *ls) UnMute(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", + "UnMute", id, time.Since(begin), err) + }(time.Now()) + return s.Service.UnMute(ctx, c, id) +} + +func (s *ls) Block(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", + "Block", id, time.Since(begin), err) + }(time.Now()) + return s.Service.Block(ctx, c, id) +} + +func (s *ls) UnBlock(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", + "UnBlock", id, time.Since(begin), err) + }(time.Now()) + return s.Service.UnBlock(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 c9511f9..c05097b 100644 --- a/service/service.go +++ b/service/service.go @@ -34,7 +34,7 @@ type Service interface { ServeUserSearchPage(ctx context.Context, c *model.Client, id string, q string, offset int) (err error) ServeSettingsPage(ctx context.Context, c *model.Client) (err error) NewSession(ctx context.Context, instance string) (redirectUrl string, sessionID string, err error) - Signin(ctx context.Context, c *model.Client, sessionID string, + Signin(ctx context.Context, c *model.Client, sessionID string, code string) (token string, userID string, err error) Post(ctx context.Context, c *model.Client, content string, replyToID string, format string, visibility string, isNSFW bool, files []*multipart.FileHeader) (id string, err error) @@ -44,6 +44,10 @@ type Service interface { UnRetweet(ctx context.Context, c *model.Client, id string) (count int64, err error) Follow(ctx context.Context, c *model.Client, id string) (err error) UnFollow(ctx context.Context, c *model.Client, id string) (err error) + Mute(ctx context.Context, c *model.Client, id string) (err error) + 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) 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) @@ -848,6 +852,26 @@ func (svc *service) UnFollow(ctx context.Context, c *model.Client, id string) (e return } +func (svc *service) Mute(ctx context.Context, c *model.Client, id string) (err error) { + _, err = c.AccountMute(ctx, id) + return +} + +func (svc *service) UnMute(ctx context.Context, c *model.Client, id string) (err error) { + _, err = c.AccountUnmute(ctx, id) + return +} + +func (svc *service) Block(ctx context.Context, c *model.Client, id string) (err error) { + _, err = c.AccountBlock(ctx, id) + return +} + +func (svc *service) UnBlock(ctx context.Context, c *model.Client, id string) (err error) { + _, err = c.AccountUnblock(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 6316748..81af4fa 100644 --- a/service/transport.go +++ b/service/transport.go @@ -451,6 +451,70 @@ func NewHandler(s Service, staticDir string) http.Handler { w.WriteHeader(http.StatusFound) } + mute := func(w http.ResponseWriter, req *http.Request) { + c := newClient(w) + ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token")) + id, _ := mux.Vars(req)["id"] + + err := s.Mute(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) + } + + unMute := func(w http.ResponseWriter, req *http.Request) { + c := newClient(w) + ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token")) + id, _ := mux.Vars(req)["id"] + + err := s.UnMute(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) + } + + block := func(w http.ResponseWriter, req *http.Request) { + c := newClient(w) + ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token")) + id, _ := mux.Vars(req)["id"] + + err := s.Block(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) + } + + unBlock := func(w http.ResponseWriter, req *http.Request) { + c := newClient(w) + ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token")) + id, _ := mux.Vars(req)["id"] + + err := s.UnBlock(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")) @@ -635,6 +699,10 @@ func NewHandler(s Service, staticDir string) http.Handler { r.HandleFunc("/unretweet/{id}", unretweet).Methods(http.MethodPost) r.HandleFunc("/follow/{id}", follow).Methods(http.MethodPost) r.HandleFunc("/unfollow/{id}", unfollow).Methods(http.MethodPost) + r.HandleFunc("/mute/{id}", mute).Methods(http.MethodPost) + 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("/settings", settings).Methods(http.MethodPost) r.HandleFunc("/muteconv/{id}", muteConversation).Methods(http.MethodPost) r.HandleFunc("/unmuteconv/{id}", unMuteConversation).Methods(http.MethodPost) diff --git a/templates/user.tmpl b/templates/user.tmpl index 3042672..fb80de4 100644 --- a/templates/user.tmpl +++ b/templates/user.tmpl @@ -25,17 +25,42 @@ - {{end}} + {{else}} +
+ + +
+ {{end}} {{if .User.Pleroma.Relationship.Requested}} + -
{{end}} - {{if not .User.Pleroma.Relationship.Following}} -
+ +
+ {{if .User.Pleroma.Relationship.Blocking}} + - + + + {{else}} +
+ + +
+ {{end}} + - + {{if .User.Pleroma.Relationship.Muting}} +
+ + +
+ {{else}} +
+ +
{{end}}