Other: Fix all linter errors

This commit is contained in:
Leander Beernaert
2022-10-18 13:54:12 +02:00
committed by James Houlahan
parent b36972ce71
commit 7c62312220
45 changed files with 206 additions and 176 deletions

View File

@ -27,6 +27,7 @@ import (
"github.com/ProtonMail/proton-bridge/v2/internal/events"
"github.com/ProtonMail/proton-bridge/v2/internal/vault"
"github.com/bradenaw/juniper/xslices"
"github.com/sirupsen/logrus"
"gitlab.protontech.ch/go/liteapi"
)
@ -60,7 +61,7 @@ func (user *User) handleAPIEvent(ctx context.Context, event liteapi.Event) error
}
// handleUserEvent handles the given user event.
func (user *User) handleUserEvent(ctx context.Context, userEvent liteapi.User) error {
func (user *User) handleUserEvent(_ context.Context, userEvent liteapi.User) error {
user.apiUser.Save(userEvent)
user.eventCh.Enqueue(events.UserChanged{
@ -71,7 +72,7 @@ func (user *User) handleUserEvent(ctx context.Context, userEvent liteapi.User) e
}
// handleAddressEvents handles the given address events.
// TODO: If split address mode, need to signal back to bridge to update the addresses!
// GODT-1945: If split address mode, need to signal back to bridge to update the addresses.
func (user *User) handleAddressEvents(ctx context.Context, addressEvents []liteapi.AddressEvent) error {
for _, event := range addressEvents {
switch event.Action {
@ -89,6 +90,9 @@ func (user *User) handleAddressEvents(ctx context.Context, addressEvents []litea
if err := user.handleDeleteAddressEvent(ctx, event); err != nil {
return fmt.Errorf("failed to delete address: %w", err)
}
case liteapi.EventUpdateFlags:
logrus.Warn("Not implemented yet.")
}
}
@ -127,7 +131,7 @@ func (user *User) handleCreateAddressEvent(ctx context.Context, event liteapi.Ad
return nil
}
func (user *User) handleUpdateAddressEvent(ctx context.Context, event liteapi.AddressEvent) error {
func (user *User) handleUpdateAddressEvent(_ context.Context, event liteapi.AddressEvent) error { //nolint:unparam
user.apiAddrs.Set(event.Address.ID, event.Address)
user.eventCh.Enqueue(events.UserAddressUpdated{
@ -139,7 +143,7 @@ func (user *User) handleUpdateAddressEvent(ctx context.Context, event liteapi.Ad
return nil
}
func (user *User) handleDeleteAddressEvent(ctx context.Context, event liteapi.AddressEvent) error {
func (user *User) handleDeleteAddressEvent(_ context.Context, event liteapi.AddressEvent) error {
var email string
if ok := user.apiAddrs.GetDelete(event.ID, func(apiAddr liteapi.Address) {
@ -189,7 +193,7 @@ func (user *User) handleLabelEvents(ctx context.Context, labelEvents []liteapi.L
return nil
}
func (user *User) handleCreateLabelEvent(ctx context.Context, event liteapi.LabelEvent) error {
func (user *User) handleCreateLabelEvent(_ context.Context, event liteapi.LabelEvent) error { //nolint:unparam
user.updateCh.IterValues(func(updateCh *queue.QueuedChannel[imap.Update]) {
updateCh.Enqueue(newMailboxCreatedUpdate(imap.LabelID(event.ID), getMailboxName(event.Label)))
})
@ -197,7 +201,7 @@ func (user *User) handleCreateLabelEvent(ctx context.Context, event liteapi.Labe
return nil
}
func (user *User) handleUpdateLabelEvent(ctx context.Context, event liteapi.LabelEvent) error {
func (user *User) handleUpdateLabelEvent(_ context.Context, event liteapi.LabelEvent) error { //nolint:unparam
user.updateCh.IterValues(func(updateCh *queue.QueuedChannel[imap.Update]) {
updateCh.Enqueue(imap.NewMailboxUpdated(imap.LabelID(event.ID), getMailboxName(event.Label)))
})
@ -205,7 +209,7 @@ func (user *User) handleUpdateLabelEvent(ctx context.Context, event liteapi.Labe
return nil
}
func (user *User) handleDeleteLabelEvent(ctx context.Context, event liteapi.LabelEvent) error {
func (user *User) handleDeleteLabelEvent(_ context.Context, event liteapi.LabelEvent) error { //nolint:unparam
user.updateCh.IterValues(func(updateCh *queue.QueuedChannel[imap.Update]) {
updateCh.Enqueue(imap.NewMailboxDeleted(imap.LabelID(event.ID)))
})
@ -258,7 +262,7 @@ func (user *User) handleCreateMessageEvent(ctx context.Context, event liteapi.Me
})
}
func (user *User) handleUpdateMessageEvent(ctx context.Context, event liteapi.MessageEvent) error {
func (user *User) handleUpdateMessageEvent(_ context.Context, event liteapi.MessageEvent) error { //nolint:unparam
update := imap.NewMessageLabelsUpdated(
imap.MessageID(event.ID),
mapTo[string, imap.LabelID](xslices.Filter(event.Message.LabelIDs, wantLabelID)),
@ -283,6 +287,10 @@ func getMailboxName(label liteapi.Label) []string {
case liteapi.LabelTypeLabel:
name = append([]string{labelPrefix}, label.Path...)
case liteapi.LabelTypeContactGroup:
fallthrough
case liteapi.LabelTypeSystem:
fallthrough
default:
name = label.Path
}

View File

@ -20,15 +20,15 @@ package user
import (
"context"
"fmt"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"time"
"github.com/ProtonMail/gluon/imap"
"github.com/ProtonMail/gluon/queue"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/ProtonMail/proton-bridge/v2/internal/safe"
"github.com/ProtonMail/proton-bridge/v2/internal/vault"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"gitlab.protontech.ch/go/liteapi"
"golang.org/x/exp/slices"
)
@ -94,6 +94,10 @@ func (conn *imapConnector) GetLabel(ctx context.Context, labelID imap.LabelID) (
case liteapi.LabelTypeFolder:
name = []string{folderPrefix, label.Name}
case liteapi.LabelTypeContactGroup:
fallthrough
case liteapi.LabelTypeSystem:
fallthrough
default:
name = []string{label.Name}
}
@ -163,6 +167,9 @@ func (conn *imapConnector) UpdateLabel(ctx context.Context, labelID imap.LabelID
case liteapi.LabelTypeSystem:
return fmt.Errorf("cannot rename system label %q", label.Name)
case liteapi.LabelTypeContactGroup:
return fmt.Errorf("cannot rename contact group label %q", label.Name)
}
if _, err := conn.client.UpdateLabel(ctx, label.ID, liteapi.UpdateLabelReq{
@ -212,7 +219,6 @@ func (conn *imapConnector) CreateMessage(
flags imap.FlagSet,
date time.Time,
) (imap.Message, []byte, error) {
var msgFlags liteapi.MessageFlag
switch labelID {
@ -288,18 +294,18 @@ func (conn *imapConnector) MoveMessages(ctx context.Context, messageIDs []imap.M
func (conn *imapConnector) MarkMessagesSeen(ctx context.Context, messageIDs []imap.MessageID, seen bool) error {
if seen {
return conn.client.MarkMessagesRead(ctx, mapTo[imap.MessageID, string](messageIDs)...)
} else {
return conn.client.MarkMessagesUnread(ctx, mapTo[imap.MessageID, string](messageIDs)...)
}
return conn.client.MarkMessagesUnread(ctx, mapTo[imap.MessageID, string](messageIDs)...)
}
// MarkMessagesFlagged sets the flagged value of the given messages.
func (conn *imapConnector) MarkMessagesFlagged(ctx context.Context, messageIDs []imap.MessageID, flagged bool) error {
if flagged {
return conn.client.LabelMessages(ctx, mapTo[imap.MessageID, string](messageIDs), liteapi.StarredLabel)
} else {
return conn.client.UnlabelMessages(ctx, mapTo[imap.MessageID, string](messageIDs), liteapi.StarredLabel)
}
return conn.client.UnlabelMessages(ctx, mapTo[imap.MessageID, string](messageIDs), liteapi.StarredLabel)
}
// GetUpdates returns a stream of updates that the gluon server should apply.

View File

@ -51,7 +51,7 @@ type smtpSession struct {
// from is the current sending address (taken from the return path).
from string
// fromAddrID is the ID of the curent sending address (taken from the return path).
// fromAddrID is the ID of the current sending address (taken from the return path).
fromAddrID string
// to holds all to for the current message.
@ -72,7 +72,7 @@ func newSMTPSession(user *User, email string) (*smtpSession, error) {
})
}
// Discard currently processed message.
// Reset Discard currently processed message.
func (session *smtpSession) Reset() {
logrus.Info("SMTP session reset")
@ -82,7 +82,7 @@ func (session *smtpSession) Reset() {
session.to = nil
}
// Free all resources associated with session.
// Logout Free all resources associated with session.
func (session *smtpSession) Logout() error {
defer session.Reset()
@ -91,7 +91,7 @@ func (session *smtpSession) Logout() error {
return nil
}
// Set return path for currently processed message.
// Mail Set return path for currently processed message.
func (session *smtpSession) Mail(from string, opts smtp.MailOptions) error {
logrus.Info("SMTP session mail")
@ -127,7 +127,7 @@ func (session *smtpSession) Mail(from string, opts smtp.MailOptions) error {
})
}
// Add recipient for currently processed message.
// Rcpt Add recipient for currently processed message.
func (session *smtpSession) Rcpt(to string) error {
logrus.Info("SMTP session rcpt")
@ -142,8 +142,8 @@ func (session *smtpSession) Rcpt(to string) error {
return nil
}
// Set currently processed message contents and send it.
func (session *smtpSession) Data(r io.Reader) error {
// Data Set currently processed message contents and send it.
func (session *smtpSession) Data(r io.Reader) error { //nolint:funlen
logrus.Info("SMTP session data")
ctx, cancel := context.WithCancel(context.Background())
@ -234,7 +234,7 @@ func (session *smtpSession) Data(r io.Reader) error {
}
// sendWithKey sends the message with the given address key.
func sendWithKey(
func sendWithKey( //nolint:funlen
ctx context.Context,
client *liteapi.Client,
authAddrID string,
@ -260,6 +260,14 @@ func sendWithKey(
case rfc822.TextPlain:
decBody = string(message.PlainBody)
case rfc822.MultipartRelated:
fallthrough
case rfc822.MultipartMixed:
fallthrough
case rfc822.MessageRFC822:
fallthrough
default:
break
}
encBody, err := addrKR.Encrypt(crypto.NewPlainMessageFromString(decBody), nil)
@ -311,7 +319,7 @@ func sendWithKey(
return res, nil
}
func getParentID(
func getParentID( //nolint:funlen
ctx context.Context,
client *liteapi.Client,
authAddrID string,
@ -402,7 +410,7 @@ func createDraft(
return strings.EqualFold(email, sanitizeEmail(template.Sender.Address))
}); idx < 0 {
return liteapi.Message{}, fmt.Errorf("address %q is not owned by user", template.Sender.Address)
} else {
} else { //nolint:revive
template.Sender.Address = constructEmail(template.Sender.Address, emails[idx])
}

View File

@ -68,6 +68,17 @@ func newContactSettings(settings liteapi.ContactSettings) *contactSettings {
case liteapi.PGPInlineScheme:
metadata.Scheme = pgpInline
case liteapi.InternalScheme:
fallthrough
case liteapi.EncryptedOutsideScheme:
fallthrough
case liteapi.ClearScheme:
fallthrough
case liteapi.ClearMIMEScheme:
fallthrough
default:
break
}
}
@ -253,7 +264,7 @@ func (b *sendPrefsBuilder) build() (p liteapi.SendPreferences) {
p.EncryptionScheme = liteapi.ClearScheme
}
return
return p
}
// setPGPSettings returns a SendPreferences with the following possible values:

View File

@ -137,7 +137,7 @@ func syncLabels(ctx context.Context, client *liteapi.Client, updateCh ...*queue.
return nil
}
func syncMessages(
func syncMessages( //nolint:funlen
ctx context.Context,
userID string,
client *liteapi.Client,

View File

@ -47,7 +47,7 @@ func defaultJobOpts() message.JobOptions {
}
}
func buildRFC822(ctx context.Context, full liteapi.FullMessage, addrKR *crypto.KeyRing) (*buildRes, error) {
func buildRFC822(_ context.Context, full liteapi.FullMessage, addrKR *crypto.KeyRing) (*buildRes, error) {
literal, err := message.BuildRFC822(addrKR, full.Message, full.AttData, defaultJobOpts())
if err != nil {
return nil, fmt.Errorf("failed to build message %s: %w", full.ID, err)

View File

@ -35,7 +35,7 @@ func mapTo[From, To any](from []From) []To {
for _, from := range from {
val, ok := reflect.ValueOf(from).Convert(reflect.TypeOf(to).Elem()).Interface().(To)
if !ok {
panic(fmt.Sprintf("cannot convert %T to %T", from, *new(To)))
panic(fmt.Sprintf("cannot convert %T to %T", from, *new(To))) //nolint:gocritic
}
to = append(to, val)

View File

@ -38,8 +38,8 @@ import (
)
var (
EventPeriod = 20 * time.Second // nolint:gochecknoglobals
EventJitter = 20 * time.Second // nolint:gochecknoglobals
EventPeriod = 20 * time.Second // nolint:gochecknoglobals,revive
EventJitter = 20 * time.Second // nolint:gochecknoglobals,revive
)
type User struct {
@ -55,7 +55,7 @@ type User struct {
syncLock try.Group
}
func New(ctx context.Context, encVault *vault.User, client *liteapi.Client, apiUser liteapi.User) (*User, error) {
func New(ctx context.Context, encVault *vault.User, client *liteapi.Client, apiUser liteapi.User) (*User, error) { //nolint:funlen
// Get the user's API addresses.
apiAddrs, err := client.GetAddresses(ctx)
if err != nil {
@ -125,7 +125,7 @@ func New(ctx context.Context, encVault *vault.User, client *liteapi.Client, apiU
})
})
// TODO: Don't start the event loop until the initial sync has finished!
// GODT-1946 - Don't start the event loop until the initial sync has finished.
eventCh := user.client.NewEventStream(EventPeriod, EventJitter, user.vault.EventID())
// If we haven't synced yet, do it first.
@ -276,7 +276,7 @@ func (user *User) MaxSpace() int {
})
}
// GetEventCh returns a channel which notifies of events happening to the user (such as deauth, address change)
// GetEventCh returns a channel which notifies of events happening to the user (such as deauth, address change).
func (user *User) GetEventCh() <-chan events.Event {
return user.eventCh.GetChannel()
}
@ -464,7 +464,7 @@ func (user *User) startSync() <-chan error {
}
// AbortSync aborts any ongoing sync.
// TODO: Should probably be done automatically when one of the user's IMAP connectors is closed.
// GODT-1947: Should probably be done automatically when one of the user's IMAP connectors is closed.
func (user *User) stopSync() {
select {
case user.syncStopCh <- struct{}{}:

View File

@ -96,7 +96,7 @@ func TestUser_Deauth(t *testing.T) {
})
}
func withAPI(t *testing.T, ctx context.Context, fn func(context.Context, *server.Server, *liteapi.Manager)) {
func withAPI(_ *testing.T, ctx context.Context, fn func(context.Context, *server.Server, *liteapi.Manager)) { //nolint:revive
server := server.New()
defer server.Close()
@ -104,11 +104,11 @@ func withAPI(t *testing.T, ctx context.Context, fn func(context.Context, *server
}
func withAccount(t *testing.T, s *server.Server, username, password string, emails []string, fn func(string, []string)) {
var addrIDs []string
userID, addrID, err := s.CreateUser(username, emails[0], []byte(password))
require.NoError(t, err)
addrIDs := make([]string, 0, len(emails))
addrIDs = append(addrIDs, addrID)
for _, email := range emails[1:] {
@ -121,7 +121,7 @@ func withAccount(t *testing.T, s *server.Server, username, password string, emai
fn(userID, addrIDs)
}
func withUser(t *testing.T, ctx context.Context, s *server.Server, m *liteapi.Manager, username, password string, fn func(*user.User)) {
func withUser(t *testing.T, ctx context.Context, _ *server.Server, m *liteapi.Manager, username, password string, fn func(*user.User)) { //nolint:revive
client, apiAuth, err := m.NewClientWithLogin(ctx, username, []byte(password))
require.NoError(t, err)
defer func() { require.NoError(t, client.Close()) }()