Skip to content

Commit

Permalink
[bugfix] Fix relationship not updating 'following' on accept follow r…
Browse files Browse the repository at this point in the history
…equest (#1658)
  • Loading branch information
tsmethurst committed Mar 31, 2023
1 parent fe4ea96 commit 344c7e5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
3 changes: 3 additions & 0 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ func (c *Caches) setuphooks() {
// Invalidate follow request target account ID cached visibility.
c.Visibility.Invalidate("ItemID", followReq.TargetAccountID)
c.Visibility.Invalidate("RequesterID", followReq.TargetAccountID)

// Invalidate any cached follow corresponding to this request.
c.GTS.Follow().Invalidate("AccountID.TargetAccountID", followReq.AccountID, followReq.TargetAccountID)
})

c.GTS.Status().SetInvalidateCallback(func(status *gtsmodel.Status) {
Expand Down
11 changes: 4 additions & 7 deletions internal/db/bundb/relationship_follow_req.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ func (r *relationshipDB) AcceptFollowRequest(ctx context.Context, sourceAccountI
return nil, r.conn.ProcessError(err)
}

// Invalidate follow request from cache lookups.
// Invalidate follow request from cache lookups; this will
// invalidate the follow as well via the invalidate hook.
r.state.Caches.GTS.FollowRequest().Invalidate("ID", followReq.ID)

// Delete original follow request notification
Expand All @@ -225,12 +226,8 @@ func (r *relationshipDB) RejectFollowRequest(ctx context.Context, sourceAccountI
}

// Delete original follow request.
if _, err := r.conn.
NewDelete().
Table("follow_requests").
Where("? = ?", bun.Ident("id"), followReq.ID).
Exec(ctx); err != nil {
return r.conn.ProcessError(err)
if err := r.DeleteFollowRequestByID(ctx, followReq.ID); err != nil {
return err
}

// Delete original follow request notification
Expand Down
28 changes: 26 additions & 2 deletions internal/db/bundb/relationship_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,25 +568,41 @@ func (suite *RelationshipTestSuite) TestAcceptFollowRequestOK() {
account := suite.testAccounts["admin_account"]
targetAccount := suite.testAccounts["local_account_2"]

// Fetch relationship before follow request.
relationship, err := suite.db.GetRelationship(ctx, account.ID, targetAccount.ID)
if err != nil {
suite.FailNow(err.Error())
}
suite.False(relationship.Following)
suite.False(relationship.Requested)

followRequest := &gtsmodel.FollowRequest{
ID: "01GEF753FWHCHRDWR0QEHBXM8W",
URI: "http://localhost:8080/weeeeeeeeeeeeeeeee",
AccountID: account.ID,
TargetAccountID: targetAccount.ID,
}

if err := suite.db.Put(ctx, followRequest); err != nil {
if err := suite.db.PutFollowRequest(ctx, followRequest); err != nil {
suite.FailNow(err.Error())
}

// Fetch relationship while follow requested.
relationship, err = suite.db.GetRelationship(ctx, account.ID, targetAccount.ID)
if err != nil {
suite.FailNow(err.Error())
}
suite.False(relationship.Following)
suite.True(relationship.Requested)

followRequestNotification := &gtsmodel.Notification{
ID: "01GV8MY1Q9KX2ZSWN4FAQ3V1PB",
OriginAccountID: account.ID,
TargetAccountID: targetAccount.ID,
NotificationType: gtsmodel.NotificationFollowRequest,
}

if err := suite.db.Put(ctx, followRequestNotification); err != nil {
if err := suite.db.PutNotification(ctx, followRequestNotification); err != nil {
suite.FailNow(err.Error())
}

Expand All @@ -599,6 +615,14 @@ func (suite *RelationshipTestSuite) TestAcceptFollowRequestOK() {
notification, err := suite.db.GetNotificationByID(ctx, followRequestNotification.ID)
suite.ErrorIs(err, db.ErrNoEntries)
suite.Nil(notification)

// Fetch relationship while followed.
relationship, err = suite.db.GetRelationship(ctx, account.ID, targetAccount.ID)
if err != nil {
suite.FailNow(err.Error())
}
suite.True(relationship.Following)
suite.False(relationship.Requested)
}

func (suite *RelationshipTestSuite) TestAcceptFollowRequestNoNotification() {
Expand Down

0 comments on commit 344c7e5

Please sign in to comment.