diff options
author | 2023-02-22 16:05:26 +0100 | |
---|---|---|
committer | 2023-02-22 16:05:26 +0100 | |
commit | b6fbdc66c1ce1ec61ebfb6fcc0351ea627a1d288 (patch) | |
tree | c79d1107375597ab8a79045c80dd62dc95a204e7 /internal/processing/account | |
parent | [bugfix] Remove initial storage cleanup (#1545) (diff) | |
download | gotosocial-b6fbdc66c1ce1ec61ebfb6fcc0351ea627a1d288.tar.xz |
[chore] Deinterface processor and subprocessors (#1501)
* [chore] Deinterface processor and subprocessors
* expose subprocessors via function calls
* missing license header
Diffstat (limited to 'internal/processing/account')
16 files changed, 236 insertions, 403 deletions
diff --git a/internal/processing/account/account.go b/internal/processing/account/account.go index 7263bacd2..41315d483 100644 --- a/internal/processing/account/account.go +++ b/internal/processing/account/account.go @@ -19,15 +19,9 @@ package account import ( - "context" - "mime/multipart" - "time" - - apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" "github.com/superseriousbusiness/gotosocial/internal/concurrency" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/federation" - "github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/media" "github.com/superseriousbusiness/gotosocial/internal/messages" @@ -35,63 +29,12 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/text" "github.com/superseriousbusiness/gotosocial/internal/typeutils" "github.com/superseriousbusiness/gotosocial/internal/visibility" - "github.com/superseriousbusiness/oauth2/v4" ) -// Processor wraps a bunch of functions for processing account actions. -type Processor interface { - // Create processes the given form for creating a new account, returning an oauth token for that account if successful. - Create(ctx context.Context, applicationToken oauth2.TokenInfo, application *gtsmodel.Application, form *apimodel.AccountCreateRequest) (*apimodel.Token, gtserror.WithCode) - // Delete deletes an account, and all of that account's statuses, media, follows, notifications, etc etc etc. - // The origin passed here should be either the ID of the account doing the delete (can be itself), or the ID of a domain block. - Delete(ctx context.Context, account *gtsmodel.Account, origin string) gtserror.WithCode - // DeleteLocal is like delete, but specifically for deletion of local accounts rather than federated ones. - // Unlike Delete, it will propagate the deletion out across the federating API to other instances. - DeleteLocal(ctx context.Context, account *gtsmodel.Account, form *apimodel.AccountDeleteRequest) gtserror.WithCode - // Get processes the given request for account information. - Get(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Account, gtserror.WithCode) - // GetLocalByUsername processes the given request for account information targeting a local account by username. - GetLocalByUsername(ctx context.Context, requestingAccount *gtsmodel.Account, username string) (*apimodel.Account, gtserror.WithCode) - // GetCustomCSSForUsername returns custom css for the given local username. - GetCustomCSSForUsername(ctx context.Context, username string) (string, gtserror.WithCode) - // GetRSSFeedForUsername returns RSS feed for the given local username. - GetRSSFeedForUsername(ctx context.Context, username string) (func() (string, gtserror.WithCode), time.Time, gtserror.WithCode) - // Update processes the update of an account with the given form - Update(ctx context.Context, account *gtsmodel.Account, form *apimodel.UpdateCredentialsRequest) (*apimodel.Account, gtserror.WithCode) - // StatusesGet fetches a number of statuses (in time descending order) from the given account, filtered by visibility for - // the account given in authed. - StatusesGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string, limit int, excludeReplies bool, excludeReblogs bool, maxID string, minID string, pinned bool, mediaOnly bool, publicOnly bool) (*apimodel.PageableResponse, gtserror.WithCode) - // WebStatusesGet fetches a number of statuses (in descending order) from the given account. It selects only - // statuses which are suitable for showing on the public web profile of an account. - WebStatusesGet(ctx context.Context, targetAccountID string, maxID string) (*apimodel.PageableResponse, gtserror.WithCode) - // StatusesGet fetches a number of statuses (in time descending order) from the given account, filtered by visibility for - // the account given in authed. - BookmarksGet(ctx context.Context, requestingAccount *gtsmodel.Account, limit int, maxID string, minID string) (*apimodel.PageableResponse, gtserror.WithCode) - // FollowersGet fetches a list of the target account's followers. - FollowersGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) ([]apimodel.Account, gtserror.WithCode) - // FollowingGet fetches a list of the accounts that target account is following. - FollowingGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) ([]apimodel.Account, gtserror.WithCode) - // RelationshipGet returns a relationship model describing the relationship of the targetAccount to the Authed account. - RelationshipGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Relationship, gtserror.WithCode) - // FollowCreate handles a follow request to an account, either remote or local. - FollowCreate(ctx context.Context, requestingAccount *gtsmodel.Account, form *apimodel.AccountFollowRequest) (*apimodel.Relationship, gtserror.WithCode) - // FollowRemove handles the removal of a follow/follow request to an account, either remote or local. - FollowRemove(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Relationship, gtserror.WithCode) - // BlockCreate handles the creation of a block from requestingAccount to targetAccountID, either remote or local. - BlockCreate(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Relationship, gtserror.WithCode) - // BlockRemove handles the removal of a block from requestingAccount to targetAccountID, either remote or local. - BlockRemove(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Relationship, gtserror.WithCode) - // UpdateAvatar does the dirty work of checking the avatar part of an account update form, - // parsing and checking the image, and doing the necessary updates in the database for this to become - // the account's new avatar image. - UpdateAvatar(ctx context.Context, avatar *multipart.FileHeader, description *string, accountID string) (*gtsmodel.MediaAttachment, error) - // UpdateHeader does the dirty work of checking the header part of an account update form, - // parsing and checking the image, and doing the necessary updates in the database for this to become - // the account's new header image. - UpdateHeader(ctx context.Context, header *multipart.FileHeader, description *string, accountID string) (*gtsmodel.MediaAttachment, error) -} - -type processor struct { +// Processor wraps functionality for updating, creating, and deleting accounts in response to API requests. +// +// It also contains logic for actions towards accounts such as following, blocking, seeing follows, etc. +type Processor struct { tc typeutils.TypeConverter mediaManager media.Manager clientWorker *concurrency.WorkerPool[messages.FromClientAPI] @@ -104,8 +47,16 @@ type processor struct { } // New returns a new account processor. -func New(db db.DB, tc typeutils.TypeConverter, mediaManager media.Manager, oauthServer oauth.Server, clientWorker *concurrency.WorkerPool[messages.FromClientAPI], federator federation.Federator, parseMention gtsmodel.ParseMentionFunc) Processor { - return &processor{ +func New( + db db.DB, + tc typeutils.TypeConverter, + mediaManager media.Manager, + oauthServer oauth.Server, + clientWorker *concurrency.WorkerPool[messages.FromClientAPI], + federator federation.Federator, + parseMention gtsmodel.ParseMentionFunc, +) Processor { + return Processor{ tc: tc, mediaManager: mediaManager, clientWorker: clientWorker, diff --git a/internal/processing/account/createblock.go b/internal/processing/account/block.go index 68f28fafe..99effd3a3 100644 --- a/internal/processing/account/createblock.go +++ b/internal/processing/account/block.go @@ -20,6 +20,7 @@ package account import ( "context" + "errors" "fmt" "github.com/superseriousbusiness/gotosocial/internal/ap" @@ -32,7 +33,8 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/uris" ) -func (p *processor) BlockCreate(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Relationship, gtserror.WithCode) { +// BlockCreate handles the creation of a block from requestingAccount to targetAccountID, either remote or local. +func (p *Processor) BlockCreate(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Relationship, gtserror.WithCode) { // make sure the target account actually exists in our db targetAccount, err := p.db.GetAccountByID(ctx, targetAccountID) if err != nil { @@ -154,3 +156,37 @@ func (p *processor) BlockCreate(ctx context.Context, requestingAccount *gtsmodel return p.RelationshipGet(ctx, requestingAccount, targetAccountID) } + +// BlockRemove handles the removal of a block from requestingAccount to targetAccountID, either remote or local. +func (p *Processor) BlockRemove(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Relationship, gtserror.WithCode) { + // make sure the target account actually exists in our db + targetAccount, err := p.db.GetAccountByID(ctx, targetAccountID) + if err != nil { + return nil, gtserror.NewErrorNotFound(fmt.Errorf("BlockCreate: error getting account %s from the db: %s", targetAccountID, err)) + } + + // check if a block exists, and remove it if it does + block, err := p.db.GetBlock(ctx, requestingAccount.ID, targetAccountID) + if err == nil { + // we got a block, remove it + block.Account = requestingAccount + block.TargetAccount = targetAccount + if err := p.db.DeleteBlockByID(ctx, block.ID); err != nil { + return nil, gtserror.NewErrorInternalError(fmt.Errorf("BlockRemove: error removing block from db: %s", err)) + } + + // send the UNDO activity to the client worker for async processing + p.clientWorker.Queue(messages.FromClientAPI{ + APObjectType: ap.ActivityBlock, + APActivityType: ap.ActivityUndo, + GTSModel: block, + OriginAccount: requestingAccount, + TargetAccount: targetAccount, + }) + } else if !errors.Is(err, db.ErrNoEntries) { + return nil, gtserror.NewErrorInternalError(fmt.Errorf("BlockRemove: error getting possible block from db: %s", err)) + } + + // return whatever relationship results from all this + return p.RelationshipGet(ctx, requestingAccount, targetAccountID) +} diff --git a/internal/processing/account/getbookmarks.go b/internal/processing/account/bookmarks.go index 0a63a074f..7551b1e0c 100644 --- a/internal/processing/account/getbookmarks.go +++ b/internal/processing/account/bookmarks.go @@ -28,7 +28,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/util" ) -func (p *processor) BookmarksGet(ctx context.Context, requestingAccount *gtsmodel.Account, limit int, maxID string, minID string) (*apimodel.PageableResponse, gtserror.WithCode) { +func (p *Processor) BookmarksGet(ctx context.Context, requestingAccount *gtsmodel.Account, limit int, maxID string, minID string) (*apimodel.PageableResponse, gtserror.WithCode) { if requestingAccount == nil { return nil, gtserror.NewErrorForbidden(fmt.Errorf("cannot retrieve bookmarks without a requesting account")) } diff --git a/internal/processing/account/create.go b/internal/processing/account/create.go index b0efccf7e..8b82bc681 100644 --- a/internal/processing/account/create.go +++ b/internal/processing/account/create.go @@ -33,7 +33,8 @@ import ( "github.com/superseriousbusiness/oauth2/v4" ) -func (p *processor) Create(ctx context.Context, applicationToken oauth2.TokenInfo, application *gtsmodel.Application, form *apimodel.AccountCreateRequest) (*apimodel.Token, gtserror.WithCode) { +// Create processes the given form for creating a new account, returning an oauth token for that account if successful. +func (p *Processor) Create(ctx context.Context, applicationToken oauth2.TokenInfo, application *gtsmodel.Application, form *apimodel.AccountCreateRequest) (*apimodel.Token, gtserror.WithCode) { emailAvailable, err := p.db.IsEmailAvailable(ctx, form.Email) if err != nil { return nil, gtserror.NewErrorBadRequest(err) diff --git a/internal/processing/account/delete.go b/internal/processing/account/delete.go index 32321e196..7a31b45d4 100644 --- a/internal/processing/account/delete.go +++ b/internal/processing/account/delete.go @@ -34,28 +34,9 @@ import ( "golang.org/x/crypto/bcrypt" ) -// Delete handles the complete deletion of an account. -// -// To be done in this function: -// 1. Delete account's application(s), clients, and oauth tokens -// 2. Delete account's blocks -// 3. Delete account's emoji -// 4. Delete account's follow requests -// 5. Delete account's follows -// 6. Delete account's statuses -// 7. Delete account's media attachments -// 8. Delete account's mentions -// 9. Delete account's polls -// 10. Delete account's notifications -// 11. Delete account's bookmarks -// 12. Delete account's faves -// 13. Delete account's mutes -// 14. Delete account's streams -// 15. Delete account's tags -// 16. Delete account's user -// 17. Delete account's timeline -// 18. Delete account itself -func (p *processor) Delete(ctx context.Context, account *gtsmodel.Account, origin string) gtserror.WithCode { +// Delete deletes an account, and all of that account's statuses, media, follows, notifications, etc etc etc. +// The origin passed here should be either the ID of the account doing the delete (can be itself), or the ID of a domain block. +func (p *Processor) Delete(ctx context.Context, account *gtsmodel.Account, origin string) gtserror.WithCode { fields := kv.Fields{{"username", account.Username}} if account.Domain != "" { @@ -289,7 +270,9 @@ func (p *processor) Delete(ctx context.Context, account *gtsmodel.Account, origi return nil } -func (p *processor) DeleteLocal(ctx context.Context, account *gtsmodel.Account, form *apimodel.AccountDeleteRequest) gtserror.WithCode { +// DeleteLocal is like Delete, but specifically for deletion of local accounts rather than federated ones. +// Unlike Delete, it will propagate the deletion out across the federating API to other instances. +func (p *Processor) DeleteLocal(ctx context.Context, account *gtsmodel.Account, form *apimodel.AccountDeleteRequest) gtserror.WithCode { fromClientAPIMessage := messages.FromClientAPI{ APObjectType: ap.ActorPerson, APActivityType: ap.ActivityDelete, diff --git a/internal/processing/account/createfollow.go b/internal/processing/account/follow.go index 721cf39c7..d4d479be7 100644 --- a/internal/processing/account/createfollow.go +++ b/internal/processing/account/follow.go @@ -32,7 +32,8 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/uris" ) -func (p *processor) FollowCreate(ctx context.Context, requestingAccount *gtsmodel.Account, form *apimodel.AccountFollowRequest) (*apimodel.Relationship, gtserror.WithCode) { +// 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) { // if there's a block between the accounts we shouldn't create the request ofc if blocked, err := p.db.IsBlocked(ctx, requestingAccount.ID, form.ID, true); err != nil { return nil, gtserror.NewErrorInternalError(err) @@ -119,3 +120,86 @@ func (p *processor) FollowCreate(ctx context.Context, requestingAccount *gtsmode // return whatever relationship results from this return p.RelationshipGet(ctx, requestingAccount, form.ID) } + +// 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) { + // if there's a block between the accounts we shouldn't do anything + blocked, err := p.db.IsBlocked(ctx, requestingAccount.ID, targetAccountID, true) + if err != nil { + return nil, gtserror.NewErrorInternalError(err) + } + if blocked { + return nil, gtserror.NewErrorNotFound(fmt.Errorf("AccountFollowRemove: block exists between accounts")) + } + + // make sure the target account actually exists in our db + targetAcct, err := p.db.GetAccountByID(ctx, targetAccountID) + if err != nil { + if err == db.ErrNoEntries { + return nil, gtserror.NewErrorNotFound(fmt.Errorf("AccountFollowRemove: account %s not found in the db: %s", targetAccountID, err)) + } + } + + // check if a follow request exists, and remove it if it does (storing the URI for later) + var frChanged bool + var frURI string + fr := >smodel.FollowRequest{} + if err := p.db.GetWhere(ctx, []db.Where{ + {Key: "account_id", Value: requestingAccount.ID}, + {Key: "target_account_id", Value: targetAccountID}, + }, fr); err == nil { + frURI = fr.URI + if err := p.db.DeleteByID(ctx, fr.ID, fr); err != nil { + return nil, gtserror.NewErrorInternalError(fmt.Errorf("AccountFollowRemove: error removing follow request from db: %s", err)) + } + frChanged = true + } + + // now do the same thing for any existing follow + var fChanged bool + var fURI string + f := >smodel.Follow{} + if err := p.db.GetWhere(ctx, []db.Where{ + {Key: "account_id", Value: requestingAccount.ID}, + {Key: "target_account_id", Value: targetAccountID}, + }, f); err == nil { + fURI = f.URI + if err := p.db.DeleteByID(ctx, f.ID, f); err != nil { + return nil, gtserror.NewErrorInternalError(fmt.Errorf("AccountFollowRemove: error removing follow from db: %s", err)) + } + fChanged = true + } + + // follow request status changed so send the UNDO activity to the channel for async processing + if frChanged { + p.clientWorker.Queue(messages.FromClientAPI{ + APObjectType: ap.ActivityFollow, + APActivityType: ap.ActivityUndo, + GTSModel: >smodel.Follow{ + AccountID: requestingAccount.ID, + TargetAccountID: targetAccountID, + URI: frURI, + }, + OriginAccount: requestingAccount, + TargetAccount: targetAcct, + }) + } + + // follow status changed so send the UNDO activity to the channel for async processing + if fChanged { + p.clientWorker.Queue(messages.FromClientAPI{ + APObjectType: ap.ActivityFollow, + APActivityType: ap.ActivityUndo, + GTSModel: >smodel.Follow{ + AccountID: requestingAccount.ID, + TargetAccountID: targetAccountID, + URI: fURI, + }, + OriginAccount: requestingAccount, + TargetAccount: targetAcct, + }) + } + + // return whatever relationship results from all this + return p.RelationshipGet(ctx, requestingAccount, targetAccountID) +} diff --git a/internal/processing/account/get.go b/internal/processing/account/get.go index 0592555da..11de1ddac 100644 --- a/internal/processing/account/get.go +++ b/internal/processing/account/get.go @@ -31,7 +31,8 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/transport" ) -func (p *processor) Get(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Account, gtserror.WithCode) { +// Get processes the given request for account information. +func (p *Processor) Get(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Account, gtserror.WithCode) { targetAccount, err := p.db.GetAccountByID(ctx, targetAccountID) if err != nil { if err == db.ErrNoEntries { @@ -40,10 +41,11 @@ func (p *processor) Get(ctx context.Context, requestingAccount *gtsmodel.Account return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error: %s", err)) } - return p.getAccountFor(ctx, requestingAccount, targetAccount) + return p.getFor(ctx, requestingAccount, targetAccount) } -func (p *processor) GetLocalByUsername(ctx context.Context, requestingAccount *gtsmodel.Account, username string) (*apimodel.Account, gtserror.WithCode) { +// GetLocalByUsername processes the given request for account information targeting a local account by username. +func (p *Processor) GetLocalByUsername(ctx context.Context, requestingAccount *gtsmodel.Account, username string) (*apimodel.Account, gtserror.WithCode) { targetAccount, err := p.db.GetAccountByUsernameDomain(ctx, username, "") if err != nil { if err == db.ErrNoEntries { @@ -52,10 +54,11 @@ func (p *processor) GetLocalByUsername(ctx context.Context, requestingAccount *g return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error: %s", err)) } - return p.getAccountFor(ctx, requestingAccount, targetAccount) + return p.getFor(ctx, requestingAccount, targetAccount) } -func (p *processor) GetCustomCSSForUsername(ctx context.Context, username string) (string, gtserror.WithCode) { +// GetCustomCSSForUsername returns custom css for the given local username. +func (p *Processor) GetCustomCSSForUsername(ctx context.Context, username string) (string, gtserror.WithCode) { customCSS, err := p.db.GetAccountCustomCSSByUsername(ctx, username) if err != nil { if err == db.ErrNoEntries { @@ -67,7 +70,7 @@ func (p *processor) GetCustomCSSForUsername(ctx context.Context, username string return customCSS, nil } -func (p *processor) getAccountFor(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (*apimodel.Account, gtserror.WithCode) { +func (p *Processor) getFor(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (*apimodel.Account, gtserror.WithCode) { var blocked bool var err error if requestingAccount != nil { diff --git a/internal/processing/account/getfollowers.go b/internal/processing/account/getfollowers.go deleted file mode 100644 index df6fcc3f9..000000000 --- a/internal/processing/account/getfollowers.go +++ /dev/null @@ -1,74 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2023 GoToSocial Authors admin@gotosocial.org - - 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" - "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" -) - -func (p *processor) FollowersGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) ([]apimodel.Account, gtserror.WithCode) { - if blocked, err := p.db.IsBlocked(ctx, requestingAccount.ID, targetAccountID, true); err != nil { - return nil, gtserror.NewErrorInternalError(err) - } else if blocked { - return nil, gtserror.NewErrorNotFound(fmt.Errorf("block exists between accounts")) - } - - accounts := []apimodel.Account{} - follows, err := p.db.GetAccountFollowedBy(ctx, targetAccountID, false) - if err != nil { - if err == db.ErrNoEntries { - return accounts, nil - } - return nil, gtserror.NewErrorInternalError(err) - } - - for _, f := range follows { - blocked, err := p.db.IsBlocked(ctx, requestingAccount.ID, f.AccountID, true) - if err != nil { - return nil, gtserror.NewErrorInternalError(err) - } - if blocked { - continue - } - - if f.Account == nil { - a, err := p.db.GetAccountByID(ctx, f.AccountID) - if err != nil { - if err == db.ErrNoEntries { - continue - } - return nil, gtserror.NewErrorInternalError(err) - } - f.Account = a - } - - account, err := p.tc.AccountToAPIAccountPublic(ctx, f.Account) - if err != nil { - return nil, gtserror.NewErrorInternalError(err) - } - accounts = append(accounts, *account) - } - return accounts, nil -} diff --git a/internal/processing/account/getrelationship.go b/internal/processing/account/getrelationship.go deleted file mode 100644 index b8406c38a..000000000 --- a/internal/processing/account/getrelationship.go +++ /dev/null @@ -1,47 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2023 GoToSocial Authors admin@gotosocial.org - - 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" - "fmt" - - apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" - "github.com/superseriousbusiness/gotosocial/internal/gtserror" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" -) - -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")) - } - - gtsR, err := p.db.GetRelationship(ctx, requestingAccount.ID, targetAccountID) - if err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("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 r, nil -} diff --git a/internal/processing/account/getfollowing.go b/internal/processing/account/relationships.go index fc584c778..cb2789829 100644 --- a/internal/processing/account/getfollowing.go +++ b/internal/processing/account/relationships.go @@ -20,6 +20,7 @@ package account import ( "context" + "errors" "fmt" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" @@ -28,7 +29,54 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -func (p *processor) FollowingGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) ([]apimodel.Account, gtserror.WithCode) { +// 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.db.IsBlocked(ctx, requestingAccount.ID, targetAccountID, true); err != nil { + return nil, gtserror.NewErrorInternalError(err) + } else if blocked { + return nil, gtserror.NewErrorNotFound(fmt.Errorf("block exists between accounts")) + } + + accounts := []apimodel.Account{} + follows, err := p.db.GetAccountFollowedBy(ctx, targetAccountID, false) + if err != nil { + if err == db.ErrNoEntries { + return accounts, nil + } + return nil, gtserror.NewErrorInternalError(err) + } + + for _, f := range follows { + blocked, err := p.db.IsBlocked(ctx, requestingAccount.ID, f.AccountID, true) + if err != nil { + return nil, gtserror.NewErrorInternalError(err) + } + if blocked { + continue + } + + if f.Account == nil { + a, err := p.db.GetAccountByID(ctx, f.AccountID) + if err != nil { + if err == db.ErrNoEntries { + continue + } + return nil, gtserror.NewErrorInternalError(err) + } + f.Account = a + } + + account, err := p.tc.AccountToAPIAccountPublic(ctx, f.Account) + if err != nil { + return nil, gtserror.NewErrorInternalError(err) + } + accounts = append(accounts, *account) + } + return accounts, 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.db.IsBlocked(ctx, requestingAccount.ID, targetAccountID, true); err != nil { return nil, gtserror.NewErrorInternalError(err) } else if blocked { @@ -72,3 +120,22 @@ func (p *processor) FollowingGet(ctx context.Context, requestingAccount *gtsmode } return accounts, 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")) + } + + gtsR, err := p.db.GetRelationship(ctx, requestingAccount.ID, targetAccountID) + if err != nil { + return nil, gtserror.NewErrorInternalError(fmt.Errorf("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 r, nil +} diff --git a/internal/processing/account/removeblock.go b/internal/processing/account/removeblock.go deleted file mode 100644 index 4316af10f..000000000 --- a/internal/processing/account/removeblock.go +++ /dev/null @@ -1,65 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2023 GoToSocial Authors admin@gotosocial.org - - 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" - "fmt" - - "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" -) - -func (p *processor) BlockRemove(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Relationship, gtserror.WithCode) { - // make sure the target account actually exists in our db - targetAccount, err := p.db.GetAccountByID(ctx, targetAccountID) - if err != nil { - return nil, gtserror.NewErrorNotFound(fmt.Errorf("BlockCreate: error getting account %s from the db: %s", targetAccountID, err)) - } - - // check if a block exists, and remove it if it does - block, err := p.db.GetBlock(ctx, requestingAccount.ID, targetAccountID) - if err == nil { - // we got a block, remove it - block.Account = requestingAccount - block.TargetAccount = targetAccount - if err := p.db.DeleteBlockByID(ctx, block.ID); err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("BlockRemove: error removing block from db: %s", err)) - } - - // send the UNDO activity to the client worker for async processing - p.clientWorker.Queue(messages.FromClientAPI{ - APObjectType: ap.ActivityBlock, - APActivityType: ap.ActivityUndo, - GTSModel: block, - OriginAccount: requestingAccount, - TargetAccount: targetAccount, - }) - } else if !errors.Is(err, db.ErrNoEntries) { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("BlockRemove: error getting possible block from db: %s", err)) - } - - // return whatever relationship results from all this - return p.RelationshipGet(ctx, requestingAccount, targetAccountID) -} diff --git a/internal/processing/account/removefollow.go b/internal/processing/account/removefollow.go deleted file mode 100644 index 83ced1238..000000000 --- a/internal/processing/account/removefollow.go +++ /dev/null @@ -1,113 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2023 GoToSocial Authors admin@gotosocial.org - - 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" - "fmt" - - "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" -) - -func (p *processor) FollowRemove(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Relationship, gtserror.WithCode) { - // if there's a block between the accounts we shouldn't do anything - blocked, err := p.db.IsBlocked(ctx, requestingAccount.ID, targetAccountID, true) - if err != nil { - return nil, gtserror.NewErrorInternalError(err) - } - if blocked { - return nil, gtserror.NewErrorNotFound(fmt.Errorf("AccountFollowRemove: block exists between accounts")) - } - - // make sure the target account actually exists in our db - targetAcct, err := p.db.GetAccountByID(ctx, targetAccountID) - if err != nil { - if err == db.ErrNoEntries { - return nil, gtserror.NewErrorNotFound(fmt.Errorf("AccountFollowRemove: account %s not found in the db: %s", targetAccountID, err)) - } - } - - // check if a follow request exists, and remove it if it does (storing the URI for later) - var frChanged bool - var frURI string - fr := >smodel.FollowRequest{} - if err := p.db.GetWhere(ctx, []db.Where{ - {Key: "account_id", Value: requestingAccount.ID}, - {Key: "target_account_id", Value: targetAccountID}, - }, fr); err == nil { - frURI = fr.URI - if err := p.db.DeleteByID(ctx, fr.ID, fr); err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("AccountFollowRemove: error removing follow request from db: %s", err)) - } - frChanged = true - } - - // now do the same thing for any existing follow - var fChanged bool - var fURI string - f := >smodel.Follow{} - if err := p.db.GetWhere(ctx, []db.Where{ - {Key: "account_id", Value: requestingAccount.ID}, - {Key: "target_account_id", Value: targetAccountID}, - }, f); err == nil { - fURI = f.URI - if err := p.db.DeleteByID(ctx, f.ID, f); err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("AccountFollowRemove: error removing follow from db: %s", err)) - } - fChanged = true - } - - // follow request status changed so send the UNDO activity to the channel for async processing - if frChanged { - p.clientWorker.Queue(messages.FromClientAPI{ - APObjectType: ap.ActivityFollow, - APActivityType: ap.ActivityUndo, - GTSModel: >smodel.Follow{ - AccountID: requestingAccount.ID, - TargetAccountID: targetAccountID, - URI: frURI, - }, - OriginAccount: requestingAccount, - TargetAccount: targetAcct, - }) - } - - // follow status changed so send the UNDO activity to the channel for async processing - if fChanged { - p.clientWorker.Queue(messages.FromClientAPI{ - APObjectType: ap.ActivityFollow, - APActivityType: ap.ActivityUndo, - GTSModel: >smodel.Follow{ - AccountID: requestingAccount.ID, - TargetAccountID: targetAccountID, - URI: fURI, - }, - OriginAccount: requestingAccount, - TargetAccount: targetAcct, - }) - } - - // return whatever relationship results from all this - return p.RelationshipGet(ctx, requestingAccount, targetAccountID) -} diff --git a/internal/processing/account/getrss.go b/internal/processing/account/rss.go index 2298f39ae..22065cf8e 100644 --- a/internal/processing/account/getrss.go +++ b/internal/processing/account/rss.go @@ -32,7 +32,8 @@ import ( const rssFeedLength = 20 -func (p *processor) GetRSSFeedForUsername(ctx context.Context, username string) (func() (string, gtserror.WithCode), time.Time, gtserror.WithCode) { +// GetRSSFeedForUsername returns RSS feed for the given local username. +func (p *Processor) GetRSSFeedForUsername(ctx context.Context, username string) (func() (string, gtserror.WithCode), time.Time, gtserror.WithCode) { account, err := p.db.GetAccountByUsernameDomain(ctx, username, "") if err != nil { if err == db.ErrNoEntries { diff --git a/internal/processing/account/getrss_test.go b/internal/processing/account/rss_test.go index 6c699abae..6c699abae 100644 --- a/internal/processing/account/getrss_test.go +++ b/internal/processing/account/rss_test.go diff --git a/internal/processing/account/getstatuses.go b/internal/processing/account/statuses.go index b231bb450..29833086d 100644 --- a/internal/processing/account/getstatuses.go +++ b/internal/processing/account/statuses.go @@ -29,7 +29,9 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/util" ) -func (p *processor) StatusesGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string, limit int, excludeReplies bool, excludeReblogs bool, maxID string, minID string, pinnedOnly bool, mediaOnly bool, publicOnly bool) (*apimodel.PageableResponse, gtserror.WithCode) { +// StatusesGet fetches a number of statuses (in time descending order) from the given account, filtered by visibility for +// the account given in authed. +func (p *Processor) StatusesGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string, limit int, excludeReplies bool, excludeReblogs bool, maxID string, minID string, pinnedOnly bool, mediaOnly bool, publicOnly bool) (*apimodel.PageableResponse, gtserror.WithCode) { if requestingAccount != nil { if blocked, err := p.db.IsBlocked(ctx, requestingAccount.ID, targetAccountID, true); err != nil { return nil, gtserror.NewErrorInternalError(err) @@ -96,7 +98,9 @@ func (p *processor) StatusesGet(ctx context.Context, requestingAccount *gtsmodel }) } -func (p *processor) WebStatusesGet(ctx context.Context, targetAccountID string, maxID string) (*apimodel.PageableResponse, gtserror.WithCode) { +// WebStatusesGet fetches a number of statuses (in descending order) from the given account. It selects only +// statuses which are suitable for showing on the public web profile of an account. +func (p *Processor) WebStatusesGet(ctx context.Context, targetAccountID string, maxID string) (*apimodel.PageableResponse, gtserror.WithCode) { acct, err := p.db.GetAccountByID(ctx, targetAccountID) if err != nil { if err == db.ErrNoEntries { diff --git a/internal/processing/account/update.go b/internal/processing/account/update.go index e6867bfd3..cffbbb0c5 100644 --- a/internal/processing/account/update.go +++ b/internal/processing/account/update.go @@ -33,10 +33,12 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/media" "github.com/superseriousbusiness/gotosocial/internal/messages" "github.com/superseriousbusiness/gotosocial/internal/text" + "github.com/superseriousbusiness/gotosocial/internal/typeutils" "github.com/superseriousbusiness/gotosocial/internal/validate" ) -func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form *apimodel.UpdateCredentialsRequest) (*apimodel.Account, gtserror.WithCode) { +// Update processes the update of an account with the given form. +func (p *Processor) Update(ctx context.Context, account *gtsmodel.Account, form *apimodel.UpdateCredentialsRequest) (*apimodel.Account, gtserror.WithCode) { if form.Discoverable != nil { account.Discoverable = form.Discoverable } @@ -138,7 +140,7 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form if err := validate.Privacy(*form.Source.Privacy); err != nil { return nil, gtserror.NewErrorBadRequest(err) } - privacy := p.tc.APIVisToVis(apimodel.Visibility(*form.Source.Privacy)) + privacy := typeutils.APIVisToVis(apimodel.Visibility(*form.Source.Privacy)) account.Privacy = privacy } @@ -185,7 +187,7 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form // UpdateAvatar does the dirty work of checking the avatar part of an account update form, // parsing and checking the image, and doing the necessary updates in the database for this to become // the account's new avatar image. -func (p *processor) UpdateAvatar(ctx context.Context, avatar *multipart.FileHeader, description *string, accountID string) (*gtsmodel.MediaAttachment, error) { +func (p *Processor) UpdateAvatar(ctx context.Context, avatar *multipart.FileHeader, description *string, accountID string) (*gtsmodel.MediaAttachment, error) { maxImageSize := config.GetMediaImageMaxSize() if avatar.Size > int64(maxImageSize) { return nil, fmt.Errorf("UpdateAvatar: avatar with size %d exceeded max image size of %d bytes", avatar.Size, maxImageSize) @@ -213,7 +215,7 @@ func (p *processor) UpdateAvatar(ctx context.Context, avatar *multipart.FileHead // UpdateHeader does the dirty work of checking the header part of an account update form, // parsing and checking the image, and doing the necessary updates in the database for this to become // the account's new header image. -func (p *processor) UpdateHeader(ctx context.Context, header *multipart.FileHeader, description *string, accountID string) (*gtsmodel.MediaAttachment, error) { +func (p *Processor) UpdateHeader(ctx context.Context, header *multipart.FileHeader, description *string, accountID string) (*gtsmodel.MediaAttachment, error) { maxImageSize := config.GetMediaImageMaxSize() if header.Size > int64(maxImageSize) { return nil, fmt.Errorf("UpdateHeader: header with size %d exceeded max image size of %d bytes", header.Size, maxImageSize) |