[feature/frontend] Allow setting alt-text for avatar + header (#3086)

This commit is contained in:
tobi
2024-07-08 15:47:03 +02:00
committed by GitHub
parent 43c480aec4
commit d70f4e166d
18 changed files with 395 additions and 140 deletions

View File

@ -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, "")
blocked, err := p.state.DB.IsEitherBlocked(ctx, requestingAccount.ID, targetAccount.ID)
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))
err := gtserror.Newf("db error checking blocks: %w", err)
return nil, gtserror.NewErrorInternalError(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)
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))
}
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
}

View File

@ -204,11 +204,16 @@ func (p *Processor) Update(ctx context.Context, account *gtsmodel.Account, form
}
}
if form.AvatarDescription != nil {
desc := text.SanitizeToPlaintext(*form.AvatarDescription)
form.AvatarDescription = util.Ptr(desc)
}
if form.Avatar != nil && form.Avatar.Size != 0 {
avatarInfo, errWithCode := p.UpdateAvatar(ctx,
account,
form.Avatar,
nil,
form.AvatarDescription,
)
if errWithCode != nil {
return nil, errWithCode
@ -216,13 +221,29 @@ func (p *Processor) Update(ctx context.Context, account *gtsmodel.Account, form
account.AvatarMediaAttachmentID = avatarInfo.ID
account.AvatarMediaAttachment = avatarInfo
log.Tracef(ctx, "new avatar info for account %s is %+v", account.ID, avatarInfo)
} else if form.AvatarDescription != nil && account.AvatarMediaAttachment != nil {
// Update just existing description if possible.
account.AvatarMediaAttachment.Description = *form.AvatarDescription
if err := p.state.DB.UpdateAttachment(
ctx,
account.AvatarMediaAttachment,
"description",
); err != nil {
err := gtserror.Newf("db error updating account avatar description: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
}
if form.HeaderDescription != nil {
desc := text.SanitizeToPlaintext(*form.HeaderDescription)
form.HeaderDescription = util.Ptr(desc)
}
if form.Header != nil && form.Header.Size != 0 {
headerInfo, errWithCode := p.UpdateHeader(ctx,
account,
form.Header,
nil,
form.HeaderDescription,
)
if errWithCode != nil {
return nil, errWithCode
@ -230,6 +251,17 @@ func (p *Processor) Update(ctx context.Context, account *gtsmodel.Account, form
account.HeaderMediaAttachmentID = headerInfo.ID
account.HeaderMediaAttachment = headerInfo
log.Tracef(ctx, "new header info for account %s is %+v", account.ID, headerInfo)
} else if form.HeaderDescription != nil && account.HeaderMediaAttachment != nil {
// Update just existing description if possible.
account.HeaderMediaAttachment.Description = *form.HeaderDescription
if err := p.state.DB.UpdateAttachment(
ctx,
account.HeaderMediaAttachment,
"description",
); err != nil {
err := gtserror.Newf("db error updating account avatar description: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
}
if form.Locked != nil {