diff options
Diffstat (limited to 'internal/processing/account/get.go')
-rw-r--r-- | internal/processing/account/get.go | 111 |
1 files changed, 62 insertions, 49 deletions
diff --git a/internal/processing/account/get.go b/internal/processing/account/get.go index 500c0c2e5..32d45054d 100644 --- a/internal/processing/account/get.go +++ b/internal/processing/account/get.go @@ -20,7 +20,6 @@ package account import ( "context" "errors" - "fmt" "net/url" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" @@ -36,66 +35,42 @@ func (p *Processor) Get(ctx context.Context, requestingAccount *gtsmodel.Account targetAccount, err := p.state.DB.GetAccountByID(ctx, targetAccountID) if err != nil { if errors.Is(err, db.ErrNoEntries) { - return nil, gtserror.NewErrorNotFound(errors.New("account not found")) + err := gtserror.New("account not found") + return nil, gtserror.NewErrorNotFound(err) } - return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error: %w", err)) + err := gtserror.Newf("db error getting account: %w", err) + return nil, gtserror.NewErrorInternalError(err) } - return p.getFor(ctx, requestingAccount, targetAccount) -} - -// 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.state.DB.GetAccountByUsernameDomain(ctx, username, "") - if err != nil { - if errors.Is(err, db.ErrNoEntries) { - return nil, gtserror.NewErrorNotFound(errors.New("account not found")) - } - return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error: %w", err)) - } - - return p.getFor(ctx, requestingAccount, targetAccount) -} - -// GetCustomCSSForUsername returns custom css for the given local username. -func (p *Processor) GetCustomCSSForUsername(ctx context.Context, username string) (string, gtserror.WithCode) { - customCSS, err := p.state.DB.GetAccountCustomCSSByUsername(ctx, username) + blocked, err := p.state.DB.IsEitherBlocked(ctx, requestingAccount.ID, targetAccount.ID) if err != nil { - if errors.Is(err, db.ErrNoEntries) { - return "", gtserror.NewErrorNotFound(errors.New("account not found")) - } - return "", gtserror.NewErrorInternalError(fmt.Errorf("db error: %w", err)) + err := gtserror.Newf("db error checking blocks: %w", err) + return nil, gtserror.NewErrorInternalError(err) } - return customCSS, nil -} - -func (p *Processor) getFor(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (*apimodel.Account, gtserror.WithCode) { - var err error - - if requestingAccount != nil { - blocked, err := p.state.DB.IsEitherBlocked(ctx, requestingAccount.ID, targetAccount.ID) + if blocked { + apiAccount, err := p.converter.AccountToAPIAccountBlocked(ctx, targetAccount) if err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("error checking account block: %w", err)) - } - - if blocked { - apiAccount, err := p.converter.AccountToAPIAccountBlocked(ctx, targetAccount) - if err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting account: %w", err)) - } - return apiAccount, nil + err := gtserror.Newf("error converting account: %w", err) + return nil, gtserror.NewErrorInternalError(err) } + return apiAccount, nil } if targetAccount.Domain != "" { targetAccountURI, err := url.Parse(targetAccount.URI) if err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("error parsing url %s: %w", targetAccount.URI, err)) + err := gtserror.Newf("error parsing account URI: %w", err) + return nil, gtserror.NewErrorInternalError(err) } - // Perform a last-minute fetch of target account to ensure remote account header / avatar is cached. - latest, _, err := p.federator.GetAccountByURI(gtscontext.SetFastFail(ctx), requestingAccount.Username, targetAccountURI) + // Perform a last-minute fetch of target account to + // ensure remote account header / avatar is cached. + latest, _, err := p.federator.GetAccountByURI( + gtscontext.SetFastFail(ctx), + requestingAccount.Username, + targetAccountURI, + ) if err != nil { log.Errorf(ctx, "error fetching latest target account: %v", err) } else { @@ -105,15 +80,53 @@ func (p *Processor) getFor(ctx context.Context, requestingAccount *gtsmodel.Acco } var apiAccount *apimodel.Account - - if requestingAccount != nil && targetAccount.ID == requestingAccount.ID { + if targetAccount.ID == requestingAccount.ID { + // This is requester's own account, + // show additional details. apiAccount, err = p.converter.AccountToAPIAccountSensitive(ctx, targetAccount) } else { + // This is a different account, + // show the "public" view. apiAccount, err = p.converter.AccountToAPIAccountPublic(ctx, targetAccount) } if err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting account: %w", err)) + err := gtserror.Newf("error converting account: %w", err) + return nil, gtserror.NewErrorInternalError(err) } return apiAccount, nil } + +// GetWeb returns the web model of a local account by username. +func (p *Processor) GetWeb(ctx context.Context, username string) (*apimodel.Account, gtserror.WithCode) { + targetAccount, err := p.state.DB.GetAccountByUsernameDomain(ctx, username, "") + if err != nil { + if errors.Is(err, db.ErrNoEntries) { + err := gtserror.New("account not found") + return nil, gtserror.NewErrorNotFound(err) + } + err := gtserror.Newf("db error getting account: %w", err) + return nil, gtserror.NewErrorInternalError(err) + } + + webAccount, err := p.converter.AccountToWebAccount(ctx, targetAccount) + if err != nil { + err := gtserror.Newf("error converting account: %w", err) + return nil, gtserror.NewErrorInternalError(err) + } + + return webAccount, nil +} + +// GetCustomCSSForUsername returns custom css for the given local username. +func (p *Processor) GetCustomCSSForUsername(ctx context.Context, username string) (string, gtserror.WithCode) { + customCSS, err := p.state.DB.GetAccountCustomCSSByUsername(ctx, username) + if err != nil { + if errors.Is(err, db.ErrNoEntries) { + return "", gtserror.NewErrorNotFound(gtserror.New("account not found")) + } + return "", gtserror.NewErrorInternalError(gtserror.Newf("db error: %w", err)) + } + + return customCSS, nil +} |