From 2648484a1de4443fdb748edb144c90c4a7a37472 Mon Sep 17 00:00:00 2001 From: r Date: Sat, 21 Dec 2019 05:48:48 +0000 Subject: Add about page --- renderer/model.go | 10 ++++++++++ renderer/renderer.go | 4 ++++ service/auth.go | 8 ++++++++ service/logging.go | 8 ++++++++ service/service.go | 16 ++++++++++++++++ service/transport.go | 10 ++++++++++ templates/about.tmpl | 18 ++++++++++++++++++ templates/navigation.tmpl | 1 + 8 files changed, 75 insertions(+) create mode 100644 templates/about.tmpl diff --git a/renderer/model.go b/renderer/model.go index 7e52850..053c2fd 100644 --- a/renderer/model.go +++ b/renderer/model.go @@ -86,3 +86,13 @@ func NewUserPageTemplateData(user *mastodon.Account, statuses []*mastodon.Status NavbarData: navbarData, } } + +type AboutPageTemplateData struct { + NavbarData *NavbarTemplateData +} + +func NewAboutPageTemplateData(navbarData *NavbarTemplateData) *AboutPageTemplateData { + return &AboutPageTemplateData{ + NavbarData: navbarData, + } +} diff --git a/renderer/renderer.go b/renderer/renderer.go index 9bbe06e..c9cae28 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -19,6 +19,7 @@ type Renderer interface { RenderThreadPage(ctx context.Context, writer io.Writer, data *ThreadPageTemplateData) (err error) RenderNotificationPage(ctx context.Context, writer io.Writer, data *NotificationPageTemplateData) (err error) RenderUserPage(ctx context.Context, writer io.Writer, data *UserPageTemplateData) (err error) + RenderAboutPage(ctx context.Context, writer io.Writer, data *AboutPageTemplateData) (err error) } type renderer struct { @@ -71,6 +72,9 @@ func (r *renderer) RenderUserPage(ctx context.Context, writer io.Writer, data *U return r.template.ExecuteTemplate(writer, "user.tmpl", data) } +func (r *renderer) RenderAboutPage(ctx context.Context, writer io.Writer, data *AboutPageTemplateData) (err error) { + return r.template.ExecuteTemplate(writer, "about.tmpl", data) +} func EmojiFilter(content string, emojis []mastodon.Emoji) string { var replacements []string diff --git a/service/auth.go b/service/auth.go index 2b6fdd6..3cdf357 100644 --- a/service/auth.go +++ b/service/auth.go @@ -127,6 +127,14 @@ func (s *authService) ServeUserPage(ctx context.Context, client io.Writer, c *ma return s.Service.ServeUserPage(ctx, client, c, id, maxID, minID) } +func (s *authService) ServeAboutPage(ctx context.Context, client io.Writer, c *mastodon.Client) (err error) { + c, err = s.getClient(ctx) + if err != nil { + return + } + return s.Service.ServeAboutPage(ctx, client, c) +} + func (s *authService) Like(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) { c, err = s.getClient(ctx) if err != nil { diff --git a/service/logging.go b/service/logging.go index 9b398af..e0d47ed 100644 --- a/service/logging.go +++ b/service/logging.go @@ -93,6 +93,14 @@ func (s *loggingService) ServeUserPage(ctx context.Context, client io.Writer, c return s.Service.ServeUserPage(ctx, client, c, id, maxID, minID) } +func (s *loggingService) ServeAboutPage(ctx context.Context, client io.Writer, c *mastodon.Client) (err error) { + defer func(begin time.Time) { + s.logger.Printf("method=%v, took=%v, err=%v\n", + "ServeAboutPage", time.Since(begin), err) + }(time.Now()) + return s.Service.ServeAboutPage(ctx, client, c) +} + func (s *loggingService) Like(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) { defer func(begin time.Time) { s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n", diff --git a/service/service.go b/service/service.go index 63f74d3..4d74449 100644 --- a/service/service.go +++ b/service/service.go @@ -33,6 +33,7 @@ type Service interface { ServeThreadPage(ctx context.Context, client io.Writer, c *mastodon.Client, id string, reply bool) (err error) ServeNotificationPage(ctx context.Context, client io.Writer, c *mastodon.Client, maxID string, minID string) (err error) ServeUserPage(ctx context.Context, client io.Writer, c *mastodon.Client, id string, maxID string, minID string) (err error) + ServeAboutPage(ctx context.Context, client io.Writer, c *mastodon.Client) (err error) Like(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) UnLike(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) Retweet(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) @@ -411,6 +412,21 @@ func (svc *service) ServeUserPage(ctx context.Context, client io.Writer, c *mast return } +func (svc *service) ServeAboutPage(ctx context.Context, client io.Writer, c *mastodon.Client) (err error) { + navbarData, err := svc.getNavbarTemplateData(ctx, client, c) + if err != nil { + return + } + + data := renderer.NewAboutPageTemplateData(navbarData) + err = svc.renderer.RenderAboutPage(ctx, client, data) + if err != nil { + return + } + + return +} + func (svc *service) getNavbarTemplateData(ctx context.Context, client io.Writer, c *mastodon.Client) (data *renderer.NavbarTemplateData, err error) { notifications, err := c.GetNotifications(ctx, nil) if err != nil { diff --git a/service/transport.go b/service/transport.go index 6759fcc..2b75662 100644 --- a/service/transport.go +++ b/service/transport.go @@ -228,6 +228,16 @@ func NewHandler(s Service, staticDir string) http.Handler { w.WriteHeader(http.StatusFound) }).Methods(http.MethodPost) + r.HandleFunc("/about", func(w http.ResponseWriter, req *http.Request) { + ctx := getContextWithSession(context.Background(), req) + + err := s.ServeAboutPage(ctx, w, nil) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + }).Methods(http.MethodGet) + r.HandleFunc("/signout", func(w http.ResponseWriter, req *http.Request) { // TODO remove session from database w.Header().Add("Set-Cookie", fmt.Sprintf("session_id=;max-age=0")) diff --git a/templates/about.tmpl b/templates/about.tmpl new file mode 100644 index 0000000..0b4bea0 --- /dev/null +++ b/templates/about.tmpl @@ -0,0 +1,18 @@ +{{template "header.tmpl"}} +{{template "navigation.tmpl" .NavbarData}} +
About
+ +
+

+ A web client for Mastodon Network. +

+

+ The source code is released under + CC0 + and is available on + git.freesoftwareextremist.com/web. +

+
+ +{{template "footer.tmpl"}} + diff --git a/templates/navigation.tmpl b/templates/navigation.tmpl index d86971c..5338677 100644 --- a/templates/navigation.tmpl +++ b/templates/navigation.tmpl @@ -1,5 +1,6 @@ -- cgit v1.2.3