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
diff --git a/templates/thread.tmpl b/templates/thread.tmpl index afb307b..7ec0e94 100644 --- a/templates/thread.tmpl +++ b/templates/thread.tmpl @@ -1,4 +1,4 @@ -{{template "header.tmpl"}} +{{template "header.tmpl" .HeaderData}} {{template "navigation.tmpl" .NavbarData}}
Thread
diff --git a/templates/timeline.tmpl b/templates/timeline.tmpl index 5bff2d0..b688501 100644 --- a/templates/timeline.tmpl +++ b/templates/timeline.tmpl @@ -1,4 +1,4 @@ -{{template "header.tmpl"}} +{{template "header.tmpl" .HeaderData}} {{template "navigation.tmpl" .NavbarData}}
{{.Title}}
diff --git a/templates/user.tmpl b/templates/user.tmpl index c7aa1e8..fa19338 100644 --- a/templates/user.tmpl +++ b/templates/user.tmpl @@ -1,4 +1,4 @@ -{{template "header.tmpl"}} +{{template "header.tmpl" .HeaderData}} {{template "navigation.tmpl" .NavbarData}}
User