fix: mark emails as read when opened (#233)

arclayto created

Add MarkEmailAsRead() that sets the IMAP \Seen flag via UidStore.
Called from FetchEmailBodyFromMailbox() after successfully fetching
the email body. Uses the existing BODY.PEEK[] fetch to keep fetch
and flag-setting separate.

Fixes #232

Change summary

fetcher/fetcher.go | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)

Detailed changes

fetcher/fetcher.go 🔗

@@ -791,9 +791,29 @@ func FetchEmailBodyFromMailbox(account *config.Account, mailbox string, uid uint
 		}
 	}
 
+	MarkEmailAsRead(account, mailbox, uid)
+
 	return body, attachments, nil
 }
 
+func MarkEmailAsRead(account *config.Account, mailbox string, uid uint32) error {
+	c, err := connect(account)
+	if err != nil {
+		return err
+	}
+	defer c.Logout()
+
+	if _, err := c.Select(mailbox, false); err != nil {
+		return err
+	}
+
+	seqSet := new(imap.SeqSet)
+	seqSet.AddNum(uid)
+	item := imap.FormatFlagsOp(imap.AddFlags, true)
+	flags := []interface{}{imap.SeenFlag}
+	return c.UidStore(seqSet, item, flags, nil)
+}
+
 func FetchAttachmentFromMailbox(account *config.Account, mailbox string, uid uint32, partID string, encoding string) ([]byte, error) {
 	c, err := connect(account)
 	if err != nil {