From 1aff0569bfecf427d097a1601d9c5ac449c65d49 Mon Sep 17 00:00:00 2001 From: r Date: Tue, 31 Dec 2019 11:00:21 +0000 Subject: [PATCH] Add option to mask nsfw attachments --- mastodon/status.go | 1 + model/settings.go | 2 ++ service/service.go | 25 +++++++++++++++++++++---- service/transport.go | 2 ++ templates/settings.tmpl | 4 ++++ templates/status.tmpl | 4 ++-- 6 files changed, 32 insertions(+), 6 deletions(-) diff --git a/mastodon/status.go b/mastodon/status.go index 298c666..6646c60 100644 --- a/mastodon/status.go +++ b/mastodon/status.go @@ -55,6 +55,7 @@ type Status struct { ReplyMap map[string][]ReplyInfo `json:"reply_map"` ReplyNumber int `json:"reply_number"` ThreadInNewTab bool `json:"thread_in_new_tab"` + MaskNSFW bool `json:"mask_nsfw"` } // Context hold information for mastodon context. diff --git a/model/settings.go b/model/settings.go index 55db08a..02bebcb 100644 --- a/model/settings.go +++ b/model/settings.go @@ -4,6 +4,7 @@ type Settings struct { DefaultVisibility string `json:"default_visibility"` CopyScope bool `json:"copy_scope"` ThreadInNewTab bool `json:"thread_in_new_tab"` + MaskNSFW bool `json:"mask_nfsw"` } func NewSettings() *Settings { @@ -11,5 +12,6 @@ func NewSettings() *Settings { DefaultVisibility: "public", CopyScope: true, ThreadInNewTab: false, + MaskNSFW: true, } } diff --git a/service/service.go b/service/service.go index c9c48eb..27e0d4a 100644 --- a/service/service.go +++ b/service/service.go @@ -280,6 +280,11 @@ func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer, for i := range statuses { statuses[i].ThreadInNewTab = c.Session.Settings.ThreadInNewTab + statuses[i].MaskNSFW = c.Session.Settings.MaskNSFW + if statuses[i].Reblog != nil { + statuses[i].Reblog.ThreadInNewTab = c.Session.Settings.ThreadInNewTab + statuses[i].Reblog.MaskNSFW = c.Session.Settings.MaskNSFW + } } if len(maxID) > 0 && len(statuses) > 0 { @@ -394,6 +399,7 @@ func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *mo for i := range statuses { statuses[i].ShowReplies = true statuses[i].ReplyMap = replyMap + statuses[i].MaskNSFW = c.Session.Settings.MaskNSFW addToReplyMap(replyMap, statuses[i].InReplyToID, statuses[i].ID, i+1) } @@ -434,9 +440,10 @@ func (svc *service) ServeNotificationPage(ctx context.Context, client io.Writer, var unreadCount int for i := range notifications { - switch notifications[i].Type { - case "reblog", "favourite": - if notifications[i].Status != nil { + if notifications[i].Status != nil { + notifications[i].Status.MaskNSFW = c.Session.Settings.MaskNSFW + switch notifications[i].Type { + case "reblog", "favourite": notifications[i].Status.HideAccountInfo = true } } @@ -496,6 +503,13 @@ func (svc *service) ServeUserPage(ctx context.Context, client io.Writer, c *mode return } + for i := range statuses { + statuses[i].MaskNSFW = c.Session.Settings.MaskNSFW + if statuses[i].Reblog != nil { + statuses[i].Reblog.MaskNSFW = c.Session.Settings.MaskNSFW + } + } + if len(pg.MaxID) > 0 { hasNext = true nextLink = "/user/" + id + "?max_id=" + pg.MaxID @@ -666,7 +680,6 @@ func (svc *service) ServeFollowersPage(ctx context.Context, client io.Writer, c return } - fmt.Println(len(followers), pg.MaxID) if len(followers) == 20 && len(pg.MaxID) > 0 { hasNext = true nextLink = "/followers/" + id + "?max_id=" + pg.MaxID @@ -706,6 +719,10 @@ func (svc *service) ServeSearchPage(ctx context.Context, client io.Writer, c *mo hasNext = len(results.Accounts) == 20 case "statuses": hasNext = len(results.Statuses) == 20 + for i := range results.Statuses { + results.Statuses[i].MaskNSFW = c.Session.Settings.MaskNSFW + } + } if hasNext { diff --git a/service/transport.go b/service/transport.go index 83eeedf..8075377 100644 --- a/service/transport.go +++ b/service/transport.go @@ -355,10 +355,12 @@ func NewHandler(s Service, staticDir string) http.Handler { visibility := req.FormValue("visibility") copyScope := req.FormValue("copy_scope") == "true" threadInNewTab := req.FormValue("thread_in_new_tab") == "true" + maskNSFW := req.FormValue("mask_nsfw") == "true" settings := &model.Settings{ DefaultVisibility: visibility, CopyScope: copyScope, ThreadInNewTab: threadInNewTab, + MaskNSFW: maskNSFW, } err := s.SaveSettings(ctx, w, nil, settings) diff --git a/templates/settings.tmpl b/templates/settings.tmpl index df04cb1..c4a1012 100644 --- a/templates/settings.tmpl +++ b/templates/settings.tmpl @@ -20,6 +20,10 @@ +
+ + +
diff --git a/templates/status.tmpl b/templates/status.tmpl index 4c94062..2013f47 100644 --- a/templates/status.tmpl +++ b/templates/status.tmpl @@ -61,7 +61,7 @@ {{if eq .Type "image"}} status-image - {{if $.Sensitive}} + {{if (and $.MaskNSFW $.Sensitive)}}
{{end}}
@@ -76,7 +76,7 @@

Your browser doesn't support HTML5 video

- {{if $.Sensitive}} + {{if (and $.MaskNSFW $.Sensitive)}}
{{end}}