aboutsummaryrefslogtreecommitdiff
path: root/worker/imap/imap.go
diff options
context:
space:
mode:
authorBen Burwell <ben@benburwell.com>2019-07-04 22:34:52 -0400
committerBen Burwell <ben@benburwell.com>2019-07-04 22:37:29 -0400
commitb46b497f99d7f4dcba8936ce0ebfe8cf982cec1f (patch)
treeb9212d4b196706d3c0e2d93b8dfd8cc39cbacf3b /worker/imap/imap.go
parent6574dedd8a4afdaedd3677283955a866214bd99a (diff)
Factor UI models out of the worker message packagemdps1
Before, the information needed to display different parts of the UI was tightly coupled to the specific messages being sent back and forth to the backend worker. Separating out a models package allows us to be more specific about exactly what a backend is able to and required to provide for the UI.
Diffstat (limited to 'worker/imap/imap.go')
-rw-r--r--worker/imap/imap.go75
1 files changed, 75 insertions, 0 deletions
diff --git a/worker/imap/imap.go b/worker/imap/imap.go
index 28bac93..06bcd00 100644
--- a/worker/imap/imap.go
+++ b/worker/imap/imap.go
@@ -2,6 +2,8 @@ package imap
import (
"github.com/emersion/go-imap"
+
+ "git.sr.ht/~sircmpwn/aerc/models"
)
func toSeqSet(uids []uint32) *imap.SeqSet {
@@ -11,3 +13,76 @@ func toSeqSet(uids []uint32) *imap.SeqSet {
}
return &set
}
+
+func translateBodyStructure(bs *imap.BodyStructure) *models.BodyStructure {
+ if bs == nil {
+ return nil
+ }
+ var parts []*models.BodyStructure
+ for _, part := range bs.Parts {
+ parts = append(parts, translateBodyStructure(part))
+ }
+ return &models.BodyStructure{
+ MIMEType: bs.MIMEType,
+ MIMESubType: bs.MIMESubType,
+ Params: bs.Params,
+ Description: bs.Description,
+ Encoding: bs.Encoding,
+ Parts: parts,
+ Disposition: bs.Disposition,
+ DispositionParams: bs.DispositionParams,
+ }
+}
+
+func translateEnvelope(e *imap.Envelope) *models.Envelope {
+ if e == nil {
+ return nil
+ }
+ return &models.Envelope{
+ Date: e.Date,
+ Subject: e.Subject,
+ From: translateAddresses(e.From),
+ ReplyTo: translateAddresses(e.ReplyTo),
+ To: translateAddresses(e.To),
+ Cc: translateAddresses(e.Cc),
+ Bcc: translateAddresses(e.Bcc),
+ MessageId: e.MessageId,
+ }
+}
+
+func translateAddress(a *imap.Address) *models.Address {
+ if a == nil {
+ return nil
+ }
+ return &models.Address{
+ Name: a.PersonalName,
+ Mailbox: a.MailboxName,
+ Host: a.HostName,
+ }
+}
+
+func translateAddresses(addrs []*imap.Address) []*models.Address {
+ var converted []*models.Address
+ for _, addr := range addrs {
+ converted = append(converted, translateAddress(addr))
+ }
+ return converted
+}
+
+var flagMap = map[string]models.Flag{
+ imap.SeenFlag: models.SeenFlag,
+ imap.RecentFlag: models.RecentFlag,
+ imap.AnsweredFlag: models.AnsweredFlag,
+ imap.DeletedFlag: models.DeletedFlag,
+ imap.FlaggedFlag: models.FlaggedFlag,
+}
+
+func translateFlags(imapFlags []string) []models.Flag {
+ var flags []models.Flag
+ for _, imapFlag := range imapFlags {
+ if flag, ok := flagMap[imapFlag]; ok {
+ flags = append(flags, flag)
+ }
+ }
+ return flags
+}