π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mob]Allow unique favorites per app
Neeraj Gupta22282218827c
diff --git a/server/migrations/111_update_favorites_constraint.down.sql b/server/migrations/111_update_favorites_constraint.down.sqlnew file mode 100644index 0000000000..20e3beab0a--- /dev/null+++ b/server/migrations/111_update_favorites_constraint.down.sql@@ -0,0 +1,7 @@+-- Revert favorites uniqueness back to per (owner_id) only++DROP INDEX IF EXISTS collections_favorites_constraint_index_v2;++CREATE UNIQUE INDEX IF NOT EXISTS collections_favorites_constraint_index ON collections (owner_id)+WHERE (type = 'favorites');+diff --git a/server/migrations/111_update_favorites_constraint.up.sql b/server/migrations/111_update_favorites_constraint.up.sqlnew file mode 100644index 0000000000..e65da6ed7d--- /dev/null+++ b/server/migrations/111_update_favorites_constraint.up.sql@@ -0,0 +1,9 @@+-- Make favorites unique per (owner_id, app), similar to uncategorized++CREATE UNIQUE INDEX IF NOT EXISTS collections_favorites_constraint_index_v2 ON collections (owner_id, app)+WHERE (type = 'favorites');++-- Drop the older index if it exists++DROP INDEX IF EXISTS collections_favorites_constraint_index;+diff --git a/server/pkg/controller/collections/collection.go b/server/pkg/controller/collections/collection.goindex fd81c38fbf..5bb1e2a6ca 100644--- a/server/pkg/controller/collections/collection.go+++ b/server/pkg/controller/collections/collection.go@@ -56,8 +56,8 @@ func (c *CollectionController) Create(collection ente.Collection, ownerID int64)}collection, err := c.CollectionRepo.Create(collection)if err != nil {- if err == ente.ErrUncategorizeCollectionAlreadyExists || err == ente.ErrFavoriteCollectionAlreadyExist {- dbCollection, err := c.CollectionRepo.GetCollectionByType(ownerID, collectionType)+ if err == ente.ErrUncategorizeCollectionAlreadyExists || err == ente.ErrFavoriteCollectionAlreadyExist {+ dbCollection, err := c.CollectionRepo.GetCollectionByType(ownerID, collectionType, collection.App)if err != nil {return ente.Collection{}, stacktrace.Propagate(err, "")}diff --git a/server/pkg/repo/collection.go b/server/pkg/repo/collection.goindex efbdaa9136..dff5fd729d 100644--- a/server/pkg/repo/collection.go+++ b/server/pkg/repo/collection.go@@ -46,16 +46,17 @@ func (repo *CollectionRepository) Create(c ente.Collection) (ente.Collection, erreturn ente.Collection{}, ente.ErrInvalidApp}- err := repo.DB.QueryRow(`INSERT INTO collections(owner_id, encrypted_key, key_decryption_nonce, name, encrypted_name, name_decryption_nonce, type, attributes, updation_time, magic_metadata, pub_magic_metadata, app)- VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING collection_id`,- c.Owner.ID, c.EncryptedKey, c.KeyDecryptionNonce, c.Name, c.EncryptedName, c.NameDecryptionNonce, c.Type, c.Attributes, c.UpdationTime, c.MagicMetadata, c.PublicMagicMetadata, c.App).Scan(&c.ID)- if err != nil {- if err.Error() == "pq: duplicate key value violates unique constraint \"collections_favorites_constraint_index\"" {- return ente.Collection{}, ente.ErrFavoriteCollectionAlreadyExist- } else if err.Error() == "pq: duplicate key value violates unique constraint \"collections_uncategorized_constraint_index_v2\"" {- return ente.Collection{}, ente.ErrUncategorizeCollectionAlreadyExists- }- }+ err := repo.DB.QueryRow(`INSERT INTO collections(owner_id, encrypted_key, key_decryption_nonce, name, encrypted_name, name_decryption_nonce, type, attributes, updation_time, magic_metadata, pub_magic_metadata, app)+ VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING collection_id`,+ c.Owner.ID, c.EncryptedKey, c.KeyDecryptionNonce, c.Name, c.EncryptedName, c.NameDecryptionNonce, c.Type, c.Attributes, c.UpdationTime, c.MagicMetadata, c.PublicMagicMetadata, c.App).Scan(&c.ID)+ if err != nil {+ if err.Error() == "pq: duplicate key value violates unique constraint \"collections_favorites_constraint_index\"" ||+ err.Error() == "pq: duplicate key value violates unique constraint \"collections_favorites_constraint_index_v2\"" {+ return ente.Collection{}, ente.ErrFavoriteCollectionAlreadyExist+ } else if err.Error() == "pq: duplicate key value violates unique constraint \"collections_uncategorized_constraint_index_v2\"" {+ return ente.Collection{}, ente.ErrUncategorizeCollectionAlreadyExists+ }+ }return c, stacktrace.Propagate(err, "")}@@ -132,10 +133,10 @@ func (repo *CollectionRepository) GetWithSharingDetailsForUser(collectionID int6}return c, nil}-func (repo *CollectionRepository) GetCollectionByType(userID int64, collectionType string) (ente.Collection, error) {- row := repo.DB.QueryRow(`SELECT collection_id, owner_id, encrypted_key, key_decryption_nonce, name, encrypted_name, name_decryption_nonce, type, attributes, updation_time, is_deleted, magic_metadata- FROM collections- WHERE owner_id = $1 and type = $2`, userID, collectionType)+func (repo *CollectionRepository) GetCollectionByType(userID int64, collectionType string, app string) (ente.Collection, error) {+ row := repo.DB.QueryRow(`SELECT collection_id, owner_id, encrypted_key, key_decryption_nonce, name, encrypted_name, name_decryption_nonce, type, attributes, updation_time, is_deleted, magic_metadata+ FROM collections+ WHERE owner_id = $1 and type = $2 and app = $3`, userID, collectionType, app)var c ente.Collectionvar name, encryptedName, nameDecryptionNonce sql.NullStringif err := row.Scan(&c.ID, &c.Owner.ID, &c.EncryptedKey, &c.KeyDecryptionNonce, &name, &encryptedName, &nameDecryptionNonce, &c.Type, &c.Attributes, &c.UpdationTime, &c.IsDeleted, &c.MagicMetadata); err != nil {