diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 10dc835..b6cb7ff 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -9,15 +9,15 @@ jobs: - name: Get sources uses: actions/checkout@v3 - - name: Set up Go 1.18 + - name: Set up Go 1.21 uses: actions/setup-go@v3 with: - go-version: '1.18' + go-version: '1.21' - name: Run golangci-lint uses: golangci/golangci-lint-action@v3 with: - version: v1.50.0 + version: v1.55.2 args: --timeout=180s skip-cache: true diff --git a/keyring.go b/keyring.go index 2037b11..7aecf98 100644 --- a/keyring.go +++ b/keyring.go @@ -12,6 +12,7 @@ import ( "github.com/ProtonMail/go-crypto/openpgp/armor" "github.com/ProtonMail/gopenpgp/v2/crypto" "github.com/bradenaw/juniper/xslices" + "golang.org/x/exp/slices" ) func ExtractSignatures(kr *crypto.KeyRing, arm string) ([]Signature, error) { @@ -148,7 +149,7 @@ func (keys Keys) Unlock(passphrase []byte, userKR *crypto.KeyRing) (*crypto.KeyR return nil, err } - for _, key := range xslices.Filter(keys, func(key Key) bool { return bool(key.Active) }) { + for _, key := range slices.DeleteFunc(slices.Clone(keys), func(key Key) bool { return !bool(key.Active) }) { unlocked, err := key.Unlock(passphrase, userKR) if err != nil { log.WithField("KeyID", key.ID).WithError(err).Warning("Cannot unlock key") diff --git a/message_import.go b/message_import.go index f0d8321..3cd9f24 100644 --- a/message_import.go +++ b/message_import.go @@ -13,6 +13,7 @@ import ( "github.com/bradenaw/juniper/stream" "github.com/bradenaw/juniper/xslices" "github.com/go-resty/resty/v2" + "golang.org/x/exp/slices" ) const ( @@ -38,7 +39,7 @@ func (c *Client) ImportMessages(ctx context.Context, addrKR *crypto.KeyRing, wor } // If any of the messages exceed the maximum import size, return an error. - if xslices.Any(req, func(req ImportReq) bool { return len(req.Message) > maxImportSize }) { + if slices.ContainsFunc(req, func(req ImportReq) bool { return len(req.Message) > maxImportSize }) { return nil, ErrImportSizeExceeded } diff --git a/salt_types.go b/salt_types.go index 8f06861..b2120ce 100644 --- a/salt_types.go +++ b/salt_types.go @@ -5,7 +5,7 @@ import ( "fmt" "github.com/ProtonMail/go-srp" - "github.com/bradenaw/juniper/xslices" + "golang.org/x/exp/slices" ) type Salt struct { @@ -15,7 +15,7 @@ type Salt struct { type Salts []Salt func (salts Salts) SaltForKey(keyPass []byte, keyID string) ([]byte, error) { - idx := xslices.IndexFunc(salts, func(salt Salt) bool { + idx := slices.IndexFunc(salts, func(salt Salt) bool { return salt.ID == keyID }) diff --git a/server/backend/api.go b/server/backend/api.go index 6fc3731..f9e1181 100644 --- a/server/backend/api.go +++ b/server/backend/api.go @@ -341,8 +341,8 @@ func (b *Backend) GetLabels(userID string, types ...proton.LabelType) ([]proton. } if len(types) > 0 { - res = xslices.Filter(res, func(label proton.Label) bool { - return slices.Contains(types, label.Type) + res = slices.DeleteFunc(res, func(label proton.Label) bool { + return !slices.Contains(types, label.Type) }) } @@ -431,7 +431,7 @@ func (b *Backend) DeleteLabel(userID, labelID string) error { return err } - acc.labelIDs = xslices.Filter(acc.labelIDs, func(otherID string) bool { return otherID != labelID }) + acc.labelIDs = slices.DeleteFunc(acc.labelIDs, func(otherID string) bool { return otherID == labelID }) acc.updateIDs = append(acc.updateIDs, updateID) } @@ -500,7 +500,7 @@ func (b *Backend) GetMessages(userID string, page, pageSize int, filter proton.M // deleted in between this metadata request. The backend has the information stored differently and can // resolve these gaps. if filter.EndID != "" { - index := xslices.IndexFunc(metadata, func(metadata proton.MessageMetadata) bool { + index := slices.IndexFunc(metadata, func(metadata proton.MessageMetadata) bool { return metadata.ID == filter.EndID }) @@ -509,38 +509,38 @@ func (b *Backend) GetMessages(userID string, page, pageSize int, filter proton.M } } - metadata = xslices.Filter(metadata, func(metadata proton.MessageMetadata) bool { + metadata = slices.DeleteFunc(metadata, func(metadata proton.MessageMetadata) bool { if len(filter.ID) > 0 { if !slices.Contains(filter.ID, metadata.ID) { - return false + return true } } if filter.Subject != "" { if !strings.Contains(metadata.Subject, filter.Subject) { - return false + return true } } if filter.AddressID != "" { if filter.AddressID != metadata.AddressID { - return false + return true } } if filter.ExternalID != "" { if filter.ExternalID != metadata.ExternalID { - return false + return true } } if filter.LabelID != "" { if !slices.Contains(metadata.LabelIDs, filter.LabelID) { - return false + return true } } - return true + return false }) pages := xslices.Chunk(metadata, pageSize) @@ -712,7 +712,7 @@ func (b *Backend) DeleteMessage(userID, messageID string) error { return err } - acc.messageIDs = xslices.Filter(acc.messageIDs, func(otherID string) bool { return otherID != messageID }) + acc.messageIDs = slices.DeleteFunc(acc.messageIDs, func(otherID string) bool { return otherID == messageID }) acc.updateIDs = append(acc.updateIDs, updateID) return nil @@ -1045,7 +1045,7 @@ func (b *Backend) GetEvent(userID, rawEventID string) (event proton.Event, more return withMessages(b, func(messages map[string]*message) (proton.Event, error) { return withLabels(b, func(labels map[string]*label) (proton.Event, error) { return withAtts(b, func(attachments map[string]*attachment) (proton.Event, error) { - firstUpdate := xslices.Index(acc.updateIDs, eventID) + 1 + firstUpdate := slices.Index(acc.updateIDs, eventID) + 1 lastUpdate := getLastUpdateIndex(len(acc.updateIDs), firstUpdate, b.maxUpdatesPerEvent) updates, err := withUpdates(b, func(updates map[ID]update) ([]update, error) { diff --git a/server/backend/backend.go b/server/backend/backend.go index 408242d..c8da539 100644 --- a/server/backend/backend.go +++ b/server/backend/backend.go @@ -235,7 +235,7 @@ func (b *Backend) RemoveUserKey(userID, keyID string) error { return fmt.Errorf("user %s does not exist", userID) } - idx := xslices.IndexFunc(user.keys, func(key key) bool { + idx := slices.IndexFunc(user.keys, func(key key) bool { return key.keyID == keyID }) @@ -463,7 +463,7 @@ func (b *Backend) RemoveAddress(userID, addrID string) error { func (b *Backend) RemoveAddressKey(userID, addrID, keyID string) error { return writeBackendRet(b, func(b *unsafeBackend) error { return b.withAcc(userID, func(acc *account) error { - idx := xslices.IndexFunc(acc.addresses[addrID].keys, func(key key) bool { + idx := slices.IndexFunc(acc.addresses[addrID].keys, func(key key) bool { return key.keyID == keyID }) diff --git a/server/backend/message.go b/server/backend/message.go index 59c5873..0e67768 100644 --- a/server/backend/message.go +++ b/server/backend/message.go @@ -320,16 +320,16 @@ func (msg *message) addLabel(labelID string, labels map[string]*label) { } func (msg *message) addFlagLabel(labelID string, labels map[string]*label) { - msg.labelIDs = xslices.Filter(msg.labelIDs, func(otherLabelID string) bool { - return labels[otherLabelID].labelType == proton.LabelTypeLabel + msg.labelIDs = slices.DeleteFunc(msg.labelIDs, func(otherLabelID string) bool { + return labels[otherLabelID].labelType != proton.LabelTypeLabel }) msg.sysLabel = nil } func (msg *message) addSystemLabel(labelID string, labels map[string]*label) { - msg.labelIDs = xslices.Filter(msg.labelIDs, func(otherLabelID string) bool { - return labels[otherLabelID].labelType == proton.LabelTypeLabel + msg.labelIDs = slices.DeleteFunc(msg.labelIDs, func(otherLabelID string) bool { + return labels[otherLabelID].labelType != proton.LabelTypeLabel }) msg.sysLabel = &labelID @@ -337,8 +337,8 @@ func (msg *message) addSystemLabel(labelID string, labels map[string]*label) { func (msg *message) addUserLabel(label *label, labels map[string]*label) { if label.labelType != proton.LabelTypeLabel { - msg.labelIDs = xslices.Filter(msg.labelIDs, func(otherLabelID string) bool { - return labels[otherLabelID].labelType == proton.LabelTypeLabel + msg.labelIDs = slices.DeleteFunc(msg.labelIDs, func(otherLabelID string) bool { + return labels[otherLabelID].labelType != proton.LabelTypeLabel }) msg.sysLabel = pointer("") @@ -380,8 +380,8 @@ func (msg *message) remSystemLabel(labelID string, labels map[string]*label) { } func (msg *message) remUserLabel(label *label, labels map[string]*label) { - msg.labelIDs = xslices.Filter(msg.labelIDs, func(otherLabelID string) bool { - return otherLabelID != label.labelID + msg.labelIDs = slices.DeleteFunc(msg.labelIDs, func(otherLabelID string) bool { + return otherLabelID == label.labelID }) } diff --git a/server/backend/updates.go b/server/backend/updates.go index 75544af..83d2b68 100644 --- a/server/backend/updates.go +++ b/server/backend/updates.go @@ -1,8 +1,9 @@ package backend import ( + "slices" + "github.com/ProtonMail/go-proton-api" - "github.com/bradenaw/juniper/xslices" ) func merge(updates []update) []update { @@ -10,7 +11,7 @@ func merge(updates []update) []update { return updates } - if merged := merge(updates[1:]); xslices.IndexFunc(merged, func(other update) bool { + if merged := merge(updates[1:]); slices.IndexFunc(merged, func(other update) bool { return other.replaces(updates[0]) }) < 0 { return append([]update{updates[0]}, merged...) diff --git a/server/server_test.go b/server/server_test.go index a3c0bef..23c7c75 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -2218,12 +2218,12 @@ func TestServer_GetMessageGroupCount(t *testing.T) { counts, err := c.GetGroupedMessageCount(ctx) require.NoError(t, err) - counts = xslices.Filter(counts, func(t proton.MessageGroupCount) bool { + counts = slices.DeleteFunc(counts, func(t proton.MessageGroupCount) bool { switch t.LabelID { case proton.InboxLabel, proton.TrashLabel, proton.ArchiveLabel, proton.AllMailLabel, proton.SentLabel: - return true - default: return false + default: + return true } }) require.NotEmpty(t, counts) @@ -2377,7 +2377,7 @@ func TestServer_Contacts(t *testing.T) { require.Len(t, contacts, len(testContacts)) for _, v := range testContacts { - require.NotEqual(t, -1, xslices.IndexFunc(contacts, func(contact proton.Contact) bool { + require.NotEqual(t, -1, slices.IndexFunc(contacts, func(contact proton.Contact) bool { return contact.Name == v.Name })) }