diff options
author | Drew DeVault <sir@cmpwn.com> | 2020-10-22 12:29:55 -0400 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2020-10-22 12:29:55 -0400 |
commit | 9e2c420461cef709811ac84f958fae20b93b800b (patch) | |
tree | d5add39f231a9edb86988173cc938f6a1b21532f /plugins/base/routes.go | |
parent | 8de661b3bb47eec32cfbfab52c404e9fd610e8e2 (diff) | |
download | alps-9e2c420461cef709811ac84f958fae20b93b800b.tar.gz alps-9e2c420461cef709811ac84f958fae20b93b800b.zip |
Categorize and normalize folder listing
Diffstat (limited to 'plugins/base/routes.go')
-rw-r--r-- | plugins/base/routes.go | 74 |
1 files changed, 61 insertions, 13 deletions
diff --git a/plugins/base/routes.go b/plugins/base/routes.go index b61b844..977b017 100644 --- a/plugins/base/routes.go +++ b/plugins/base/routes.go @@ -65,12 +65,57 @@ func registerRoutes(p *alps.GoPlugin) { type MailboxRenderData struct { alps.BaseRenderData - Mailbox *MailboxStatus - Inbox *MailboxStatus - Mailboxes []MailboxInfo - Messages []IMAPMessage - PrevPage, NextPage int - Query string + Mailbox *MailboxStatus + Inbox *MailboxStatus + CategorizedMailboxes CategorizedMailboxes + Mailboxes []MailboxInfo + Messages []IMAPMessage + PrevPage, NextPage int + Query string +} + +// Organizes mailboxes into common/uncommon categories +type CategorizedMailboxes struct { + Common struct { + Inbox *MailboxInfo + Drafts *MailboxInfo + Sent *MailboxInfo + Junk *MailboxInfo + Trash *MailboxInfo + Archive *MailboxInfo + } + Additional []*MailboxInfo +} + +func categorizeMailboxes(mailboxes []MailboxInfo, + inbox *MailboxStatus, active *MailboxStatus) CategorizedMailboxes { + + var out CategorizedMailboxes + mmap := map[string]**MailboxInfo{ + "INBOX": &out.Common.Inbox, + "Drafts": &out.Common.Drafts, + "Sent": &out.Common.Sent, + "Junk": &out.Common.Junk, + "Trash": &out.Common.Trash, + "Archive": &out.Common.Archive, + } + for i, _ := range mailboxes { + // Populate unseen & active states + if mailboxes[i].Name == active.Name { + mailboxes[i].Unseen = int(active.Unseen) + mailboxes[i].Active = true + } + if mailboxes[i].Name == inbox.Name { + mailboxes[i].Unseen = int(inbox.Unseen) + } + + if ptr, ok := mmap[mailboxes[i].Name]; ok { + *ptr = &mailboxes[i] + } else { + out.Additional = append(out.Additional, &mailboxes[i]) + } + } + return out } func handleGetMailbox(ctx *alps.Context) error { @@ -154,15 +199,18 @@ func handleGetMailbox(ctx *alps.Context) error { title = fmt.Sprintf("(%d) %s", mbox.Unseen, title) } + categorized := categorizeMailboxes(mailboxes, inbox, mbox) + return ctx.Render(http.StatusOK, "mailbox.html", &MailboxRenderData{ BaseRenderData: *alps.NewBaseRenderData(ctx).WithTitle(title), - Mailbox: mbox, - Inbox: inbox, - Mailboxes: mailboxes, - Messages: msgs, - PrevPage: prevPage, - NextPage: nextPage, - Query: query, + Mailbox: mbox, + Inbox: inbox, + CategorizedMailboxes: categorized, + Mailboxes: mailboxes, + Messages: msgs, + PrevPage: prevPage, + NextPage: nextPage, + Query: query, }) } |