Fix duplicate drafts on repeated saves
This commit is contained in:
parent
1321cea241
commit
6ecb243620
|
@ -9,7 +9,6 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
nettextproto "net/textproto"
|
|
||||||
|
|
||||||
"github.com/dustin/go-humanize"
|
"github.com/dustin/go-humanize"
|
||||||
"github.com/emersion/go-imap"
|
"github.com/emersion/go-imap"
|
||||||
|
@ -571,20 +570,20 @@ func markMessageAnswered(conn *imapclient.Client, mboxName string, uid uint32) e
|
||||||
return conn.UidStore(seqSet, item, flags, nil)
|
return conn.UidStore(seqSet, item, flags, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func appendMessage(c *imapclient.Client, msg *OutgoingMessage, mboxType mailboxType) (*MailboxInfo, uint32, error) {
|
func appendMessage(c *imapclient.Client, msg *OutgoingMessage, mboxType mailboxType) (*MailboxInfo, error) {
|
||||||
mbox, err := getMailboxByType(c, mboxType)
|
mbox, err := getMailboxByType(c, mboxType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if mbox == nil {
|
if mbox == nil {
|
||||||
return nil, 0, fmt.Errorf("Unable to resolve mailbox")
|
return nil, fmt.Errorf("Unable to resolve mailbox")
|
||||||
}
|
}
|
||||||
|
|
||||||
// IMAP needs to know in advance the final size of the message, so
|
// IMAP needs to know in advance the final size of the message, so
|
||||||
// there's no way around storing it in a buffer here.
|
// there's no way around storing it in a buffer here.
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
if err := msg.WriteTo(&buf); err != nil {
|
if err := msg.WriteTo(&buf); err != nil {
|
||||||
return nil, 0, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := []string{imap.SeenFlag}
|
flags := []string{imap.SeenFlag}
|
||||||
|
@ -592,20 +591,9 @@ func appendMessage(c *imapclient.Client, msg *OutgoingMessage, mboxType mailboxT
|
||||||
flags = append(flags, imap.DraftFlag)
|
flags = append(flags, imap.DraftFlag)
|
||||||
}
|
}
|
||||||
if err := c.Append(mbox.Name, flags, time.Now(), &buf); err != nil {
|
if err := c.Append(mbox.Name, flags, time.Now(), &buf); err != nil {
|
||||||
return nil, 0, err
|
return nil, err
|
||||||
}
|
|
||||||
criteria := &imap.SearchCriteria{
|
|
||||||
Header: make(nettextproto.MIMEHeader),
|
|
||||||
}
|
|
||||||
criteria.Header.Add("Message-Id", msg.MessageID)
|
|
||||||
if uids, err := c.UidSearch(criteria); err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
} else {
|
|
||||||
if len(uids) != 1 {
|
|
||||||
panic(fmt.Errorf("Duplicate message ID"))
|
|
||||||
}
|
|
||||||
return mbox, uids[0], nil
|
|
||||||
}
|
}
|
||||||
|
return mbox, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteMessage(c *imapclient.Client, mboxName string, uid uint32) error {
|
func deleteMessage(c *imapclient.Client, mboxName string, uid uint32) error {
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/textproto"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -507,7 +508,7 @@ func submitCompose(ctx *alps.Context, msg *OutgoingMessage, options *composeOpti
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ctx.Session.DoIMAP(func(c *imapclient.Client) error {
|
err = ctx.Session.DoIMAP(func(c *imapclient.Client) error {
|
||||||
if _, _, err := appendMessage(c, msg, mailboxSent); err != nil {
|
if _, err := appendMessage(c, msg, mailboxSent); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if draft := options.Draft; draft != nil {
|
if draft := options.Draft; draft != nil {
|
||||||
|
@ -625,15 +626,29 @@ func handleCompose(ctx *alps.Context, msg *OutgoingMessage, options *composeOpti
|
||||||
uid uint32
|
uid uint32
|
||||||
)
|
)
|
||||||
err = ctx.Session.DoIMAP(func(c *imapclient.Client) error {
|
err = ctx.Session.DoIMAP(func(c *imapclient.Client) error {
|
||||||
drafts, uid, err = appendMessage(c, msg, mailboxDrafts)
|
drafts, err = appendMessage(c, msg, mailboxDrafts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if draft := options.Draft; draft != nil {
|
if draft := options.Draft; draft != nil {
|
||||||
if err := deleteMessage(c, draft.Mailbox, draft.Uid); err != nil {
|
if err := deleteMessage(c, draft.Mailbox, draft.Uid); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
criteria := &imap.SearchCriteria{
|
||||||
|
Header: make(textproto.MIMEHeader),
|
||||||
|
}
|
||||||
|
criteria.Header.Add("Message-Id", msg.MessageID)
|
||||||
|
if uids, err := c.UidSearch(criteria); err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
if len(uids) != 1 {
|
||||||
|
panic(fmt.Errorf("Duplicate message ID"))
|
||||||
|
}
|
||||||
|
uid = uids[0]
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue