Skip to content

armgit / ente

Public Imported from GitHub
0

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

[server] Add bulk collection sharing endpoints (#11809)

Neeraj7919a9802129
diff --cc server/pkg/middleware/rate_limit.go
index e872105baa,91e117fc70..bc1c60a6ae
--- a/server/pkg/middleware/rate_limit.go
+++ b/server/pkg/middleware/rate_limit.go
@@@ -262,8 -262,11 +262,11 @@@ func (r *RateLimitMiddleware) getLimite
if isPublicCollectionUploadURLPath(reqPath) {
return r.limit250ReqPerMin
}
+ if reqPath == "/collections/share/bulk" || reqPath == "/collections/unshare/bulk" {
+ return r.limit60ReqPerMin
+ }
if reqPath == "/users/public-key" {
- return r.limit200ReqPerMin
+ return r.limit60ReqPerMin
}
if reqPath == "/paste/guard" || reqPath == "/paste/consume" {
return r.limit200ReqPerMin
diff --cc server/pkg/repo/collection.go
index e7f1730262,b4b826aa17..9dcb7898fd
--- a/server/pkg/repo/collection.go
+++ b/server/pkg/repo/collection.go
@@@ -578,108 -636,63 +636,132 @@@ func (repo *CollectionRepository) Updat
// UnShare un-shares a collection from a userID
func (repo *CollectionRepository) UnShare(collectionID int64, toUserID int64) error {
+ _, err := repo.UnShareContext(context.Background(), collectionID, toUserID)
+ return err
+ }
+
+ // UnShareContext revokes access to a collection.
+ func (repo *CollectionRepository) UnShareContext(
+ ctx context.Context,
+ collectionID int64,
+ toUserID int64,
+ ) (ente.CollectionShareStatus, error) {
updationTime := time.Microseconds()
- context := context.Background()
- tx, err := repo.DB.BeginTx(context, nil)
+ tx, err := repo.DB.BeginTx(ctx, nil)
if err != nil {
- return stacktrace.Propagate(err, "")
+ return "", stacktrace.Propagate(err, "")
+ }
+ defer tx.Rollback()
+
+ status := ente.CollectionAlreadyUnshared
+ var isDeleted bool
+ err = tx.QueryRowContext(ctx, `SELECT is_deleted
+ FROM collection_shares
+ WHERE collection_id = $1 AND to_user_id = $2
+ FOR UPDATE`, collectionID, toUserID).Scan(&isDeleted)
+ if errors.Is(err, sql.ErrNoRows) {
+ return ente.CollectionNotShared, nil
}
- _, err = tx.ExecContext(context, `UPDATE collection_shares
- SET is_deleted = $1, updation_time = $2
- WHERE collection_id = $3 AND to_user_id = $4`, true, updationTime, collectionID, toUserID)
if err != nil {
- tx.Rollback()
- return stacktrace.Propagate(err, "")
+ return "", stacktrace.Propagate(err, "")
+ }
+ if !isDeleted {
+ status = ente.CollectionUnshared
}
- // remove all the files which were added by this user
- // todo: should we also add c_owner_id != toUserId
- _, err = tx.ExecContext(context, `UPDATE collection_files
- SET is_deleted = $1, updation_time = $2
- WHERE collection_id = $3 AND f_owner_id = $4`, true, updationTime, collectionID, toUserID)
+ _, err = tx.ExecContext(ctx, `UPDATE collection_shares
+ SET is_deleted = TRUE, updation_time = $1
+ WHERE collection_id = $2 AND to_user_id = $3 AND is_deleted = FALSE`,
+ updationTime, collectionID, toUserID)
if err != nil {
- tx.Rollback()
- return stacktrace.Propagate(err, "")
+ return "", stacktrace.Propagate(err, "")
}
- _, err = tx.ExecContext(context, `UPDATE collections SET updation_time = $1
- WHERE collection_id = $2`, updationTime, collectionID)
+ _, err = tx.ExecContext(ctx, `UPDATE collection_files
+ SET is_deleted = TRUE, updation_time = $1
+ WHERE collection_id = $2 AND f_owner_id = $3 AND is_deleted = FALSE`,
+ updationTime, collectionID, toUserID)
if err != nil {
- tx.Rollback()
- return stacktrace.Propagate(err, "")
+ return "", stacktrace.Propagate(err, "")
}
- err = tx.Commit()
- return stacktrace.Propagate(err, "")
+ if _, err = tx.ExecContext(ctx, `UPDATE collections SET updation_time = $1
+ WHERE collection_id = $2`, updationTime, collectionID); err != nil {
+ return "", stacktrace.Propagate(err, "")
+ }
+ if err := tx.Commit(); err != nil {
+ return "", stacktrace.Propagate(err, "")
+ }
+ return status, nil
}
+func (repo *CollectionRepository) RepairCollectionSelfShare(ctx context.Context, collectionID int64, userID int64, updationTime int64) (bool, error) {
+ tx, err := repo.DB.BeginTx(ctx, nil)
+ if err != nil {
+ return false, stacktrace.Propagate(err, "")
+ }
+ defer tx.Rollback()
+
+ rows, err := tx.QueryContext(ctx, `
+ WITH target AS (
+ SELECT cs.collection_id, c.owner_id
+ FROM collection_shares cs
+ INNER JOIN collections c
+ ON c.collection_id = cs.collection_id
+ WHERE cs.is_deleted = FALSE
+ AND cs.collection_id = $1
+ AND c.owner_id = $2
+ AND cs.from_user_id = $2
+ AND cs.to_user_id = $2
+ GROUP BY cs.collection_id, c.owner_id
+ ),
+ deleted_shares AS (
+ DELETE FROM collection_shares cs
+ USING target
+ WHERE cs.collection_id = target.collection_id
+ AND cs.from_user_id = target.owner_id
+ AND cs.to_user_id = target.owner_id
+ AND cs.is_deleted = FALSE
+ RETURNING cs.collection_id
+ ),
+ updated_collections AS (
+ UPDATE collections c
+ SET updation_time = $3
+ FROM target
+ WHERE c.collection_id = target.collection_id
+ AND EXISTS (
+ SELECT 1
+ FROM deleted_shares ds
+ WHERE ds.collection_id = c.collection_id
+ )
+ RETURNING c.collection_id, c.owner_id, c.updation_time
+ )
+ SELECT collection_id
+ FROM updated_collections
+ ORDER BY collection_id
+ `, collectionID, userID, updationTime)
+ if err != nil {
+ return false, stacktrace.Propagate(err, "")
+ }
+
+ repaired := false
+ for rows.Next() {
+ var repairedCollectionID int64
+ if err := rows.Scan(&repairedCollectionID); err != nil {
+ return false, stacktrace.Propagate(err, "")
+ }
+ repaired = true
+ }
+ if err := rows.Err(); err != nil {
+ return false, stacktrace.Propagate(err, "")
+ }
+ if err := rows.Close(); err != nil {
+ return false, stacktrace.Propagate(err, "")
+ }
+ if err := tx.Commit(); err != nil {
+ return false, stacktrace.Propagate(err, "")
+ }
+ return repaired, nil
+}
+
// AddFiles adds files to a collection
func (repo *CollectionRepository) AddFiles(
collectionID int64,