diff --git a/mastodon/status.go b/mastodon/status.go index e6e247f..d6a9330 100644 --- a/mastodon/status.go +++ b/mastodon/status.go @@ -54,10 +54,7 @@ type Status struct { ShowReplies bool `json:"show_replies"` ReplyMap map[string][]ReplyInfo `json:"reply_map"` ReplyNumber int `json:"reply_number"` - ThreadInNewTab bool `json:"thread_in_new_tab"` - MaskNSFW bool `json:"mask_nsfw"` RetweetedByID string `json:"retweeted_by_id"` - DarkMode bool `json:"dark_mode"` } // Context hold information for mastodon context. diff --git a/renderer/model.go b/renderer/model.go index 64b9946..cc0a6ce 100644 --- a/renderer/model.go +++ b/renderer/model.go @@ -5,12 +5,17 @@ import ( "mastodon" ) +type Context struct { + MaskNSFW bool + FluorideMode bool + ThreadInNewTab bool + DarkMode bool +} + type HeaderData struct { Title string NotificationCount int CustomCSS string - FluorideMode bool - DarkMode bool } type NavbarData struct { diff --git a/renderer/renderer.go b/renderer/renderer.go index 42bffad..4d2c74d 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -1,7 +1,6 @@ package renderer import ( - "context" "io" "strconv" "strings" @@ -44,22 +43,26 @@ var ( } ) +type TemplateData struct { + Data interface{} + Ctx *Context +} + type Renderer interface { - RenderErrorPage(ctx context.Context, writer io.Writer, data *ErrorData) - RenderHomePage(ctx context.Context, writer io.Writer, data *HomePageData) (err error) - RenderSigninPage(ctx context.Context, writer io.Writer, data *SigninData) (err error) - RenderTimelinePage(ctx context.Context, writer io.Writer, data *TimelineData) (err error) - RenderThreadPage(ctx context.Context, writer io.Writer, data *ThreadData) (err error) - RenderNotificationPage(ctx context.Context, writer io.Writer, data *NotificationData) (err error) - RenderUserPage(ctx context.Context, writer io.Writer, data *UserData) (err error) - RenderAboutPage(ctx context.Context, writer io.Writer, data *AboutData) (err error) - RenderEmojiPage(ctx context.Context, writer io.Writer, data *EmojiData) (err error) - RenderLikedByPage(ctx context.Context, writer io.Writer, data *LikedByData) (err error) - RenderRetweetedByPage(ctx context.Context, writer io.Writer, data *RetweetedByData) (err error) - RenderFollowingPage(ctx context.Context, writer io.Writer, data *FollowingData) (err error) - RenderFollowersPage(ctx context.Context, writer io.Writer, data *FollowersData) (err error) - RenderSearchPage(ctx context.Context, writer io.Writer, data *SearchData) (err error) - RenderSettingsPage(ctx context.Context, writer io.Writer, data *SettingsData) (err error) + RenderSigninPage(ctx *Context, writer io.Writer, data *SigninData) (err error) + RenderErrorPage(ctx *Context, writer io.Writer, data *ErrorData) + RenderTimelinePage(ctx *Context, writer io.Writer, data *TimelineData) (err error) + RenderThreadPage(ctx *Context, writer io.Writer, data *ThreadData) (err error) + RenderNotificationPage(ctx *Context, writer io.Writer, data *NotificationData) (err error) + RenderUserPage(ctx *Context, writer io.Writer, data *UserData) (err error) + RenderAboutPage(ctx *Context, writer io.Writer, data *AboutData) (err error) + RenderEmojiPage(ctx *Context, writer io.Writer, data *EmojiData) (err error) + RenderLikedByPage(ctx *Context, writer io.Writer, data *LikedByData) (err error) + RenderRetweetedByPage(ctx *Context, writer io.Writer, data *RetweetedByData) (err error) + RenderFollowingPage(ctx *Context, writer io.Writer, data *FollowingData) (err error) + RenderFollowersPage(ctx *Context, writer io.Writer, data *FollowersData) (err error) + RenderSearchPage(ctx *Context, writer io.Writer, data *SearchData) (err error) + RenderSettingsPage(ctx *Context, writer io.Writer, data *SettingsData) (err error) } type renderer struct { @@ -76,6 +79,7 @@ func NewRenderer(templateGlobPattern string) (r *renderer, err error) { "FormatTimeRFC3339": FormatTimeRFC3339, "FormatTimeRFC822": FormatTimeRFC822, "GetIcon": GetIcon, + "WithContext": WithContext, }).ParseGlob(templateGlobPattern) if err != nil { return @@ -85,65 +89,61 @@ func NewRenderer(templateGlobPattern string) (r *renderer, err error) { }, nil } -func (r *renderer) RenderErrorPage(ctx context.Context, writer io.Writer, errorData *ErrorData) { - r.template.ExecuteTemplate(writer, "error.tmpl", errorData) +func (r *renderer) RenderSigninPage(ctx *Context, writer io.Writer, signinData *SigninData) (err error) { + return r.template.ExecuteTemplate(writer, "signin.tmpl", WithContext(signinData, ctx)) +} + +func (r *renderer) RenderErrorPage(ctx *Context, writer io.Writer, errorData *ErrorData) { + r.template.ExecuteTemplate(writer, "error.tmpl", WithContext(errorData, ctx)) return } -func (r *renderer) RenderHomePage(ctx context.Context, writer io.Writer, homePageData *HomePageData) (err error) { - return r.template.ExecuteTemplate(writer, "homepage.tmpl", homePageData) +func (r *renderer) RenderTimelinePage(ctx *Context, writer io.Writer, data *TimelineData) (err error) { + return r.template.ExecuteTemplate(writer, "timeline.tmpl", WithContext(data, ctx)) } -func (r *renderer) RenderSigninPage(ctx context.Context, writer io.Writer, signinData *SigninData) (err error) { - return r.template.ExecuteTemplate(writer, "signin.tmpl", signinData) +func (r *renderer) RenderThreadPage(ctx *Context, writer io.Writer, data *ThreadData) (err error) { + return r.template.ExecuteTemplate(writer, "thread.tmpl", WithContext(data, ctx)) } -func (r *renderer) RenderTimelinePage(ctx context.Context, writer io.Writer, data *TimelineData) (err error) { - return r.template.ExecuteTemplate(writer, "timeline.tmpl", data) +func (r *renderer) RenderNotificationPage(ctx *Context, writer io.Writer, data *NotificationData) (err error) { + return r.template.ExecuteTemplate(writer, "notification.tmpl", WithContext(data, ctx)) } -func (r *renderer) RenderThreadPage(ctx context.Context, writer io.Writer, data *ThreadData) (err error) { - return r.template.ExecuteTemplate(writer, "thread.tmpl", data) +func (r *renderer) RenderUserPage(ctx *Context, writer io.Writer, data *UserData) (err error) { + return r.template.ExecuteTemplate(writer, "user.tmpl", WithContext(data, ctx)) } -func (r *renderer) RenderNotificationPage(ctx context.Context, writer io.Writer, data *NotificationData) (err error) { - return r.template.ExecuteTemplate(writer, "notification.tmpl", data) +func (r *renderer) RenderAboutPage(ctx *Context, writer io.Writer, data *AboutData) (err error) { + return r.template.ExecuteTemplate(writer, "about.tmpl", WithContext(data, ctx)) } -func (r *renderer) RenderUserPage(ctx context.Context, writer io.Writer, data *UserData) (err error) { - return r.template.ExecuteTemplate(writer, "user.tmpl", data) +func (r *renderer) RenderEmojiPage(ctx *Context, writer io.Writer, data *EmojiData) (err error) { + return r.template.ExecuteTemplate(writer, "emoji.tmpl", WithContext(data, ctx)) } -func (r *renderer) RenderAboutPage(ctx context.Context, writer io.Writer, data *AboutData) (err error) { - return r.template.ExecuteTemplate(writer, "about.tmpl", data) +func (r *renderer) RenderLikedByPage(ctx *Context, writer io.Writer, data *LikedByData) (err error) { + return r.template.ExecuteTemplate(writer, "likedby.tmpl", WithContext(data, ctx)) } -func (r *renderer) RenderEmojiPage(ctx context.Context, writer io.Writer, data *EmojiData) (err error) { - return r.template.ExecuteTemplate(writer, "emoji.tmpl", data) +func (r *renderer) RenderRetweetedByPage(ctx *Context, writer io.Writer, data *RetweetedByData) (err error) { + return r.template.ExecuteTemplate(writer, "retweetedby.tmpl", WithContext(data, ctx)) } -func (r *renderer) RenderLikedByPage(ctx context.Context, writer io.Writer, data *LikedByData) (err error) { - return r.template.ExecuteTemplate(writer, "likedby.tmpl", data) +func (r *renderer) RenderFollowingPage(ctx *Context, writer io.Writer, data *FollowingData) (err error) { + return r.template.ExecuteTemplate(writer, "following.tmpl", WithContext(data, ctx)) } -func (r *renderer) RenderRetweetedByPage(ctx context.Context, writer io.Writer, data *RetweetedByData) (err error) { - return r.template.ExecuteTemplate(writer, "retweetedby.tmpl", data) +func (r *renderer) RenderFollowersPage(ctx *Context, writer io.Writer, data *FollowersData) (err error) { + return r.template.ExecuteTemplate(writer, "followers.tmpl", WithContext(data, ctx)) } -func (r *renderer) RenderFollowingPage(ctx context.Context, writer io.Writer, data *FollowingData) (err error) { - return r.template.ExecuteTemplate(writer, "following.tmpl", data) +func (r *renderer) RenderSearchPage(ctx *Context, writer io.Writer, data *SearchData) (err error) { + return r.template.ExecuteTemplate(writer, "search.tmpl", WithContext(data, ctx)) } -func (r *renderer) RenderFollowersPage(ctx context.Context, writer io.Writer, data *FollowersData) (err error) { - return r.template.ExecuteTemplate(writer, "followers.tmpl", data) -} - -func (r *renderer) RenderSearchPage(ctx context.Context, writer io.Writer, data *SearchData) (err error) { - return r.template.ExecuteTemplate(writer, "search.tmpl", data) -} - -func (r *renderer) RenderSettingsPage(ctx context.Context, writer io.Writer, data *SettingsData) (err error) { - return r.template.ExecuteTemplate(writer, "settings.tmpl", data) +func (r *renderer) RenderSettingsPage(ctx *Context, writer io.Writer, data *SettingsData) (err error) { + return r.template.ExecuteTemplate(writer, "settings.tmpl", WithContext(data, ctx)) } func EmojiFilter(content string, emojis []mastodon.Emoji) string { @@ -222,3 +222,7 @@ func GetIcon(name string, darkMode bool) (icon string) { icon, _ = icons[name] return } + +func WithContext(data interface{}, ctx *Context) TemplateData { + return TemplateData{data, ctx} +} 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 } diff --git a/templates/about.tmpl b/templates/about.tmpl index 60bb5f6..b3c9a49 100644 --- a/templates/about.tmpl +++ b/templates/about.tmpl @@ -1,5 +1,6 @@ -{{template "header.tmpl" .HeaderData}} -{{template "navigation.tmpl" .NavbarData}} +{{with .Data}} +{{template "header.tmpl" (WithContext .HeaderData $.Ctx)}} +{{template "navigation.tmpl" (WithContext .NavbarData $.Ctx)}}