From 656ff3931cf86c50a0b54da43cbf5f68344c58e0 Mon Sep 17 00:00:00 2001 From: r Date: Wed, 25 Dec 2019 16:38:47 +0000 Subject: [PATCH] Update header template and add option for custom css --- config/config.go | 4 +++ default.conf | 1 + main.go | 9 ++++- renderer/model.go | 23 ++++++++---- service/service.go | 72 ++++++++++++++++++++++--------------- static/custom.css | 3 ++ templates/about.tmpl | 2 +- templates/emoji.tmpl | 2 +- templates/error.tmpl | 2 +- templates/header.tmpl | 7 ++-- templates/homepage.tmpl | 2 +- templates/notification.tmpl | 2 +- templates/signin.tmpl | 2 +- templates/thread.tmpl | 2 +- templates/timeline.tmpl | 2 +- templates/user.tmpl | 2 +- 16 files changed, 90 insertions(+), 47 deletions(-) create mode 100644 static/custom.css diff --git a/config/config.go b/config/config.go index 844672a..17fceca 100644 --- a/config/config.go +++ b/config/config.go @@ -16,6 +16,7 @@ type config struct { StaticDirectory string TemplatesGlobPattern string DatabasePath string + CustomCSS string Logfile string } @@ -41,6 +42,7 @@ func getDefaultConfig() *config { StaticDirectory: "static", TemplatesGlobPattern: "templates/*", DatabasePath: "database.db", + CustomCSS: "", Logfile: "", } } @@ -83,6 +85,8 @@ func Parse(r io.Reader) (c *config, err error) { c.TemplatesGlobPattern = val case "database_path": c.DatabasePath = val + case "custom_css": + c.CustomCSS = val case "logfile": c.Logfile = val default: diff --git a/default.conf b/default.conf index a50ad6f..eede82b 100644 --- a/default.conf +++ b/default.conf @@ -4,4 +4,5 @@ client_scope=read write follow client_website=http://localhost:8080 static_directory=static templates_glob_pattern=templates/* +#custom_css=custom.css database_path=database diff --git a/main.go b/main.go index ad62976..f9fdc8f 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "net/http" "os" "path/filepath" + "strings" "time" "web/config" @@ -52,6 +53,12 @@ func main() { sessionRepo := repository.NewSessionRepository(sessionDB) appRepo := repository.NewAppRepository(appDB) + customCSS := config.CustomCSS + if !strings.HasPrefix(customCSS, "http://") && + !strings.HasPrefix(customCSS, "https://") { + customCSS = "/static/" + customCSS + } + var logger *log.Logger if len(config.Logfile) < 1 { logger = log.New(os.Stdout, "", log.LstdFlags) @@ -64,7 +71,7 @@ func main() { logger = log.New(lf, "", log.LstdFlags) } - s := service.NewService(config.ClientName, config.ClientScope, config.ClientWebsite, renderer, sessionRepo, appRepo) + s := service.NewService(config.ClientName, config.ClientScope, config.ClientWebsite, customCSS, renderer, sessionRepo, appRepo) s = service.NewAuthService(sessionRepo, appRepo, s) s = service.NewLoggingService(logger, s) handler := service.NewHandler(s, config.StaticDirectory) diff --git a/renderer/model.go b/renderer/model.go index dfa46b1..4dfac84 100644 --- a/renderer/model.go +++ b/renderer/model.go @@ -5,12 +5,24 @@ import ( "web/model" ) +type HeaderData struct { + Title string + NotificationCount int + CustomCSS string +} + type NavbarData struct { User *mastodon.Account NotificationCount int } +type CommonData struct { + HeaderData *HeaderData + NavbarData *NavbarData +} + type TimelineData struct { + *CommonData Title string Statuses []*mastodon.Status HasNext bool @@ -18,36 +30,35 @@ type TimelineData struct { HasPrev bool PrevLink string PostContext model.PostContext - NavbarData *NavbarData } type ThreadData struct { + *CommonData Statuses []*mastodon.Status PostContext model.PostContext ReplyMap map[string][]mastodon.ReplyInfo - NavbarData *NavbarData } type NotificationData struct { + *CommonData Notifications []*mastodon.Notification HasNext bool NextLink string - NavbarData *NavbarData } type UserData struct { + *CommonData User *mastodon.Account Statuses []*mastodon.Status HasNext bool NextLink string - NavbarData *NavbarData } type AboutData struct { - NavbarData *NavbarData + *CommonData } type EmojiData struct { Emojis []*mastodon.Emoji - NavbarData *NavbarData + CommonData *CommonData } diff --git a/service/service.go b/service/service.go index c268b75..ea0d078 100644 --- a/service/service.go +++ b/service/service.go @@ -50,18 +50,20 @@ type service struct { clientName string clientScope string clientWebsite string + customCSS string renderer renderer.Renderer sessionRepo model.SessionRepository appRepo model.AppRepository } func NewService(clientName string, clientScope string, clientWebsite string, - renderer renderer.Renderer, sessionRepo model.SessionRepository, + customCSS string, renderer renderer.Renderer, sessionRepo model.SessionRepository, appRepo model.AppRepository) Service { return &service{ clientName: clientName, clientScope: clientScope, clientWebsite: clientWebsite, + customCSS: customCSS, renderer: renderer, sessionRepo: sessionRepo, appRepo: appRepo, @@ -272,7 +274,7 @@ func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer, DefaultVisibility: c.Session.Settings.DefaultVisibility, } - navbarData, err := svc.getNavbarTemplateData(ctx, client, c) + commonData, err := svc.getCommonData(ctx, client, c) if err != nil { return } @@ -285,7 +287,7 @@ func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer, HasPrev: hasPrev, PrevLink: prevLink, PostContext: postContext, - NavbarData: navbarData, + CommonData: commonData, } err = svc.renderer.RenderTimelinePage(ctx, client, data) @@ -349,7 +351,7 @@ func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *mo addToReplyMap(replyMap, statuses[i].InReplyToID, statuses[i].ID, i+1) } - navbarData, err := svc.getNavbarTemplateData(ctx, client, c) + commonData, err := svc.getCommonData(ctx, client, c) if err != nil { return } @@ -358,7 +360,7 @@ func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *mo Statuses: statuses, PostContext: postContext, ReplyMap: replyMap, - NavbarData: navbarData, + CommonData: commonData, } err = svc.renderer.RenderThreadPage(ctx, client, data) @@ -409,7 +411,7 @@ func (svc *service) ServeNotificationPage(ctx context.Context, client io.Writer, nextLink = "/notifications?max_id=" + pg.MaxID } - navbarData, err := svc.getNavbarTemplateData(ctx, client, c) + commonData, err := svc.getCommonData(ctx, client, c) if err != nil { return } @@ -418,7 +420,7 @@ func (svc *service) ServeNotificationPage(ctx context.Context, client io.Writer, Notifications: notifications, HasNext: hasNext, NextLink: nextLink, - NavbarData: navbarData, + CommonData: commonData, } err = svc.renderer.RenderNotificationPage(ctx, client, data) if err != nil { @@ -453,7 +455,7 @@ func (svc *service) ServeUserPage(ctx context.Context, client io.Writer, c *mode nextLink = "/user/" + id + "?max_id=" + pg.MaxID } - navbarData, err := svc.getNavbarTemplateData(ctx, client, c) + commonData, err := svc.getCommonData(ctx, client, c) if err != nil { return } @@ -463,7 +465,7 @@ func (svc *service) ServeUserPage(ctx context.Context, client io.Writer, c *mode Statuses: statuses, HasNext: hasNext, NextLink: nextLink, - NavbarData: navbarData, + CommonData: commonData, } err = svc.renderer.RenderUserPage(ctx, client, data) @@ -475,13 +477,13 @@ func (svc *service) ServeUserPage(ctx context.Context, client io.Writer, c *mode } func (svc *service) ServeAboutPage(ctx context.Context, client io.Writer, c *model.Client) (err error) { - navbarData, err := svc.getNavbarTemplateData(ctx, client, c) + commonData, err := svc.getCommonData(ctx, client, c) if err != nil { return } data := &renderer.AboutData{ - NavbarData: navbarData, + CommonData: commonData, } err = svc.renderer.RenderAboutPage(ctx, client, data) if err != nil { @@ -492,7 +494,7 @@ func (svc *service) ServeAboutPage(ctx context.Context, client io.Writer, c *mod } func (svc *service) ServeEmojiPage(ctx context.Context, client io.Writer, c *model.Client) (err error) { - navbarData, err := svc.getNavbarTemplateData(ctx, client, c) + commonData, err := svc.getCommonData(ctx, client, c) if err != nil { return } @@ -504,7 +506,7 @@ func (svc *service) ServeEmojiPage(ctx context.Context, client io.Writer, c *mod data := &renderer.EmojiData{ Emojis: emojis, - NavbarData: navbarData, + CommonData: commonData, } err = svc.renderer.RenderEmojiPage(ctx, client, data) @@ -515,27 +517,39 @@ func (svc *service) ServeEmojiPage(ctx context.Context, client io.Writer, c *mod return } -func (svc *service) getNavbarTemplateData(ctx context.Context, client io.Writer, c *model.Client) (data *renderer.NavbarData, err error) { - notifications, err := c.GetNotifications(ctx, nil) - if err != nil { - return +func (svc *service) getCommonData(ctx context.Context, client io.Writer, c *model.Client) (data *renderer.CommonData, err error) { + data = new(renderer.CommonData) + + data.HeaderData = &renderer.HeaderData{ + Title: "Web", + NotificationCount: 0, + CustomCSS: svc.customCSS, } - var notificationCount int - for i := range notifications { - if notifications[i].Pleroma != nil && !notifications[i].Pleroma.IsSeen { - notificationCount++ + if c != nil && c.Session.IsLoggedIn() { + notifications, err := c.GetNotifications(ctx, nil) + if err != nil { + return nil, err } - } - u, err := c.GetAccountCurrentUser(ctx) - if err != nil { - return - } + var notificationCount int + for i := range notifications { + if notifications[i].Pleroma != nil && !notifications[i].Pleroma.IsSeen { + notificationCount++ + } + } - data = &renderer.NavbarData{ - User: u, - NotificationCount: notificationCount, + u, err := c.GetAccountCurrentUser(ctx) + if err != nil { + return nil, err + } + + data.NavbarData = &renderer.NavbarData{ + User: u, + NotificationCount: notificationCount, + } + + data.HeaderData.NotificationCount = notificationCount } return diff --git a/static/custom.css b/static/custom.css new file mode 100644 index 0000000..a1c192a --- /dev/null +++ b/static/custom.css @@ -0,0 +1,3 @@ +html { + background: #000000; +} diff --git a/templates/about.tmpl b/templates/about.tmpl index 0b4bea0..6038501 100644 --- a/templates/about.tmpl +++ b/templates/about.tmpl @@ -1,4 +1,4 @@ -{{template "header.tmpl"}} +{{template "header.tmpl" .HeaderData}} {{template "navigation.tmpl" .NavbarData}}
About
diff --git a/templates/emoji.tmpl b/templates/emoji.tmpl index a0cf263..aba4726 100644 --- a/templates/emoji.tmpl +++ b/templates/emoji.tmpl @@ -1,4 +1,4 @@ -{{template "header.tmpl"}} +{{template "header.tmpl" .HeaderData}} {{template "navigation.tmpl" .NavbarData}}
Emojis
diff --git a/templates/error.tmpl b/templates/error.tmpl index d4bd817..0389cdd 100644 --- a/templates/error.tmpl +++ b/templates/error.tmpl @@ -1,4 +1,4 @@ -{{template "header.tmpl"}} +{{template "header.tmpl" .HeaderData}}
Error
{{.}}
diff --git a/templates/header.tmpl b/templates/header.tmpl index 9334e81..8d05014 100644 --- a/templates/header.tmpl +++ b/templates/header.tmpl @@ -3,8 +3,11 @@ - Web - + {{if gt .NotificationCount 0}}({{.NotificationCount}}) {{end}}{{.Title}} + + {{if .CustomCSS}} + + {{end}} diff --git a/templates/homepage.tmpl b/templates/homepage.tmpl index 63e613a..5e8e8a0 100644 --- a/templates/homepage.tmpl +++ b/templates/homepage.tmpl @@ -1,4 +1,4 @@ -{{template "header.tmpl"}} +{{template "header.tmpl" .HeaderData}}
Home
Signin {{template "footer.tmpl"}} diff --git a/templates/notification.tmpl b/templates/notification.tmpl index 259328a..c4f6f28 100644 --- a/templates/notification.tmpl +++ b/templates/notification.tmpl @@ -1,4 +1,4 @@ -{{template "header.tmpl"}} +{{template "header.tmpl" .HeaderData}} {{template "navigation.tmpl" .NavbarData}}
Notifications
diff --git a/templates/signin.tmpl b/templates/signin.tmpl index fb14a66..5dd03f6 100644 --- a/templates/signin.tmpl +++ b/templates/signin.tmpl @@ -1,4 +1,4 @@ -{{template "header.tmpl"}} +{{template "header.tmpl" .HeaderData}}
Signin