diff options
Diffstat (limited to 'internal/db/bundb')
| -rw-r--r-- | internal/db/bundb/relationship_follow.go | 23 | ||||
| -rw-r--r-- | internal/db/bundb/relationship_follow_req.go | 40 | ||||
| -rw-r--r-- | internal/db/bundb/relationship_test.go | 15 | 
3 files changed, 60 insertions, 18 deletions
| diff --git a/internal/db/bundb/relationship_follow.go b/internal/db/bundb/relationship_follow.go index 39b85075c..88850e72a 100644 --- a/internal/db/bundb/relationship_follow.go +++ b/internal/db/bundb/relationship_follow.go @@ -232,6 +232,29 @@ func (r *relationshipDB) deleteFollow(ctx context.Context, id string) error {  	return nil  } +func (r *relationshipDB) DeleteFollow(ctx context.Context, sourceAccountID string, targetAccountID string) error { +	defer r.state.Caches.GTS.Follow().Invalidate("AccountID.TargetAccountID", sourceAccountID, targetAccountID) + +	// Load follow into cache before attempting a delete, +	// as we need it cached in order to trigger the invalidate +	// callback. This in turn invalidates others. +	follow, err := r.GetFollow( +		gtscontext.SetBarebones(ctx), +		sourceAccountID, +		targetAccountID, +	) +	if err != nil { +		if errors.Is(err, db.ErrNoEntries) { +			// Already gone. +			return nil +		} +		return err +	} + +	// Finally delete follow from DB. +	return r.deleteFollow(ctx, follow.ID) +} +  func (r *relationshipDB) DeleteFollowByID(ctx context.Context, id string) error {  	defer r.state.Caches.GTS.Follow().Invalidate("ID", id) diff --git a/internal/db/bundb/relationship_follow_req.go b/internal/db/bundb/relationship_follow_req.go index c2ac3d2f1..f233bdd58 100644 --- a/internal/db/bundb/relationship_follow_req.go +++ b/internal/db/bundb/relationship_follow_req.go @@ -231,36 +231,42 @@ func (r *relationshipDB) AcceptFollowRequest(ctx context.Context, sourceAccountI  }  func (r *relationshipDB) RejectFollowRequest(ctx context.Context, sourceAccountID string, targetAccountID string) db.Error { +	// Delete follow request first. +	if err := r.DeleteFollowRequest(ctx, sourceAccountID, targetAccountID); err != nil { +		return err +	} + +	// Delete follow request notification +	return r.state.DB.DeleteNotifications(ctx, []string{ +		string(gtsmodel.NotificationFollowRequest), +	}, targetAccountID, sourceAccountID) +} + +func (r *relationshipDB) DeleteFollowRequest(ctx context.Context, sourceAccountID string, targetAccountID string) error {  	defer r.state.Caches.GTS.FollowRequest().Invalidate("AccountID.TargetAccountID", sourceAccountID, targetAccountID)  	// Load followreq into cache before attempting a delete,  	// as we need it cached in order to trigger the invalidate  	// callback. This in turn invalidates others. -	_, err := r.GetFollowRequest(gtscontext.SetBarebones(ctx), +	follow, err := r.GetFollowRequest( +		gtscontext.SetBarebones(ctx),  		sourceAccountID,  		targetAccountID,  	)  	if err != nil { +		if errors.Is(err, db.ErrNoEntries) { +			// Already gone. +			return nil +		}  		return err  	} -	// Attempt to delete follow request. -	if _, err = r.conn.NewDelete(). +	// Finally delete followreq from DB. +	_, err = r.conn.NewDelete().  		Table("follow_requests"). -		Where("? = ? AND ? = ?", -			bun.Ident("account_id"), -			sourceAccountID, -			bun.Ident("target_account_id"), -			targetAccountID, -		). -		Exec(ctx); err != nil { -		return r.conn.ProcessError(err) -	} - -	// Delete original follow request notification -	return r.state.DB.DeleteNotifications(ctx, []string{ -		string(gtsmodel.NotificationFollowRequest), -	}, targetAccountID, sourceAccountID) +		Where("? = ?", bun.Ident("id"), follow.ID). +		Exec(ctx) +	return r.conn.ProcessError(err)  }  func (r *relationshipDB) DeleteFollowRequestByID(ctx context.Context, id string) error { diff --git a/internal/db/bundb/relationship_test.go b/internal/db/bundb/relationship_test.go index 63fdb9632..d3f4a31d1 100644 --- a/internal/db/bundb/relationship_test.go +++ b/internal/db/bundb/relationship_test.go @@ -734,7 +734,7 @@ func (suite *RelationshipTestSuite) TestRejectFollowRequestNotExisting() {  	targetAccount := suite.testAccounts["local_account_2"]  	err := suite.db.RejectFollowRequest(ctx, account.ID, targetAccount.ID) -	suite.ErrorIs(err, db.ErrNoEntries) +	suite.NoError(err)  }  func (suite *RelationshipTestSuite) TestGetAccountFollowRequests() { @@ -836,6 +836,19 @@ func (suite *RelationshipTestSuite) TestGetFollowNotExisting() {  	suite.Nil(follow)  } +func (suite *RelationshipTestSuite) TestDeleteFollow() { +	ctx := context.Background() +	originAccount := suite.testAccounts["local_account_1"] +	targetAccount := suite.testAccounts["admin_account"] + +	err := suite.db.DeleteFollow(ctx, originAccount.ID, targetAccount.ID) +	suite.NoError(err) + +	follow, err := suite.db.GetFollow(ctx, originAccount.ID, targetAccount.ID) +	suite.EqualError(err, db.ErrNoEntries.Error()) +	suite.Nil(follow) +} +  func (suite *RelationshipTestSuite) TestUnfollowRequestExisting() {  	ctx := context.Background()  	originAccount := suite.testAccounts["admin_account"] | 
