mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[chore] reformat GetAccount() functionality, support updating accounts based on last_fetch (#1411)
* reformat GetAccount() functionality, and add UpdateAccount() function. * use fetched_at instead of last_webfingered_at * catch local "not found" errors. small formatting / error string changes * remove now unused error type * return nil when wrapping nil error * update expected error messages * return correct url for foss satan webfinger * add AP model for Some_User * normalize local domain * return notretrievable where appropriate * expose NewErrNotRetrievable * ensure webfinger for new accounts searched by uri * update local account short circuit * allow enrich to fail for already-known accounts * remove unused LastWebfingeredAt * expose test maps on mock http client * update Update test * reformat GetAccount() functionality, and add UpdateAccount() function. * use fetched_at instead of last_webfingered_at * catch local "not found" errors. small formatting / error string changes * remove nil error checks (we shouldn't be passing nil errors to newError() initializers) * remove mutex unlock on transport init fail (it hasn't yet been locked!) * woops add back the error wrapping to use ErrNotRetrievable * caches were never being started... 🙈 --------- Signed-off-by: kim <grufwub@gmail.com> Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
This commit is contained in:
@ -27,6 +27,8 @@ import (
|
||||
|
||||
"codeberg.org/gruf/go-kv"
|
||||
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/config"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/federation/dereferencing"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
@ -85,7 +87,7 @@ func (p *processor) SearchGet(ctx context.Context, authed *oauth.Auth, search *a
|
||||
|
||||
if username, domain, err := util.ExtractNamestringParts(maybeNamestring); err == nil {
|
||||
l.Trace("search term is a mention, looking it up...")
|
||||
foundAccount, err := p.searchAccountByMention(ctx, authed, username, domain, search.Resolve)
|
||||
foundAccount, err := p.searchAccountByUsernameDomain(ctx, authed, username, domain, search.Resolve)
|
||||
if err != nil {
|
||||
var errNotRetrievable *dereferencing.ErrNotRetrievable
|
||||
if !errors.As(err, &errNotRetrievable) {
|
||||
@ -210,27 +212,70 @@ func (p *processor) searchStatusByURI(ctx context.Context, authed *oauth.Auth, u
|
||||
|
||||
if !*status.Local && statusable != nil {
|
||||
// Attempt to dereference the status thread while we are here
|
||||
p.federator.DereferenceRemoteThread(transport.WithFastfail(ctx), authed.Account.Username, uri, status, statusable)
|
||||
p.federator.DereferenceThread(transport.WithFastfail(ctx), authed.Account.Username, uri, status, statusable)
|
||||
}
|
||||
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func (p *processor) searchAccountByURI(ctx context.Context, authed *oauth.Auth, uri *url.URL, resolve bool) (*gtsmodel.Account, error) {
|
||||
return p.federator.GetAccount(transport.WithFastfail(ctx), dereferencing.GetAccountParams{
|
||||
RequestingUsername: authed.Account.Username,
|
||||
RemoteAccountID: uri,
|
||||
Blocking: true,
|
||||
SkipResolve: !resolve,
|
||||
})
|
||||
if !resolve {
|
||||
var (
|
||||
account *gtsmodel.Account
|
||||
err error
|
||||
uriStr = uri.String()
|
||||
)
|
||||
|
||||
// Search the database for existing account with ID URI.
|
||||
account, err = p.db.GetAccountByURI(ctx, uriStr)
|
||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||
return nil, fmt.Errorf("searchAccountByURI: error checking database for account %s: %w", uriStr, err)
|
||||
}
|
||||
|
||||
if account == nil {
|
||||
// Else, search the database for existing by ID URL.
|
||||
account, err = p.db.GetAccountByURL(ctx, uriStr)
|
||||
if err != nil {
|
||||
if !errors.Is(err, db.ErrNoEntries) {
|
||||
return nil, fmt.Errorf("searchAccountByURI: error checking database for account %s: %w", uriStr, err)
|
||||
}
|
||||
return nil, dereferencing.NewErrNotRetrievable(err)
|
||||
}
|
||||
}
|
||||
|
||||
return account, nil
|
||||
}
|
||||
|
||||
return p.federator.GetAccountByURI(
|
||||
transport.WithFastfail(ctx),
|
||||
authed.Account.Username,
|
||||
uri, false,
|
||||
)
|
||||
}
|
||||
|
||||
func (p *processor) searchAccountByMention(ctx context.Context, authed *oauth.Auth, username string, domain string, resolve bool) (*gtsmodel.Account, error) {
|
||||
return p.federator.GetAccount(transport.WithFastfail(ctx), dereferencing.GetAccountParams{
|
||||
RequestingUsername: authed.Account.Username,
|
||||
RemoteAccountUsername: username,
|
||||
RemoteAccountHost: domain,
|
||||
Blocking: true,
|
||||
SkipResolve: !resolve,
|
||||
})
|
||||
func (p *processor) searchAccountByUsernameDomain(ctx context.Context, authed *oauth.Auth, username string, domain string, resolve bool) (*gtsmodel.Account, error) {
|
||||
if !resolve {
|
||||
if domain == config.GetHost() || domain == config.GetAccountDomain() {
|
||||
// We do local lookups using an empty domain,
|
||||
// else it will fail the db search below.
|
||||
domain = ""
|
||||
}
|
||||
|
||||
// Search the database for existing account with USERNAME@DOMAIN
|
||||
account, err := p.db.GetAccountByUsernameDomain(ctx, username, domain)
|
||||
if err != nil {
|
||||
if !errors.Is(err, db.ErrNoEntries) {
|
||||
return nil, fmt.Errorf("searchAccountByUsernameDomain: error checking database for account %s@%s: %w", username, domain, err)
|
||||
}
|
||||
return nil, dereferencing.NewErrNotRetrievable(err)
|
||||
}
|
||||
|
||||
return account, nil
|
||||
}
|
||||
|
||||
return p.federator.GetAccountByUsernameDomain(
|
||||
transport.WithFastfail(ctx),
|
||||
authed.Account.Username,
|
||||
username, domain, false,
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user