From 5147897c6c8ba3428ea6998f77241182ee8caa24 Mon Sep 17 00:00:00 2001 From: r Date: Sat, 17 Dec 2022 08:26:51 +0000 Subject: [PATCH] Add support for expiring mutes --- mastodon/accounts.go | 7 +++---- renderer/model.go | 5 +++++ renderer/renderer.go | 1 + service/service.go | 17 +++++++++++++++-- service/transport.go | 18 ++++++++++-------- templates/mute.tmpl | 29 +++++++++++++++++++++++++++++ templates/user.tmpl | 12 +----------- 7 files changed, 64 insertions(+), 25 deletions(-) create mode 100644 templates/mute.tmpl diff --git a/mastodon/accounts.go b/mastodon/accounts.go index 540adb2..f4e9002 100644 --- a/mastodon/accounts.go +++ b/mastodon/accounts.go @@ -246,11 +246,10 @@ func (c *Client) AccountUnblock(ctx context.Context, id string) (*Relationship, } // AccountMute mute the account. -func (c *Client) AccountMute(ctx context.Context, id string, notifications *bool) (*Relationship, error) { +func (c *Client) AccountMute(ctx context.Context, id string, notifications bool, duration int) (*Relationship, error) { params := url.Values{} - if notifications != nil { - params.Set("notifications", strconv.FormatBool(*notifications)) - } + params.Set("notifications", strconv.FormatBool(notifications)) + params.Set("duration", strconv.Itoa(duration)) var relationship Relationship err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/mute", url.PathEscape(string(id))), params, &relationship, nil) if err != nil { diff --git a/renderer/model.go b/renderer/model.go index e7cfbfb..8311f58 100644 --- a/renderer/model.go +++ b/renderer/model.go @@ -155,3 +155,8 @@ type FiltersData struct { *CommonData Filters []*mastodon.Filter } + +type MuteData struct { + *CommonData + User *mastodon.Account +} diff --git a/renderer/renderer.go b/renderer/renderer.go index 7afeb14..7732554 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -33,6 +33,7 @@ const ( SearchPage = "search.tmpl" SettingsPage = "settings.tmpl" FiltersPage = "filters.tmpl" + MutePage = "mute.tmpl" ) type TemplateData struct { diff --git a/service/service.go b/service/service.go index 432f938..0d2e196 100644 --- a/service/service.go +++ b/service/service.go @@ -678,6 +678,19 @@ func (s *service) UserSearchPage(c *client, return s.renderer.Render(c.rctx, c.w, renderer.UserSearchPage, data) } +func (s *service) MutePage(c *client, id string) (err error) { + user, err := c.GetAccount(c.ctx, id) + if err != nil { + return + } + cdata := s.cdata(c, "Mute"+user.DisplayName+" @"+user.Acct, 0, 0, "") + data := &renderer.UserData{ + User: user, + CommonData: cdata, + } + return s.renderer.Render(c.rctx, c.w, renderer.MutePage, data) +} + func (s *service) AboutPage(c *client) (err error) { cdata := s.cdata(c, "about", 0, 0, "") data := &renderer.AboutData{ @@ -930,8 +943,8 @@ func (s *service) Reject(c *client, id string) (err error) { return c.FollowRequestReject(c.ctx, id) } -func (s *service) Mute(c *client, id string, notifications *bool) (err error) { - _, err = c.AccountMute(c.ctx, id, notifications) +func (s *service) Mute(c *client, id string, notifications bool, duration int) (err error) { + _, err = c.AccountMute(c.ctx, id, notifications, duration) return } diff --git a/service/transport.go b/service/transport.go index 471a7d4..5c6472c 100644 --- a/service/transport.go +++ b/service/transport.go @@ -171,6 +171,11 @@ func NewHandler(s *service, logger *log.Logger, staticDir string) http.Handler { return s.UserSearchPage(c, id, sq, offset) }, SESSION, HTML) + mutePage := handle(func(c *client) error { + id, _ := mux.Vars(c.r)["id"] + return s.MutePage(c, id) + }, SESSION, HTML) + aboutPage := handle(func(c *client) error { return s.AboutPage(c) }, SESSION, HTML) @@ -361,17 +366,13 @@ func NewHandler(s *service, logger *log.Logger, staticDir string) http.Handler { mute := handle(func(c *client) error { id, _ := mux.Vars(c.r)["id"] - q := c.r.URL.Query() - var notifications *bool - if r, ok := q["notifications"]; ok && len(r) > 0 { - notifications = new(bool) - *notifications = r[0] == "true" - } - err := s.Mute(c, id, notifications) + notifications, _ := strconv.ParseBool(c.r.FormValue("notifications")) + duration, _ := strconv.Atoi(c.r.FormValue("duration")) + err := s.Mute(c, id, notifications, duration) if err != nil { return err } - c.redirect(c.r.FormValue("referrer")) + c.redirect("/user/" + id) return nil }, CSRF, HTML) @@ -673,6 +674,7 @@ func NewHandler(s *service, logger *log.Logger, staticDir string) http.Handler { r.HandleFunc("/user/{id}", userPage).Methods(http.MethodGet) r.HandleFunc("/user/{id}/{type}", userPage).Methods(http.MethodGet) r.HandleFunc("/usersearch/{id}", userSearchPage).Methods(http.MethodGet) + r.HandleFunc("/mute/{id}", mutePage).Methods(http.MethodGet) r.HandleFunc("/about", aboutPage).Methods(http.MethodGet) r.HandleFunc("/emojis", emojisPage).Methods(http.MethodGet) r.HandleFunc("/search", searchPage).Methods(http.MethodGet) diff --git a/templates/mute.tmpl b/templates/mute.tmpl new file mode 100644 index 0000000..47d0533 --- /dev/null +++ b/templates/mute.tmpl @@ -0,0 +1,29 @@ +{{with .Data}} +{{template "header.tmpl" (WithContext .CommonData $.Ctx)}} +
Mute {{.User.Acct}}
+ +
+ + +
+ + +
+
+ + +
+ +
+ +{{template "footer.tmpl"}} +{{end}} diff --git a/templates/user.tmpl b/templates/user.tmpl index e30088b..5ea52d9 100644 --- a/templates/user.tmpl +++ b/templates/user.tmpl @@ -79,17 +79,7 @@ {{else}} -
- - - -
- - -
- - - -
+ mute {{end}} {{if .User.Pleroma.Relationship.Following}} -