mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-12-07 15:38:07 -06:00
[chore] Tidy up some of the search logic (#1082)
* start refactoring some of the search + deref logic
* add tests for search api
* rename GetRemoteAccount + GetRemoteStatus
* make search function a bit simpler + clearer
* fix little fucky wucky uwu owo i'm just a little guy
* update faulty switch statements
* update test to use storage struct
* redo switches for clarity
* reduce repeated logic in search tests
* fastfail getstatus by uri
* debug log + trace log better
* add implementation note
* return early if no result for namestring search
* return + check on dereferencing error types
* errors hah what errors
* remove unneeded error type alias, add custom error text during stringification itself
* fix a woops recursion 🙈
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: kim <grufwub@gmail.com>
This commit is contained in:
parent
daf44ac2b7
commit
97f5453378
23 changed files with 890 additions and 282 deletions
|
|
@ -27,12 +27,12 @@ import (
|
|||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
)
|
||||
|
||||
func (f *federator) GetRemoteAccount(ctx context.Context, params dereferencing.GetRemoteAccountParams) (*gtsmodel.Account, error) {
|
||||
return f.dereferencer.GetRemoteAccount(ctx, params)
|
||||
func (f *federator) GetAccount(ctx context.Context, params dereferencing.GetAccountParams) (*gtsmodel.Account, error) {
|
||||
return f.dereferencer.GetAccount(ctx, params)
|
||||
}
|
||||
|
||||
func (f *federator) GetRemoteStatus(ctx context.Context, username string, remoteStatusID *url.URL, refetch, includeParent bool) (*gtsmodel.Status, ap.Statusable, error) {
|
||||
return f.dereferencer.GetRemoteStatus(ctx, username, remoteStatusID, refetch, includeParent)
|
||||
func (f *federator) GetStatus(ctx context.Context, username string, remoteStatusID *url.URL, refetch, includeParent bool) (*gtsmodel.Status, ap.Statusable, error) {
|
||||
return f.dereferencer.GetStatus(ctx, username, remoteStatusID, refetch, includeParent)
|
||||
}
|
||||
|
||||
func (f *federator) EnrichRemoteStatus(ctx context.Context, username string, status *gtsmodel.Status, includeParent bool) (*gtsmodel.Status, error) {
|
||||
|
|
|
|||
|
|
@ -50,20 +50,20 @@ func instanceAccount(account *gtsmodel.Account) bool {
|
|||
(account.Username == "internal.fetch" && strings.Contains(account.Note, "internal service actor"))
|
||||
}
|
||||
|
||||
// GetRemoteAccountParams wraps parameters for a remote account lookup.
|
||||
type GetRemoteAccountParams struct {
|
||||
// GetAccountParams wraps parameters for an account lookup.
|
||||
type GetAccountParams struct {
|
||||
// The username of the user doing the lookup request (optional).
|
||||
// If not set, then the GtS instance account will be used to do the lookup.
|
||||
RequestingUsername string
|
||||
// The ActivityPub URI of the remote account (optional).
|
||||
// If not set (nil), the ActivityPub URI of the remote account will be discovered
|
||||
// The ActivityPub URI of the account (optional).
|
||||
// If not set (nil), the ActivityPub URI of the account will be discovered
|
||||
// via webfinger, so you must set RemoteAccountUsername and RemoteAccountHost
|
||||
// if this parameter is not set.
|
||||
RemoteAccountID *url.URL
|
||||
// The username of the remote account (optional).
|
||||
// The username of the account (optional).
|
||||
// If RemoteAccountID is not set, then this value must be set.
|
||||
RemoteAccountUsername string
|
||||
// The host of the remote account (optional).
|
||||
// The host of the account (optional).
|
||||
// If RemoteAccountID is not set, then this value must be set.
|
||||
RemoteAccountHost string
|
||||
// Whether to do a blocking call to the remote instance. If true,
|
||||
|
|
@ -82,17 +82,51 @@ type GetRemoteAccountParams struct {
|
|||
PartialAccount *gtsmodel.Account
|
||||
}
|
||||
|
||||
// GetRemoteAccount completely dereferences a remote account, converts it to a GtS model account,
|
||||
type lookupType int
|
||||
|
||||
const (
|
||||
lookupPartialLocal lookupType = iota
|
||||
lookupPartial
|
||||
lookupURILocal
|
||||
lookupURI
|
||||
lookupMentionLocal
|
||||
lookupMention
|
||||
lookupBad
|
||||
)
|
||||
|
||||
func getLookupType(params GetAccountParams) lookupType {
|
||||
switch {
|
||||
case params.PartialAccount != nil:
|
||||
if params.PartialAccount.Domain == "" || params.PartialAccount.Domain == config.GetHost() || params.PartialAccount.Domain == config.GetAccountDomain() {
|
||||
return lookupPartialLocal
|
||||
}
|
||||
return lookupPartial
|
||||
case params.RemoteAccountID != nil:
|
||||
if host := params.RemoteAccountID.Host; host == config.GetHost() || host == config.GetAccountDomain() {
|
||||
return lookupURILocal
|
||||
}
|
||||
return lookupURI
|
||||
case params.RemoteAccountUsername != "":
|
||||
if params.RemoteAccountHost == "" || params.RemoteAccountHost == config.GetHost() || params.RemoteAccountHost == config.GetAccountDomain() {
|
||||
return lookupMentionLocal
|
||||
}
|
||||
return lookupMention
|
||||
default:
|
||||
return lookupBad
|
||||
}
|
||||
}
|
||||
|
||||
// GetAccount completely dereferences an account, converts it to a GtS model account,
|
||||
// puts or updates it in the database (if necessary), and returns it to a caller.
|
||||
//
|
||||
// If a local account is passed into this function for whatever reason (hey, it happens!), then it
|
||||
// will be returned from the database without making any remote calls.
|
||||
// GetAccount will guard against trying to do http calls to fetch an account that belongs to this instance.
|
||||
// Instead of making calls, it will just return the account early if it finds it, or return an error.
|
||||
//
|
||||
// Even if a fastfail context is used, and something goes wrong, an account might still be returned instead
|
||||
// of an error, if we already had the account in our database (in other words, if we just needed to try
|
||||
// fingering/refreshing the account again). The rationale for this is that it's more useful to be able
|
||||
// to provide *something* to the caller, even if that something is not necessarily 100% up to date.
|
||||
func (d *deref) GetRemoteAccount(ctx context.Context, params GetRemoteAccountParams) (foundAccount *gtsmodel.Account, err error) {
|
||||
func (d *deref) GetAccount(ctx context.Context, params GetAccountParams) (foundAccount *gtsmodel.Account, err error) {
|
||||
/*
|
||||
In this function we want to retrieve a gtsmodel representation of a remote account, with its proper
|
||||
accountDomain set, while making as few calls to remote instances as possible to save time and bandwidth.
|
||||
|
|
@ -113,88 +147,93 @@ func (d *deref) GetRemoteAccount(ctx context.Context, params GetRemoteAccountPar
|
|||
from that.
|
||||
*/
|
||||
|
||||
skipResolve := params.SkipResolve
|
||||
|
||||
// this first step checks if we have the
|
||||
// account in the database somewhere already,
|
||||
// or if we've been provided it as a partial
|
||||
switch {
|
||||
case params.PartialAccount != nil:
|
||||
switch getLookupType(params) {
|
||||
case lookupPartialLocal:
|
||||
params.SkipResolve = true
|
||||
fallthrough
|
||||
case lookupPartial:
|
||||
foundAccount = params.PartialAccount
|
||||
if foundAccount.Domain == "" || foundAccount.Domain == config.GetHost() || foundAccount.Domain == config.GetAccountDomain() {
|
||||
// this is actually a local account,
|
||||
// make sure we don't try to resolve
|
||||
skipResolve = true
|
||||
}
|
||||
case params.RemoteAccountID != nil:
|
||||
uri := params.RemoteAccountID
|
||||
host := uri.Host
|
||||
if host == config.GetHost() || host == config.GetAccountDomain() {
|
||||
// this is actually a local account,
|
||||
// make sure we don't try to resolve
|
||||
skipResolve = true
|
||||
case lookupURILocal:
|
||||
params.SkipResolve = true
|
||||
fallthrough
|
||||
case lookupURI:
|
||||
// see if we have this in the db already with this uri/url
|
||||
uri := params.RemoteAccountID.String()
|
||||
|
||||
if a, dbErr := d.db.GetAccountByURI(ctx, uri); dbErr == nil {
|
||||
// got it, break here to leave early
|
||||
foundAccount = a
|
||||
break
|
||||
} else if !errors.Is(dbErr, db.ErrNoEntries) {
|
||||
// a real error
|
||||
err = newErrDB(fmt.Errorf("GetRemoteAccount: unexpected error while looking for account with uri %s: %w", uri, dbErr))
|
||||
break
|
||||
}
|
||||
|
||||
if a, dbErr := d.db.GetAccountByURI(ctx, uri.String()); dbErr == nil {
|
||||
// dbErr was just db.ErrNoEntries so search by url instead
|
||||
if a, dbErr := d.db.GetAccountByURL(ctx, uri); dbErr == nil {
|
||||
// got it
|
||||
foundAccount = a
|
||||
} else if dbErr != db.ErrNoEntries {
|
||||
err = fmt.Errorf("GetRemoteAccount: database error looking for account with uri %s: %s", uri, err)
|
||||
break
|
||||
} else if !errors.Is(dbErr, db.ErrNoEntries) {
|
||||
// a real error
|
||||
err = newErrDB(fmt.Errorf("GetRemoteAccount: unexpected error while looking for account with url %s: %w", uri, dbErr))
|
||||
break
|
||||
}
|
||||
case params.RemoteAccountUsername != "" && (params.RemoteAccountHost == "" || params.RemoteAccountHost == config.GetHost() || params.RemoteAccountHost == config.GetAccountDomain()):
|
||||
// either no domain is provided or this seems
|
||||
// to be a local account, so don't resolve
|
||||
skipResolve = true
|
||||
|
||||
if a, dbErr := d.db.GetAccountByUsernameDomain(ctx, params.RemoteAccountUsername, ""); dbErr == nil {
|
||||
foundAccount = a
|
||||
} else if dbErr != db.ErrNoEntries {
|
||||
err = fmt.Errorf("GetRemoteAccount: database error looking for local account with username %s: %s", params.RemoteAccountUsername, err)
|
||||
}
|
||||
case params.RemoteAccountUsername != "" && params.RemoteAccountHost != "":
|
||||
case lookupMentionLocal:
|
||||
params.SkipResolve = true
|
||||
params.RemoteAccountHost = ""
|
||||
fallthrough
|
||||
case lookupMention:
|
||||
// see if we have this in the db already with this username/host
|
||||
if a, dbErr := d.db.GetAccountByUsernameDomain(ctx, params.RemoteAccountUsername, params.RemoteAccountHost); dbErr == nil {
|
||||
foundAccount = a
|
||||
} else if dbErr != db.ErrNoEntries {
|
||||
err = fmt.Errorf("GetRemoteAccount: database error looking for account with username %s and domain %s: %s", params.RemoteAccountUsername, params.RemoteAccountHost, err)
|
||||
} else if !errors.Is(dbErr, db.ErrNoEntries) {
|
||||
// a real error
|
||||
err = newErrDB(fmt.Errorf("GetRemoteAccount: unexpected error while looking for account %s: %w", params.RemoteAccountUsername, dbErr))
|
||||
}
|
||||
default:
|
||||
err = errors.New("GetRemoteAccount: no identifying parameters were set so we cannot get account")
|
||||
err = newErrBadRequest(errors.New("GetRemoteAccount: no identifying parameters were set so we cannot get account"))
|
||||
}
|
||||
|
||||
// bail if we've set a real error, and not just no entries in the db
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if skipResolve {
|
||||
// if we can't resolve, return already
|
||||
// since there's nothing more we can do
|
||||
if params.SkipResolve {
|
||||
// if we can't resolve, return already since there's nothing more we can do
|
||||
if foundAccount == nil {
|
||||
err = errors.New("GetRemoteAccount: couldn't retrieve account locally and won't try to resolve it")
|
||||
err = newErrNotRetrievable(errors.New("GetRemoteAccount: couldn't retrieve account locally and not allowed to resolve it"))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var accountable ap.Accountable
|
||||
if params.RemoteAccountUsername == "" || params.RemoteAccountHost == "" {
|
||||
// try to populate the missing params
|
||||
// the first one is easy ...
|
||||
params.RemoteAccountHost = params.RemoteAccountID.Host
|
||||
// ... but we still need the username so we can do a finger for the accountDomain
|
||||
// if we reach this point, we have some remote calls to make
|
||||
|
||||
// check if we got the account earlier
|
||||
var accountable ap.Accountable
|
||||
if params.RemoteAccountUsername == "" && params.RemoteAccountHost == "" {
|
||||
// if we're still missing some params, try to populate them now
|
||||
params.RemoteAccountHost = params.RemoteAccountID.Host
|
||||
if foundAccount != nil {
|
||||
// username is easy if we found something already
|
||||
params.RemoteAccountUsername = foundAccount.Username
|
||||
} else {
|
||||
// if we didn't already have it, we have dereference it from remote and just...
|
||||
accountable, err = d.dereferenceAccountable(ctx, params.RequestingUsername, params.RemoteAccountID)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("GetRemoteAccount: error dereferencing accountable: %s", err)
|
||||
// if we didn't already have it, we have to dereference it from remote
|
||||
var derefErr error
|
||||
accountable, derefErr = d.dereferenceAccountable(ctx, params.RequestingUsername, params.RemoteAccountID)
|
||||
if derefErr != nil {
|
||||
err = wrapDerefError(derefErr, "GetRemoteAccount: error dereferencing Accountable")
|
||||
return
|
||||
}
|
||||
|
||||
// ... take the username (for now)
|
||||
params.RemoteAccountUsername, err = ap.ExtractPreferredUsername(accountable)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("GetRemoteAccount: error extracting accountable username: %s", err)
|
||||
var apError error
|
||||
params.RemoteAccountUsername, apError = ap.ExtractPreferredUsername(accountable)
|
||||
if apError != nil {
|
||||
err = newErrOther(fmt.Errorf("GetRemoteAccount: error extracting Accountable username: %w", apError))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -221,11 +260,24 @@ func (d *deref) GetRemoteAccount(ctx context.Context, params GetRemoteAccountPar
|
|||
// - we were passed a partial account in params OR
|
||||
// - we haven't webfingered the account for two days AND the account isn't an instance account
|
||||
var fingered time.Time
|
||||
if params.RemoteAccountID == nil || foundAccount == nil || params.PartialAccount != nil || (foundAccount.LastWebfingeredAt.Before(time.Now().Add(webfingerInterval)) && !instanceAccount(foundAccount)) {
|
||||
accountDomain, params.RemoteAccountID, err = d.fingerRemoteAccount(ctx, params.RequestingUsername, params.RemoteAccountUsername, params.RemoteAccountHost)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("GetRemoteAccount: error while fingering: %s", err)
|
||||
return
|
||||
var refreshFinger bool
|
||||
if foundAccount != nil {
|
||||
refreshFinger = foundAccount.LastWebfingeredAt.Before(time.Now().Add(webfingerInterval)) && !instanceAccount(foundAccount)
|
||||
}
|
||||
|
||||
if params.RemoteAccountID == nil || foundAccount == nil || params.PartialAccount != nil || refreshFinger {
|
||||
if ad, accountURI, fingerError := d.fingerRemoteAccount(ctx, params.RequestingUsername, params.RemoteAccountUsername, params.RemoteAccountHost); fingerError != nil {
|
||||
if !refreshFinger {
|
||||
// only return with an error if this wasn't just a refresh finger;
|
||||
// that is, if we actually *needed* to finger in order to get the account,
|
||||
// otherwise we can just continue and we'll try again in 2 days
|
||||
err = newErrNotRetrievable(fmt.Errorf("GetRemoteAccount: error while fingering: %w", fingerError))
|
||||
return
|
||||
}
|
||||
log.Infof("error doing non-vital webfinger refresh call to %s: %s", params.RemoteAccountHost, err)
|
||||
} else {
|
||||
accountDomain = ad
|
||||
params.RemoteAccountID = accountURI
|
||||
}
|
||||
fingered = time.Now()
|
||||
}
|
||||
|
|
@ -234,24 +286,30 @@ func (d *deref) GetRemoteAccount(ctx context.Context, params GetRemoteAccountPar
|
|||
// if we just fingered and now have a discovered account domain but still no account,
|
||||
// we should do a final lookup in the database with the discovered username + accountDomain
|
||||
// to make absolutely sure we don't already have this account
|
||||
a := >smodel.Account{}
|
||||
where := []db.Where{{Key: "username", Value: params.RemoteAccountUsername}, {Key: "domain", Value: accountDomain}}
|
||||
if dbErr := d.db.GetWhere(ctx, where, a); dbErr == nil {
|
||||
if a, dbErr := d.db.GetAccountByUsernameDomain(ctx, params.RemoteAccountUsername, accountDomain); dbErr == nil {
|
||||
foundAccount = a
|
||||
} else if dbErr != db.ErrNoEntries {
|
||||
err = fmt.Errorf("GetRemoteAccount: database error looking for account with username %s and host %s: %s", params.RemoteAccountUsername, params.RemoteAccountHost, err)
|
||||
} else if !errors.Is(dbErr, db.ErrNoEntries) {
|
||||
// a real error
|
||||
err = newErrDB(fmt.Errorf("GetRemoteAccount: unexpected error while looking for account %s: %w", params.RemoteAccountUsername, dbErr))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// we may also have some extra information already, like the account we had in the db, or the
|
||||
// we may have some extra information already, like the account we had in the db, or the
|
||||
// accountable representation that we dereferenced from remote
|
||||
if foundAccount == nil {
|
||||
// we still don't have the account, so deference it if we didn't earlier
|
||||
// if we still don't have a remoteAccountID here we're boned
|
||||
if params.RemoteAccountID == nil {
|
||||
err = newErrNotRetrievable(errors.New("GetRemoteAccount: could not populate find an account nor populate params.RemoteAccountID"))
|
||||
return
|
||||
}
|
||||
|
||||
// deference accountable if we didn't earlier
|
||||
if accountable == nil {
|
||||
accountable, err = d.dereferenceAccountable(ctx, params.RequestingUsername, params.RemoteAccountID)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("GetRemoteAccount: error dereferencing accountable: %s", err)
|
||||
var derefErr error
|
||||
accountable, derefErr = d.dereferenceAccountable(ctx, params.RequestingUsername, params.RemoteAccountID)
|
||||
if derefErr != nil {
|
||||
err = wrapDerefError(derefErr, "GetRemoteAccount: error dereferencing Accountable")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -259,7 +317,7 @@ func (d *deref) GetRemoteAccount(ctx context.Context, params GetRemoteAccountPar
|
|||
// then convert
|
||||
foundAccount, err = d.typeConverter.ASRepresentationToAccount(ctx, accountable, accountDomain, false)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("GetRemoteAccount: error converting accountable to account: %s", err)
|
||||
err = newErrOther(fmt.Errorf("GetRemoteAccount: error converting Accountable to account: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -267,23 +325,21 @@ func (d *deref) GetRemoteAccount(ctx context.Context, params GetRemoteAccountPar
|
|||
var ulid string
|
||||
ulid, err = id.NewRandomULID()
|
||||
if err != nil {
|
||||
err = fmt.Errorf("GetRemoteAccount: error generating new id for account: %s", err)
|
||||
err = newErrOther(fmt.Errorf("GetRemoteAccount: error generating new id for account: %w", err))
|
||||
return
|
||||
}
|
||||
foundAccount.ID = ulid
|
||||
|
||||
_, err = d.populateAccountFields(ctx, foundAccount, params.RequestingUsername, params.Blocking)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("GetRemoteAccount: error populating further account fields: %s", err)
|
||||
return
|
||||
if _, populateErr := d.populateAccountFields(ctx, foundAccount, params.RequestingUsername, params.Blocking); populateErr != nil {
|
||||
// it's not the end of the world if we can't populate account fields, but we do want to log it
|
||||
log.Errorf("GetRemoteAccount: error populating further account fields: %s", populateErr)
|
||||
}
|
||||
|
||||
foundAccount.LastWebfingeredAt = fingered
|
||||
foundAccount.UpdatedAt = time.Now()
|
||||
|
||||
err = d.db.PutAccount(ctx, foundAccount)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("GetRemoteAccount: error putting new account: %s", err)
|
||||
if dbErr := d.db.PutAccount(ctx, foundAccount); dbErr != nil {
|
||||
err = newErrDB(fmt.Errorf("GetRemoteAccount: error putting new account: %w", dbErr))
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -303,9 +359,10 @@ func (d *deref) GetRemoteAccount(ctx context.Context, params GetRemoteAccountPar
|
|||
if foundAccount.SharedInboxURI == nil {
|
||||
// we need the accountable for this, so get it if we don't have it yet
|
||||
if accountable == nil {
|
||||
accountable, err = d.dereferenceAccountable(ctx, params.RequestingUsername, params.RemoteAccountID)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("GetRemoteAccount: error dereferencing accountable: %s", err)
|
||||
var derefErr error
|
||||
accountable, derefErr = d.dereferenceAccountable(ctx, params.RequestingUsername, params.RemoteAccountID)
|
||||
if derefErr != nil {
|
||||
err = wrapDerefError(derefErr, "GetRemoteAccount: error dereferencing Accountable")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -330,10 +387,10 @@ func (d *deref) GetRemoteAccount(ctx context.Context, params GetRemoteAccountPar
|
|||
|
||||
// make sure the account fields are populated before returning:
|
||||
// the caller might want to block until everything is loaded
|
||||
var fieldsChanged bool
|
||||
fieldsChanged, err = d.populateAccountFields(ctx, foundAccount, params.RequestingUsername, params.Blocking)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetRemoteAccount: error populating remoteAccount fields: %s", err)
|
||||
fieldsChanged, populateErr := d.populateAccountFields(ctx, foundAccount, params.RequestingUsername, params.Blocking)
|
||||
if populateErr != nil {
|
||||
// it's not the end of the world if we can't populate account fields, but we do want to log it
|
||||
log.Errorf("GetRemoteAccount: error populating further account fields: %s", populateErr)
|
||||
}
|
||||
|
||||
var fingeredChanged bool
|
||||
|
|
@ -343,9 +400,9 @@ func (d *deref) GetRemoteAccount(ctx context.Context, params GetRemoteAccountPar
|
|||
}
|
||||
|
||||
if accountDomainChanged || sharedInboxChanged || fieldsChanged || fingeredChanged {
|
||||
err = d.db.UpdateAccount(ctx, foundAccount)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetRemoteAccount: error updating remoteAccount: %s", err)
|
||||
if dbErr := d.db.UpdateAccount(ctx, foundAccount); dbErr != nil {
|
||||
err = newErrDB(fmt.Errorf("GetRemoteAccount: error updating remoteAccount: %w", err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -366,22 +423,22 @@ func (d *deref) dereferenceAccountable(ctx context.Context, username string, rem
|
|||
|
||||
transport, err := d.transportController.NewTransportForUsername(ctx, username)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("DereferenceAccountable: transport err: %s", err)
|
||||
return nil, fmt.Errorf("DereferenceAccountable: transport err: %w", err)
|
||||
}
|
||||
|
||||
b, err := transport.Dereference(ctx, remoteAccountID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("DereferenceAccountable: error deferencing %s: %s", remoteAccountID.String(), err)
|
||||
return nil, fmt.Errorf("DereferenceAccountable: error deferencing %s: %w", remoteAccountID.String(), err)
|
||||
}
|
||||
|
||||
m := make(map[string]interface{})
|
||||
if err := json.Unmarshal(b, &m); err != nil {
|
||||
return nil, fmt.Errorf("DereferenceAccountable: error unmarshalling bytes into json: %s", err)
|
||||
return nil, fmt.Errorf("DereferenceAccountable: error unmarshalling bytes into json: %w", err)
|
||||
}
|
||||
|
||||
t, err := streams.ToType(ctx, m)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("DereferenceAccountable: error resolving json into ap vocab type: %s", err)
|
||||
return nil, fmt.Errorf("DereferenceAccountable: error resolving json into ap vocab type: %w", err)
|
||||
}
|
||||
|
||||
switch t.GetTypeName() {
|
||||
|
|
@ -417,11 +474,11 @@ func (d *deref) dereferenceAccountable(ctx context.Context, username string, rem
|
|||
return p, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("DereferenceAccountable: type name %s not supported", t.GetTypeName())
|
||||
return nil, newErrWrongType(fmt.Errorf("DereferenceAccountable: type name %s not supported as Accountable", t.GetTypeName()))
|
||||
}
|
||||
|
||||
// populateAccountFields populates any fields on the given account that weren't populated by the initial
|
||||
// dereferencing. This includes things like header and avatar etc.
|
||||
// populateAccountFields makes a best effort to populate fields on an account such as emojis, avatar, header.
|
||||
// Will return true if one of these things changed on the passed-in account.
|
||||
func (d *deref) populateAccountFields(ctx context.Context, account *gtsmodel.Account, requestingUsername string, blocking bool) (bool, error) {
|
||||
// if we're dealing with an instance account, just bail, we don't need to do anything
|
||||
if instanceAccount(account) {
|
||||
|
|
@ -430,10 +487,15 @@ func (d *deref) populateAccountFields(ctx context.Context, account *gtsmodel.Acc
|
|||
|
||||
accountURI, err := url.Parse(account.URI)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("populateAccountFields: couldn't parse account URI %s: %s", account.URI, err)
|
||||
return false, fmt.Errorf("populateAccountFields: couldn't parse account URI %s: %w", account.URI, err)
|
||||
}
|
||||
|
||||
if blocked, err := d.db.IsDomainBlocked(ctx, accountURI.Host); blocked || err != nil {
|
||||
blocked, dbErr := d.db.IsDomainBlocked(ctx, accountURI.Host)
|
||||
if dbErr != nil {
|
||||
return false, fmt.Errorf("populateAccountFields: eror checking for block of domain %s: %w", accountURI.Host, err)
|
||||
}
|
||||
|
||||
if blocked {
|
||||
return false, fmt.Errorf("populateAccountFields: domain %s is blocked", accountURI.Host)
|
||||
}
|
||||
|
||||
|
|
@ -441,14 +503,14 @@ func (d *deref) populateAccountFields(ctx context.Context, account *gtsmodel.Acc
|
|||
|
||||
// fetch the header and avatar
|
||||
if mediaChanged, err := d.fetchRemoteAccountMedia(ctx, account, requestingUsername, blocking); err != nil {
|
||||
return false, fmt.Errorf("populateAccountFields: error fetching header/avi for account: %s", err)
|
||||
return false, fmt.Errorf("populateAccountFields: error fetching header/avi for account: %w", err)
|
||||
} else if mediaChanged {
|
||||
changed = mediaChanged
|
||||
}
|
||||
|
||||
// fetch any emojis used in note, fields, display name, etc
|
||||
if emojisChanged, err := d.fetchRemoteAccountEmojis(ctx, account, requestingUsername); err != nil {
|
||||
return false, fmt.Errorf("populateAccountFields: error fetching emojis for account: %s", err)
|
||||
return false, fmt.Errorf("populateAccountFields: error fetching emojis for account: %w", err)
|
||||
} else if emojisChanged {
|
||||
changed = emojisChanged
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ func (suite *AccountTestSuite) TestDereferenceGroup() {
|
|||
fetchingAccount := suite.testAccounts["local_account_1"]
|
||||
|
||||
groupURL := testrig.URLMustParse("https://unknown-instance.com/groups/some_group")
|
||||
group, err := suite.dereferencer.GetRemoteAccount(context.Background(), dereferencing.GetRemoteAccountParams{
|
||||
group, err := suite.dereferencer.GetAccount(context.Background(), dereferencing.GetAccountParams{
|
||||
RequestingUsername: fetchingAccount.Username,
|
||||
RemoteAccountID: groupURL,
|
||||
})
|
||||
|
|
@ -62,7 +62,7 @@ func (suite *AccountTestSuite) TestDereferenceService() {
|
|||
fetchingAccount := suite.testAccounts["local_account_1"]
|
||||
|
||||
serviceURL := testrig.URLMustParse("https://owncast.example.org/federation/user/rgh")
|
||||
service, err := suite.dereferencer.GetRemoteAccount(context.Background(), dereferencing.GetRemoteAccountParams{
|
||||
service, err := suite.dereferencer.GetAccount(context.Background(), dereferencing.GetAccountParams{
|
||||
RequestingUsername: fetchingAccount.Username,
|
||||
RemoteAccountID: serviceURL,
|
||||
})
|
||||
|
|
@ -93,7 +93,7 @@ func (suite *AccountTestSuite) TestDereferenceLocalAccountAsRemoteURL() {
|
|||
fetchingAccount := suite.testAccounts["local_account_1"]
|
||||
targetAccount := suite.testAccounts["local_account_2"]
|
||||
|
||||
fetchedAccount, err := suite.dereferencer.GetRemoteAccount(context.Background(), dereferencing.GetRemoteAccountParams{
|
||||
fetchedAccount, err := suite.dereferencer.GetAccount(context.Background(), dereferencing.GetAccountParams{
|
||||
RequestingUsername: fetchingAccount.Username,
|
||||
RemoteAccountID: testrig.URLMustParse(targetAccount.URI),
|
||||
})
|
||||
|
|
@ -111,7 +111,7 @@ func (suite *AccountTestSuite) TestDereferenceLocalAccountAsRemoteURLNoSharedInb
|
|||
suite.FailNow(err.Error())
|
||||
}
|
||||
|
||||
fetchedAccount, err := suite.dereferencer.GetRemoteAccount(context.Background(), dereferencing.GetRemoteAccountParams{
|
||||
fetchedAccount, err := suite.dereferencer.GetAccount(context.Background(), dereferencing.GetAccountParams{
|
||||
RequestingUsername: fetchingAccount.Username,
|
||||
RemoteAccountID: testrig.URLMustParse(targetAccount.URI),
|
||||
})
|
||||
|
|
@ -124,7 +124,7 @@ func (suite *AccountTestSuite) TestDereferenceLocalAccountAsUsername() {
|
|||
fetchingAccount := suite.testAccounts["local_account_1"]
|
||||
targetAccount := suite.testAccounts["local_account_2"]
|
||||
|
||||
fetchedAccount, err := suite.dereferencer.GetRemoteAccount(context.Background(), dereferencing.GetRemoteAccountParams{
|
||||
fetchedAccount, err := suite.dereferencer.GetAccount(context.Background(), dereferencing.GetAccountParams{
|
||||
RequestingUsername: fetchingAccount.Username,
|
||||
RemoteAccountUsername: targetAccount.Username,
|
||||
})
|
||||
|
|
@ -137,7 +137,7 @@ func (suite *AccountTestSuite) TestDereferenceLocalAccountAsUsernameDomain() {
|
|||
fetchingAccount := suite.testAccounts["local_account_1"]
|
||||
targetAccount := suite.testAccounts["local_account_2"]
|
||||
|
||||
fetchedAccount, err := suite.dereferencer.GetRemoteAccount(context.Background(), dereferencing.GetRemoteAccountParams{
|
||||
fetchedAccount, err := suite.dereferencer.GetAccount(context.Background(), dereferencing.GetAccountParams{
|
||||
RequestingUsername: fetchingAccount.Username,
|
||||
RemoteAccountUsername: targetAccount.Username,
|
||||
RemoteAccountHost: config.GetHost(),
|
||||
|
|
@ -151,7 +151,7 @@ func (suite *AccountTestSuite) TestDereferenceLocalAccountAsUsernameDomainAndURL
|
|||
fetchingAccount := suite.testAccounts["local_account_1"]
|
||||
targetAccount := suite.testAccounts["local_account_2"]
|
||||
|
||||
fetchedAccount, err := suite.dereferencer.GetRemoteAccount(context.Background(), dereferencing.GetRemoteAccountParams{
|
||||
fetchedAccount, err := suite.dereferencer.GetAccount(context.Background(), dereferencing.GetAccountParams{
|
||||
RequestingUsername: fetchingAccount.Username,
|
||||
RemoteAccountID: testrig.URLMustParse(targetAccount.URI),
|
||||
RemoteAccountUsername: targetAccount.Username,
|
||||
|
|
@ -165,34 +165,40 @@ func (suite *AccountTestSuite) TestDereferenceLocalAccountAsUsernameDomainAndURL
|
|||
func (suite *AccountTestSuite) TestDereferenceLocalAccountWithUnknownUsername() {
|
||||
fetchingAccount := suite.testAccounts["local_account_1"]
|
||||
|
||||
fetchedAccount, err := suite.dereferencer.GetRemoteAccount(context.Background(), dereferencing.GetRemoteAccountParams{
|
||||
fetchedAccount, err := suite.dereferencer.GetAccount(context.Background(), dereferencing.GetAccountParams{
|
||||
RequestingUsername: fetchingAccount.Username,
|
||||
RemoteAccountUsername: "thisaccountdoesnotexist",
|
||||
})
|
||||
suite.EqualError(err, "GetRemoteAccount: couldn't retrieve account locally and won't try to resolve it")
|
||||
var errNotRetrievable *dereferencing.ErrNotRetrievable
|
||||
suite.ErrorAs(err, &errNotRetrievable)
|
||||
suite.EqualError(err, "item could not be retrieved: GetRemoteAccount: couldn't retrieve account locally and not allowed to resolve it")
|
||||
suite.Nil(fetchedAccount)
|
||||
}
|
||||
|
||||
func (suite *AccountTestSuite) TestDereferenceLocalAccountWithUnknownUsernameDomain() {
|
||||
fetchingAccount := suite.testAccounts["local_account_1"]
|
||||
|
||||
fetchedAccount, err := suite.dereferencer.GetRemoteAccount(context.Background(), dereferencing.GetRemoteAccountParams{
|
||||
fetchedAccount, err := suite.dereferencer.GetAccount(context.Background(), dereferencing.GetAccountParams{
|
||||
RequestingUsername: fetchingAccount.Username,
|
||||
RemoteAccountUsername: "thisaccountdoesnotexist",
|
||||
RemoteAccountHost: "localhost:8080",
|
||||
})
|
||||
suite.EqualError(err, "GetRemoteAccount: couldn't retrieve account locally and won't try to resolve it")
|
||||
var errNotRetrievable *dereferencing.ErrNotRetrievable
|
||||
suite.ErrorAs(err, &errNotRetrievable)
|
||||
suite.EqualError(err, "item could not be retrieved: GetRemoteAccount: couldn't retrieve account locally and not allowed to resolve it")
|
||||
suite.Nil(fetchedAccount)
|
||||
}
|
||||
|
||||
func (suite *AccountTestSuite) TestDereferenceLocalAccountWithUnknownUserURI() {
|
||||
fetchingAccount := suite.testAccounts["local_account_1"]
|
||||
|
||||
fetchedAccount, err := suite.dereferencer.GetRemoteAccount(context.Background(), dereferencing.GetRemoteAccountParams{
|
||||
fetchedAccount, err := suite.dereferencer.GetAccount(context.Background(), dereferencing.GetAccountParams{
|
||||
RequestingUsername: fetchingAccount.Username,
|
||||
RemoteAccountID: testrig.URLMustParse("http://localhost:8080/users/thisaccountdoesnotexist"),
|
||||
})
|
||||
suite.EqualError(err, "GetRemoteAccount: couldn't retrieve account locally and won't try to resolve it")
|
||||
var errNotRetrievable *dereferencing.ErrNotRetrievable
|
||||
suite.ErrorAs(err, &errNotRetrievable)
|
||||
suite.EqualError(err, "item could not be retrieved: GetRemoteAccount: couldn't retrieve account locally and not allowed to resolve it")
|
||||
suite.Nil(fetchedAccount)
|
||||
}
|
||||
|
||||
|
|
@ -233,7 +239,7 @@ func (suite *AccountTestSuite) TestDereferenceRemoteAccountWithPartial() {
|
|||
},
|
||||
}
|
||||
|
||||
fetchedAccount, err := suite.dereferencer.GetRemoteAccount(context.Background(), dereferencing.GetRemoteAccountParams{
|
||||
fetchedAccount, err := suite.dereferencer.GetAccount(context.Background(), dereferencing.GetAccountParams{
|
||||
RequestingUsername: fetchingAccount.Username,
|
||||
RemoteAccountID: testrig.URLMustParse(remoteAccount.URI),
|
||||
RemoteAccountHost: remoteAccount.Domain,
|
||||
|
|
@ -286,7 +292,7 @@ func (suite *AccountTestSuite) TestDereferenceRemoteAccountWithPartial2() {
|
|||
},
|
||||
}
|
||||
|
||||
fetchedAccount, err := suite.dereferencer.GetRemoteAccount(context.Background(), dereferencing.GetRemoteAccountParams{
|
||||
fetchedAccount, err := suite.dereferencer.GetAccount(context.Background(), dereferencing.GetAccountParams{
|
||||
RequestingUsername: fetchingAccount.Username,
|
||||
RemoteAccountID: testrig.URLMustParse(remoteAccount.URI),
|
||||
RemoteAccountHost: remoteAccount.Domain,
|
||||
|
|
@ -339,7 +345,7 @@ func (suite *AccountTestSuite) TestDereferenceRemoteAccountWithPartial3() {
|
|||
},
|
||||
}
|
||||
|
||||
fetchedAccount, err := suite.dereferencer.GetRemoteAccount(context.Background(), dereferencing.GetRemoteAccountParams{
|
||||
fetchedAccount, err := suite.dereferencer.GetAccount(context.Background(), dereferencing.GetAccountParams{
|
||||
RequestingUsername: fetchingAccount.Username,
|
||||
RemoteAccountID: testrig.URLMustParse(remoteAccount.URI),
|
||||
RemoteAccountHost: remoteAccount.Domain,
|
||||
|
|
@ -386,7 +392,7 @@ func (suite *AccountTestSuite) TestDereferenceRemoteAccountWithPartial3() {
|
|||
},
|
||||
}
|
||||
|
||||
fetchedAccount2, err := suite.dereferencer.GetRemoteAccount(context.Background(), dereferencing.GetRemoteAccountParams{
|
||||
fetchedAccount2, err := suite.dereferencer.GetAccount(context.Background(), dereferencing.GetAccountParams{
|
||||
RequestingUsername: fetchingAccount.Username,
|
||||
RemoteAccountID: testrig.URLMustParse(remoteAccount.URI),
|
||||
RemoteAccountHost: remoteAccount.Domain,
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ func (d *deref) DereferenceAnnounce(ctx context.Context, announce *gtsmodel.Stat
|
|||
boostedStatus = status
|
||||
} else {
|
||||
// This is a boost of a remote status, we need to dereference it.
|
||||
status, statusable, err := d.GetRemoteStatus(ctx, requestingUsername, boostedURI, true, true)
|
||||
status, statusable, err := d.GetStatus(ctx, requestingUsername, boostedURI, true, true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("DereferenceAnnounce: error dereferencing remote status with id %s: %s", announce.BoostOf.URI, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,19 +33,17 @@ import (
|
|||
|
||||
// Dereferencer wraps logic and functionality for doing dereferencing of remote accounts, statuses, etc, from federated instances.
|
||||
type Dereferencer interface {
|
||||
GetRemoteAccount(ctx context.Context, params GetRemoteAccountParams) (*gtsmodel.Account, error)
|
||||
GetAccount(ctx context.Context, params GetAccountParams) (*gtsmodel.Account, error)
|
||||
GetStatus(ctx context.Context, username string, remoteStatusID *url.URL, refetch, includeParent bool) (*gtsmodel.Status, ap.Statusable, error)
|
||||
|
||||
GetRemoteStatus(ctx context.Context, username string, remoteStatusID *url.URL, refetch, includeParent bool) (*gtsmodel.Status, ap.Statusable, error)
|
||||
EnrichRemoteStatus(ctx context.Context, username string, status *gtsmodel.Status, includeParent bool) (*gtsmodel.Status, error)
|
||||
|
||||
GetRemoteInstance(ctx context.Context, username string, remoteInstanceURI *url.URL) (*gtsmodel.Instance, error)
|
||||
DereferenceAnnounce(ctx context.Context, announce *gtsmodel.Status, requestingUsername string) error
|
||||
DereferenceThread(ctx context.Context, username string, statusIRI *url.URL, status *gtsmodel.Status, statusable ap.Statusable)
|
||||
|
||||
GetRemoteMedia(ctx context.Context, requestingUsername string, accountID string, remoteURL string, ai *media.AdditionalMediaInfo) (*media.ProcessingMedia, error)
|
||||
GetRemoteEmoji(ctx context.Context, requestingUsername string, remoteURL string, shortcode string, domain string, id string, emojiURI string, ai *media.AdditionalEmojiInfo, refresh bool) (*media.ProcessingEmoji, error)
|
||||
|
||||
DereferenceAnnounce(ctx context.Context, announce *gtsmodel.Status, requestingUsername string) error
|
||||
DereferenceThread(ctx context.Context, username string, statusIRI *url.URL, status *gtsmodel.Status, statusable ap.Statusable)
|
||||
|
||||
Handshaking(ctx context.Context, username string, remoteAccountID *url.URL) bool
|
||||
}
|
||||
|
||||
|
|
|
|||
132
internal/federation/dereferencing/error.go
Normal file
132
internal/federation/dereferencing/error.go
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
/*
|
||||
GoToSocial
|
||||
Copyright (C) 2021-2022 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 dereferencing
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/superseriousbusiness/gotosocial/internal/transport"
|
||||
)
|
||||
|
||||
// ErrDB denotes that a proper error has occurred when doing
|
||||
// a database call, as opposed to a simple db.ErrNoEntries.
|
||||
type ErrDB struct {
|
||||
wrapped error
|
||||
}
|
||||
|
||||
func (err *ErrDB) Error() string {
|
||||
return fmt.Sprintf("database error during dereferencing: %v", err.wrapped)
|
||||
}
|
||||
|
||||
func newErrDB(err error) error {
|
||||
return &ErrDB{wrapped: err}
|
||||
}
|
||||
|
||||
// ErrNotRetrievable denotes that an item could not be dereferenced
|
||||
// with the given parameters.
|
||||
type ErrNotRetrievable struct {
|
||||
wrapped error
|
||||
}
|
||||
|
||||
func (err *ErrNotRetrievable) Error() string {
|
||||
return fmt.Sprintf("item could not be retrieved: %v", err.wrapped)
|
||||
}
|
||||
|
||||
func newErrNotRetrievable(err error) error {
|
||||
return &ErrNotRetrievable{wrapped: err}
|
||||
}
|
||||
|
||||
// ErrBadRequest denotes that insufficient or improperly formed parameters
|
||||
// were passed into one of the dereference functions.
|
||||
type ErrBadRequest struct {
|
||||
wrapped error
|
||||
}
|
||||
|
||||
func (err *ErrBadRequest) Error() string {
|
||||
return fmt.Sprintf("bad request: %v", err.wrapped)
|
||||
}
|
||||
|
||||
func newErrBadRequest(err error) error {
|
||||
return &ErrBadRequest{wrapped: err}
|
||||
}
|
||||
|
||||
// ErrTransportError indicates that something unforeseen went wrong creating
|
||||
// a transport, or while making an http call to a remote resource with a transport.
|
||||
type ErrTransportError struct {
|
||||
wrapped error
|
||||
}
|
||||
|
||||
func (err *ErrTransportError) Error() string {
|
||||
return fmt.Sprintf("transport error: %v", err.wrapped)
|
||||
}
|
||||
|
||||
func newErrTransportError(err error) error {
|
||||
return &ErrTransportError{wrapped: err}
|
||||
}
|
||||
|
||||
// ErrWrongType indicates that an unexpected type was returned from a remote call;
|
||||
// for example, we were served a Person when we were looking for a statusable.
|
||||
type ErrWrongType struct {
|
||||
wrapped error
|
||||
}
|
||||
|
||||
func (err *ErrWrongType) Error() string {
|
||||
return fmt.Sprintf("wrong received type: %v", err.wrapped)
|
||||
}
|
||||
|
||||
func newErrWrongType(err error) error {
|
||||
return &ErrWrongType{wrapped: err}
|
||||
}
|
||||
|
||||
// ErrOther denotes some other kind of weird error, perhaps from a malformed json
|
||||
// or some other weird crapola.
|
||||
type ErrOther struct {
|
||||
wrapped error
|
||||
}
|
||||
|
||||
func (err *ErrOther) Error() string {
|
||||
return fmt.Sprintf("unexpected error: %v", err.wrapped)
|
||||
}
|
||||
|
||||
func newErrOther(err error) error {
|
||||
return &ErrOther{wrapped: err}
|
||||
}
|
||||
|
||||
func wrapDerefError(derefErr error, fluff string) error {
|
||||
var (
|
||||
err error
|
||||
errWrongType *ErrWrongType
|
||||
)
|
||||
|
||||
if fluff != "" {
|
||||
err = fmt.Errorf("%s: %w", fluff, derefErr)
|
||||
}
|
||||
|
||||
switch {
|
||||
case errors.Is(derefErr, transport.ErrGone):
|
||||
err = newErrNotRetrievable(err)
|
||||
case errors.As(derefErr, &errWrongType):
|
||||
err = newErrWrongType(err)
|
||||
default:
|
||||
err = newErrTransportError(err)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
|
@ -29,6 +29,7 @@ import (
|
|||
"github.com/superseriousbusiness/activity/streams"
|
||||
"github.com/superseriousbusiness/activity/streams/vocab"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/config"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/id"
|
||||
|
|
@ -36,7 +37,7 @@ import (
|
|||
"github.com/superseriousbusiness/gotosocial/internal/media"
|
||||
)
|
||||
|
||||
// EnrichRemoteStatus takes a status that's already been inserted into the database in a minimal form,
|
||||
// EnrichRemoteStatus takes a remote status that's already been inserted into the database in a minimal form,
|
||||
// and populates it with additional fields, media, etc.
|
||||
//
|
||||
// EnrichRemoteStatus is mostly useful for calling after a status has been initially created by
|
||||
|
|
@ -51,7 +52,7 @@ func (d *deref) EnrichRemoteStatus(ctx context.Context, username string, status
|
|||
return status, nil
|
||||
}
|
||||
|
||||
// GetRemoteStatus completely dereferences a remote status, converts it to a GtS model status,
|
||||
// GetStatus completely dereferences a status, converts it to a GtS model status,
|
||||
// puts it in the database, and returns it to a caller.
|
||||
//
|
||||
// If refetch is true, then regardless of whether we have the original status in the database or not,
|
||||
|
|
@ -60,58 +61,96 @@ func (d *deref) EnrichRemoteStatus(ctx context.Context, username string, status
|
|||
// If refetch is false, the ap.Statusable will only be returned if this is a new status, so callers
|
||||
// should check whether or not this is nil.
|
||||
//
|
||||
// SIDE EFFECTS: remote status will be stored in the database, and the remote status owner will also be stored.
|
||||
func (d *deref) GetRemoteStatus(ctx context.Context, username string, remoteStatusID *url.URL, refetch, includeParent bool) (*gtsmodel.Status, ap.Statusable, error) {
|
||||
maybeStatus, err := d.db.GetStatusByURI(ctx, remoteStatusID.String())
|
||||
if err == nil && !refetch {
|
||||
// GetAccount will guard against trying to do http calls to fetch a status that belongs to this instance.
|
||||
// Instead of making calls, it will just return the status early if it finds it, or return an error.
|
||||
func (d *deref) GetStatus(ctx context.Context, username string, statusURI *url.URL, refetch, includeParent bool) (*gtsmodel.Status, ap.Statusable, error) {
|
||||
uriString := statusURI.String()
|
||||
|
||||
// try to get by URI first
|
||||
status, dbErr := d.db.GetStatusByURI(ctx, uriString)
|
||||
if dbErr != nil {
|
||||
if !errors.Is(dbErr, db.ErrNoEntries) {
|
||||
// real error
|
||||
return nil, nil, newErrDB(fmt.Errorf("GetRemoteStatus: error during GetStatusByURI for %s: %w", uriString, dbErr))
|
||||
}
|
||||
// no problem, just press on
|
||||
} else if !refetch {
|
||||
// we already had the status and we aren't being asked to refetch the AP representation
|
||||
return maybeStatus, nil, nil
|
||||
return status, nil, nil
|
||||
}
|
||||
|
||||
statusable, err := d.dereferenceStatusable(ctx, username, remoteStatusID)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("GetRemoteStatus: error dereferencing statusable: %s", err)
|
||||
// try to get by URL if we couldn't get by URI now
|
||||
if status == nil {
|
||||
status, dbErr = d.db.GetStatusByURL(ctx, uriString)
|
||||
if dbErr != nil {
|
||||
if !errors.Is(dbErr, db.ErrNoEntries) {
|
||||
// real error
|
||||
return nil, nil, newErrDB(fmt.Errorf("GetRemoteStatus: error during GetStatusByURI for %s: %w", uriString, dbErr))
|
||||
}
|
||||
// no problem, just press on
|
||||
} else if !refetch {
|
||||
// we already had the status and we aren't being asked to refetch the AP representation
|
||||
return status, nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
if maybeStatus != nil && refetch {
|
||||
// we already had the status and we've successfully fetched the AP representation as requested
|
||||
return maybeStatus, statusable, nil
|
||||
// guard against having our own statuses passed in
|
||||
if host := statusURI.Host; host == config.GetHost() || host == config.GetAccountDomain() {
|
||||
// this is our status, definitely don't search for it
|
||||
if status != nil {
|
||||
return status, nil, nil
|
||||
}
|
||||
return nil, nil, newErrNotRetrievable(fmt.Errorf("GetRemoteStatus: uri %s is apparently ours, but we have nothing in the db for it, will not proceed to dereference our own status", uriString))
|
||||
}
|
||||
|
||||
// if we got here, either we didn't have the status
|
||||
// in the db, or we had it but need to refetch it
|
||||
statusable, derefErr := d.dereferenceStatusable(ctx, username, statusURI)
|
||||
if derefErr != nil {
|
||||
return nil, nil, wrapDerefError(derefErr, "GetRemoteStatus: error dereferencing statusable")
|
||||
}
|
||||
|
||||
if status != nil && refetch {
|
||||
// we already had the status in the db, and we've also
|
||||
// now fetched the AP representation as requested
|
||||
return status, statusable, nil
|
||||
}
|
||||
|
||||
// from here on out we can consider this to be a 'new' status because we didn't have the status in the db already
|
||||
accountURI, err := ap.ExtractAttributedTo(statusable)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("GetRemoteStatus: error extracting attributedTo: %s", err)
|
||||
return nil, nil, newErrOther(fmt.Errorf("GetRemoteStatus: error extracting attributedTo: %w", err))
|
||||
}
|
||||
|
||||
_, err = d.GetRemoteAccount(ctx, GetRemoteAccountParams{
|
||||
// we need to get the author of the status else we can't serialize it properly
|
||||
if _, err = d.GetAccount(ctx, GetAccountParams{
|
||||
RequestingUsername: username,
|
||||
RemoteAccountID: accountURI,
|
||||
})
|
||||
Blocking: true,
|
||||
}); err != nil {
|
||||
return nil, nil, newErrOther(fmt.Errorf("GetRemoteStatus: couldn't get status author: %s", err))
|
||||
}
|
||||
|
||||
status, err = d.typeConverter.ASStatusToStatus(ctx, statusable)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("GetRemoteStatus: couldn't get status author: %s", err)
|
||||
return nil, nil, newErrOther(fmt.Errorf("GetRemoteStatus: error converting statusable to status: %s", err))
|
||||
}
|
||||
|
||||
gtsStatus, err := d.typeConverter.ASStatusToStatus(ctx, statusable)
|
||||
ulid, err := id.NewULIDFromTime(status.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, statusable, fmt.Errorf("GetRemoteStatus: error converting statusable to status: %s", err)
|
||||
return nil, nil, newErrOther(fmt.Errorf("GetRemoteStatus: error generating new id for status: %s", err))
|
||||
}
|
||||
status.ID = ulid
|
||||
|
||||
if err := d.populateStatusFields(ctx, status, username, includeParent); err != nil {
|
||||
return nil, nil, newErrOther(fmt.Errorf("GetRemoteStatus: error populating status fields: %s", err))
|
||||
}
|
||||
|
||||
ulid, err := id.NewULIDFromTime(gtsStatus.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("GetRemoteStatus: error generating new id for status: %s", err)
|
||||
}
|
||||
gtsStatus.ID = ulid
|
||||
|
||||
if err := d.populateStatusFields(ctx, gtsStatus, username, includeParent); err != nil {
|
||||
return nil, nil, fmt.Errorf("GetRemoteStatus: error populating status fields: %s", err)
|
||||
if err := d.db.PutStatus(ctx, status); err != nil && !errors.Is(err, db.ErrAlreadyExists) {
|
||||
return nil, nil, newErrDB(fmt.Errorf("GetRemoteStatus: error putting new status: %s", err))
|
||||
}
|
||||
|
||||
if err := d.db.PutStatus(ctx, gtsStatus); err != nil && !errors.Is(err, db.ErrAlreadyExists) {
|
||||
return nil, nil, fmt.Errorf("GetRemoteStatus: error putting new status: %s", err)
|
||||
}
|
||||
|
||||
return gtsStatus, statusable, nil
|
||||
return status, statusable, nil
|
||||
}
|
||||
|
||||
func (d *deref) dereferenceStatusable(ctx context.Context, username string, remoteStatusID *url.URL) (ap.Statusable, error) {
|
||||
|
|
@ -197,7 +236,7 @@ func (d *deref) dereferenceStatusable(ctx context.Context, username string, remo
|
|||
return p, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("DereferenceStatusable: type name %s not supported", t.GetTypeName())
|
||||
return nil, newErrWrongType(fmt.Errorf("DereferenceStatusable: type name %s not supported as Statusable", t.GetTypeName()))
|
||||
}
|
||||
|
||||
// populateStatusFields fetches all the information we temporarily pinned to an incoming
|
||||
|
|
@ -314,7 +353,7 @@ func (d *deref) populateStatusMentions(ctx context.Context, status *gtsmodel.Sta
|
|||
if targetAccount == nil {
|
||||
// we didn't find the account in our database already
|
||||
// check if we can get the account remotely (dereference it)
|
||||
if a, err := d.GetRemoteAccount(ctx, GetRemoteAccountParams{
|
||||
if a, err := d.GetAccount(ctx, GetAccountParams{
|
||||
RequestingUsername: requestingUsername,
|
||||
RemoteAccountID: targetAccountURI,
|
||||
}); err != nil {
|
||||
|
|
@ -430,7 +469,7 @@ func (d *deref) populateStatusRepliedTo(ctx context.Context, status *gtsmodel.St
|
|||
return err
|
||||
}
|
||||
|
||||
replyToStatus, _, err := d.GetRemoteStatus(ctx, requestingUsername, statusURI, false, false)
|
||||
replyToStatus, _, err := d.GetStatus(ctx, requestingUsername, statusURI, false, false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("populateStatusRepliedTo: couldn't get reply to status with uri %s: %s", status.InReplyToURI, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ func (suite *StatusTestSuite) TestDereferenceSimpleStatus() {
|
|||
fetchingAccount := suite.testAccounts["local_account_1"]
|
||||
|
||||
statusURL := testrig.URLMustParse("https://unknown-instance.com/users/brand_new_person/statuses/01FE4NTHKWW7THT67EF10EB839")
|
||||
status, _, err := suite.dereferencer.GetRemoteStatus(context.Background(), fetchingAccount.Username, statusURL, false, false)
|
||||
status, _, err := suite.dereferencer.GetStatus(context.Background(), fetchingAccount.Username, statusURL, false, false)
|
||||
suite.NoError(err)
|
||||
suite.NotNil(status)
|
||||
|
||||
|
|
@ -77,7 +77,7 @@ func (suite *StatusTestSuite) TestDereferenceStatusWithMention() {
|
|||
fetchingAccount := suite.testAccounts["local_account_1"]
|
||||
|
||||
statusURL := testrig.URLMustParse("https://unknown-instance.com/users/brand_new_person/statuses/01FE5Y30E3W4P7TRE0R98KAYQV")
|
||||
status, _, err := suite.dereferencer.GetRemoteStatus(context.Background(), fetchingAccount.Username, statusURL, false, false)
|
||||
status, _, err := suite.dereferencer.GetStatus(context.Background(), fetchingAccount.Username, statusURL, false, false)
|
||||
suite.NoError(err)
|
||||
suite.NotNil(status)
|
||||
|
||||
|
|
@ -128,7 +128,7 @@ func (suite *StatusTestSuite) TestDereferenceStatusWithImageAndNoContent() {
|
|||
fetchingAccount := suite.testAccounts["local_account_1"]
|
||||
|
||||
statusURL := testrig.URLMustParse("https://turnip.farm/users/turniplover6969/statuses/70c53e54-3146-42d5-a630-83c8b6c7c042")
|
||||
status, _, err := suite.dereferencer.GetRemoteStatus(context.Background(), fetchingAccount.Username, statusURL, false, false)
|
||||
status, _, err := suite.dereferencer.GetStatus(context.Background(), fetchingAccount.Username, statusURL, false, false)
|
||||
suite.NoError(err)
|
||||
suite.NotNil(status)
|
||||
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@ func (d *deref) dereferenceStatusAncestors(ctx context.Context, username string,
|
|||
l.Tracef("following remote status ancestors: %s", status.InReplyToURI)
|
||||
|
||||
// Fetch the remote status found at this IRI
|
||||
remoteStatus, _, err := d.GetRemoteStatus(ctx, username, replyIRI, false, false)
|
||||
remoteStatus, _, err := d.GetStatus(ctx, username, replyIRI, false, false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error fetching remote status %q: %w", status.InReplyToURI, err)
|
||||
}
|
||||
|
|
@ -276,7 +276,7 @@ stackLoop:
|
|||
}
|
||||
|
||||
// Dereference the remote status and store in the database
|
||||
_, statusable, err := d.GetRemoteStatus(ctx, username, itemIRI, true, false)
|
||||
_, statusable, err := d.GetStatus(ctx, username, itemIRI, true, false)
|
||||
if err != nil {
|
||||
l.Errorf("error dereferencing remote status %q: %s", itemIRI.String(), err)
|
||||
continue itemLoop
|
||||
|
|
|
|||
|
|
@ -206,7 +206,7 @@ func (f *federator) AuthenticatePostInbox(ctx context.Context, w http.ResponseWr
|
|||
}
|
||||
}
|
||||
|
||||
requestingAccount, err := f.GetRemoteAccount(transport.WithFastfail(ctx), dereferencing.GetRemoteAccountParams{
|
||||
requestingAccount, err := f.GetAccount(transport.WithFastfail(ctx), dereferencing.GetAccountParams{
|
||||
RequestingUsername: username,
|
||||
RemoteAccountID: publicKeyOwnerURI,
|
||||
})
|
||||
|
|
|
|||
|
|
@ -53,14 +53,14 @@ type Federator interface {
|
|||
// If something goes wrong during authentication, nil, false, and an error will be returned.
|
||||
AuthenticateFederatedRequest(ctx context.Context, username string) (*url.URL, gtserror.WithCode)
|
||||
|
||||
/*
|
||||
dereferencing functions
|
||||
*/
|
||||
DereferenceRemoteThread(ctx context.Context, username string, statusURI *url.URL, status *gtsmodel.Status, statusable ap.Statusable)
|
||||
DereferenceAnnounce(ctx context.Context, announce *gtsmodel.Status, requestingUsername string) error
|
||||
|
||||
GetRemoteAccount(ctx context.Context, params dereferencing.GetRemoteAccountParams) (*gtsmodel.Account, error)
|
||||
|
||||
GetRemoteStatus(ctx context.Context, username string, remoteStatusID *url.URL, refetch, includeParent bool) (*gtsmodel.Status, ap.Statusable, error)
|
||||
GetAccount(ctx context.Context, params dereferencing.GetAccountParams) (*gtsmodel.Account, error)
|
||||
GetStatus(ctx context.Context, username string, remoteStatusID *url.URL, refetch, includeParent bool) (*gtsmodel.Status, ap.Statusable, error)
|
||||
EnrichRemoteStatus(ctx context.Context, username string, status *gtsmodel.Status, includeParent bool) (*gtsmodel.Status, error)
|
||||
|
||||
GetRemoteInstance(ctx context.Context, username string, remoteInstanceURI *url.URL) (*gtsmodel.Instance, error)
|
||||
|
||||
// Handshaking returns true if the given username is currently in the process of dereferencing the remoteAccountID.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue