fix(GODT-2802): Consolidate Address Mode

This commit is contained in:
Leander Beernaert
2023-07-25 09:15:52 +02:00
parent 09c523e2d2
commit 1b0f930471
4 changed files with 58 additions and 29 deletions

View File

@ -31,6 +31,7 @@ import (
"github.com/ProtonMail/proton-bridge/v3/internal/services/sendrecorder"
"github.com/ProtonMail/proton-bridge/v3/internal/services/userevents"
"github.com/ProtonMail/proton-bridge/v3/internal/services/useridentity"
"github.com/ProtonMail/proton-bridge/v3/internal/usertypes"
"github.com/ProtonMail/proton-bridge/v3/pkg/cpc"
"github.com/sirupsen/logrus"
)
@ -41,13 +42,6 @@ type Telemetry interface {
ReportSMTPAuthFailed(username string)
}
type AddressMode int
const (
AddressModeCombined AddressMode = iota
AddressModeSplit
)
type Service struct {
userID string
panicHandler async.PanicHandler
@ -67,7 +61,7 @@ type Service struct {
addressSubscriber *userevents.AddressChanneledSubscriber
userSubscriber *userevents.UserChanneledSubscriber
addressMode AddressMode
addressMode usertypes.AddressMode
}
func NewService(
@ -80,7 +74,7 @@ func NewService(
keyPassProvider useridentity.KeyPassProvider,
telemetry Telemetry,
eventService userevents.Subscribable,
mode AddressMode,
mode usertypes.AddressMode,
identityState *useridentity.State,
) *Service {
subscriberName := fmt.Sprintf("smpt-%v", userID)
@ -122,7 +116,7 @@ func (s *Service) SendMail(ctx context.Context, authID string, from string, to [
return err
}
func (s *Service) SetAddressMode(ctx context.Context, mode AddressMode) error {
func (s *Service) SetAddressMode(ctx context.Context, mode usertypes.AddressMode) error {
_, err := s.cpc.Send(ctx, &setAddressModeReq{mode: mode})
return err
@ -248,7 +242,7 @@ func (s *Service) sendMail(ctx context.Context, req *sendMailReq) error {
}
type setAddressModeReq struct {
mode AddressMode
mode usertypes.AddressMode
}
type checkAuthReq struct {

View File

@ -160,7 +160,7 @@ func (s *Service) smtpSendMail(ctx context.Context, authID string, from string,
func (s *Service) sendWithKey(
ctx context.Context,
authAddrID string,
addrMode AddressMode,
addrMode usertypes.AddressMode,
settings proton.MailSettings,
userKR, addrKR *crypto.KeyRing,
emails []string,
@ -241,7 +241,7 @@ func getParentID(
ctx context.Context,
client *proton.Client,
authAddrID string,
addrMode AddressMode,
addrMode usertypes.AddressMode,
references []string,
) (string, error) {
var (
@ -263,7 +263,7 @@ func getParentID(
for _, internal := range internal {
var addrID string
if addrMode == AddressModeSplit {
if addrMode == usertypes.AddressModeSplit {
addrID = authAddrID
}
@ -291,7 +291,7 @@ func getParentID(
if parentID == "" && len(external) > 0 {
var addrID string
if addrMode == AddressModeSplit {
if addrMode == usertypes.AddressModeSplit {
addrID = authAddrID
}

View File

@ -224,7 +224,7 @@ func New(
encVault,
user,
eventService,
vaultToSMTPAddressMode(encVault.AddressMode()),
usertypes.VaultToAddressMode(encVault.AddressMode()),
identityState.Clone(),
)
@ -402,7 +402,7 @@ func (user *User) SetAddressMode(ctx context.Context, mode vault.AddressMode) er
return fmt.Errorf("failed to set address mode: %w", err)
}
if err := user.smtpService.SetAddressMode(ctx, vaultToSMTPAddressMode(mode)); err != nil {
if err := user.smtpService.SetAddressMode(ctx, usertypes.VaultToAddressMode(mode)); err != nil {
return fmt.Errorf("failed to set smtp address mode: %w", err)
}
@ -917,15 +917,3 @@ func sleepCtx(ctx context.Context, d time.Duration) {
case <-time.After(d):
}
}
func vaultToSMTPAddressMode(mode vault.AddressMode) smtp.AddressMode {
var smtpAddressMode smtp.AddressMode
if mode == vault.SplitMode {
smtpAddressMode = smtp.AddressModeSplit
} else {
smtpAddressMode = smtp.AddressModeCombined
}
return smtpAddressMode
}

View File

@ -0,0 +1,47 @@
// Copyright (c) 2023 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
package usertypes
import "github.com/ProtonMail/proton-bridge/v3/internal/vault"
type AddressMode int
const (
AddressModeCombined AddressMode = iota
AddressModeSplit
)
func VaultToAddressMode(mode vault.AddressMode) AddressMode {
var smtpAddressMode AddressMode
if mode == vault.SplitMode {
smtpAddressMode = AddressModeSplit
} else {
smtpAddressMode = AddressModeCombined
}
return smtpAddressMode
}
func AddressModeToVault(mode AddressMode) vault.AddressMode {
if mode == AddressModeCombined {
return vault.CombinedMode
}
return vault.SplitMode
}