diff options
author | 2023-08-02 09:31:09 +0200 | |
---|---|---|
committer | 2023-08-02 08:31:09 +0100 | |
commit | cec29e2a8d2d6ca49bb6c789f0ed3226849a7359 (patch) | |
tree | 042ba1ddd9d42552403b3a00e224e45e498b962a /internal/processing/search | |
parent | [feature] Allow users to skip http client tls verification for testing purpos... (diff) | |
download | gotosocial-cec29e2a8d2d6ca49bb6c789f0ed3226849a7359.tar.xz |
[bugfix] Allow instance accounts to be shown in search results in certain circumstances (#2053)
Diffstat (limited to 'internal/processing/search')
-rw-r--r-- | internal/processing/search/accounts.go | 21 | ||||
-rw-r--r-- | internal/processing/search/get.go | 26 | ||||
-rw-r--r-- | internal/processing/search/lookup.go | 14 | ||||
-rw-r--r-- | internal/processing/search/util.go | 6 |
4 files changed, 61 insertions, 6 deletions
diff --git a/internal/processing/search/accounts.go b/internal/processing/search/accounts.go index eb88647a3..cfcc65b2b 100644 --- a/internal/processing/search/accounts.go +++ b/internal/processing/search/accounts.go @@ -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, + ) } diff --git a/internal/processing/search/get.go b/internal/processing/search/get.go index 8e1881ab1..830e3481b 100644 --- a/internal/processing/search/get.go +++ b/internal/processing/search/get.go @@ -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, ) } diff --git a/internal/processing/search/lookup.go b/internal/processing/search/lookup.go index d50183221..8b48d4514 100644 --- a/internal/processing/search/lookup.go +++ b/internal/processing/search/lookup.go @@ -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 } diff --git a/internal/processing/search/util.go b/internal/processing/search/util.go index 171d0e570..c0eac0ca3 100644 --- a/internal/processing/search/util.go +++ b/internal/processing/search/util.go @@ -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 } |