diff options
author | r <r@freesoftwareextremist.com> | 2020-01-14 16:57:16 +0000 |
---|---|---|
committer | r <r@freesoftwareextremist.com> | 2020-01-14 16:57:16 +0000 |
commit | 55987a4c1bd7364eba7438e7326a76a9a550b668 (patch) | |
tree | 366dd5280f307ece3fc2343be3f24036f577f93b /service | |
parent | 3c6653a77b8951672ea169d175db154495a981fe (diff) | |
download | bloat-55987a4c1bd7364eba7438e7326a76a9a550b668.tar.gz bloat-55987a4c1bd7364eba7438e7326a76a9a550b668.zip |
Refactor renderer and templates
Diffstat (limited to 'service')
-rw-r--r-- | service/auth.go | 9 | ||||
-rw-r--r-- | service/logging.go | 12 | ||||
-rw-r--r-- | service/service.go | 107 | ||||
-rw-r--r-- | service/transport.go | 68 |
4 files changed, 87 insertions, 109 deletions
diff --git a/service/auth.go b/service/auth.go index 2f63717..e517383 100644 --- a/service/auth.go +++ b/service/auth.go @@ -73,12 +73,9 @@ func (s *authService) GetUserToken(ctx context.Context, sessionID string, c *mod return } -func (s *authService) ServeHomePage(ctx context.Context, client io.Writer) (err error) { - return s.Service.ServeHomePage(ctx, client) -} - -func (s *authService) ServeErrorPage(ctx context.Context, client io.Writer, err error) { - s.Service.ServeErrorPage(ctx, client, err) +func (s *authService) ServeErrorPage(ctx context.Context, client io.Writer, c *model.Client, err error) { + c, _ = s.getClient(ctx) + s.Service.ServeErrorPage(ctx, client, c, err) } func (s *authService) ServeSigninPage(ctx context.Context, client io.Writer) (err error) { diff --git a/service/logging.go b/service/logging.go index 27c038d..cafd815 100644 --- a/service/logging.go +++ b/service/logging.go @@ -37,20 +37,12 @@ func (s *loggingService) GetUserToken(ctx context.Context, sessionID string, c * return s.Service.GetUserToken(ctx, sessionID, c, code) } -func (s *loggingService) ServeHomePage(ctx context.Context, client io.Writer) (err error) { - defer func(begin time.Time) { - s.logger.Printf("method=%v, took=%v, err=%v\n", - "ServeHomePage", time.Since(begin), err) - }(time.Now()) - return s.Service.ServeHomePage(ctx, client) -} - -func (s *loggingService) ServeErrorPage(ctx context.Context, client io.Writer, err error) { +func (s *loggingService) ServeErrorPage(ctx context.Context, client io.Writer, c *model.Client, err error) { defer func(begin time.Time) { s.logger.Printf("method=%v, err=%v, took=%v\n", "ServeErrorPage", err, time.Since(begin)) }(time.Now()) - s.Service.ServeErrorPage(ctx, client, err) + s.Service.ServeErrorPage(ctx, client, c, err) } func (s *loggingService) ServeSigninPage(ctx context.Context, client io.Writer) (err error) { diff --git a/service/service.go b/service/service.go index b3c2103..0c01dce 100644 --- a/service/service.go +++ b/service/service.go @@ -26,10 +26,9 @@ var ( ) type Service interface { - ServeHomePage(ctx context.Context, client io.Writer) (err error) GetAuthUrl(ctx context.Context, instance string) (url string, sessionID string, err error) GetUserToken(ctx context.Context, sessionID string, c *model.Client, token string) (accessToken string, err error) - ServeErrorPage(ctx context.Context, client io.Writer, err error) + ServeErrorPage(ctx context.Context, client io.Writer, c *model.Client, err error) ServeSigninPage(ctx context.Context, client io.Writer) (err error) ServeTimelinePage(ctx context.Context, client io.Writer, c *model.Client, timelineType string, maxID string, sinceID string, minID string) (err error) ServeThreadPage(ctx context.Context, client io.Writer, c *model.Client, id string, reply bool) (err error) @@ -79,6 +78,15 @@ func NewService(clientName string, clientScope string, clientWebsite string, } } +func getRendererContext(s model.Settings) *renderer.Context { + return &renderer.Context{ + MaskNSFW: s.MaskNSFW, + ThreadInNewTab: s.ThreadInNewTab, + FluorideMode: s.FluorideMode, + DarkMode: s.DarkMode, + } +} + func (svc *service) GetAuthUrl(ctx context.Context, instance string) ( redirectUrl string, sessionID string, err error) { var instanceURL string @@ -202,20 +210,7 @@ func (svc *service) GetUserToken(ctx context.Context, sessionID string, c *model return res.AccessToken, nil } -func (svc *service) ServeHomePage(ctx context.Context, client io.Writer) (err error) { - commonData, err := svc.getCommonData(ctx, client, nil, "home") - if err != nil { - return - } - - data := &renderer.HomePageData{ - CommonData: commonData, - } - - return svc.renderer.RenderHomePage(ctx, client, data) -} - -func (svc *service) ServeErrorPage(ctx context.Context, client io.Writer, err error) { +func (svc *service) ServeErrorPage(ctx context.Context, client io.Writer, c *model.Client, err error) { var errStr string if err != nil { errStr = err.Error() @@ -231,7 +226,15 @@ func (svc *service) ServeErrorPage(ctx context.Context, client io.Writer, err er Error: errStr, } - svc.renderer.RenderErrorPage(ctx, client, data) + var s model.Settings + if c != nil { + s = c.Session.Settings + } else { + s = *model.NewSettings() + } + rCtx := getRendererContext(s) + + svc.renderer.RenderErrorPage(rCtx, client, data) } func (svc *service) ServeSigninPage(ctx context.Context, client io.Writer) (err error) { @@ -244,7 +247,8 @@ func (svc *service) ServeSigninPage(ctx context.Context, client io.Writer) (err CommonData: commonData, } - return svc.renderer.RenderSigninPage(ctx, client, data) + rCtx := getRendererContext(*model.NewSettings()) + return svc.renderer.RenderSigninPage(rCtx, client, data) } func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer, @@ -279,14 +283,8 @@ 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 - statuses[i].DarkMode = c.Session.Settings.DarkMode if statuses[i].Reblog != nil { statuses[i].Reblog.RetweetedByID = statuses[i].ID - statuses[i].Reblog.ThreadInNewTab = c.Session.Settings.ThreadInNewTab - statuses[i].Reblog.MaskNSFW = c.Session.Settings.MaskNSFW - statuses[i].Reblog.DarkMode = c.Session.Settings.DarkMode } } @@ -319,7 +317,6 @@ func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer, postContext := model.PostContext{ DefaultVisibility: c.Session.Settings.DefaultVisibility, Formats: svc.postFormats, - DarkMode: c.Session.Settings.DarkMode, } commonData, err := svc.getCommonData(ctx, client, c, timelineType+" timeline ") @@ -337,8 +334,9 @@ func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer, PostContext: postContext, CommonData: commonData, } + rCtx := getRendererContext(c.Session.Settings) - err = svc.renderer.RenderTimelinePage(ctx, client, data) + err = svc.renderer.RenderTimelinePage(rCtx, client, data) if err != nil { return } @@ -404,8 +402,6 @@ 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 - statuses[i].DarkMode = c.Session.Settings.DarkMode addToReplyMap(replyMap, statuses[i].InReplyToID, statuses[i].ID, i+1) } @@ -420,8 +416,9 @@ func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *mo ReplyMap: replyMap, CommonData: commonData, } + rCtx := getRendererContext(c.Session.Settings) - err = svc.renderer.RenderThreadPage(ctx, client, data) + err = svc.renderer.RenderThreadPage(rCtx, client, data) if err != nil { return } @@ -448,8 +445,6 @@ func (svc *service) ServeNotificationPage(ctx context.Context, client io.Writer, for i := range notifications { if notifications[i].Status != nil { notifications[i].Status.CreatedAt = notifications[i].CreatedAt - notifications[i].Status.MaskNSFW = c.Session.Settings.MaskNSFW - notifications[i].Status.DarkMode = c.Session.Settings.DarkMode switch notifications[i].Type { case "reblog", "favourite": notifications[i].Status.HideAccountInfo = true @@ -482,9 +477,10 @@ func (svc *service) ServeNotificationPage(ctx context.Context, client io.Writer, HasNext: hasNext, NextLink: nextLink, CommonData: commonData, - DarkMode: c.Session.Settings.DarkMode, } - err = svc.renderer.RenderNotificationPage(ctx, client, data) + rCtx := getRendererContext(c.Session.Settings) + + err = svc.renderer.RenderNotificationPage(rCtx, client, data) if err != nil { return } @@ -512,15 +508,6 @@ func (svc *service) ServeUserPage(ctx context.Context, client io.Writer, c *mode return } - for i := range statuses { - statuses[i].MaskNSFW = c.Session.Settings.MaskNSFW - statuses[i].DarkMode = c.Session.Settings.DarkMode - if statuses[i].Reblog != nil { - statuses[i].Reblog.MaskNSFW = c.Session.Settings.MaskNSFW - statuses[i].Reblog.DarkMode = c.Session.Settings.DarkMode - } - } - if len(pg.MaxID) > 0 { hasNext = true nextLink = "/user/" + id + "?max_id=" + pg.MaxID @@ -537,10 +524,10 @@ func (svc *service) ServeUserPage(ctx context.Context, client io.Writer, c *mode HasNext: hasNext, NextLink: nextLink, CommonData: commonData, - DarkMode: c.Session.Settings.DarkMode, } + rCtx := getRendererContext(c.Session.Settings) - err = svc.renderer.RenderUserPage(ctx, client, data) + err = svc.renderer.RenderUserPage(rCtx, client, data) if err != nil { return } @@ -557,7 +544,9 @@ func (svc *service) ServeAboutPage(ctx context.Context, client io.Writer, c *mod data := &renderer.AboutData{ CommonData: commonData, } - err = svc.renderer.RenderAboutPage(ctx, client, data) + rCtx := getRendererContext(c.Session.Settings) + + err = svc.renderer.RenderAboutPage(rCtx, client, data) if err != nil { return } @@ -580,8 +569,9 @@ func (svc *service) ServeEmojiPage(ctx context.Context, client io.Writer, c *mod Emojis: emojis, CommonData: commonData, } + rCtx := getRendererContext(c.Session.Settings) - err = svc.renderer.RenderEmojiPage(ctx, client, data) + err = svc.renderer.RenderEmojiPage(rCtx, client, data) if err != nil { return } @@ -604,8 +594,9 @@ func (svc *service) ServeLikedByPage(ctx context.Context, client io.Writer, c *m CommonData: commonData, Users: likers, } + rCtx := getRendererContext(c.Session.Settings) - err = svc.renderer.RenderLikedByPage(ctx, client, data) + err = svc.renderer.RenderLikedByPage(rCtx, client, data) if err != nil { return } @@ -628,8 +619,9 @@ func (svc *service) ServeRetweetedByPage(ctx context.Context, client io.Writer, CommonData: commonData, Users: retweeters, } + rCtx := getRendererContext(c.Session.Settings) - err = svc.renderer.RenderRetweetedByPage(ctx, client, data) + err = svc.renderer.RenderRetweetedByPage(rCtx, client, data) if err != nil { return } @@ -668,8 +660,9 @@ func (svc *service) ServeFollowingPage(ctx context.Context, client io.Writer, c HasNext: hasNext, NextLink: nextLink, } + rCtx := getRendererContext(c.Session.Settings) - err = svc.renderer.RenderFollowingPage(ctx, client, data) + err = svc.renderer.RenderFollowingPage(rCtx, client, data) if err != nil { return } @@ -708,8 +701,9 @@ func (svc *service) ServeFollowersPage(ctx context.Context, client io.Writer, c HasNext: hasNext, NextLink: nextLink, } + rCtx := getRendererContext(c.Session.Settings) - err = svc.renderer.RenderFollowersPage(ctx, client, data) + err = svc.renderer.RenderFollowersPage(rCtx, client, data) if err != nil { return } @@ -731,11 +725,6 @@ 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 - results.Statuses[i].DarkMode = c.Session.Settings.DarkMode - } - } if hasNext { @@ -761,8 +750,9 @@ func (svc *service) ServeSearchPage(ctx context.Context, client io.Writer, c *mo HasNext: hasNext, NextLink: nextLink, } + rCtx := getRendererContext(c.Session.Settings) - err = svc.renderer.RenderSearchPage(ctx, client, data) + err = svc.renderer.RenderSearchPage(rCtx, client, data) if err != nil { return } @@ -780,8 +770,9 @@ func (svc *service) ServeSettingsPage(ctx context.Context, client io.Writer, c * CommonData: commonData, Settings: &c.Session.Settings, } + rCtx := getRendererContext(c.Session.Settings) - err = svc.renderer.RenderSettingsPage(ctx, client, data) + err = svc.renderer.RenderSettingsPage(rCtx, client, data) if err != nil { return } @@ -837,8 +828,6 @@ func (svc *service) getCommonData(ctx context.Context, client io.Writer, c *mode } data.HeaderData.NotificationCount = notificationCount - data.HeaderData.FluorideMode = c.Session.Settings.FluorideMode - data.HeaderData.DarkMode = c.Session.Settings.DarkMode } return diff --git a/service/transport.go b/service/transport.go index 0072f95..8cca4f5 100644 --- a/service/transport.go +++ b/service/transport.go @@ -41,7 +41,7 @@ func NewHandler(s Service, staticDir string) http.Handler { r.HandleFunc("/signin", func(w http.ResponseWriter, req *http.Request) { err := s.ServeSigninPage(ctx, w) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } }).Methods(http.MethodGet) @@ -50,7 +50,7 @@ func NewHandler(s Service, staticDir string) http.Handler { instance := req.FormValue("instance") url, sessionID, err := s.GetAuthUrl(ctx, instance) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } @@ -69,7 +69,7 @@ func NewHandler(s Service, staticDir string) http.Handler { token := req.URL.Query().Get("code") _, err := s.GetUserToken(ctx, "", nil, token) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } @@ -92,7 +92,7 @@ func NewHandler(s Service, staticDir string) http.Handler { err := s.ServeTimelinePage(ctx, w, nil, timelineType, maxID, sinceID, minID) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } }).Methods(http.MethodGet) @@ -103,7 +103,7 @@ func NewHandler(s Service, staticDir string) http.Handler { reply := req.URL.Query().Get("reply") err := s.ServeThreadPage(ctx, w, nil, id, len(reply) > 1) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } }).Methods(http.MethodGet) @@ -114,7 +114,7 @@ func NewHandler(s Service, staticDir string) http.Handler { err := s.ServeLikedByPage(ctx, w, nil, id) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } }).Methods(http.MethodGet) @@ -125,7 +125,7 @@ func NewHandler(s Service, staticDir string) http.Handler { err := s.ServeRetweetedByPage(ctx, w, nil, id) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } }).Methods(http.MethodGet) @@ -139,7 +139,7 @@ func NewHandler(s Service, staticDir string) http.Handler { err := s.ServeFollowingPage(ctx, w, nil, id, maxID, minID) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } }).Methods(http.MethodGet) @@ -153,7 +153,7 @@ func NewHandler(s Service, staticDir string) http.Handler { err := s.ServeFollowersPage(ctx, w, nil, id, maxID, minID) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } }).Methods(http.MethodGet) @@ -165,7 +165,7 @@ func NewHandler(s Service, staticDir string) http.Handler { _, err := s.Like(ctx, w, nil, id) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } @@ -184,7 +184,7 @@ func NewHandler(s Service, staticDir string) http.Handler { _, err := s.UnLike(ctx, w, nil, id) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } @@ -203,7 +203,7 @@ func NewHandler(s Service, staticDir string) http.Handler { _, err := s.Retweet(ctx, w, nil, id) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } @@ -222,7 +222,7 @@ func NewHandler(s Service, staticDir string) http.Handler { _, err := s.UnRetweet(ctx, w, nil, id) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } @@ -239,13 +239,13 @@ func NewHandler(s Service, staticDir string) http.Handler { id, _ := mux.Vars(req)["id"] count, err := s.Like(ctx, w, nil, id) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } err = serveJson(w, count) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } }).Methods(http.MethodPost) @@ -255,13 +255,13 @@ func NewHandler(s Service, staticDir string) http.Handler { id, _ := mux.Vars(req)["id"] count, err := s.UnLike(ctx, w, nil, id) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } err = serveJson(w, count) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } }).Methods(http.MethodPost) @@ -271,13 +271,13 @@ func NewHandler(s Service, staticDir string) http.Handler { id, _ := mux.Vars(req)["id"] count, err := s.Retweet(ctx, w, nil, id) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } err = serveJson(w, count) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } }).Methods(http.MethodPost) @@ -287,13 +287,13 @@ func NewHandler(s Service, staticDir string) http.Handler { id, _ := mux.Vars(req)["id"] count, err := s.UnRetweet(ctx, w, nil, id) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } err = serveJson(w, count) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } }).Methods(http.MethodPost) @@ -303,7 +303,7 @@ func NewHandler(s Service, staticDir string) http.Handler { err := req.ParseMultipartForm(4 << 20) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } @@ -317,7 +317,7 @@ func NewHandler(s Service, staticDir string) http.Handler { id, err := s.PostTweet(ctx, w, nil, content, replyToID, format, visibility, isNSFW, files) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } @@ -337,7 +337,7 @@ func NewHandler(s Service, staticDir string) http.Handler { err := s.ServeNotificationPage(ctx, w, nil, maxID, minID) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } }).Methods(http.MethodGet) @@ -351,7 +351,7 @@ func NewHandler(s Service, staticDir string) http.Handler { err := s.ServeUserPage(ctx, w, nil, id, maxID, minID) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } }).Methods(http.MethodGet) @@ -363,7 +363,7 @@ func NewHandler(s Service, staticDir string) http.Handler { err := s.Follow(ctx, w, nil, id) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } @@ -378,7 +378,7 @@ func NewHandler(s Service, staticDir string) http.Handler { err := s.UnFollow(ctx, w, nil, id) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } @@ -391,7 +391,7 @@ func NewHandler(s Service, staticDir string) http.Handler { err := s.ServeAboutPage(ctx, w, nil) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } }).Methods(http.MethodGet) @@ -401,7 +401,7 @@ func NewHandler(s Service, staticDir string) http.Handler { err := s.ServeEmojiPage(ctx, w, nil) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } }).Methods(http.MethodGet) @@ -418,14 +418,14 @@ func NewHandler(s Service, staticDir string) http.Handler { if len(offsetStr) > 1 { offset, err = strconv.Atoi(offsetStr) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } } err = s.ServeSearchPage(ctx, w, nil, q, qType, offset) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } }).Methods(http.MethodGet) @@ -435,7 +435,7 @@ func NewHandler(s Service, staticDir string) http.Handler { err := s.ServeSettingsPage(ctx, w, nil) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } }).Methods(http.MethodGet) @@ -455,12 +455,12 @@ func NewHandler(s Service, staticDir string) http.Handler { ThreadInNewTab: threadInNewTab, MaskNSFW: maskNSFW, FluorideMode: fluorideMode, - DarkMode: darkMode, + DarkMode: darkMode, } err := s.SaveSettings(ctx, w, nil, settings) if err != nil { - s.ServeErrorPage(ctx, w, err) + s.ServeErrorPage(ctx, w, nil, err) return } |