plugins/base: delete previous draft

This commit is contained in:
Simon Ser 2020-01-24 20:27:05 +01:00
parent d31c56ec98
commit 3384c39a17
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
2 changed files with 34 additions and 5 deletions

View file

@ -436,3 +436,19 @@ func appendMessage(c *imapclient.Client, msg *OutgoingMessage, mboxType mailboxT
return true, nil return true, nil
} }
func deleteMessage(c *imapclient.Client, mboxName string, uid uint32) error {
if err := ensureMailboxSelected(c, mboxName); err != nil {
return err
}
seqSet := new(imap.SeqSet)
seqSet.AddNum(uid)
item := imap.FormatFlagsOp(imap.AddFlags, true)
flags := []interface{}{imap.DeletedFlag}
if err := c.UidStore(seqSet, item, flags, nil); err != nil {
return err
}
return c.Expunge(nil)
}

View file

@ -288,7 +288,7 @@ type messagePath struct {
// Send message, append it to the Sent mailbox, mark the original message as // Send message, append it to the Sent mailbox, mark the original message as
// answered // answered
func submitCompose(ctx *koushin.Context, msg *OutgoingMessage, inReplyTo *messagePath) error { func submitCompose(ctx *koushin.Context, msg *OutgoingMessage, draft *messagePath, inReplyTo *messagePath) error {
err := ctx.Session.DoSMTP(func(c *smtp.Client) error { err := ctx.Session.DoSMTP(func(c *smtp.Client) error {
return sendMessage(c, msg) return sendMessage(c, msg)
}) })
@ -309,8 +309,15 @@ func submitCompose(ctx *koushin.Context, msg *OutgoingMessage, inReplyTo *messag
} }
err = ctx.Session.DoIMAP(func(c *imapclient.Client) error { err = ctx.Session.DoIMAP(func(c *imapclient.Client) error {
_, err := appendMessage(c, msg, mailboxSent) if _, err := appendMessage(c, msg, mailboxSent); err != nil {
return err return err
}
if draft != nil {
if err := deleteMessage(c, draft.Mailbox, draft.Uid); err != nil {
return err
}
}
return nil
}) })
if err != nil { if err != nil {
return fmt.Errorf("failed to save message to Sent mailbox: %v", err) return fmt.Errorf("failed to save message to Sent mailbox: %v", err)
@ -319,7 +326,7 @@ func submitCompose(ctx *koushin.Context, msg *OutgoingMessage, inReplyTo *messag
return ctx.Redirect(http.StatusFound, "/mailbox/INBOX") return ctx.Redirect(http.StatusFound, "/mailbox/INBOX")
} }
func handleCompose(ctx *koushin.Context, msg *OutgoingMessage, source *messagePath, inReplyTo *messagePath) error { func handleCompose(ctx *koushin.Context, msg *OutgoingMessage, draft *messagePath, inReplyTo *messagePath) error {
if msg.From == "" && strings.ContainsRune(ctx.Session.Username(), '@') { if msg.From == "" && strings.ContainsRune(ctx.Session.Username(), '@') {
msg.From = ctx.Session.Username() msg.From = ctx.Session.Username()
} }
@ -352,13 +359,19 @@ func handleCompose(ctx *koushin.Context, msg *OutgoingMessage, source *messagePa
if !copied { if !copied {
return fmt.Errorf("no Draft mailbox found") return fmt.Errorf("no Draft mailbox found")
} }
if draft != nil {
if err := deleteMessage(c, draft.Mailbox, draft.Uid); err != nil {
return err
}
}
return nil return nil
}) })
if err != nil { if err != nil {
return fmt.Errorf("failed to save message to Draft mailbox: %v", err) return fmt.Errorf("failed to save message to Draft mailbox: %v", err)
} }
return ctx.Redirect(http.StatusFound, "/mailbox/INBOX")
} else { } else {
return submitCompose(ctx, msg, inReplyTo) return submitCompose(ctx, msg, draft, inReplyTo)
} }
} }