Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Inline slices contains helpers

Manav Rathia108964b5cae
diff --git a/server/pkg/controller/appstore.go b/server/pkg/controller/appstore.go
index bdca2511d9..a30bd2dd6a 100644
--- a/server/pkg/controller/appstore.go
+++ b/server/pkg/controller/appstore.go
@@ -5,6 +5,7 @@ import (
"database/sql"
"errors"
"fmt"
+ "slices"
"strconv"
"strings"
"time"
@@ -24,7 +25,6 @@ import (
"github.com/ente-io/museum/ente"
"github.com/ente-io/museum/pkg/repo"
"github.com/ente-io/museum/pkg/repo/remotestore"
- "github.com/ente-io/museum/pkg/utils/array"
)
// AppStoreController provides abstractions for handling billing on AppStore
@@ -109,7 +109,7 @@ func (c *AppStoreController) HandleNotification(ctx *gin.Context, notification a
return stacktrace.Propagate(err, "")
}
latestReceiptInfo := c.getLatestReceiptInfo(purchase.LatestReceiptInfo)
- if latestReceiptInfo.TransactionID == latestReceiptInfo.OriginalTransactionID && !array.StringInList(string(notification.NotificationType), SubsUpdateNotificationTypes) {
+ if latestReceiptInfo.TransactionID == latestReceiptInfo.OriginalTransactionID && !slices.Contains(SubsUpdateNotificationTypes, string(notification.NotificationType)) {
var logMsg = fmt.Sprintf("Ignoring notification of type %s", notification.NotificationType)
if notification.NotificationType != appstore.NotificationTypeInitialBuy {
// log unexpected notification types
diff --git a/server/pkg/controller/billing.go b/server/pkg/controller/billing.go
index 61a7193aa0..b96cbdf438 100644
--- a/server/pkg/controller/billing.go
+++ b/server/pkg/controller/billing.go
@@ -13,7 +13,6 @@ import (
"github.com/ente-io/museum/pkg/controller/discord"
"github.com/ente-io/museum/pkg/controller/email"
- "github.com/ente-io/museum/pkg/utils/array"
"github.com/ente-io/museum/pkg/utils/billing"
"github.com/ente-io/museum/pkg/utils/network"
"github.com/ente-io/museum/pkg/utils/time"
@@ -304,7 +303,7 @@ func (c *BillingController) VerifySubscription(
}
func (c *BillingController) getAllPlans(countryCode string, stripeAccountCountry ente.StripeAccountCountry) []ente.BillingPlan {
- if array.StringInList(countryCode, billing.CountriesInEU) {
+ if slices.Contains(billing.CountriesInEU, countryCode) {
countryCode = "EU"
}
countryWisePlans := c.BillingPlansPerAccount[stripeAccountCountry]
diff --git a/server/pkg/controller/collections/collection.go b/server/pkg/controller/collections/collection.go
index d140daad4c..1897a95137 100644
--- a/server/pkg/controller/collections/collection.go
+++ b/server/pkg/controller/collections/collection.go
@@ -3,6 +3,7 @@ package collections
import (
"context"
"fmt"
+ "slices"
"github.com/ente-io/museum/pkg/controller"
"github.com/ente-io/museum/pkg/controller/access"
@@ -10,7 +11,6 @@ import (
"github.com/ente-io/museum/pkg/controller/public"
"github.com/ente-io/museum/pkg/repo/cast"
socialrepo "github.com/ente-io/museum/pkg/repo/social"
- "github.com/ente-io/museum/pkg/utils/array"
"github.com/ente-io/museum/pkg/utils/auth"
"github.com/gin-gonic/gin"
@@ -61,7 +61,7 @@ func (c *CollectionController) Create(collection ente.Collection, ownerID int64)
if collection.Type == "CollectionType.album" {
collection.Type = "album"
}
- if !array.StringInList(collection.Type, ente.ValidCollectionTypes) {
+ if !slices.Contains(ente.ValidCollectionTypes, collection.Type) {
return ente.Collection{}, stacktrace.Propagate(fmt.Errorf("unexpected collection type %s", collection.Type), "")
}
collection, err := c.CollectionRepo.Create(collection)
@@ -117,7 +117,7 @@ func (c *CollectionController) GetFile(ctx *gin.Context, collectionID int64, fil
if err != nil {
return nil, stacktrace.Propagate(err, "")
}
- if !array.Int64InList(collectionID, cIDs) {
+ if !slices.Contains(cIDs, collectionID) {
return nil, stacktrace.Propagate(ente.ErrPermissionDenied, "")
}
}
diff --git a/server/pkg/controller/collections/share.go b/server/pkg/controller/collections/share.go
index d30df37438..ed2ef43684 100644
--- a/server/pkg/controller/collections/share.go
+++ b/server/pkg/controller/collections/share.go
@@ -4,11 +4,11 @@ import (
"context"
"database/sql"
"errors"
+ "slices"
"github.com/ente-io/museum/ente"
"github.com/ente-io/museum/pkg/controller/access"
"github.com/ente-io/museum/pkg/controller/public"
- "github.com/ente-io/museum/pkg/utils/array"
"github.com/ente-io/museum/pkg/utils/auth"
emailUtil "github.com/ente-io/museum/pkg/utils/email"
"github.com/ente-io/museum/pkg/utils/time"
@@ -181,7 +181,7 @@ func (c *CollectionController) Leave(ctx *gin.Context, cID int64) error {
if err != nil {
return stacktrace.Propagate(err, "")
}
- if !array.Int64InList(cID, sharedCollectionIDs) {
+ if !slices.Contains(sharedCollectionIDs, cID) {
return nil
}
err = c.CastRepo.RevokeForGivenUserAndCollection(ctx, cID, userID)
diff --git a/server/pkg/controller/filedata/controller.go b/server/pkg/controller/filedata/controller.go
index cdb1457f17..c1e8e9cf82 100644
--- a/server/pkg/controller/filedata/controller.go
+++ b/server/pkg/controller/filedata/controller.go
@@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
+ "slices"
+
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
@@ -268,7 +270,7 @@ func (c *Controller) fetchS3FileMetadata(ctx context.Context, row fileData.Row,
// If the current primary bucket is different from the latest bucket where data was written,
// check and use the preferred bucket if the data is replicated there.
if !strings.EqualFold(preferredBucket, dc) {
- if array.StringInList(preferredBucket, row.ReplicatedBuckets) {
+ if slices.Contains(row.ReplicatedBuckets, preferredBucket) {
dc = preferredBucket
}
}
diff --git a/server/pkg/controller/offer/offer.go b/server/pkg/controller/offer/offer.go
index c49063a546..009a39467e 100644
--- a/server/pkg/controller/offer/offer.go
+++ b/server/pkg/controller/offer/offer.go
@@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"os"
+ "slices"
"github.com/ente-io/museum/pkg/controller/usercache"
@@ -15,7 +16,6 @@ import (
"github.com/ente-io/museum/pkg/controller/discord"
"github.com/ente-io/museum/pkg/repo"
"github.com/ente-io/museum/pkg/repo/storagebonus"
- "github.com/ente-io/museum/pkg/utils/array"
"github.com/ente-io/museum/pkg/utils/billing"
"github.com/ente-io/museum/pkg/utils/config"
emailUtil "github.com/ente-io/museum/pkg/utils/email"
@@ -62,7 +62,7 @@ func NewOfferController(
}
func (c *OfferController) GetBlackFridayOffers(countryCode string) []ente.BlackFridayOffer {
- if array.StringInList(countryCode, billing.CountriesInEU) {
+ if slices.Contains(billing.CountriesInEU, countryCode) {
countryCode = "EU"
}
diff --git a/server/pkg/middleware/collection_link.go b/server/pkg/middleware/collection_link.go
index afaeb5dff1..ba0d80baae 100644
--- a/server/pkg/middleware/collection_link.go
+++ b/server/pkg/middleware/collection_link.go
@@ -9,6 +9,7 @@ import (
"fmt"
"net/http"
"net/url"
+ "slices"
"strconv"
"strings"
@@ -26,7 +27,6 @@ import (
"github.com/ente-io/museum/pkg/controller"
"github.com/ente-io/museum/pkg/controller/discord"
"github.com/ente-io/museum/pkg/repo"
- "github.com/ente-io/museum/pkg/utils/array"
"github.com/ente-io/museum/pkg/utils/auth"
"github.com/ente-io/museum/pkg/utils/network"
"github.com/ente-io/museum/pkg/utils/time"
@@ -278,7 +278,7 @@ func (m *CollectionLinkMiddleware) validatePassword(c *gin.Context, reqPath stri
// /public-collection/info is allowed before password unlock so clients can
// fetch the KDF parameters needed to verify the password. Device-limit
// admission is intentionally still applied to this public entrypoint.
- if array.StringInList(reqPath, passwordWhiteListedURLs) {
+ if slices.Contains(passwordWhiteListedURLs, reqPath) {
return nil
}
accessTokenJWT := auth.GetAccessTokenJWT(c)
diff --git a/server/pkg/middleware/file_link.go b/server/pkg/middleware/file_link.go
index 26e277469f..9d3ac2f27d 100644
--- a/server/pkg/middleware/file_link.go
+++ b/server/pkg/middleware/file_link.go
@@ -4,10 +4,10 @@ import (
"context"
"fmt"
"net/http"
+ "slices"
publicCtrl "github.com/ente-io/museum/pkg/controller/public"
"github.com/ente-io/museum/pkg/repo/public"
- "github.com/ente-io/museum/pkg/utils/array"
"github.com/ente-io/museum/ente"
"github.com/ente-io/museum/pkg/controller"
@@ -206,7 +206,7 @@ func (m *FileLinkMiddleware) validatePassword(
) error {
accessTokenJWT := auth.GetAccessTokenJWT(c)
if accessTokenJWT == "" {
- if array.StringInList(reqPath, filePasswordWhiteListedURLs) {
+ if slices.Contains(filePasswordWhiteListedURLs, reqPath) {
return nil
}
return &ente.ErrPassProtectedResource
diff --git a/server/pkg/repo/contact/contact.go b/server/pkg/repo/contact/contact.go
index f2629bf5e8..0e6c04b3a3 100644
--- a/server/pkg/repo/contact/contact.go
+++ b/server/pkg/repo/contact/contact.go
@@ -5,11 +5,11 @@ import (
"database/sql"
"errors"
"fmt"
+ "slices"
"time"
"github.com/ente-io/museum/ente"
contactmodel "github.com/ente-io/museum/ente/contact"
- "github.com/ente-io/museum/pkg/utils/array"
"github.com/ente-io/museum/pkg/utils/crypto"
"github.com/ente-io/stacktrace"
"github.com/lib/pq"
@@ -630,10 +630,10 @@ func (r *Repository) MarkAttachmentReplicationAsDone(ctx context.Context, row co
}
func (r *Repository) RegisterReplicationAttempt(ctx context.Context, row contactmodel.Attachment, dstBucketID string) error {
- if array.StringInList(dstBucketID, row.DeleteFromBuckets) {
+ if slices.Contains(row.DeleteFromBuckets, dstBucketID) {
return r.MoveBetweenBuckets(row, dstBucketID, DeletionColumn, InflightRepColumn)
}
- if !array.StringInList(dstBucketID, row.InflightRepBuckets) {
+ if !slices.Contains(row.InflightRepBuckets, dstBucketID) {
return r.AddBucket(row, dstBucketID, InflightRepColumn)
}
return nil
diff --git a/server/pkg/repo/filedata/repository.go b/server/pkg/repo/filedata/repository.go
index 2fbc3b3a4b..74e25e895d 100644
--- a/server/pkg/repo/filedata/repository.go
+++ b/server/pkg/repo/filedata/repository.go
@@ -4,13 +4,14 @@ import (
"context"
"database/sql"
"fmt"
+ "slices"
+ "time"
+
"github.com/ente-io/museum/ente"
"github.com/ente-io/museum/ente/filedata"
"github.com/ente-io/museum/pkg/repo"
- "github.com/ente-io/museum/pkg/utils/array"
"github.com/ente-io/stacktrace"
"github.com/lib/pq"
- "time"
)
// Repository defines the methods for inserting, updating, and retrieving file data.
@@ -276,10 +277,10 @@ func (r *Repository) MarkReplicationAsDone(ctx context.Context, row filedata.Row
}
func (r *Repository) RegisterReplicationAttempt(ctx context.Context, row filedata.Row, dstBucketID string) error {
- if array.StringInList(dstBucketID, row.DeleteFromBuckets) {
+ if slices.Contains(row.DeleteFromBuckets, dstBucketID) {
return r.MoveBetweenBuckets(row, dstBucketID, DeletionColumn, InflightRepColumn)
}
- if !array.StringInList(dstBucketID, row.InflightReplicas) {
+ if !slices.Contains(row.InflightReplicas, dstBucketID) {
return r.AddBucket(row, dstBucketID, InflightRepColumn)
}
return nil
diff --git a/server/pkg/utils/array/array.go b/server/pkg/utils/array/array.go
index 75b36e1539..ef18f05b07 100644
--- a/server/pkg/utils/array/array.go
+++ b/server/pkg/utils/array/array.go
@@ -1,7 +1,5 @@
package array
-import "slices"
-
// UniqueInt64 returns unique elements in the input array
func UniqueInt64(input []int64) []int64 {
visited := map[int64]bool{}
@@ -30,16 +28,6 @@ func ContainsDuplicateInInt64Array(input []int64) bool {
return false
}
-// StringInList returns true is given string is present inside list
-func StringInList(a string, list []string) bool {
- return slices.Contains(list, a)
-}
-
-// int64InList returns true is given int64 is present inside list
-func Int64InList(a int64, list []int64) bool {
- return slices.Contains(list, a)
-}
-
// FindMissingElementsInSecondList identifies elements in 'sourceList' that are not present in 'targetList'.
// Returns:
// - A slice of int64 representing the elements found in 'sourceList' but not in 'targetList'.
diff --git a/server/pkg/utils/s3config/s3config.go b/server/pkg/utils/s3config/s3config.go
index d6f69f2ee7..1bf9925432 100644
--- a/server/pkg/utils/s3config/s3config.go
+++ b/server/pkg/utils/s3config/s3config.go
@@ -2,6 +2,8 @@ package s3config
import (
"fmt"
+ "slices"
+
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
@@ -9,8 +11,6 @@ import (
"github.com/ente-io/museum/ente"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
-
- "github.com/ente-io/museum/pkg/utils/array"
)
// S3Config is the file which abstracts away s3 related configs for clients.
@@ -111,14 +111,14 @@ func (config *S3Config) initialize() {
config.secondaryHotDC = dcWasabiEuropeCentral_v3
hs1 := viper.GetString("s3.hot_storage.primary")
hs2 := viper.GetString("s3.hot_storage.secondary")
- if hs1 != "" && hs2 != "" && array.StringInList(hs1, dcs[:]) && array.StringInList(hs2, dcs[:]) {
+ if hs1 != "" && hs2 != "" && slices.Contains(dcs[:], hs1) && slices.Contains(dcs[:], hs2) {
config.hotDC = hs1
config.secondaryHotDC = hs2
log.Infof("Hot storage: %s (secondary: %s)", hs1, hs2)
}
config.derivedStorageDC = config.hotDC
embeddingsDC := viper.GetString("s3.derived-storage")
- if embeddingsDC != "" && array.StringInList(embeddingsDC, dcs[:]) {
+ if embeddingsDC != "" && slices.Contains(dcs[:], embeddingsDC) {
config.derivedStorageDC = embeddingsDC
log.Infof("Embeddings bucket: %s", embeddingsDC)
}