π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
wall: reject duplicate follow requests
Neeraj Guptab553ab384819
diff --git a/server/wall/controller/follow.go b/server/wall/controller/follow.goindex ce4ce3316f..cf8d834872 100644--- a/server/wall/controller/follow.go+++ b/server/wall/controller/follow.go@@ -65,6 +65,9 @@ func (c *FollowController) Request(ctx *gin.Context, req models.FollowRequestPay}created, err := c.FollowRepo.CreateRequest(ctx.Request.Context(), userID, wall.WallID)if err != nil {+ if errors.Is(stacktrace.RootCause(err), repo.ErrAlreadyFollowing) {+ return nil, ente.NewBadRequestWithMessage("already following this wall")+ }return nil, err}return &models.FollowRequestCreatedResponse{RequestID: created.RequestID, Status: created.Status}, nildiff --git a/server/wall/repo/follow.go b/server/wall/repo/follow.goindex 578cbec780..edf4d72f43 100644--- a/server/wall/repo/follow.go+++ b/server/wall/repo/follow.go@@ -3,6 +3,7 @@ package repoimport ("context""database/sql"+ "errors""fmt""strconv""strings"@@ -10,12 +11,31 @@ import ("github.com/ente-io/stacktrace")+var ErrAlreadyFollowing = errors.New("wall follower already has access")+func (r *FollowRepository) CreateRequest(ctx context.Context, requesterID int64, targetWallID string) (*WallFollowRequestRecord, error) {tx, err := r.DB.BeginTx(ctx, nil)if err != nil {return nil, stacktrace.Propagate(err, "")}defer tx.Rollback()+ var wallExists int+ if err := tx.QueryRowContext(ctx, `SELECT 1 FROM walls WHERE wall_id = $1 FOR UPDATE`, targetWallID).Scan(&wallExists); err != nil {+ return nil, stacktrace.Propagate(err, "")+ }+ var hasShare bool+ if err := tx.QueryRowContext(ctx, `+ SELECT EXISTS (+ SELECT 1+ FROM wall_follow_shares+ WHERE follower_id = $1 AND wall_id = $2+ )+ `, requesterID, targetWallID).Scan(&hasShare); err != nil {+ return nil, stacktrace.Propagate(err, "")+ }+ if hasShare {+ return nil, ErrAlreadyFollowing+ }if _, err := tx.ExecContext(ctx, `UPDATE wall_follow_requestsSET status = 'pending'diff --git a/server/wall/repo/module_test.go b/server/wall/repo/module_test.goindex 28b4b9485b..49893801c4 100644--- a/server/wall/repo/module_test.go+++ b/server/wall/repo/module_test.go@@ -335,6 +335,30 @@ func TestApproveRequestCreatesShareAndMarksRequestApproved(t *testing.T) {require.Equal(t, "approved", stored.Status)}+func TestCreateRequestRejectsAlreadyFollowing(t *testing.T) {+ ctx := context.Background()+ module := newWallTestModule(t)++ aliceID := insertWallUser(t, module, "[email protected]", "alice-public")+ bobID := insertWallUser(t, module, "[email protected]", "bob-public")+ aliceWall, err := module.Walls.CreateWall(ctx, aliceID, "alice", "alice-wall-key", "alice-profile")+ require.NoError(t, err)+ _, err = module.Walls.CreateWall(ctx, bobID, "bob", "bob-wall-key", "bob-profile")+ require.NoError(t, err)+ request, err := module.Follow.CreateRequest(ctx, bobID, aliceWall.WallID)+ require.NoError(t, err)+ _, err = module.Follow.ApproveRequest(ctx, request.RequestID, aliceWall.WallID, "share-key", aliceWall.CurrentVersion)+ require.NoError(t, err)++ duplicate, err := module.Follow.CreateRequest(ctx, bobID, aliceWall.WallID)++ require.Nil(t, duplicate)+ require.ErrorIs(t, err, ErrAlreadyFollowing)+ incoming, err := module.Follow.ListIncomingRequests(ctx, aliceID)+ require.NoError(t, err)+ require.Empty(t, incoming)+}+func TestApproveRequestRejectsStaleRequestStates(t *testing.T) {for _, status := range []string{"cancelled", "rejected", "unfollowed", "approved"} {t.Run(status, func(t *testing.T) {