summaryrefslogtreecommitdiff
path: root/internal/processing/search.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/processing/search.go')
-rw-r--r--internal/processing/search.go94
1 files changed, 52 insertions, 42 deletions
diff --git a/internal/processing/search.go b/internal/processing/search.go
index 1b0813fd9..d4ff94a21 100644
--- a/internal/processing/search.go
+++ b/internal/processing/search.go
@@ -20,6 +20,7 @@ package processing
import (
"context"
+ "errors"
"fmt"
"net/url"
"strings"
@@ -28,53 +29,70 @@ import (
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"
"github.com/superseriousbusiness/gotosocial/internal/oauth"
"github.com/superseriousbusiness/gotosocial/internal/util"
)
-func (p *processor) SearchGet(ctx context.Context, authed *oauth.Auth, searchQuery *apimodel.SearchQuery) (*apimodel.SearchResult, gtserror.WithCode) {
+func (p *processor) SearchGet(ctx context.Context, authed *oauth.Auth, search *apimodel.SearchQuery) (*apimodel.SearchResult, gtserror.WithCode) {
l := logrus.WithFields(logrus.Fields{
"func": "SearchGet",
- "query": searchQuery.Query,
+ "query": search.Query,
})
- results := &apimodel.SearchResult{
+ // tidy up the query and make sure it wasn't just spaces
+ query := strings.TrimSpace(search.Query)
+ if query == "" {
+ err := errors.New("search query was empty string after trimming space")
+ return nil, gtserror.NewErrorBadRequest(err, err.Error())
+ }
+
+ searchResult := &apimodel.SearchResult{
Accounts: []apimodel.Account{},
Statuses: []apimodel.Status{},
Hashtags: []apimodel.Tag{},
}
foundAccounts := []*gtsmodel.Account{}
foundStatuses := []*gtsmodel.Status{}
- // foundHashtags := []*gtsmodel.Tag{}
-
- // convert the query to lowercase and trim leading/trailing spaces
- query := strings.ToLower(strings.TrimSpace(searchQuery.Query))
var foundOne bool
- // check if the query is something like @whatever_username@example.org -- this means it's a remote account
- if _, domain, err := util.ExtractMentionParts(searchQuery.Query); err == nil && domain != "" {
- l.Debug("search term is a mention, looking it up...")
- foundAccount, err := p.searchAccountByMention(ctx, authed, searchQuery.Query, searchQuery.Resolve)
- if err == nil && foundAccount != nil {
+
+ /*
+ SEARCH BY MENTION
+ check if the query is something like @whatever_username@example.org -- this means it's a remote account
+ */
+ maybeNamestring := query
+ if maybeNamestring[0] != '@' {
+ maybeNamestring = "@" + maybeNamestring
+ }
+
+ if username, domain, err := util.ExtractNamestringParts(maybeNamestring); err == nil {
+ l.Debugf("search term %s is a mention, looking it up...", maybeNamestring)
+ if foundAccount, err := p.searchAccountByMention(ctx, authed, username, domain, search.Resolve); err == nil && foundAccount != nil {
foundAccounts = append(foundAccounts, foundAccount)
foundOne = true
l.Debug("got an account by searching by mention")
+ } else if err != nil {
+ l.Debugf("error looking up account %s: %s", maybeNamestring, err)
}
}
- // check if the query is a URI with a recognizable scheme and just do a lookup for that, straight up
+ /*
+ SEARCH BY URI
+ check if the query is a URI with a recognizable scheme and dereference it
+ */
if !foundOne {
if uri, err := url.Parse(query); err == nil && (uri.Scheme == "https" || uri.Scheme == "http") {
// 1. check if it's a status
- if foundStatus, err := p.searchStatusByURI(ctx, authed, uri, searchQuery.Resolve); err == nil && foundStatus != nil {
+ if foundStatus, err := p.searchStatusByURI(ctx, authed, uri, search.Resolve); err == nil && foundStatus != nil {
foundStatuses = append(foundStatuses, foundStatus)
l.Debug("got a status by searching by URI")
}
// 2. check if it's an account
- if foundAccount, err := p.searchAccountByURI(ctx, authed, uri, searchQuery.Resolve); err == nil && foundAccount != nil {
+ if foundAccount, err := p.searchAccountByURI(ctx, authed, uri, search.Resolve); err == nil && foundAccount != nil {
foundAccounts = append(foundAccounts, foundAccount)
l.Debug("got an account by searching by URI")
}
@@ -90,7 +108,7 @@ func (p *processor) SearchGet(ctx context.Context, authed *oauth.Auth, searchQue
if blocked, err := p.db.IsBlocked(ctx, authed.Account.ID, foundAccount.ID, true); err == nil && !blocked {
// all good, convert it and add it to the results
if apiAcct, err := p.tc.AccountToAPIAccountPublic(ctx, foundAccount); err == nil && apiAcct != nil {
- results.Accounts = append(results.Accounts, *apiAcct)
+ searchResult.Accounts = append(searchResult.Accounts, *apiAcct)
}
}
}
@@ -105,10 +123,10 @@ func (p *processor) SearchGet(ctx context.Context, authed *oauth.Auth, searchQue
continue
}
- results.Statuses = append(results.Statuses, *apiStatus)
+ searchResult.Statuses = append(searchResult.Statuses, *apiStatus)
}
- return results, nil
+ return searchResult, nil
}
func (p *processor) searchStatusByURI(ctx context.Context, authed *oauth.Auth, uri *url.URL, resolve bool) (*gtsmodel.Status, error) {
@@ -147,7 +165,10 @@ func (p *processor) searchAccountByURI(ctx context.Context, authed *oauth.Auth,
if resolve {
// we don't have it locally so try and dereference it
- account, err := p.federator.GetRemoteAccount(ctx, authed.Account.Username, uri, true, true)
+ account, err := p.federator.GetRemoteAccount(ctx, dereferencing.GetRemoteAccountParams{
+ RequestingUsername: authed.Account.Username,
+ RemoteAccountID: uri,
+ })
if err != nil {
return nil, fmt.Errorf("searchAccountByURI: error dereferencing account with uri %s: %s", uri.String(), err)
}
@@ -156,18 +177,14 @@ func (p *processor) searchAccountByURI(ctx context.Context, authed *oauth.Auth,
return nil, nil
}
-func (p *processor) searchAccountByMention(ctx context.Context, authed *oauth.Auth, mention string, resolve bool) (*gtsmodel.Account, error) {
- // query is for a remote account
- username, domain, err := util.ExtractMentionParts(mention)
- if err != nil {
- return nil, fmt.Errorf("searchAccountByMention: error extracting mention parts: %s", err)
- }
+func (p *processor) searchAccountByMention(ctx context.Context, authed *oauth.Auth, username string, domain string, resolve bool) (*gtsmodel.Account, error) {
+ maybeAcct := &gtsmodel.Account{}
+ var err error
// if it's a local account we can skip a whole bunch of stuff
- maybeAcct := &gtsmodel.Account{}
- if domain == config.GetHost() {
+ if domain == config.GetHost() || domain == config.GetAccountDomain() || domain == "" {
maybeAcct, err = p.db.GetLocalAccountByUsername(ctx, username)
- if err != nil {
+ if err != nil && err != db.ErrNoEntries {
return nil, fmt.Errorf("searchAccountByMention: error getting local account by username: %s", err)
}
return maybeAcct, nil
@@ -191,22 +208,15 @@ func (p *processor) searchAccountByMention(ctx context.Context, authed *oauth.Au
// we got a db.ErrNoEntries, so we just don't have the account locally stored -- check if we can dereference it
if resolve {
- // we're allowed to resolve it so let's try
- // first we need to webfinger the remote account to convert the username and domain into the activitypub URI for the account
- acctURI, err := p.federator.FingerRemoteAccount(ctx, authed.Account.Username, username, domain)
+ maybeAcct, err = p.federator.GetRemoteAccount(ctx, dereferencing.GetRemoteAccountParams{
+ RequestingUsername: authed.Account.Username,
+ RemoteAccountUsername: username,
+ RemoteAccountHost: domain,
+ })
if err != nil {
- // something went wrong doing the webfinger lookup so we can't process the request
- return nil, fmt.Errorf("error fingering remote account with username %s and domain %s: %s", username, domain, err)
- }
-
- if acctURI.Scheme == "https" || acctURI.Scheme == "http" {
- acct, err := p.federator.GetRemoteAccount(ctx, authed.Account.Username, acctURI, true, true)
- if err != nil {
- logrus.Debugf("could not get remote account by mention %s with uri %s: %s", mention, acctURI, err)
- return nil, err
- }
- return acct, nil
+ return nil, fmt.Errorf("searchAccountByMention: error getting remote account: %s", err)
}
+ return maybeAcct, nil
}
return nil, nil