Add account muting and blocking
authorr <r@freesoftwareextremist.com>
Sat, 8 Feb 2020 10:49:06 +0000 (10:49 +0000)
committerr <r@freesoftwareextremist.com>
Sat, 8 Feb 2020 10:49:06 +0000 (10:49 +0000)
service/auth.go
service/logging.go
service/service.go
service/transport.go
templates/user.tmpl

index dac03380be7e1cb2c8e6b62b70c335c0335fe60e..6c714399eabaadbb799acc242861abcc8b1607a3 100644 (file)
@@ -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 {
index 86aa1cb56c534a1437da63fec52c5ed5c01f63b3..e429fac83026debb5076a5b6731d35cd0de73200 100644 (file)
@@ -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",
index c9511f9d604ae60065a66e5181e450852cf63f22..c05097b6f6561cd90e87dc8e7346d98f4f2f951f 100644 (file)
@@ -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) {
 
index 631674884b2f8bbceb98ef539e34094f64fc3671..81af4fa2326a5961ec1d7e5c80030e2a01e2a973 100644 (file)
@@ -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)
index 30426720914db99da51cbeec605d84d0ca9e548f..fb80de4816dfee1f9118277f947b8ed5848ac4dc 100644 (file)
                                <input type="hidden" name="csrf_token" value="{{$.Ctx.CSRFToken}}">
                                <input type="submit" value="unfollow" class="btn-link">
                        </form>
-                       {{end}} 
+                       {{else}} 
+                       <form class="d-inline" action="/follow/{{.User.ID}}" method="post">
+                               <input type="hidden" name="csrf_token" value="{{$.Ctx.CSRFToken}}">
+                               <input type="submit" value="{{if .User.Pleroma.Relationship.Requested}}resend request{{else}}follow{{end}}" class="btn-link">
+                       </form>
+                       {{end}}
                        {{if .User.Pleroma.Relationship.Requested}} 
+                       -
                        <form class="d-inline" action="/unfollow/{{.User.ID}}" method="post">
                                <input type="hidden" name="csrf_token" value="{{$.Ctx.CSRFToken}}">
                                <input type="submit" value="cancel request" class="btn-link">
                        </form>
                        {{end}} 
-                       {{if not .User.Pleroma.Relationship.Following}} 
-                       <form class="d-inline" action="/follow/{{.User.ID}}" method="post">
+               </div>
+               <div>
+                       {{if .User.Pleroma.Relationship.Blocking}} 
+                       <form class="d-inline" action="/unblock/{{.User.ID}}" method="post">
                                <input type="hidden" name="csrf_token" value="{{$.Ctx.CSRFToken}}">
-                               <input type="submit" value="{{if .User.Pleroma.Relationship.Requested}}resend request{{else}}follow{{end}}" class="btn-link">
+                               <input type="submit" value="unblock" class="btn-link">
+                       </form>
+                       {{else}} 
+                       <form class="d-inline" action="/block/{{.User.ID}}" method="post">
+                               <input type="hidden" name="csrf_token" value="{{$.Ctx.CSRFToken}}">
+                               <input type="submit" value="block" class="btn-link">
+                       </form>
+                       {{end}} 
+                       -
+                       {{if .User.Pleroma.Relationship.Muting}} 
+                       <form class="d-inline" action="/unmute/{{.User.ID}}" method="post">
+                               <input type="hidden" name="csrf_token" value="{{$.Ctx.CSRFToken}}">
+                               <input type="submit" value="unmute" class="btn-link">
+                       </form>
+                       {{else}} 
+                       <form class="d-inline" action="/mute/{{.User.ID}}" method="post">
+                               <input type="hidden" name="csrf_token" value="{{$.Ctx.CSRFToken}}">
+                               <input type="submit" value="mute" class="btn-link">
                        </form>
                        {{end}} 
                </div>