[server] Clean up old logic for collection diff (#5130)
## Description We still have some traffic from old mobile client v0.7.xx that is making call to the v1 endpoint. ## Tests
This commit is contained in:
@@ -65,25 +65,7 @@ func (h *CollectionHandler) GetCollectionByID(c *gin.Context) {
|
||||
// Deprecated: Remove once rps goes to 0.
|
||||
// Get returns the list of collections accessible to a user.
|
||||
func (h *CollectionHandler) Get(c *gin.Context) {
|
||||
userID := auth.GetUserID(c.Request.Header)
|
||||
sinceTime, _ := strconv.ParseInt(c.Query("sinceTime"), 10, 64)
|
||||
|
||||
app := auth.GetApp(c)
|
||||
|
||||
// TODO: Compute both with a single query
|
||||
ownedCollections, err := h.Controller.GetOwned(userID, sinceTime, app)
|
||||
if err != nil {
|
||||
handler.Error(c, stacktrace.Propagate(err, "Failed to get owned collections"))
|
||||
return
|
||||
}
|
||||
sharedCollections, err := h.Controller.GetSharedWith(userID, sinceTime, app)
|
||||
if err != nil {
|
||||
handler.Error(c, stacktrace.Propagate(err, "Failed to get shared collections"))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"collections": append(ownedCollections, sharedCollections...),
|
||||
})
|
||||
h.GetV2(c)
|
||||
}
|
||||
|
||||
// GetV2 returns the list of collections accessible to a user
|
||||
|
||||
@@ -1,46 +1,10 @@
|
||||
package collections
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/ente-io/museum/ente"
|
||||
"github.com/ente-io/stacktrace"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
// GetOwned returns the list of collections owned by a user
|
||||
func (c *CollectionController) GetOwned(userID int64, sinceTime int64, app ente.App) ([]ente.Collection, error) {
|
||||
collections, err := c.CollectionRepo.GetCollectionsOwnedByUser(userID, sinceTime, app)
|
||||
if err != nil {
|
||||
return nil, stacktrace.Propagate(err, "")
|
||||
}
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Errorf("Panic caught: %s, stack: %s", r, string(debug.Stack()))
|
||||
}
|
||||
}()
|
||||
collectionsV2, errV2 := c.CollectionRepo.GetCollectionsOwnedByUserV2(userID, sinceTime, app)
|
||||
if errV2 != nil {
|
||||
log.WithError(errV2).Error("failed to fetch collections using v2")
|
||||
}
|
||||
isEqual := cmp.Equal(collections, collectionsV2, cmpopts.SortSlices(func(a, b ente.Collection) bool { return a.ID < b.ID }))
|
||||
if !isEqual {
|
||||
jsonV1, _ := json.Marshal(collections)
|
||||
jsonV2, _ := json.Marshal(collectionsV2)
|
||||
log.WithFields(log.Fields{
|
||||
"v1": string(jsonV1),
|
||||
"v2": string(jsonV2),
|
||||
}).Error("collections diff didn't match")
|
||||
} else {
|
||||
log.Info("collections diff matched")
|
||||
}
|
||||
}()
|
||||
return collections, nil
|
||||
}
|
||||
|
||||
// GetOwnedV2 returns the list of collections owned by a user using optimized query
|
||||
func (c *CollectionController) GetOwnedV2(userID int64, sinceTime int64, app ente.App) ([]ente.Collection, error) {
|
||||
collections, err := c.CollectionRepo.GetCollectionsOwnedByUserV2(userID, sinceTime, app)
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
t "time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
@@ -102,54 +101,6 @@ func (repo *CollectionRepository) GetCollectionByType(userID int64, collectionTy
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// GetCollectionsOwnedByUser returns the list of collections that a user owns
|
||||
// todo: refactor this method
|
||||
func (repo *CollectionRepository) GetCollectionsOwnedByUser(userID int64, updationTime int64, app ente.App) ([]ente.Collection, error) {
|
||||
rows, err := repo.DB.Query(`
|
||||
SELECT collections.collection_id, collections.owner_id, collections.encrypted_key, collections.key_decryption_nonce, collections.name, collections.encrypted_name, collections.name_decryption_nonce, collections.type, collections.app, collections.attributes, collections.updation_time, collections.is_deleted, collections.magic_metadata, collections.pub_magic_metadata
|
||||
FROM collections
|
||||
WHERE collections.owner_id = $1 AND collections.updation_time > $2 AND app = $3`, userID, updationTime, strings.ToLower(string(app)))
|
||||
if err != nil {
|
||||
return nil, stacktrace.Propagate(err, "")
|
||||
}
|
||||
defer rows.Close()
|
||||
collectionIDs := make([]int64, 0)
|
||||
collections := make([]ente.Collection, 0)
|
||||
result := make([]ente.Collection, 0)
|
||||
for rows.Next() {
|
||||
var c ente.Collection
|
||||
var name, encryptedName, nameDecryptionNonce sql.NullString
|
||||
if err := rows.Scan(&c.ID, &c.Owner.ID, &c.EncryptedKey, &c.KeyDecryptionNonce, &name, &encryptedName, &nameDecryptionNonce, &c.Type, &c.App, &c.Attributes, &c.UpdationTime, &c.IsDeleted, &c.MagicMetadata, &c.PublicMagicMetadata); err != nil {
|
||||
return collections, stacktrace.Propagate(err, "")
|
||||
}
|
||||
if name.Valid && len(name.String) > 0 {
|
||||
c.Name = name.String
|
||||
} else {
|
||||
c.EncryptedName = encryptedName.String
|
||||
c.NameDecryptionNonce = nameDecryptionNonce.String
|
||||
}
|
||||
// TODO: Pull this information in the previous query
|
||||
sharees, err := repo.GetSharees(c.ID)
|
||||
if err != nil {
|
||||
return collections, stacktrace.Propagate(err, "")
|
||||
}
|
||||
c.Sharees = sharees
|
||||
collections = append(collections, c)
|
||||
collectionIDs = append(collectionIDs, c.ID)
|
||||
}
|
||||
|
||||
urlMap, err := repo.PublicCollectionRepo.GetCollectionToActivePublicURLMap(context.Background(), collectionIDs)
|
||||
if err != nil {
|
||||
return nil, stacktrace.Propagate(err, "failed to get publicURL info")
|
||||
}
|
||||
for _, c := range collections {
|
||||
c.PublicURLs = urlMap[c.ID]
|
||||
result = append(result, c)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (repo *CollectionRepository) GetCollectionsOwnedByUserV2(userID int64, updationTime int64, app ente.App) ([]ente.Collection, error) {
|
||||
rows, err := repo.DB.Query(`
|
||||
SELECT
|
||||
|
||||
Reference in New Issue
Block a user