forked from Silverfish/proton-bridge
GODT-35: Finish all details and make tests pass
This commit is contained in:
@ -5,11 +5,10 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
imap "github.com/emersion/go-imap"
|
||||
sasl "github.com/emersion/go-sasl"
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
reflect "reflect"
|
||||
)
|
||||
|
||||
// MockPanicHandler is a mock of PanicHandler interface
|
||||
|
||||
@ -19,7 +19,9 @@ package transfer
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -37,6 +39,8 @@ const (
|
||||
imapRetries = 10
|
||||
imapReconnectTimeout = 30 * time.Minute
|
||||
imapReconnectSleep = time.Minute
|
||||
|
||||
protonStatusURL = "http://protonstatus.com/vpn_status"
|
||||
)
|
||||
|
||||
type imapErrorLogger struct {
|
||||
@ -117,19 +121,15 @@ func (p *IMAPProvider) tryReconnect(ensureSelectedIn string) error {
|
||||
return previousErr
|
||||
}
|
||||
|
||||
// FIXME(conman): This should register as connection observer.
|
||||
err := checkConnection()
|
||||
log.WithError(err).Debug("Connection check")
|
||||
if err != nil {
|
||||
time.Sleep(imapReconnectSleep)
|
||||
previousErr = err
|
||||
continue
|
||||
}
|
||||
|
||||
/*
|
||||
err := pmapi.CheckConnection()
|
||||
log.WithError(err).Debug("Connection check")
|
||||
if err != nil {
|
||||
time.Sleep(imapReconnectSleep)
|
||||
previousErr = err
|
||||
continue
|
||||
}
|
||||
*/
|
||||
|
||||
err := p.reauth()
|
||||
err = p.reauth()
|
||||
log.WithError(err).Debug("Reauth")
|
||||
if err != nil {
|
||||
time.Sleep(imapReconnectSleep)
|
||||
@ -289,3 +289,23 @@ func (p *IMAPProvider) fetchHelper(uid bool, ensureSelectedIn string, seqSet *im
|
||||
return err
|
||||
}, ensureSelectedIn)
|
||||
}
|
||||
|
||||
// checkConnection returns an error if there is no internet connection.
|
||||
// Note we don't want to use client manager because it only reports connection
|
||||
// issues with API; we are only interested here whether we can reach
|
||||
// third-party IMAP servers.
|
||||
func checkConnection() error {
|
||||
client := &http.Client{Timeout: time.Second * 10}
|
||||
|
||||
resp, err := client.Get(protonStatusURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_ = resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
return fmt.Errorf("HTTP status code %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -52,15 +52,10 @@ func (p *PMAPIProvider) CreateMailbox(mailbox Mailbox) (Mailbox, error) {
|
||||
return Mailbox{}, errors.New("mailbox is already created")
|
||||
}
|
||||
|
||||
exclusive := 0
|
||||
if mailbox.IsExclusive {
|
||||
exclusive = 1
|
||||
}
|
||||
|
||||
label, err := p.client.CreateLabel(context.TODO(), &pmapi.Label{
|
||||
label, err := p.client.CreateLabel(context.Background(), &pmapi.Label{
|
||||
Name: mailbox.Name,
|
||||
Color: mailbox.Color,
|
||||
Exclusive: exclusive,
|
||||
Exclusive: pmapi.Boolean(mailbox.IsExclusive),
|
||||
Type: pmapi.LabelTypeMailbox,
|
||||
})
|
||||
if err != nil {
|
||||
@ -126,7 +121,7 @@ func (p *PMAPIProvider) importDraft(msg Message, globalMailbox *Mailbox) (string
|
||||
}
|
||||
|
||||
if message.Sender == nil {
|
||||
mainAddress := p.client().Addresses().Main()
|
||||
mainAddress := p.client.Addresses().Main()
|
||||
message.Sender = &mail.Address{
|
||||
Name: mainAddress.DisplayName,
|
||||
Address: mainAddress.Email,
|
||||
@ -227,14 +222,6 @@ func (p *PMAPIProvider) generateImportMsgReq(rules transferRules, progress *Prog
|
||||
}
|
||||
}
|
||||
|
||||
var unread pmapi.Boolean
|
||||
|
||||
if msg.Unread {
|
||||
unread = pmapi.True
|
||||
} else {
|
||||
unread = pmapi.False
|
||||
}
|
||||
|
||||
labelIDs := []string{}
|
||||
for _, target := range msg.Targets {
|
||||
// Frontend should not set All Mail to Rules, but to be sure...
|
||||
@ -249,7 +236,7 @@ func (p *PMAPIProvider) generateImportMsgReq(rules transferRules, progress *Prog
|
||||
return &pmapi.ImportMsgReq{
|
||||
Metadata: &pmapi.ImportMetadata{
|
||||
AddressID: p.addressID,
|
||||
Unread: unread,
|
||||
Unread: pmapi.Boolean(msg.Unread),
|
||||
Time: message.Time,
|
||||
Flags: computeMessageFlags(message.Header),
|
||||
LabelIDs: labelIDs,
|
||||
|
||||
@ -153,10 +153,10 @@ func setupPMAPIRules(rules transferRules) {
|
||||
func setupPMAPIClientExpectationForExport(m *mocks) {
|
||||
m.pmapiClient.EXPECT().KeyRingForAddressID(gomock.Any()).Return(m.keyring, nil).AnyTimes()
|
||||
m.pmapiClient.EXPECT().ListLabels(gomock.Any()).Return([]*pmapi.Label{
|
||||
{ID: "label1", Name: "Foo", Color: "blue", Exclusive: 0, Order: 2},
|
||||
{ID: "label2", Name: "Bar", Color: "green", Exclusive: 0, Order: 1},
|
||||
{ID: "folder1", Name: "One", Color: "red", Exclusive: 1, Order: 1},
|
||||
{ID: "folder2", Name: "Two", Color: "orange", Exclusive: 1, Order: 2},
|
||||
{ID: "label1", Name: "Foo", Color: "blue", Exclusive: false, Order: 2},
|
||||
{ID: "label2", Name: "Bar", Color: "green", Exclusive: false, Order: 1},
|
||||
{ID: "folder1", Name: "One", Color: "red", Exclusive: true, Order: 1},
|
||||
{ID: "folder2", Name: "Two", Color: "orange", Exclusive: true, Order: 2},
|
||||
}, nil).AnyTimes()
|
||||
m.pmapiClient.EXPECT().CountMessages(gomock.Any(), gomock.Any()).Return([]*pmapi.MessagesCount{
|
||||
{LabelID: "label1", Total: 10},
|
||||
|
||||
@ -30,9 +30,17 @@ import (
|
||||
const (
|
||||
pmapiRetries = 10
|
||||
pmapiReconnectTimeout = 30 * time.Minute
|
||||
pmapiReconnectSleep = time.Minute
|
||||
pmapiReconnectSleep = 10 * time.Second
|
||||
)
|
||||
|
||||
func (p *PMAPIProvider) SetConnectionUp() {
|
||||
p.connection = true
|
||||
}
|
||||
|
||||
func (p *PMAPIProvider) SetConnectionDown() {
|
||||
p.connection = false
|
||||
}
|
||||
|
||||
func (p *PMAPIProvider) ensureConnection(callback func() error) error {
|
||||
var callErr error
|
||||
for i := 1; i <= pmapiRetries; i++ {
|
||||
@ -58,18 +66,10 @@ func (p *PMAPIProvider) tryReconnect() error {
|
||||
return previousErr
|
||||
}
|
||||
|
||||
// FIXME(conman): This should register as a connection observer somehow...
|
||||
// Maybe the entire "provider" could register as an observer and pause if it is notified of dropped connection?
|
||||
|
||||
/*
|
||||
err := p.clientManager.CheckConnection()
|
||||
log.WithError(err).Debug("Connection check")
|
||||
if err != nil {
|
||||
time.Sleep(pmapiReconnectSleep)
|
||||
previousErr = err
|
||||
continue
|
||||
}
|
||||
*/
|
||||
if !p.connection {
|
||||
time.Sleep(pmapiReconnectSleep)
|
||||
continue
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
@ -83,7 +83,7 @@ func (p *PMAPIProvider) listMessages(filter *pmapi.MessagesFilter) (messages []*
|
||||
p.timeIt.start("listing", key)
|
||||
defer p.timeIt.stop("listing", key)
|
||||
|
||||
messages, count, err = p.client.ListMessages(context.TODO(), filter)
|
||||
messages, count, err = p.client.ListMessages(context.Background(), filter)
|
||||
return err
|
||||
})
|
||||
return
|
||||
@ -94,7 +94,7 @@ func (p *PMAPIProvider) getMessage(msgID string) (message *pmapi.Message, err er
|
||||
p.timeIt.start("download", msgID)
|
||||
defer p.timeIt.stop("download", msgID)
|
||||
|
||||
message, err = p.client.GetMessage(context.TODO(), msgID)
|
||||
message, err = p.client.GetMessage(context.Background(), msgID)
|
||||
return err
|
||||
})
|
||||
return
|
||||
@ -105,7 +105,7 @@ func (p *PMAPIProvider) importRequest(msgSourceID string, req pmapi.ImportMsgReq
|
||||
p.timeIt.start("upload", msgSourceID)
|
||||
defer p.timeIt.stop("upload", msgSourceID)
|
||||
|
||||
res, err = p.client.Import(context.TODO(), req)
|
||||
res, err = p.client.Import(context.Background(), req)
|
||||
return err
|
||||
})
|
||||
return
|
||||
@ -116,7 +116,7 @@ func (p *PMAPIProvider) createDraft(msgSourceID string, message *pmapi.Message,
|
||||
p.timeIt.start("upload", msgSourceID)
|
||||
defer p.timeIt.stop("upload", msgSourceID)
|
||||
|
||||
draft, err = p.client.CreateDraft(context.TODO(), message, parent, action)
|
||||
draft, err = p.client.CreateDraft(context.Background(), message, parent, action)
|
||||
return err
|
||||
})
|
||||
return
|
||||
@ -129,7 +129,7 @@ func (p *PMAPIProvider) createAttachment(msgSourceID string, att *pmapi.Attachme
|
||||
p.timeIt.start("upload", key)
|
||||
defer p.timeIt.stop("upload", key)
|
||||
|
||||
created, err = p.client.CreateAttachment(context.TODO(), att, r, sig)
|
||||
created, err = p.client.CreateAttachment(context.Background(), att, r, sig)
|
||||
return err
|
||||
})
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user