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{}.
This commit is contained in:
Simon Ser 2019-12-09 16:17:36 +01:00
parent 4f74722c14
commit 9617b412e8
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48

View file

@ -16,6 +16,14 @@ import (
"github.com/labstack/echo/v4" "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 { func handleGetMailbox(ectx echo.Context) error {
ctx := ectx.(*context) ctx := ectx.(*context)
@ -58,12 +66,13 @@ func handleGetMailbox(ectx echo.Context) error {
nextPage = page + 1 nextPage = page + 1
} }
return ctx.Render(http.StatusOK, "mailbox.html", map[string]interface{}{ return ctx.Render(http.StatusOK, "mailbox.html", &MailboxRenderData{
"Mailbox": mbox, Mailbox: mbox,
"Mailboxes": mailboxes, Mailboxes: mailboxes,
"Messages": msgs, Messages: msgs,
"PrevPage": prevPage, PrevPage: prevPage,
"NextPage": nextPage, NextPage: nextPage,
Extra: make(map[string]interface{}),
}) })
} }
@ -108,6 +117,15 @@ func handleLogout(ectx echo.Context) error {
return ctx.Redirect(http.StatusFound, "/login") 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 { func handleGetPart(ctx *context, raw bool) error {
mboxName, uid, err := parseMboxAndUid(ctx.Param("mbox"), ctx.Param("uid")) mboxName, uid, err := parseMboxAndUid(ctx.Param("mbox"), ctx.Param("uid"))
if err != nil { if err != nil {
@ -166,15 +184,21 @@ func handleGetPart(ctx *context, raw bool) error {
body = string(b) body = string(b)
} }
return ctx.Render(http.StatusOK, "message.html", map[string]interface{}{ return ctx.Render(http.StatusOK, "message.html", &MessageRenderData{
"Mailbox": mbox, Mailbox: mbox,
"Message": msg, Message: msg,
"Body": body, Body: body,
"PartPath": partPathString, PartPath: partPathString,
"MailboxPage": (mbox.Messages - msg.SeqNum) / messagesPerPage, 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 { func handleCompose(ectx echo.Context) error {
ctx := ectx.(*context) ctx := ectx.(*context)
@ -269,7 +293,8 @@ func handleCompose(ectx echo.Context) error {
return ctx.Redirect(http.StatusFound, "/mailbox/INBOX") return ctx.Redirect(http.StatusFound, "/mailbox/INBOX")
} }
return ctx.Render(http.StatusOK, "compose.html", map[string]interface{}{ return ctx.Render(http.StatusOK, "compose.html", &ComposeRenderData{
"Message": &msg, Message: &msg,
Extra: make(map[string]interface{}),
}) })
} }