Skip to content

armgit / ente

Public Imported from GitHub
0

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

Use nanoId for entity_data.id

Neeraj Gupta05df5962ef22
diff --git a/server/ente/base/id.go b/server/ente/base/id.go
new file mode 100644
index 0000000000..f6579a05c6
--- /dev/null
+++ b/server/ente/base/id.go
@@ -0,0 +1,30 @@
+package base
+
+import (
+ "errors"
+ "fmt"
+ "github.com/matoous/go-nanoid/v2"
+)
+
+// Ref https://github.com/ente-io/ente/blob/main/web/packages/base/id.ts#L4
+const alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
+
+// NewID generates a new random identifier with the given prefix.
+func NewID(prefix string) (*string, error) {
+ if len(prefix) < 2 {
+ return nil, errors.New("prefix must be at least 2 characters long")
+ }
+ // check that prefix only contains alphabet characters
+ for _, c := range prefix {
+ if !(c >= 'a' && c <= 'z') {
+ return nil, errors.New("prefix must only contain lower case alphabet characters")
+ }
+ }
+ // Generate a nanoid with a custom alphabet and length of 22
+ id, err := gonanoid.Generate(alphabet, 22)
+ if err != nil {
+ return nil, err
+ }
+ result := fmt.Sprintf("%s_%s", prefix, id)
+ return &result, nil
+}
diff --git a/server/ente/userentity/entity.go b/server/ente/userentity/entity.go
index 965938b63d..4d75a04c77 100644
--- a/server/ente/userentity/entity.go
+++ b/server/ente/userentity/entity.go
@@ -1,14 +1,20 @@
package userentity
+import "github.com/ente-io/museum/ente/base"
+
type EntityType string
const (
Location EntityType = "location"
Person EntityType = "person"
- // PersonV2 is a new version of Person entity, where the data is gzipped before encryption
- PersonV2 EntityType = "person_v2"
+ // Profile is a new version of Person entity, where the data is gzipped before encryption
+ Profile EntityType = "profile"
)
+func (et EntityType) GetNewID() (*string, error) {
+ return base.NewID(string(et))
+}
+
type EntityKey struct {
UserID int64 `json:"userID" binding:"required"`
Type EntityType `json:"type" binding:"required"`
diff --git a/server/go.mod b/server/go.mod
index 60a0e7ea65..9ca4a93871 100644
--- a/server/go.mod
+++ b/server/go.mod
@@ -2,7 +2,6 @@ module github.com/ente-io/museum
go 1.21
-
require (
firebase.google.com/go v3.13.0+incompatible
github.com/GoKillers/libsodium-go v0.0.0-20171022220152-dd733721c3cb
@@ -24,6 +23,7 @@ require (
github.com/kong/go-srp v0.0.0-20191210190804-cde1efa3c083
github.com/lib/pq v1.8.0
github.com/lithammer/shortuuid/v3 v3.0.4
+ github.com/matoous/go-nanoid/v2 v2.1.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pquerna/otp v1.3.0
github.com/prometheus/client_golang v1.11.1
diff --git a/server/go.sum b/server/go.sum
index 544b2b032c..ed9338a487 100644
--- a/server/go.sum
+++ b/server/go.sum
@@ -435,6 +435,8 @@ github.com/lithammer/shortuuid/v3 v3.0.4/go.mod h1:RviRjexKqIzx/7r1peoAITm6m7gni
github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/markbates/pkger v0.15.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI=
+github.com/matoous/go-nanoid/v2 v2.1.0 h1:P64+dmq21hhWdtvZfEAofnvJULaRR1Yib0+PnU669bE=
+github.com/matoous/go-nanoid/v2 v2.1.0/go.mod h1:KlbGNQ+FhrUNIHUxZdL63t7tl4LaPkZNpUULS8H4uVM=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
diff --git a/server/pkg/repo/userentity/data.go b/server/pkg/repo/userentity/data.go
index 25b0e5a52a..88ce4181fd 100644
--- a/server/pkg/repo/userentity/data.go
+++ b/server/pkg/repo/userentity/data.go
@@ -8,14 +8,17 @@ import (
model "github.com/ente-io/museum/ente/userentity"
"github.com/ente-io/stacktrace"
- "github.com/google/uuid"
"github.com/sirupsen/logrus"
)
// Create inserts a new entry
func (r *Repository) Create(ctx context.Context, userID int64, entry model.EntityDataRequest) (string, error) {
- id := uuid.New()
- err := r.DB.QueryRow(`INSERT into entity_data(
+ idPrt, err := entry.Type.GetNewID()
+ if err != nil {
+ return "", stacktrace.Propagate(err, "failed to generate new id")
+ }
+ id := *idPrt
+ err = r.DB.QueryRow(`INSERT into entity_data(
id,
user_id,
type,
@@ -28,9 +31,9 @@ func (r *Repository) Create(ctx context.Context, userID int64, entry model.Entit
entry.Header). // $5 header
Scan(&id)
if err != nil {
- return id.String(), stacktrace.Propagate(err, "failed to create enity data")
+ return id, stacktrace.Propagate(err, "failed to create enity data")
}
- return id.String(), nil
+ return id, nil
}
func (r *Repository) Get(ctx context.Context, userID int64, id string) (*model.EntityData, error) {