Refector render structs

This commit is contained in:
r 2019-12-25 14:13:59 +00:00
parent c18a1be21a
commit bde2c03495
3 changed files with 70 additions and 95 deletions

View File

@ -5,19 +5,12 @@ import (
"web/model" "web/model"
) )
type NavbarTemplateData struct { type NavbarData struct {
User *mastodon.Account User *mastodon.Account
NotificationCount int NotificationCount int
} }
func NewNavbarTemplateData(notificationCount int, user *mastodon.Account) *NavbarTemplateData { type TimelineData struct {
return &NavbarTemplateData{
NotificationCount: notificationCount,
User: user,
}
}
type TimelinePageTemplateData struct {
Title string Title string
Statuses []*mastodon.Status Statuses []*mastodon.Status
HasNext bool HasNext bool
@ -25,91 +18,36 @@ type TimelinePageTemplateData struct {
HasPrev bool HasPrev bool
PrevLink string PrevLink string
PostContext model.PostContext PostContext model.PostContext
NavbarData *NavbarTemplateData NavbarData *NavbarData
} }
func NewTimelinePageTemplateData(title string, statuses []*mastodon.Status, hasNext bool, nextLink string, hasPrev bool, type ThreadData struct {
prevLink string, postContext model.PostContext, navbarData *NavbarTemplateData) *TimelinePageTemplateData {
return &TimelinePageTemplateData{
Title: title,
Statuses: statuses,
HasNext: hasNext,
NextLink: nextLink,
HasPrev: hasPrev,
PrevLink: prevLink,
PostContext: postContext,
NavbarData: navbarData,
}
}
type ThreadPageTemplateData struct {
Statuses []*mastodon.Status Statuses []*mastodon.Status
PostContext model.PostContext PostContext model.PostContext
ReplyMap map[string][]mastodon.ReplyInfo ReplyMap map[string][]mastodon.ReplyInfo
NavbarData *NavbarTemplateData NavbarData *NavbarData
} }
func NewThreadPageTemplateData(statuses []*mastodon.Status, postContext model.PostContext, replyMap map[string][]mastodon.ReplyInfo, navbarData *NavbarTemplateData) *ThreadPageTemplateData { type NotificationData struct {
return &ThreadPageTemplateData{
Statuses: statuses,
PostContext: postContext,
ReplyMap: replyMap,
NavbarData: navbarData,
}
}
type NotificationPageTemplateData struct {
Notifications []*mastodon.Notification Notifications []*mastodon.Notification
HasNext bool HasNext bool
NextLink string NextLink string
NavbarData *NavbarTemplateData NavbarData *NavbarData
} }
func NewNotificationPageTemplateData(notifications []*mastodon.Notification, hasNext bool, nextLink string, navbarData *NavbarTemplateData) *NotificationPageTemplateData { type UserData struct {
return &NotificationPageTemplateData{
Notifications: notifications,
HasNext: hasNext,
NextLink: nextLink,
NavbarData: navbarData,
}
}
type UserPageTemplateData struct {
User *mastodon.Account User *mastodon.Account
Statuses []*mastodon.Status Statuses []*mastodon.Status
HasNext bool HasNext bool
NextLink string NextLink string
NavbarData *NavbarTemplateData NavbarData *NavbarData
} }
func NewUserPageTemplateData(user *mastodon.Account, statuses []*mastodon.Status, hasNext bool, nextLink string, navbarData *NavbarTemplateData) *UserPageTemplateData { type AboutData struct {
return &UserPageTemplateData{ NavbarData *NavbarData
User: user,
Statuses: statuses,
HasNext: hasNext,
NextLink: nextLink,
NavbarData: navbarData,
}
} }
type AboutPageTemplateData struct { type EmojiData struct {
NavbarData *NavbarTemplateData
}
func NewAboutPageTemplateData(navbarData *NavbarTemplateData) *AboutPageTemplateData {
return &AboutPageTemplateData{
NavbarData: navbarData,
}
}
type EmojiPageTemplateData struct {
NavbarData *NavbarTemplateData
Emojis []*mastodon.Emoji Emojis []*mastodon.Emoji
} NavbarData *NavbarData
func NewEmojiPageTemplateData(navbarData *NavbarTemplateData, emojis []*mastodon.Emoji) *EmojiPageTemplateData {
return &EmojiPageTemplateData{
NavbarData: navbarData,
Emojis: emojis,
}
} }

View File

@ -15,12 +15,12 @@ type Renderer interface {
RenderErrorPage(ctx context.Context, writer io.Writer, err error) RenderErrorPage(ctx context.Context, writer io.Writer, err error)
RenderHomePage(ctx context.Context, writer io.Writer) (err error) RenderHomePage(ctx context.Context, writer io.Writer) (err error)
RenderSigninPage(ctx context.Context, writer io.Writer) (err error) RenderSigninPage(ctx context.Context, writer io.Writer) (err error)
RenderTimelinePage(ctx context.Context, writer io.Writer, data *TimelinePageTemplateData) (err error) RenderTimelinePage(ctx context.Context, writer io.Writer, data *TimelineData) (err error)
RenderThreadPage(ctx context.Context, writer io.Writer, data *ThreadPageTemplateData) (err error) RenderThreadPage(ctx context.Context, writer io.Writer, data *ThreadData) (err error)
RenderNotificationPage(ctx context.Context, writer io.Writer, data *NotificationPageTemplateData) (err error) RenderNotificationPage(ctx context.Context, writer io.Writer, data *NotificationData) (err error)
RenderUserPage(ctx context.Context, writer io.Writer, data *UserPageTemplateData) (err error) RenderUserPage(ctx context.Context, writer io.Writer, data *UserData) (err error)
RenderAboutPage(ctx context.Context, writer io.Writer, data *AboutPageTemplateData) (err error) RenderAboutPage(ctx context.Context, writer io.Writer, data *AboutData) (err error)
RenderEmojiPage(ctx context.Context, writer io.Writer, data *EmojiPageTemplateData) (err error) RenderEmojiPage(ctx context.Context, writer io.Writer, data *EmojiData) (err error)
} }
type renderer struct { type renderer struct {
@ -57,27 +57,27 @@ func (r *renderer) RenderSigninPage(ctx context.Context, writer io.Writer) (err
return r.template.ExecuteTemplate(writer, "signin.tmpl", nil) return r.template.ExecuteTemplate(writer, "signin.tmpl", nil)
} }
func (r *renderer) RenderTimelinePage(ctx context.Context, writer io.Writer, data *TimelinePageTemplateData) (err error) { func (r *renderer) RenderTimelinePage(ctx context.Context, writer io.Writer, data *TimelineData) (err error) {
return r.template.ExecuteTemplate(writer, "timeline.tmpl", data) return r.template.ExecuteTemplate(writer, "timeline.tmpl", data)
} }
func (r *renderer) RenderThreadPage(ctx context.Context, writer io.Writer, data *ThreadPageTemplateData) (err error) { func (r *renderer) RenderThreadPage(ctx context.Context, writer io.Writer, data *ThreadData) (err error) {
return r.template.ExecuteTemplate(writer, "thread.tmpl", data) return r.template.ExecuteTemplate(writer, "thread.tmpl", data)
} }
func (r *renderer) RenderNotificationPage(ctx context.Context, writer io.Writer, data *NotificationPageTemplateData) (err error) { func (r *renderer) RenderNotificationPage(ctx context.Context, writer io.Writer, data *NotificationData) (err error) {
return r.template.ExecuteTemplate(writer, "notification.tmpl", data) return r.template.ExecuteTemplate(writer, "notification.tmpl", data)
} }
func (r *renderer) RenderUserPage(ctx context.Context, writer io.Writer, data *UserPageTemplateData) (err error) { func (r *renderer) RenderUserPage(ctx context.Context, writer io.Writer, data *UserData) (err error) {
return r.template.ExecuteTemplate(writer, "user.tmpl", data) return r.template.ExecuteTemplate(writer, "user.tmpl", data)
} }
func (r *renderer) RenderAboutPage(ctx context.Context, writer io.Writer, data *AboutPageTemplateData) (err error) { func (r *renderer) RenderAboutPage(ctx context.Context, writer io.Writer, data *AboutData) (err error) {
return r.template.ExecuteTemplate(writer, "about.tmpl", data) return r.template.ExecuteTemplate(writer, "about.tmpl", data)
} }
func (r *renderer) RenderEmojiPage(ctx context.Context, writer io.Writer, data *EmojiPageTemplateData) (err error) { func (r *renderer) RenderEmojiPage(ctx context.Context, writer io.Writer, data *EmojiData) (err error) {
return r.template.ExecuteTemplate(writer, "emoji.tmpl", data) return r.template.ExecuteTemplate(writer, "emoji.tmpl", data)
} }

View File

@ -277,7 +277,17 @@ func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer,
return return
} }
data := renderer.NewTimelinePageTemplateData(title, statuses, hasNext, nextLink, hasPrev, prevLink, postContext, navbarData) data := &renderer.TimelineData{
Title: title,
Statuses: statuses,
HasNext: hasNext,
NextLink: nextLink,
HasPrev: hasPrev,
PrevLink: prevLink,
PostContext: postContext,
NavbarData: navbarData,
}
err = svc.renderer.RenderTimelinePage(ctx, client, data) err = svc.renderer.RenderTimelinePage(ctx, client, data)
if err != nil { if err != nil {
return return
@ -344,7 +354,13 @@ func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *mo
return return
} }
data := renderer.NewThreadPageTemplateData(statuses, postContext, replyMap, navbarData) data := &renderer.ThreadData{
Statuses: statuses,
PostContext: postContext,
ReplyMap: replyMap,
NavbarData: navbarData,
}
err = svc.renderer.RenderThreadPage(ctx, client, data) err = svc.renderer.RenderThreadPage(ctx, client, data)
if err != nil { if err != nil {
return return
@ -398,7 +414,12 @@ func (svc *service) ServeNotificationPage(ctx context.Context, client io.Writer,
return return
} }
data := renderer.NewNotificationPageTemplateData(notifications, hasNext, nextLink, navbarData) data := &renderer.NotificationData{
Notifications: notifications,
HasNext: hasNext,
NextLink: nextLink,
NavbarData: navbarData,
}
err = svc.renderer.RenderNotificationPage(ctx, client, data) err = svc.renderer.RenderNotificationPage(ctx, client, data)
if err != nil { if err != nil {
return return
@ -437,7 +458,14 @@ func (svc *service) ServeUserPage(ctx context.Context, client io.Writer, c *mode
return return
} }
data := renderer.NewUserPageTemplateData(user, statuses, hasNext, nextLink, navbarData) data := &renderer.UserData{
User: user,
Statuses: statuses,
HasNext: hasNext,
NextLink: nextLink,
NavbarData: navbarData,
}
err = svc.renderer.RenderUserPage(ctx, client, data) err = svc.renderer.RenderUserPage(ctx, client, data)
if err != nil { if err != nil {
return return
@ -452,7 +480,9 @@ func (svc *service) ServeAboutPage(ctx context.Context, client io.Writer, c *mod
return return
} }
data := renderer.NewAboutPageTemplateData(navbarData) data := &renderer.AboutData{
NavbarData: navbarData,
}
err = svc.renderer.RenderAboutPage(ctx, client, data) err = svc.renderer.RenderAboutPage(ctx, client, data)
if err != nil { if err != nil {
return return
@ -472,7 +502,11 @@ func (svc *service) ServeEmojiPage(ctx context.Context, client io.Writer, c *mod
return return
} }
data := renderer.NewEmojiPageTemplateData(navbarData, emojis) data := &renderer.EmojiData{
Emojis: emojis,
NavbarData: navbarData,
}
err = svc.renderer.RenderEmojiPage(ctx, client, data) err = svc.renderer.RenderEmojiPage(ctx, client, data)
if err != nil { if err != nil {
return return
@ -481,7 +515,7 @@ func (svc *service) ServeEmojiPage(ctx context.Context, client io.Writer, c *mod
return return
} }
func (svc *service) getNavbarTemplateData(ctx context.Context, client io.Writer, c *model.Client) (data *renderer.NavbarTemplateData, err error) { func (svc *service) getNavbarTemplateData(ctx context.Context, client io.Writer, c *model.Client) (data *renderer.NavbarData, err error) {
notifications, err := c.GetNotifications(ctx, nil) notifications, err := c.GetNotifications(ctx, nil)
if err != nil { if err != nil {
return return
@ -499,7 +533,10 @@ func (svc *service) getNavbarTemplateData(ctx context.Context, client io.Writer,
return return
} }
data = renderer.NewNavbarTemplateData(notificationCount, u) data = &renderer.NavbarData{
User: u,
NotificationCount: notificationCount,
}
return return
} }