GODT-1433 Message.Type is deprecated, use Flags instead.

This commit is contained in:
Jakub
2021-11-30 17:03:50 +01:00
parent 55beb9227f
commit a0dc764bb9
9 changed files with 50 additions and 45 deletions

View File

@ -133,7 +133,7 @@ func (q *sendRecorder) isSendingOrSent(client messageGetter, hash string) (isSen
if err != nil { if err != nil {
return return
} }
if message.Type == pmapi.MessageTypeDraft { if message.IsDraft() {
// If message is in draft for a long time, let's assume there is // If message is in draft for a long time, let's assume there is
// some problem and message will not be sent anymore. // some problem and message will not be sent anymore.
if time.Since(time.Unix(message.Time, 0)).Minutes() > 10 { if time.Since(time.Unix(message.Time, 0)).Minutes() > 10 {
@ -141,8 +141,8 @@ func (q *sendRecorder) isSendingOrSent(client messageGetter, hash string) (isSen
} }
isSending = true isSending = true
} }
// MessageTypeInboxAndSent can be when message was sent to myself. // Message can be in Inbox and Sent when message was sent to myself.
if message.Type == pmapi.MessageTypeSent || message.Type == pmapi.MessageTypeInboxAndSent { if message.Has(pmapi.FlagSent) {
wasSent = true wasSent = true
} }

View File

@ -395,6 +395,9 @@ func TestSendRecorder_isSendingOrSent(t *testing.T) {
q.addMessage("hash") q.addMessage("hash")
q.setMessageID("hash", "messageID") q.setMessageID("hash", "messageID")
draftFlag := pmapi.FlagInternal | pmapi.FlagE2E
selfSent := pmapi.FlagSent | pmapi.FlagReceived
testCases := []struct { testCases := []struct {
hash string hash string
message *pmapi.Message message *pmapi.Message
@ -402,14 +405,14 @@ func TestSendRecorder_isSendingOrSent(t *testing.T) {
wantIsSending bool wantIsSending bool
wantWasSent bool wantWasSent bool
}{ }{
{"badhash", &pmapi.Message{Type: pmapi.MessageTypeDraft}, nil, false, false}, {"badhash", &pmapi.Message{Flags: draftFlag}, nil, false, false},
{"hash", nil, errors.New("message not found"), false, false}, {"hash", nil, errors.New("message not found"), false, false},
{"hash", &pmapi.Message{Type: pmapi.MessageTypeInbox}, nil, false, false}, {"hash", &pmapi.Message{Flags: pmapi.FlagReceived}, nil, false, false},
{"hash", &pmapi.Message{Type: pmapi.MessageTypeDraft, Time: time.Now().Add(-20 * time.Minute).Unix()}, nil, false, false}, {"hash", &pmapi.Message{Flags: draftFlag, Time: time.Now().Add(-20 * time.Minute).Unix()}, nil, false, false},
{"hash", &pmapi.Message{Type: pmapi.MessageTypeDraft, Time: time.Now().Unix()}, nil, true, false}, {"hash", &pmapi.Message{Flags: draftFlag, Time: time.Now().Unix()}, nil, true, false},
{"hash", &pmapi.Message{Type: pmapi.MessageTypeSent}, nil, false, true}, {"hash", &pmapi.Message{Flags: pmapi.FlagSent}, nil, false, true},
{"hash", &pmapi.Message{Type: pmapi.MessageTypeInboxAndSent}, nil, false, true}, {"hash", &pmapi.Message{Flags: selfSent}, nil, false, true},
{"", &pmapi.Message{Type: pmapi.MessageTypeInboxAndSent}, nil, false, false}, {"", &pmapi.Message{Flags: selfSent}, nil, false, false},
} }
for i, tc := range testCases { for i, tc := range testCases {
tc := tc // bind tc := tc // bind

View File

@ -370,7 +370,7 @@ func (storeMailbox *Mailbox) txCreateOrUpdateMessages(tx *bolt.Tx, msgs []*pmapi
// Draft bodies can change and bodies are not re-fetched by IMAP clients. // Draft bodies can change and bodies are not re-fetched by IMAP clients.
// Every change has to be a new message; we need to delete the old one and always recreate it. // Every change has to be a new message; we need to delete the old one and always recreate it.
if msg.Type == pmapi.MessageTypeDraft || msg.IsDraft() { if msg.IsDraft() {
if err := storeMailbox.txDeleteMessage(tx, msg.ID); err != nil { if err := storeMailbox.txDeleteMessage(tx, msg.ID); err != nil {
return errors.Wrap(err, "cannot delete old draft") return errors.Wrap(err, "cannot delete old draft")
} }

View File

@ -78,23 +78,23 @@ func TestCreateOrUpdateMessageMetadata(t *testing.T) {
m.newStoreNoEvents(t, true) m.newStoreNoEvents(t, true)
insertMessage(t, m, "msg1", "Test message 1", addrID1, false, []string{pmapi.AllMailLabel}) insertMessage(t, m, "msg1", "Test message 1", addrID1, false, []string{pmapi.AllMailLabel})
msg, err := m.store.getMessageFromDB("msg1") metadata, err := m.store.getMessageFromDB("msg1")
require.Nil(t, err) require.Nil(t, err)
message := &Message{msg: msg, store: m.store, storeMailbox: nil} msg := &Message{msg: metadata, store: m.store, storeMailbox: nil}
// Check non-meta and calculated data are cleared/empty. // Check non-meta and calculated data are cleared/empty.
a.Equal(t, "", message.msg.Body) a.Equal(t, "", metadata.Body)
a.Equal(t, []*pmapi.Attachment(nil), message.msg.Attachments) a.Equal(t, []*pmapi.Attachment(nil), metadata.Attachments)
a.Equal(t, "", message.msg.MIMEType) a.Equal(t, "", metadata.MIMEType)
a.Equal(t, make(mail.Header), message.msg.Header) a.Equal(t, make(mail.Header), metadata.Header)
wantHeader, wantSize := putBodystructureAndSizeToDB(m, "msg1") wantHeader, wantSize := putBodystructureAndSizeToDB(m, "msg1")
// Check cached data. // Check cached data.
require.Nil(t, err) require.Nil(t, err)
a.Equal(t, wantHeader, message.GetMIMEHeader()) a.Equal(t, wantHeader, msg.GetMIMEHeader())
haveSize, err := message.GetRFC822Size() haveSize, err := msg.GetRFC822Size()
require.Nil(t, err) require.Nil(t, err)
a.Equal(t, wantSize, haveSize) a.Equal(t, wantSize, haveSize)
@ -102,8 +102,8 @@ func TestCreateOrUpdateMessageMetadata(t *testing.T) {
insertMessage(t, m, "msg1", "Test message 1", addrID1, false, []string{pmapi.AllMailLabel}) insertMessage(t, m, "msg1", "Test message 1", addrID1, false, []string{pmapi.AllMailLabel})
require.Nil(t, err) require.Nil(t, err)
a.Equal(t, wantHeader, message.GetMIMEHeader()) a.Equal(t, wantHeader, msg.GetMIMEHeader())
haveSize, err = message.GetRFC822Size() haveSize, err = msg.GetRFC822Size()
require.Nil(t, err) require.Nil(t, err)
a.Equal(t, wantSize, haveSize) a.Equal(t, wantSize, haveSize)
} }

View File

@ -74,19 +74,19 @@ const (
// Message flag definitions. // Message flag definitions.
const ( const (
FlagReceived = 1 FlagReceived = int64(1)
FlagSent = 2 FlagSent = int64(2)
FlagInternal = 4 FlagInternal = int64(4)
FlagE2E = 8 FlagE2E = int64(8)
FlagAuto = 16 FlagAuto = int64(16)
FlagReplied = 32 FlagReplied = int64(32)
FlagRepliedAll = 64 FlagRepliedAll = int64(64)
FlagForwarded = 128 FlagForwarded = int64(128)
FlagAutoreplied = 256 FlagAutoreplied = int64(256)
FlagImported = 512 FlagImported = int64(512)
FlagOpened = 1024 FlagOpened = int64(1024)
FlagReceiptSent = 2048 FlagReceiptSent = int64(2048)
) )
// Draft flags. // Draft flags.
@ -139,13 +139,6 @@ const (
RemoveLabels // Remove specified labels from current ones. RemoveLabels // Remove specified labels from current ones.
) )
const (
MessageTypeInbox int = iota
MessageTypeDraft
MessageTypeSent
MessageTypeInboxAndSent
)
// Due to API limitations, we shouldn't make requests with more than 100 message IDs at a time. // Due to API limitations, we shouldn't make requests with more than 100 message IDs at a time.
const messageIDPageSize = 100 const messageIDPageSize = 100
@ -166,7 +159,6 @@ type Message struct {
ConversationID string `json:",omitempty"` // only filter ConversationID string `json:",omitempty"` // only filter
Subject string Subject string
Unread Boolean Unread Boolean
Type int
Flags int64 Flags int64
Sender *mail.Address Sender *mail.Address
ReplyTo *mail.Address `json:",omitempty"` ReplyTo *mail.Address `json:",omitempty"`

View File

@ -177,10 +177,8 @@ func (ctl *Controller) SetDraftBody(username string, messageID string, body stri
message.Body = body message.Body = body
// assuming this is draft we set following // assuming this is draft we set following
// - Draft type (NOTE: Type is not part of pmapi.MessageEvent, but it's there on API)
// - It must not have FlagReceived and FlagSent // - It must not have FlagReceived and FlagSent
// - Standard labelsIDs NOTE:wrong behaviour once we will have edge case tests for drafts outside draft folder // - Standard labelsIDs NOTE:wrong behaviour once we will have edge case tests for drafts outside draft folder
message.Type = pmapi.MessageTypeDraft
message.Flags = pmapi.FlagE2E | pmapi.FlagInternal message.Flags = pmapi.FlagE2E | pmapi.FlagInternal
message.LabelIDs = []string{pmapi.AllDraftsLabel, pmapi.AllMailLabel, pmapi.DraftLabel} message.LabelIDs = []string{pmapi.AllDraftsLabel, pmapi.AllMailLabel, pmapi.DraftLabel}

View File

@ -179,6 +179,7 @@ func (api *FakePMAPI) SendMessage(ctx context.Context, messageID string, sendMes
} }
message.Time = time.Now().Unix() message.Time = time.Now().Unix()
message.LabelIDs = append(message.LabelIDs, pmapi.SentLabel) message.LabelIDs = append(message.LabelIDs, pmapi.SentLabel)
message.Flags |= pmapi.FlagSent
api.addEventMessage(pmapi.EventUpdate, message) api.addEventMessage(pmapi.EventUpdate, message)
return message, nil, nil return message, nil, nil
} }

View File

@ -25,6 +25,12 @@ Feature: IMAP operations with Drafts
When IMAP client fetches body of UID "1" When IMAP client fetches body of UID "1"
Then IMAP response is "OK" Then IMAP response is "OK"
Then IMAP response contains "Nope" Then IMAP response contains "Nope"
When IMAP client sends command "UID FETCH 1 RFC822.SIZE"
Then IMAP response is "OK"
Then IMAP response contains "495"
When IMAP client sends command "UID FETCH 1 BODYSTRUCTURE"
Then IMAP response is "OK"
Then IMAP response contains "4 14"
Given the body of draft "msg1" for "user" has changed to "Yes I am" Given the body of draft "msg1" for "user" has changed to "Yes I am"
And the event loop of "user" loops once And the event loop of "user" loops once
And mailbox "Drafts" for "user" has 1 messages And mailbox "Drafts" for "user" has 1 messages
@ -32,4 +38,10 @@ Feature: IMAP operations with Drafts
Then IMAP response is "OK" Then IMAP response is "OK"
Then IMAP response contains "Yes I am" Then IMAP response contains "Yes I am"
Then IMAP response does not contain "Nope" Then IMAP response does not contain "Nope"
When IMAP client sends command "UID FETCH 2 RFC822.SIZE"
Then IMAP response is "OK"
Then IMAP response contains "499"
When IMAP client sends command "UID FETCH 2 BODYSTRUCTURE"
Then IMAP response is "OK"
Then IMAP response contains "8 14"

View File

@ -108,16 +108,15 @@ func thereAreMessagesInMailboxesForAddressOfUser(mailboxNames, bddAddressID, bdd
if message.HasLabelID(pmapi.SentLabel) { if message.HasLabelID(pmapi.SentLabel) {
message.Flags |= pmapi.FlagSent message.Flags |= pmapi.FlagSent
message.Type = pmapi.MessageTypeSent
} else { } else {
// some tests (Outlook move by DELETE EXPUNGE APPEND) imply creating hard copies of emails, // some tests (Outlook move by DELETE EXPUNGE APPEND) imply creating hard copies of emails,
// and the importMessage() function flags the email as Sent if the 'Received' key in not present in the // and the importMessage() function flags the email as Sent if the 'Received' key in not present in the
// header. // header.
header.Add("Received", "from dummy.protonmail.com") header.Add("Received", "from dummy.protonmail.com")
message.Flags |= pmapi.FlagReceived
} }
if message.HasLabelID(pmapi.DraftLabel) { if message.HasLabelID(pmapi.DraftLabel) {
message.Type = pmapi.MessageTypeDraft
message.Flags = pmapi.FlagInternal | pmapi.FlagE2E message.Flags = pmapi.FlagInternal | pmapi.FlagE2E
} }