diff options
author | Drew DeVault <sir@cmpwn.com> | 2020-11-19 13:14:12 -0500 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2020-11-19 13:14:12 -0500 |
commit | 51d762ac5fd21c6cac898b2e5eed2eabdb0fe841 (patch) | |
tree | 9a62e4920c44dd7f3c5afcd49f9ced7573d7f4de /plugins | |
parent | 8cc742f45df7544c20f43e781d2c7e4aa6a3f4f5 (diff) | |
download | alps-51d762ac5fd21c6cac898b2e5eed2eabdb0fe841.tar.gz alps-51d762ac5fd21c6cac898b2e5eed2eabdb0fe841.zip |
Implement mailbox subscriptions
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/base/routes.go | 85 |
1 files changed, 72 insertions, 13 deletions
diff --git a/plugins/base/routes.go b/plugins/base/routes.go index 61d92a6..8c230a4 100644 --- a/plugins/base/routes.go +++ b/plugins/base/routes.go @@ -80,6 +80,7 @@ type IMAPBaseRenderData struct { Mailboxes []MailboxInfo Mailbox *MailboxStatus Inbox *MailboxStatus + Subscriptions map[string]*MailboxStatus } type MailboxRenderData struct { @@ -89,17 +90,22 @@ type MailboxRenderData struct { Query string } +type MailboxDetails struct { + Info *MailboxInfo + Status *MailboxStatus +} + // 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 + Inbox MailboxDetails + Drafts MailboxDetails + Sent MailboxDetails + Junk MailboxDetails + Trash MailboxDetails + Archive MailboxDetails + } + Additional []MailboxDetails } func newIMAPBaseRenderData(ctx *alps.Context, @@ -110,6 +116,12 @@ func newIMAPBaseRenderData(ctx *alps.Context, return nil, echo.NewHTTPError(http.StatusBadRequest, err) } + settings, err := loadSettings(ctx.Session.Store()) + if err != nil { + return nil, fmt.Errorf("failed to load settings: %v", err) + } + + subscriptions := make(map[string]*MailboxStatus) var mailboxes []MailboxInfo var active, inbox *MailboxStatus err = ctx.Session.DoIMAP(func(c *imapclient.Client) error { @@ -117,11 +129,13 @@ func newIMAPBaseRenderData(ctx *alps.Context, if mailboxes, err = listMailboxes(c); err != nil { return err } + if mboxName != "" { if active, err = getMailboxStatus(c, mboxName); err != nil { return echo.NewHTTPError(http.StatusNotFound, err) } } + if mboxName == "INBOX" { inbox = active } else { @@ -129,6 +143,14 @@ func newIMAPBaseRenderData(ctx *alps.Context, return err } } + + for _, sub := range settings.Subscriptions { + if status, err := getMailboxStatus(c, sub); err != nil { + return err + } else { + subscriptions[sub] = status + } + } return nil }) if err != nil { @@ -136,7 +158,7 @@ func newIMAPBaseRenderData(ctx *alps.Context, } var categorized CategorizedMailboxes - mmap := map[string]**MailboxInfo{ + mmap := map[string]*MailboxDetails{ "INBOX": &categorized.Common.Inbox, "Drafts": &categorized.Common.Drafts, "Sent": &categorized.Common.Sent, @@ -157,11 +179,16 @@ func newIMAPBaseRenderData(ctx *alps.Context, mailboxes[i].Total = int(inbox.Messages) } + status, _ := subscriptions[mailboxes[i].Name] if ptr, ok := mmap[mailboxes[i].Name]; ok { - *ptr = &mailboxes[i] + ptr.Info = &mailboxes[i] + ptr.Status = status } else { - categorized.Additional = append( - categorized.Additional, &mailboxes[i]) + categorized.Additional = append(categorized.Additional, + MailboxDetails{ + Info: &mailboxes[i], + Status: status, + }) } } @@ -171,6 +198,7 @@ func newIMAPBaseRenderData(ctx *alps.Context, Mailboxes: mailboxes, Inbox: inbox, Mailbox: active, + Subscriptions: subscriptions, }, nil } @@ -1156,6 +1184,7 @@ type Settings struct { MessagesPerPage int Signature string From string + Subscriptions []string } func loadSettings(s alps.Store) (*Settings, error) { @@ -1186,7 +1215,20 @@ func (s *Settings) check() error { type SettingsRenderData struct { alps.BaseRenderData - Settings *Settings + Mailboxes []MailboxInfo + Settings *Settings + Subscriptions Subscriptions +} + +type Subscriptions []string + +func (s Subscriptions) Has(sub string) bool { + for _, cand := range s { + if cand == sub { + return true + } + } + return false } func handleSettings(ctx *alps.Context) error { @@ -1195,6 +1237,15 @@ func handleSettings(ctx *alps.Context) error { return fmt.Errorf("failed to load settings: %v", err) } + var mailboxes []MailboxInfo + err = ctx.Session.DoIMAP(func(c *imapclient.Client) error { + mailboxes, err = listMailboxes(c) + return err + }) + if err != nil { + return err + } + if ctx.Request().Method == http.MethodPost { settings.MessagesPerPage, err = strconv.Atoi(ctx.FormValue("messages_per_page")) if err != nil { @@ -1203,6 +1254,12 @@ func handleSettings(ctx *alps.Context) error { settings.Signature = ctx.FormValue("signature") settings.From = ctx.FormValue("from") + params, err := ctx.FormParams() + if err != nil { + return err + } + settings.Subscriptions = params["subscriptions"] + if err := settings.check(); err != nil { return echo.NewHTTPError(http.StatusBadRequest, err) } @@ -1216,5 +1273,7 @@ func handleSettings(ctx *alps.Context) error { return ctx.Render(http.StatusOK, "settings.html", &SettingsRenderData{ BaseRenderData: *alps.NewBaseRenderData(ctx), Settings: settings, + Mailboxes: mailboxes, + Subscriptions: Subscriptions(settings.Subscriptions), }) } |