[bugfix] Allow instance accounts to be shown in search results in certain circumstances (#2053)

This commit is contained in:
tobi
2023-08-02 09:31:09 +02:00
committed by GitHub
parent 2be83fdca5
commit cec29e2a8d
6 changed files with 203 additions and 15 deletions

View File

@@ -49,6 +49,13 @@ func (p *Processor) Accounts(
resolve bool,
following bool,
) ([]*apimodel.Account, gtserror.WithCode) {
// Don't include instance accounts in this search.
//
// We don't want someone to start typing '@mastodon'
// and then get a million instance service accounts
// in their search results.
const includeInstanceAccounts = false
var (
foundAccounts = make([]*gtsmodel.Account, 0, limit)
appendAccount = func(foundAccount *gtsmodel.Account) { foundAccounts = append(foundAccounts, foundAccount) }
@@ -83,7 +90,12 @@ func (p *Processor) Accounts(
// if caller supplied an offset greater than 0, return
// nothing as though there were no additional results.
if offset > 0 {
return p.packageAccounts(ctx, requestingAccount, foundAccounts)
return p.packageAccounts(
ctx,
requestingAccount,
foundAccounts,
includeInstanceAccounts,
)
}
// Return all accounts we can find that match the
@@ -106,5 +118,10 @@ func (p *Processor) Accounts(
}
// Return whatever we got (if anything).
return p.packageAccounts(ctx, requestingAccount, foundAccounts)
return p.packageAccounts(
ctx,
requestingAccount,
foundAccounts,
includeInstanceAccounts,
)
}

View File

@@ -70,6 +70,13 @@ func (p *Processor) Get(
queryType = strings.TrimSpace(strings.ToLower(req.QueryType)) // Trim trailing/leading whitespace; convert to lowercase.
resolve = req.Resolve
following = req.Following
// Include instance accounts in the first
// parts of this search. This will be
// changed to 'false' when doing text
// search in the database in the latter
// parts of this function.
includeInstanceAccounts = true
)
// Validate query.
@@ -109,7 +116,12 @@ func (p *Processor) Get(
// supply an offset greater than 0, return nothing as
// though there were no additional results.
if req.Offset > 0 {
return p.packageSearchResult(ctx, account, nil, nil, nil, req.APIv1)
return p.packageSearchResult(
ctx,
account,
nil, nil, nil, // No results.
req.APIv1, includeInstanceAccounts,
)
}
var (
@@ -167,6 +179,7 @@ func (p *Processor) Get(
foundStatuses,
foundTags,
req.APIv1,
includeInstanceAccounts,
)
}
}
@@ -196,6 +209,7 @@ func (p *Processor) Get(
foundStatuses,
foundTags,
req.APIv1,
includeInstanceAccounts,
)
}
@@ -236,11 +250,20 @@ func (p *Processor) Get(
foundStatuses,
foundTags,
req.APIv1,
includeInstanceAccounts,
)
}
// As a last resort, search for accounts and
// statuses using the query as arbitrary text.
//
// At this point we no longer want to include
// instance accounts in the results, since searching
// for something like 'mastodon', for example, will
// include a million instance/service accounts that
// have 'mastodon' in the domain, and therefore in
// the username, making the search results useless.
includeInstanceAccounts = false
if err := p.byText(
ctx,
account,
@@ -267,6 +290,7 @@ func (p *Processor) Get(
foundStatuses,
foundTags,
req.APIv1,
includeInstanceAccounts,
)
}

View File

@@ -44,6 +44,13 @@ func (p *Processor) Lookup(
requestingAccount *gtsmodel.Account,
query string,
) (*apimodel.Account, gtserror.WithCode) {
// Include instance accounts in this search.
//
// Lookup is for one specific account so we
// can't return loads of instance accounts by
// accident.
const includeInstanceAccounts = true
// Validate query.
query = strings.TrimSpace(query)
if query == "" {
@@ -96,7 +103,12 @@ func (p *Processor) Lookup(
// using the packageAccounts function to return it. This
// may cause the account to be filtered out if it's not
// visible to the caller, so anticipate this.
accounts, errWithCode := p.packageAccounts(ctx, requestingAccount, []*gtsmodel.Account{account})
accounts, errWithCode := p.packageAccounts(
ctx,
requestingAccount,
[]*gtsmodel.Account{account},
includeInstanceAccounts,
)
if errWithCode != nil {
return nil, errWithCode
}

View File

@@ -48,11 +48,12 @@ func (p *Processor) packageAccounts(
ctx context.Context,
requestingAccount *gtsmodel.Account,
accounts []*gtsmodel.Account,
includeInstanceAccounts bool,
) ([]*apimodel.Account, gtserror.WithCode) {
apiAccounts := make([]*apimodel.Account, 0, len(accounts))
for _, account := range accounts {
if account.IsInstance() {
if !includeInstanceAccounts && account.IsInstance() {
// No need to show instance accounts.
continue
}
@@ -169,8 +170,9 @@ func (p *Processor) packageSearchResult(
statuses []*gtsmodel.Status,
tags []*gtsmodel.Tag,
v1 bool,
includeInstanceAccounts bool,
) (*apimodel.SearchResult, gtserror.WithCode) {
apiAccounts, errWithCode := p.packageAccounts(ctx, requestingAccount, accounts)
apiAccounts, errWithCode := p.packageAccounts(ctx, requestingAccount, accounts, includeInstanceAccounts)
if errWithCode != nil {
return nil, errWithCode
}