mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-17 23:56:56 +00:00
feat: make store use ClientManager
This commit is contained in:
@ -24,7 +24,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func cleanup(client *pmapi.Client) error {
|
||||
func cleanup(client pmapi.Client) error {
|
||||
if err := cleanSystemFolders(client); err != nil {
|
||||
return errors.Wrap(err, "failed to clean system folders")
|
||||
}
|
||||
@ -37,7 +37,7 @@ func cleanup(client *pmapi.Client) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func cleanSystemFolders(client *pmapi.Client) error {
|
||||
func cleanSystemFolders(client pmapi.Client) error {
|
||||
for _, labelID := range []string{pmapi.InboxLabel, pmapi.SentLabel, pmapi.ArchiveLabel, pmapi.AllMailLabel, pmapi.DraftLabel} {
|
||||
for {
|
||||
messages, total, err := client.ListMessages(&pmapi.MessagesFilter{
|
||||
@ -69,7 +69,7 @@ func cleanSystemFolders(client *pmapi.Client) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func cleanCustomLables(client *pmapi.Client) error {
|
||||
func cleanCustomLables(client pmapi.Client) error {
|
||||
labels, err := client.ListLabels()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to list labels")
|
||||
@ -87,7 +87,7 @@ func cleanCustomLables(client *pmapi.Client) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func cleanTrash(client *pmapi.Client) error {
|
||||
func cleanTrash(client pmapi.Client) error {
|
||||
for {
|
||||
_, total, err := client.ListMessages(&pmapi.MessagesFilter{
|
||||
PageSize: 1,
|
||||
@ -110,7 +110,7 @@ func cleanTrash(client *pmapi.Client) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func emptyFolder(client *pmapi.Client, labelID string) error {
|
||||
func emptyFolder(client pmapi.Client, labelID string) error {
|
||||
err := client.EmptyFolder(labelID, "")
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@ -18,9 +18,7 @@
|
||||
package liveapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
|
||||
@ -30,33 +28,27 @@ type Controller struct {
|
||||
// Internal states.
|
||||
lock *sync.RWMutex
|
||||
calls []*fakeCall
|
||||
pmapiByUsername map[string]*pmapi.Client
|
||||
messageIDsByUsername map[string][]string
|
||||
clientManager *pmapi.ClientManager
|
||||
|
||||
// State controlled by test.
|
||||
noInternetConnection bool
|
||||
}
|
||||
|
||||
func NewController() *Controller {
|
||||
return &Controller{
|
||||
func NewController(cm *pmapi.ClientManager) (cntrl *Controller) {
|
||||
cntrl = &Controller{
|
||||
lock: &sync.RWMutex{},
|
||||
calls: []*fakeCall{},
|
||||
pmapiByUsername: map[string]*pmapi.Client{},
|
||||
messageIDsByUsername: map[string][]string{},
|
||||
clientManager: cm,
|
||||
|
||||
noInternetConnection: false,
|
||||
}
|
||||
}
|
||||
|
||||
func (cntrl *Controller) GetClient(userID string) *pmapi.Client {
|
||||
cfg := &pmapi.ClientConfig{
|
||||
AppVersion: fmt.Sprintf("Bridge_%s", os.Getenv("VERSION")),
|
||||
ClientID: "bridge-test",
|
||||
Transport: &fakeTransport{
|
||||
cntrl: cntrl,
|
||||
transport: http.DefaultTransport,
|
||||
},
|
||||
TokenManager: pmapi.NewTokenManager(),
|
||||
}
|
||||
return pmapi.NewClient(cfg, userID)
|
||||
cm.SetRoundTripper(&fakeTransport{
|
||||
cntrl: cntrl,
|
||||
transport: http.DefaultTransport,
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@ -36,11 +36,7 @@ var systemLabelNameToID = map[string]string{ //nolint[gochecknoglobals]
|
||||
}
|
||||
|
||||
func (cntrl *Controller) AddUserLabel(username string, label *pmapi.Label) error {
|
||||
client, ok := cntrl.pmapiByUsername[username]
|
||||
if !ok {
|
||||
return fmt.Errorf("user %s does not exist", username)
|
||||
}
|
||||
|
||||
client := cntrl.clientManager.GetClient(username)
|
||||
label.Exclusive = getLabelExclusive(label.Name)
|
||||
label.Name = getLabelNameWithoutPrefix(label.Name)
|
||||
label.Color = pmapi.LabelColors[0]
|
||||
@ -67,11 +63,7 @@ func (cntrl *Controller) getLabelID(username, labelName string) (string, error)
|
||||
return labelID, nil
|
||||
}
|
||||
|
||||
client, ok := cntrl.pmapiByUsername[username]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("user %s does not exist", username)
|
||||
}
|
||||
|
||||
client := cntrl.clientManager.GetClient(username)
|
||||
labels, err := client.ListLabels()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to list labels")
|
||||
|
||||
@ -31,10 +31,7 @@ import (
|
||||
)
|
||||
|
||||
func (cntrl *Controller) AddUserMessage(username string, message *pmapi.Message) error {
|
||||
client, ok := cntrl.pmapiByUsername[username]
|
||||
if !ok {
|
||||
return fmt.Errorf("user %s does not exist", username)
|
||||
}
|
||||
client := cntrl.clientManager.GetClient(username)
|
||||
|
||||
body, err := buildMessage(client, message)
|
||||
if err != nil {
|
||||
@ -64,7 +61,7 @@ func (cntrl *Controller) AddUserMessage(username string, message *pmapi.Message)
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildMessage(client *pmapi.Client, message *pmapi.Message) (*bytes.Buffer, error) {
|
||||
func buildMessage(client pmapi.Client, message *pmapi.Message) (*bytes.Buffer, error) {
|
||||
if err := encryptMessage(client, message); err != nil {
|
||||
return nil, errors.Wrap(err, "failed to encrypt message")
|
||||
}
|
||||
@ -79,7 +76,7 @@ func buildMessage(client *pmapi.Client, message *pmapi.Message) (*bytes.Buffer,
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func encryptMessage(client *pmapi.Client, message *pmapi.Message) error {
|
||||
func encryptMessage(client pmapi.Client, message *pmapi.Message) error {
|
||||
addresses, err := client.GetAddresses()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get address")
|
||||
|
||||
@ -18,9 +18,7 @@
|
||||
package liveapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/ProtonMail/bridge/pkg/pmapi"
|
||||
"github.com/cucumber/godog"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@ -30,11 +28,7 @@ func (cntrl *Controller) AddUser(user *pmapi.User, addresses *pmapi.AddressList,
|
||||
return godog.ErrPending
|
||||
}
|
||||
|
||||
client := pmapi.NewClient(&pmapi.ClientConfig{
|
||||
AppVersion: fmt.Sprintf("Bridge_%s", os.Getenv("VERSION")),
|
||||
ClientID: "bridge-cntrl",
|
||||
TokenManager: pmapi.NewTokenManager(),
|
||||
}, user.ID)
|
||||
client := cntrl.clientManager.GetClient(user.ID)
|
||||
|
||||
authInfo, err := client.AuthInfo(user.Name)
|
||||
if err != nil {
|
||||
@ -60,6 +54,5 @@ func (cntrl *Controller) AddUser(user *pmapi.User, addresses *pmapi.AddressList,
|
||||
return errors.Wrap(err, "failed to clean user")
|
||||
}
|
||||
|
||||
cntrl.pmapiByUsername[user.Name] = client
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user