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

View File

@ -15,12 +15,12 @@ type Renderer interface {
RenderErrorPage(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)
RenderTimelinePage(ctx context.Context, writer io.Writer, data *TimelinePageTemplateData) (err error)
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)
RenderEmojiPage(ctx context.Context, writer io.Writer, data *EmojiPageTemplateData) (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)
}
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)
}
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)
}
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)
}
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)
}
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)
}
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)
}
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)
}

View File

@ -277,7 +277,17 @@ func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer,
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)
if err != nil {
return
@ -344,7 +354,13 @@ func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *mo
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)
if err != nil {
return
@ -398,7 +414,12 @@ func (svc *service) ServeNotificationPage(ctx context.Context, client io.Writer,
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)
if err != nil {
return
@ -437,7 +458,14 @@ func (svc *service) ServeUserPage(ctx context.Context, client io.Writer, c *mode
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)
if err != nil {
return
@ -452,7 +480,9 @@ func (svc *service) ServeAboutPage(ctx context.Context, client io.Writer, c *mod
return
}
data := renderer.NewAboutPageTemplateData(navbarData)
data := &renderer.AboutData{
NavbarData: navbarData,
}
err = svc.renderer.RenderAboutPage(ctx, client, data)
if err != nil {
return
@ -472,7 +502,11 @@ func (svc *service) ServeEmojiPage(ctx context.Context, client io.Writer, c *mod
return
}
data := renderer.NewEmojiPageTemplateData(navbarData, emojis)
data := &renderer.EmojiData{
Emojis: emojis,
NavbarData: navbarData,
}
err = svc.renderer.RenderEmojiPage(ctx, client, data)
if err != nil {
return
@ -481,7 +515,7 @@ 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.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)
if err != nil {
return
@ -499,7 +533,10 @@ func (svc *service) getNavbarTemplateData(ctx context.Context, client io.Writer,
return
}
data = renderer.NewNavbarTemplateData(notificationCount, u)
data = &renderer.NavbarData{
User: u,
NotificationCount: notificationCount,
}
return
}