From 9617b412e8f1a2e3c3d112bbf5ece29bc2af37ee Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 9 Dec 2019 16:17:36 +0100 Subject: [PATCH] Make render data static This makes sure fields filled by vanilla koushin keep the right type. A new Extra field allows plugins to attach their own data. This also makes it easier to write Go plugins, which would have trouble working with interface{}. --- handlers.go | 53 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/handlers.go b/handlers.go index 4615aa4..238b9ba 100644 --- a/handlers.go +++ b/handlers.go @@ -16,6 +16,14 @@ import ( "github.com/labstack/echo/v4" ) +type MailboxRenderData struct { + Mailbox *imap.MailboxStatus + Mailboxes []*imap.MailboxInfo + Messages []imapMessage + PrevPage, NextPage int + Extra map[string]interface{} +} + func handleGetMailbox(ectx echo.Context) error { ctx := ectx.(*context) @@ -58,12 +66,13 @@ func handleGetMailbox(ectx echo.Context) error { nextPage = page + 1 } - return ctx.Render(http.StatusOK, "mailbox.html", map[string]interface{}{ - "Mailbox": mbox, - "Mailboxes": mailboxes, - "Messages": msgs, - "PrevPage": prevPage, - "NextPage": nextPage, + return ctx.Render(http.StatusOK, "mailbox.html", &MailboxRenderData{ + Mailbox: mbox, + Mailboxes: mailboxes, + Messages: msgs, + PrevPage: prevPage, + NextPage: nextPage, + Extra: make(map[string]interface{}), }) } @@ -108,6 +117,15 @@ func handleLogout(ectx echo.Context) error { return ctx.Redirect(http.StatusFound, "/login") } +type MessageRenderData struct { + Mailbox *imap.MailboxStatus + Message *imapMessage + Body string + PartPath string + MailboxPage int + Extra map[string]interface{} +} + func handleGetPart(ctx *context, raw bool) error { mboxName, uid, err := parseMboxAndUid(ctx.Param("mbox"), ctx.Param("uid")) if err != nil { @@ -166,15 +184,21 @@ func handleGetPart(ctx *context, raw bool) error { body = string(b) } - return ctx.Render(http.StatusOK, "message.html", map[string]interface{}{ - "Mailbox": mbox, - "Message": msg, - "Body": body, - "PartPath": partPathString, - "MailboxPage": (mbox.Messages - msg.SeqNum) / messagesPerPage, + return ctx.Render(http.StatusOK, "message.html", &MessageRenderData{ + Mailbox: mbox, + Message: msg, + Body: body, + PartPath: partPathString, + MailboxPage: int(mbox.Messages-msg.SeqNum) / messagesPerPage, + Extra: make(map[string]interface{}), }) } +type ComposeRenderData struct { + Message *OutgoingMessage + Extra map[string]interface{} +} + func handleCompose(ectx echo.Context) error { ctx := ectx.(*context) @@ -269,7 +293,8 @@ func handleCompose(ectx echo.Context) error { return ctx.Redirect(http.StatusFound, "/mailbox/INBOX") } - return ctx.Render(http.StatusOK, "compose.html", map[string]interface{}{ - "Message": &msg, + return ctx.Render(http.StatusOK, "compose.html", &ComposeRenderData{ + Message: &msg, + Extra: make(map[string]interface{}), }) }