diff options
Diffstat (limited to 'internal/processing/account')
-rw-r--r-- | internal/processing/account/account.go | 6 | ||||
-rw-r--r-- | internal/processing/account/account_test.go | 4 | ||||
-rw-r--r-- | internal/processing/account/block.go | 50 | ||||
-rw-r--r-- | internal/processing/account/delete.go | 8 | ||||
-rw-r--r-- | internal/processing/account/follow.go | 63 | ||||
-rw-r--r-- | internal/processing/account/follow_request.go | 119 | ||||
-rw-r--r-- | internal/processing/account/relationships.go | 166 |
7 files changed, 284 insertions, 132 deletions
diff --git a/internal/processing/account/account.go b/internal/processing/account/account.go index 7bef8b0c5..a32a73ac1 100644 --- a/internal/processing/account/account.go +++ b/internal/processing/account/account.go @@ -22,6 +22,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/media" "github.com/superseriousbusiness/gotosocial/internal/oauth" + "github.com/superseriousbusiness/gotosocial/internal/processing/common" "github.com/superseriousbusiness/gotosocial/internal/state" "github.com/superseriousbusiness/gotosocial/internal/text" "github.com/superseriousbusiness/gotosocial/internal/typeutils" @@ -32,6 +33,9 @@ import ( // // It also contains logic for actions towards accounts such as following, blocking, seeing follows, etc. type Processor struct { + // common processor logic + c *common.Processor + state *state.State tc typeutils.TypeConverter mediaManager *media.Manager @@ -44,6 +48,7 @@ type Processor struct { // New returns a new account processor. func New( + common *common.Processor, state *state.State, tc typeutils.TypeConverter, mediaManager *media.Manager, @@ -53,6 +58,7 @@ func New( parseMention gtsmodel.ParseMentionFunc, ) Processor { return Processor{ + c: common, state: state, tc: tc, mediaManager: mediaManager, diff --git a/internal/processing/account/account_test.go b/internal/processing/account/account_test.go index 4ba7de16e..2e4a64844 100644 --- a/internal/processing/account/account_test.go +++ b/internal/processing/account/account_test.go @@ -30,6 +30,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/oauth" "github.com/superseriousbusiness/gotosocial/internal/processing" "github.com/superseriousbusiness/gotosocial/internal/processing/account" + "github.com/superseriousbusiness/gotosocial/internal/processing/common" "github.com/superseriousbusiness/gotosocial/internal/state" "github.com/superseriousbusiness/gotosocial/internal/storage" "github.com/superseriousbusiness/gotosocial/internal/transport" @@ -113,7 +114,8 @@ func (suite *AccountStandardTestSuite) SetupTest() { suite.emailSender = testrig.NewEmailSender("../../../web/template/", suite.sentEmails) filter := visibility.NewFilter(&suite.state) - suite.accountProcessor = account.New(&suite.state, suite.tc, suite.mediaManager, suite.oauthServer, suite.federator, filter, processing.GetParseMentionFunc(suite.db, suite.federator)) + common := common.New(&suite.state, suite.tc, suite.federator, filter) + suite.accountProcessor = account.New(&common, &suite.state, suite.tc, suite.mediaManager, suite.oauthServer, suite.federator, filter, processing.GetParseMentionFunc(suite.db, suite.federator)) testrig.StandardDBSetup(suite.db, nil) testrig.StandardStorageSetup(suite.storage, "../../../testrig/media") } diff --git a/internal/processing/account/block.go b/internal/processing/account/block.go index 1ec31a753..270048100 100644 --- a/internal/processing/account/block.go +++ b/internal/processing/account/block.go @@ -28,8 +28,11 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/id" + "github.com/superseriousbusiness/gotosocial/internal/log" "github.com/superseriousbusiness/gotosocial/internal/messages" + "github.com/superseriousbusiness/gotosocial/internal/paging" "github.com/superseriousbusiness/gotosocial/internal/uris" + "github.com/superseriousbusiness/gotosocial/internal/util" ) // BlockCreate handles the creation of a block from requestingAccount to targetAccountID, either remote or local. @@ -128,6 +131,53 @@ func (p *Processor) BlockRemove(ctx context.Context, requestingAccount *gtsmodel return p.RelationshipGet(ctx, requestingAccount, targetAccountID) } +// BlocksGet ... +func (p *Processor) BlocksGet( + ctx context.Context, + requestingAccount *gtsmodel.Account, + page *paging.Page, +) (*apimodel.PageableResponse, gtserror.WithCode) { + blocks, err := p.state.DB.GetAccountBlocks(ctx, + requestingAccount.ID, + page, + ) + if err != nil && !errors.Is(err, db.ErrNoEntries) { + return nil, gtserror.NewErrorInternalError(err) + } + + // Check for empty response. + count := len(blocks) + if len(blocks) == 0 { + return util.EmptyPageableResponse(), nil + } + + items := make([]interface{}, 0, count) + + for _, block := range blocks { + // Convert target account to frontend API model. (target will never be nil) + account, err := p.tc.AccountToAPIAccountBlocked(ctx, block.TargetAccount) + if err != nil { + log.Errorf(ctx, "error converting account to public api account: %v", err) + continue + } + + // Append target to return items. + items = append(items, account) + } + + // Get the lowest and highest + // ID values, used for paging. + lo := blocks[count-1].ID + hi := blocks[0].ID + + return paging.PackageResponse(paging.ResponseParams{ + Items: items, + Path: "/api/v1/blocks", + Next: page.Next(lo, hi), + Prev: page.Prev(lo, hi), + }), nil +} + func (p *Processor) getBlockTarget(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) (*gtsmodel.Account, *gtsmodel.Block, gtserror.WithCode) { // Account should not block or unblock itself. if requestingAccount.ID == targetAccountID { diff --git a/internal/processing/account/delete.go b/internal/processing/account/delete.go index da13eb20e..e89ebf13f 100644 --- a/internal/processing/account/delete.go +++ b/internal/processing/account/delete.go @@ -160,7 +160,7 @@ func (p *Processor) deleteUserAndTokensForAccount(ctx context.Context, account * // - Follow requests created by account. func (p *Processor) deleteAccountFollows(ctx context.Context, account *gtsmodel.Account) error { // Delete follows targeting this account. - followedBy, err := p.state.DB.GetAccountFollowers(ctx, account.ID) + followedBy, err := p.state.DB.GetAccountFollowers(ctx, account.ID, nil) if err != nil && !errors.Is(err, db.ErrNoEntries) { return gtserror.Newf("db error getting follows targeting account %s: %w", account.ID, err) } @@ -172,7 +172,7 @@ func (p *Processor) deleteAccountFollows(ctx context.Context, account *gtsmodel. } // Delete follow requests targeting this account. - followRequestedBy, err := p.state.DB.GetAccountFollowRequests(ctx, account.ID) + followRequestedBy, err := p.state.DB.GetAccountFollowRequests(ctx, account.ID, nil) if err != nil && !errors.Is(err, db.ErrNoEntries) { return gtserror.Newf("db error getting follow requests targeting account %s: %w", account.ID, err) } @@ -193,7 +193,7 @@ func (p *Processor) deleteAccountFollows(ctx context.Context, account *gtsmodel. ) // Delete follows originating from this account. - following, err := p.state.DB.GetAccountFollows(ctx, account.ID) + following, err := p.state.DB.GetAccountFollows(ctx, account.ID, nil) if err != nil && !errors.Is(err, db.ErrNoEntries) { return gtserror.Newf("db error getting follows owned by account %s: %w", account.ID, err) } @@ -211,7 +211,7 @@ func (p *Processor) deleteAccountFollows(ctx context.Context, account *gtsmodel. } // Delete follow requests originating from this account. - followRequesting, err := p.state.DB.GetAccountFollowRequesting(ctx, account.ID) + followRequesting, err := p.state.DB.GetAccountFollowRequesting(ctx, account.ID, nil) if err != nil && !errors.Is(err, db.ErrNoEntries) { return gtserror.Newf("db error getting follow requests owned by account %s: %w", account.ID, err) } diff --git a/internal/processing/account/follow.go b/internal/processing/account/follow.go index 1aed92e75..8006f8d79 100644 --- a/internal/processing/account/follow.go +++ b/internal/processing/account/follow.go @@ -20,7 +20,6 @@ package account import ( "context" "errors" - "fmt" "github.com/superseriousbusiness/gotosocial/internal/ap" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" @@ -35,7 +34,7 @@ import ( // FollowCreate handles a follow request to an account, either remote or local. func (p *Processor) FollowCreate(ctx context.Context, requestingAccount *gtsmodel.Account, form *apimodel.AccountFollowRequest) (*apimodel.Relationship, gtserror.WithCode) { - targetAccount, errWithCode := p.getFollowTarget(ctx, requestingAccount.ID, form.ID) + targetAccount, errWithCode := p.getFollowTarget(ctx, requestingAccount, form.ID) if errWithCode != nil { return nil, errWithCode } @@ -46,7 +45,7 @@ func (p *Processor) FollowCreate(ctx context.Context, requestingAccount *gtsmode requestingAccount.ID, targetAccount.ID, ); err != nil && !errors.Is(err, db.ErrNoEntries) { - err = fmt.Errorf("FollowCreate: db error checking existing follow: %w", err) + err = gtserror.Newf("db error checking existing follow: %w", err) return nil, gtserror.NewErrorInternalError(err) } else if follow != nil { // Already follows, update if necessary + return relationship. @@ -66,7 +65,7 @@ func (p *Processor) FollowCreate(ctx context.Context, requestingAccount *gtsmode requestingAccount.ID, targetAccount.ID, ); err != nil && !errors.Is(err, db.ErrNoEntries) { - err = fmt.Errorf("FollowCreate: db error checking existing follow request: %w", err) + err = gtserror.Newf("db error checking existing follow request: %w", err) return nil, gtserror.NewErrorInternalError(err) } else if followRequest != nil { // Already requested, update if necessary + return relationship. @@ -100,7 +99,7 @@ func (p *Processor) FollowCreate(ctx context.Context, requestingAccount *gtsmode } if err := p.state.DB.PutFollowRequest(ctx, fr); err != nil { - err = fmt.Errorf("FollowCreate: error creating follow request in db: %s", err) + err = gtserror.Newf("error creating follow request in db: %s", err) return nil, gtserror.NewErrorInternalError(err) } @@ -112,7 +111,7 @@ func (p *Processor) FollowCreate(ctx context.Context, requestingAccount *gtsmode // Because we know the requestingAccount is also // local, we don't need to federate the accept out. if _, err := p.state.DB.AcceptFollowRequest(ctx, requestingAccount.ID, form.ID); err != nil { - err = fmt.Errorf("FollowCreate: error accepting follow request for local unlocked account: %w", err) + err = gtserror.Newf("error accepting follow request for local unlocked account: %w", err) return nil, gtserror.NewErrorInternalError(err) } } else if targetAccount.IsRemote() { @@ -132,7 +131,7 @@ func (p *Processor) FollowCreate(ctx context.Context, requestingAccount *gtsmode // FollowRemove handles the removal of a follow/follow request to an account, either remote or local. func (p *Processor) FollowRemove(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Relationship, gtserror.WithCode) { - targetAccount, errWithCode := p.getFollowTarget(ctx, requestingAccount.ID, targetAccountID) + targetAccount, errWithCode := p.getFollowTarget(ctx, requestingAccount, targetAccountID) if errWithCode != nil { return nil, errWithCode } @@ -140,7 +139,7 @@ func (p *Processor) FollowRemove(ctx context.Context, requestingAccount *gtsmode // Unfollow and deal with side effects. msgs, err := p.unfollow(ctx, requestingAccount, targetAccount) if err != nil { - return nil, gtserror.NewErrorNotFound(fmt.Errorf("FollowRemove: account %s not found in the db: %s", targetAccountID, err)) + return nil, gtserror.NewErrorNotFound(gtserror.Newf("account %s not found in the db: %s", targetAccountID, err)) } // Batch queue accreted client api messages. @@ -166,7 +165,6 @@ func (p *Processor) updateFollow( currentNotify *bool, update func(...string) error, ) (*apimodel.Relationship, gtserror.WithCode) { - if form.Reblogs == nil && form.Notify == nil { // There's nothing to update. return p.RelationshipGet(ctx, requestingAccount, form.ID) @@ -192,7 +190,7 @@ func (p *Processor) updateFollow( } if err := update(columns...); err != nil { - err = fmt.Errorf("updateFollow: error updating existing follow (request): %w", err) + err = gtserror.Newf("error updating existing follow (request): %w", err) return nil, gtserror.NewErrorInternalError(err) } @@ -201,38 +199,23 @@ func (p *Processor) updateFollow( // getFollowTarget is a convenience function which: // - Checks if account is trying to follow/unfollow itself. -// - Returns not found if there's a block in place between accounts. +// - Returns not found if target should not be visible to requester. // - Returns target account according to its id. -func (p *Processor) getFollowTarget(ctx context.Context, requestingAccountID string, targetAccountID string) (*gtsmodel.Account, gtserror.WithCode) { +func (p *Processor) getFollowTarget(ctx context.Context, requester *gtsmodel.Account, targetID string) (*gtsmodel.Account, gtserror.WithCode) { + // Check for requester. + if requester == nil { + err := errors.New("no authorized user") + return nil, gtserror.NewErrorUnauthorized(err) + } + // Account can't follow or unfollow itself. - if requestingAccountID == targetAccountID { + if requester.ID == targetID { err := errors.New("account can't follow or unfollow itself") return nil, gtserror.NewErrorNotAcceptable(err) } - // Do nothing if a block exists in either direction between accounts. - if blocked, err := p.state.DB.IsEitherBlocked(ctx, requestingAccountID, targetAccountID); err != nil { - err = fmt.Errorf("db error checking block between accounts: %w", err) - return nil, gtserror.NewErrorInternalError(err) - } else if blocked { - err = errors.New("block exists between accounts") - return nil, gtserror.NewErrorNotFound(err) - } - - // Ensure target account retrievable. - targetAccount, err := p.state.DB.GetAccountByID(ctx, targetAccountID) - if err != nil { - if !errors.Is(err, db.ErrNoEntries) { - // Real db error. - err = fmt.Errorf("db error looking for target account %s: %w", targetAccountID, err) - return nil, gtserror.NewErrorInternalError(err) - } - // Account not found. - err = fmt.Errorf("target account %s not found in the db", targetAccountID) - return nil, gtserror.NewErrorNotFound(err, err.Error()) - } - - return targetAccount, nil + // Fetch the target account for requesting user account. + return p.c.GetVisibleTargetAccount(ctx, requester, targetID) } // unfollow is a convenience function for having requesting account @@ -248,7 +231,7 @@ func (p *Processor) unfollow(ctx context.Context, requestingAccount *gtsmodel.Ac // Get follow from requesting account to target account. follow, err := p.state.DB.GetFollow(ctx, requestingAccount.ID, targetAccount.ID) if err != nil && !errors.Is(err, db.ErrNoEntries) { - err = fmt.Errorf("unfollow: error getting follow from %s targeting %s: %w", requestingAccount.ID, targetAccount.ID, err) + err = gtserror.Newf("error getting follow from %s targeting %s: %w", requestingAccount.ID, targetAccount.ID, err) return nil, err } @@ -257,7 +240,7 @@ func (p *Processor) unfollow(ctx context.Context, requestingAccount *gtsmodel.Ac err = p.state.DB.DeleteFollowByID(ctx, follow.ID) if err != nil { if !errors.Is(err, db.ErrNoEntries) { - err = fmt.Errorf("unfollow: error deleting request from %s targeting %s: %w", requestingAccount.ID, targetAccount.ID, err) + err = gtserror.Newf("error deleting request from %s targeting %s: %w", requestingAccount.ID, targetAccount.ID, err) return nil, err } @@ -284,7 +267,7 @@ func (p *Processor) unfollow(ctx context.Context, requestingAccount *gtsmodel.Ac // Get follow request from requesting account to target account. followReq, err := p.state.DB.GetFollowRequest(ctx, requestingAccount.ID, targetAccount.ID) if err != nil && !errors.Is(err, db.ErrNoEntries) { - err = fmt.Errorf("unfollow: error getting follow request from %s targeting %s: %w", requestingAccount.ID, targetAccount.ID, err) + err = gtserror.Newf("error getting follow request from %s targeting %s: %w", requestingAccount.ID, targetAccount.ID, err) return nil, err } @@ -293,7 +276,7 @@ func (p *Processor) unfollow(ctx context.Context, requestingAccount *gtsmodel.Ac err = p.state.DB.DeleteFollowRequestByID(ctx, followReq.ID) if err != nil { if !errors.Is(err, db.ErrNoEntries) { - err = fmt.Errorf("unfollow: error deleting follow request from %s targeting %s: %w", requestingAccount.ID, targetAccount.ID, err) + err = gtserror.Newf("error deleting follow request from %s targeting %s: %w", requestingAccount.ID, targetAccount.ID, err) return nil, err } diff --git a/internal/processing/account/follow_request.go b/internal/processing/account/follow_request.go new file mode 100644 index 000000000..c054637c8 --- /dev/null +++ b/internal/processing/account/follow_request.go @@ -0,0 +1,119 @@ +// GoToSocial +// Copyright (C) GoToSocial Authors admin@gotosocial.org +// SPDX-License-Identifier: AGPL-3.0-or-later +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. + +package account + +import ( + "context" + "errors" + + "github.com/superseriousbusiness/gotosocial/internal/ap" + apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" + "github.com/superseriousbusiness/gotosocial/internal/db" + "github.com/superseriousbusiness/gotosocial/internal/gtserror" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/messages" + "github.com/superseriousbusiness/gotosocial/internal/paging" +) + +// FollowRequestAccept handles the accepting of a follow request from the sourceAccountID to the requestingAccount (the currently authorized account). +func (p *Processor) FollowRequestAccept(ctx context.Context, requestingAccount *gtsmodel.Account, sourceAccountID string) (*apimodel.Relationship, gtserror.WithCode) { + follow, err := p.state.DB.AcceptFollowRequest(ctx, sourceAccountID, requestingAccount.ID) + if err != nil { + return nil, gtserror.NewErrorNotFound(err) + } + + if follow.Account != nil { + // Only enqueue work in the case we have a request creating account stored. + // NOTE: due to how AcceptFollowRequest works, the inverse shouldn't be possible. + p.state.Workers.EnqueueClientAPI(ctx, messages.FromClientAPI{ + APObjectType: ap.ActivityFollow, + APActivityType: ap.ActivityAccept, + GTSModel: follow, + OriginAccount: follow.Account, + TargetAccount: follow.TargetAccount, + }) + } + + return p.RelationshipGet(ctx, requestingAccount, sourceAccountID) +} + +// FollowRequestReject handles the rejection of a follow request from the sourceAccountID to the requestingAccount (the currently authorized account). +func (p *Processor) FollowRequestReject(ctx context.Context, requestingAccount *gtsmodel.Account, sourceAccountID string) (*apimodel.Relationship, gtserror.WithCode) { + followRequest, err := p.state.DB.GetFollowRequest(ctx, sourceAccountID, requestingAccount.ID) + if err != nil { + return nil, gtserror.NewErrorNotFound(err) + } + + err = p.state.DB.RejectFollowRequest(ctx, sourceAccountID, requestingAccount.ID) + if err != nil { + return nil, gtserror.NewErrorNotFound(err) + } + + if followRequest.Account != nil { + // Only enqueue work in the case we have a request creating account stored. + // NOTE: due to how GetFollowRequest works, the inverse shouldn't be possible. + p.state.Workers.EnqueueClientAPI(ctx, messages.FromClientAPI{ + APObjectType: ap.ActivityFollow, + APActivityType: ap.ActivityReject, + GTSModel: followRequest, + OriginAccount: followRequest.Account, + TargetAccount: followRequest.TargetAccount, + }) + } + + return p.RelationshipGet(ctx, requestingAccount, sourceAccountID) +} + +// FollowRequestsGet fetches a list of the accounts that are follow requesting the given requestingAccount (the currently authorized account). +func (p *Processor) FollowRequestsGet(ctx context.Context, requestingAccount *gtsmodel.Account, page *paging.Page) (*apimodel.PageableResponse, gtserror.WithCode) { + // Fetch follow requests targeting the given requesting account model. + followRequests, err := p.state.DB.GetAccountFollowRequests(ctx, requestingAccount.ID, page) + if err != nil && !errors.Is(err, db.ErrNoEntries) { + return nil, gtserror.NewErrorInternalError(err) + } + + // Check for empty response. + count := len(followRequests) + if count == 0 { + return paging.EmptyResponse(), nil + } + + // Get the lowest and highest + // ID values, used for paging. + lo := followRequests[count-1].ID + hi := followRequests[0].ID + + // Func to fetch follow source at index. + getIdx := func(i int) *gtsmodel.Account { + return followRequests[i].Account + } + + // Get a filtered slice of public API account models. + items := p.c.GetVisibleAPIAccountsPaged(ctx, + requestingAccount, + getIdx, + count, + ) + + return paging.PackageResponse(paging.ResponseParams{ + Items: items, + Path: "/api/v1/follow_requests", + Next: page.Next(lo, hi), + Prev: page.Prev(lo, hi), + }), nil +} diff --git a/internal/processing/account/relationships.go b/internal/processing/account/relationships.go index d12d989ef..58c98f3ba 100644 --- a/internal/processing/account/relationships.go +++ b/internal/processing/account/relationships.go @@ -20,128 +20,120 @@ package account import ( "context" "errors" - "fmt" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" - "github.com/superseriousbusiness/gotosocial/internal/log" + "github.com/superseriousbusiness/gotosocial/internal/paging" ) // FollowersGet fetches a list of the target account's followers. -func (p *Processor) FollowersGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) ([]apimodel.Account, gtserror.WithCode) { - if blocked, err := p.state.DB.IsEitherBlocked(ctx, requestingAccount.ID, targetAccountID); err != nil { - err = fmt.Errorf("FollowersGet: db error checking block: %w", err) +func (p *Processor) FollowersGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string, page *paging.Page) (*apimodel.PageableResponse, gtserror.WithCode) { + // Fetch target account to check it exists, and visibility of requester->target. + _, errWithCode := p.c.GetVisibleTargetAccount(ctx, requestingAccount, targetAccountID) + if errWithCode != nil { + return nil, errWithCode + } + + follows, err := p.state.DB.GetAccountFollowers(ctx, targetAccountID, page) + if err != nil && !errors.Is(err, db.ErrNoEntries) { + err = gtserror.Newf("db error getting followers: %w", err) return nil, gtserror.NewErrorInternalError(err) - } else if blocked { - err = errors.New("FollowersGet: block exists between accounts") - return nil, gtserror.NewErrorNotFound(err) } - follows, err := p.state.DB.GetAccountFollowers(ctx, targetAccountID) - if err != nil { - if !errors.Is(err, db.ErrNoEntries) { - err = fmt.Errorf("FollowersGet: db error getting followers: %w", err) - return nil, gtserror.NewErrorInternalError(err) - } - return []apimodel.Account{}, nil + // Check for empty response. + count := len(follows) + if count == 0 { + return paging.EmptyResponse(), nil } - return p.accountsFromFollows(ctx, follows, requestingAccount.ID) + // Get the lowest and highest + // ID values, used for paging. + lo := follows[count-1].ID + hi := follows[0].ID + + // Func to fetch follow source at index. + getIdx := func(i int) *gtsmodel.Account { + return follows[i].Account + } + + // Get a filtered slice of public API account models. + items := p.c.GetVisibleAPIAccountsPaged(ctx, + requestingAccount, + getIdx, + len(follows), + ) + + return paging.PackageResponse(paging.ResponseParams{ + Items: items, + Path: "/api/v1/accounts/" + targetAccountID + "/followers", + Next: page.Next(lo, hi), + Prev: page.Prev(lo, hi), + }), nil } // FollowingGet fetches a list of the accounts that target account is following. -func (p *Processor) FollowingGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) ([]apimodel.Account, gtserror.WithCode) { - if blocked, err := p.state.DB.IsEitherBlocked(ctx, requestingAccount.ID, targetAccountID); err != nil { - err = fmt.Errorf("FollowingGet: db error checking block: %w", err) +func (p *Processor) FollowingGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string, page *paging.Page) (*apimodel.PageableResponse, gtserror.WithCode) { + // Fetch target account to check it exists, and visibility of requester->target. + _, errWithCode := p.c.GetVisibleTargetAccount(ctx, requestingAccount, targetAccountID) + if errWithCode != nil { + return nil, errWithCode + } + + // Fetch known accounts that follow given target account ID. + follows, err := p.state.DB.GetAccountFollows(ctx, targetAccountID, page) + if err != nil && !errors.Is(err, db.ErrNoEntries) { + err = gtserror.Newf("db error getting followers: %w", err) return nil, gtserror.NewErrorInternalError(err) - } else if blocked { - err = errors.New("FollowingGet: block exists between accounts") - return nil, gtserror.NewErrorNotFound(err) } - follows, err := p.state.DB.GetAccountFollows(ctx, targetAccountID) - if err != nil { - if !errors.Is(err, db.ErrNoEntries) { - err = fmt.Errorf("FollowingGet: db error getting followers: %w", err) - return nil, gtserror.NewErrorInternalError(err) - } - return []apimodel.Account{}, nil + // Check for empty response. + count := len(follows) + if count == 0 { + return paging.EmptyResponse(), nil } - return p.targetAccountsFromFollows(ctx, follows, requestingAccount.ID) + // Get the lowest and highest + // ID values, used for paging. + lo := follows[count-1].ID + hi := follows[0].ID + + // Func to fetch follow source at index. + getIdx := func(i int) *gtsmodel.Account { + return follows[i].TargetAccount + } + + // Get a filtered slice of public API account models. + items := p.c.GetVisibleAPIAccountsPaged(ctx, + requestingAccount, + getIdx, + len(follows), + ) + + return paging.PackageResponse(paging.ResponseParams{ + Items: items, + Path: "/api/v1/accounts/" + targetAccountID + "/following", + Next: page.Next(lo, hi), + Prev: page.Prev(lo, hi), + }), nil } // RelationshipGet returns a relationship model describing the relationship of the targetAccount to the Authed account. func (p *Processor) RelationshipGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Relationship, gtserror.WithCode) { if requestingAccount == nil { - return nil, gtserror.NewErrorForbidden(errors.New("not authed")) + return nil, gtserror.NewErrorForbidden(gtserror.New("not authed")) } gtsR, err := p.state.DB.GetRelationship(ctx, requestingAccount.ID, targetAccountID) if err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("error getting relationship: %s", err)) + return nil, gtserror.NewErrorInternalError(gtserror.Newf("error getting relationship: %s", err)) } r, err := p.tc.RelationshipToAPIRelationship(ctx, gtsR) if err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting relationship: %s", err)) + return nil, gtserror.NewErrorInternalError(gtserror.Newf("error converting relationship: %s", err)) } return r, nil } - -func (p *Processor) accountsFromFollows(ctx context.Context, follows []*gtsmodel.Follow, requestingAccountID string) ([]apimodel.Account, gtserror.WithCode) { - accounts := make([]apimodel.Account, 0, len(follows)) - for _, follow := range follows { - if follow.Account == nil { - // No account set for some reason; just skip. - log.WithContext(ctx).WithField("follow", follow).Warn("follow had no associated account") - continue - } - - if blocked, err := p.state.DB.IsEitherBlocked(ctx, requestingAccountID, follow.AccountID); err != nil { - err = fmt.Errorf("accountsFromFollows: db error checking block: %w", err) - return nil, gtserror.NewErrorInternalError(err) - } else if blocked { - continue - } - - account, err := p.tc.AccountToAPIAccountPublic(ctx, follow.Account) - if err != nil { - err = fmt.Errorf("accountsFromFollows: error converting account to api account: %w", err) - return nil, gtserror.NewErrorInternalError(err) - } - - accounts = append(accounts, *account) - } - return accounts, nil -} - -func (p *Processor) targetAccountsFromFollows(ctx context.Context, follows []*gtsmodel.Follow, requestingAccountID string) ([]apimodel.Account, gtserror.WithCode) { - accounts := make([]apimodel.Account, 0, len(follows)) - for _, follow := range follows { - if follow.TargetAccount == nil { - // No account set for some reason; just skip. - log.WithContext(ctx).WithField("follow", follow).Warn("follow had no associated target account") - continue - } - - if blocked, err := p.state.DB.IsEitherBlocked(ctx, requestingAccountID, follow.TargetAccountID); err != nil { - err = fmt.Errorf("targetAccountsFromFollows: db error checking block: %w", err) - return nil, gtserror.NewErrorInternalError(err) - } else if blocked { - continue - } - - account, err := p.tc.AccountToAPIAccountPublic(ctx, follow.TargetAccount) - if err != nil { - err = fmt.Errorf("targetAccountsFromFollows: error converting account to api account: %w", err) - return nil, gtserror.NewErrorInternalError(err) - } - - accounts = append(accounts, *account) - } - return accounts, nil -} |