2021-05-29 18:39:43 +01:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2021-12-20 17:42:19 +00:00
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
2021-05-29 18:39:43 +01:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2021-05-30 12:12:00 +01:00
|
|
|
package processing
|
2021-05-29 18:39:43 +01:00
|
|
|
|
|
|
|
import (
|
2021-08-25 14:34:33 +01:00
|
|
|
"context"
|
2021-05-29 19:35:03 +01:00
|
|
|
"fmt"
|
2021-05-29 18:39:43 +01:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
2021-05-29 19:35:03 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-12-07 12:31:39 +00:00
|
|
|
"github.com/spf13/viper"
|
2021-05-29 18:39:43 +01:00
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
2021-12-07 12:31:39 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
2021-05-29 18:39:43 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2021-06-13 17:42:28 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2021-05-29 18:39:43 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
|
|
|
)
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
func (p *processor) SearchGet(ctx context.Context, authed *oauth.Auth, searchQuery *apimodel.SearchQuery) (*apimodel.SearchResult, gtserror.WithCode) {
|
2021-10-11 13:37:33 +01:00
|
|
|
l := logrus.WithFields(logrus.Fields{
|
2021-05-29 19:35:03 +01:00
|
|
|
"func": "SearchGet",
|
|
|
|
"query": searchQuery.Query,
|
|
|
|
})
|
|
|
|
|
2021-05-29 18:39:43 +01:00
|
|
|
results := &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))
|
|
|
|
|
2021-05-29 19:35:03 +01:00
|
|
|
var foundOne bool
|
|
|
|
// check if the query is something like @whatever_username@example.org -- this means it's a remote account
|
2021-10-17 13:19:49 +01:00
|
|
|
if _, domain, err := util.ExtractMentionParts(searchQuery.Query); err == nil && domain != "" {
|
2021-05-29 19:35:03 +01:00
|
|
|
l.Debug("search term is a mention, looking it up...")
|
2021-08-25 14:34:33 +01:00
|
|
|
foundAccount, err := p.searchAccountByMention(ctx, authed, searchQuery.Query, searchQuery.Resolve)
|
2021-05-29 19:35:03 +01:00
|
|
|
if err == nil && foundAccount != nil {
|
|
|
|
foundAccounts = append(foundAccounts, foundAccount)
|
|
|
|
foundOne = true
|
|
|
|
l.Debug("got an account by searching by mention")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-29 18:39:43 +01:00
|
|
|
// check if the query is a URI and just do a lookup for that, straight up
|
2021-10-17 13:19:49 +01:00
|
|
|
if !foundOne {
|
|
|
|
if uri, err := url.Parse(query); err == nil {
|
|
|
|
// 1. check if it's a status
|
|
|
|
if foundStatus, err := p.searchStatusByURI(ctx, authed, uri, searchQuery.Resolve); err == nil && foundStatus != nil {
|
|
|
|
foundStatuses = append(foundStatuses, foundStatus)
|
|
|
|
l.Debug("got a status by searching by URI")
|
|
|
|
}
|
2021-05-29 18:39:43 +01:00
|
|
|
|
2021-10-17 13:19:49 +01:00
|
|
|
// 2. check if it's an account
|
|
|
|
if foundAccount, err := p.searchAccountByURI(ctx, authed, uri, searchQuery.Resolve); err == nil && foundAccount != nil {
|
|
|
|
foundAccounts = append(foundAccounts, foundAccount)
|
|
|
|
l.Debug("got an account by searching by URI")
|
|
|
|
}
|
2021-05-29 18:39:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
FROM HERE ON we have our search results, it's just a matter of filtering them according to what this user is allowed to see,
|
|
|
|
and then converting them into our frontend format.
|
|
|
|
*/
|
|
|
|
for _, foundAccount := range foundAccounts {
|
|
|
|
// make sure there's no block in either direction between the account and the requester
|
2021-08-25 14:34:33 +01:00
|
|
|
if blocked, err := p.db.IsBlocked(ctx, authed.Account.ID, foundAccount.ID, true); err == nil && !blocked {
|
2021-05-29 18:39:43 +01:00
|
|
|
// all good, convert it and add it to the results
|
2021-10-04 14:24:19 +01:00
|
|
|
if apiAcct, err := p.tc.AccountToAPIAccountPublic(ctx, foundAccount); err == nil && apiAcct != nil {
|
|
|
|
results.Accounts = append(results.Accounts, *apiAcct)
|
2021-05-29 18:39:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, foundStatus := range foundStatuses {
|
2021-08-25 14:34:33 +01:00
|
|
|
if visible, err := p.filter.StatusVisible(ctx, foundStatus, authed.Account); !visible || err != nil {
|
2021-05-29 18:39:43 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-04 14:24:19 +01:00
|
|
|
apiStatus, err := p.tc.StatusToAPIStatus(ctx, foundStatus, authed.Account)
|
2021-05-29 18:39:43 +01:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-04 14:24:19 +01:00
|
|
|
results.Statuses = append(results.Statuses, *apiStatus)
|
2021-05-29 18:39:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
func (p *processor) searchStatusByURI(ctx context.Context, authed *oauth.Auth, uri *url.URL, resolve bool) (*gtsmodel.Status, error) {
|
2021-10-11 13:37:33 +01:00
|
|
|
l := logrus.WithFields(logrus.Fields{
|
2021-08-10 12:32:39 +01:00
|
|
|
"func": "searchStatusByURI",
|
|
|
|
"uri": uri.String(),
|
|
|
|
"resolve": resolve,
|
|
|
|
})
|
2021-05-29 19:35:03 +01:00
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
if maybeStatus, err := p.db.GetStatusByURI(ctx, uri.String()); err == nil {
|
2021-05-29 19:35:03 +01:00
|
|
|
return maybeStatus, nil
|
2021-08-25 14:34:33 +01:00
|
|
|
} else if maybeStatus, err := p.db.GetStatusByURL(ctx, uri.String()); err == nil {
|
2021-05-29 19:35:03 +01:00
|
|
|
return maybeStatus, nil
|
2021-05-29 18:39:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// we don't have it locally so dereference it if we're allowed to
|
|
|
|
if resolve {
|
2021-09-14 11:23:56 +01:00
|
|
|
status, _, _, err := p.federator.GetRemoteStatus(ctx, authed.Account.Username, uri, true, true)
|
2021-05-29 18:39:43 +01:00
|
|
|
if err == nil {
|
2021-08-25 14:34:33 +01:00
|
|
|
if err := p.federator.DereferenceRemoteThread(ctx, authed.Account.Username, uri); err != nil {
|
2021-08-10 12:32:39 +01:00
|
|
|
// try to deref the thread while we're here
|
|
|
|
l.Debugf("searchStatusByURI: error dereferencing remote thread: %s", err)
|
2021-06-13 17:42:28 +01:00
|
|
|
}
|
2021-05-29 19:35:03 +01:00
|
|
|
return status, nil
|
2021-05-29 18:39:43 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-29 19:35:03 +01:00
|
|
|
return nil, nil
|
2021-05-29 18:39:43 +01:00
|
|
|
}
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
func (p *processor) searchAccountByURI(ctx context.Context, authed *oauth.Auth, uri *url.URL, resolve bool) (*gtsmodel.Account, error) {
|
|
|
|
if maybeAccount, err := p.db.GetAccountByURI(ctx, uri.String()); err == nil {
|
2021-05-29 19:35:03 +01:00
|
|
|
return maybeAccount, nil
|
2021-08-25 14:34:33 +01:00
|
|
|
} else if maybeAccount, err := p.db.GetAccountByURL(ctx, uri.String()); err == nil {
|
2021-05-29 19:35:03 +01:00
|
|
|
return maybeAccount, nil
|
2021-05-29 18:39:43 +01:00
|
|
|
}
|
2021-08-20 11:26:56 +01:00
|
|
|
|
2021-05-29 18:39:43 +01:00
|
|
|
if resolve {
|
|
|
|
// we don't have it locally so try and dereference it
|
2021-08-25 14:34:33 +01:00
|
|
|
account, _, err := p.federator.GetRemoteAccount(ctx, authed.Account.Username, uri, true)
|
2021-05-29 19:35:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("searchAccountByURI: error dereferencing account with uri %s: %s", uri.String(), err)
|
|
|
|
}
|
|
|
|
return account, nil
|
2021-05-29 18:39:43 +01:00
|
|
|
}
|
2021-05-29 19:35:03 +01:00
|
|
|
return nil, nil
|
2021-05-29 18:39:43 +01:00
|
|
|
}
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
func (p *processor) searchAccountByMention(ctx context.Context, authed *oauth.Auth, mention string, resolve bool) (*gtsmodel.Account, error) {
|
2021-05-29 18:39:43 +01:00
|
|
|
// query is for a remote account
|
|
|
|
username, domain, err := util.ExtractMentionParts(mention)
|
|
|
|
if err != nil {
|
2021-05-29 19:35:03 +01:00
|
|
|
return nil, fmt.Errorf("searchAccountByMention: error extracting mention parts: %s", err)
|
2021-05-29 18:39:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// if it's a local account we can skip a whole bunch of stuff
|
|
|
|
maybeAcct := >smodel.Account{}
|
2021-12-07 12:31:39 +00:00
|
|
|
host := viper.GetString(config.Keys.Host)
|
|
|
|
if domain == host {
|
2021-08-25 14:34:33 +01:00
|
|
|
maybeAcct, err = p.db.GetLocalAccountByUsername(ctx, username)
|
2021-08-20 11:26:56 +01:00
|
|
|
if err != nil {
|
2021-05-29 19:35:03 +01:00
|
|
|
return nil, fmt.Errorf("searchAccountByMention: error getting local account by username: %s", err)
|
2021-05-29 18:39:43 +01:00
|
|
|
}
|
2021-05-29 19:35:03 +01:00
|
|
|
return maybeAcct, nil
|
2021-05-29 18:39:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// it's not a local account so first we'll check if it's in the database already...
|
|
|
|
where := []db.Where{
|
|
|
|
{Key: "username", Value: username, CaseInsensitive: true},
|
|
|
|
{Key: "domain", Value: domain, CaseInsensitive: true},
|
|
|
|
}
|
2021-08-25 14:34:33 +01:00
|
|
|
err = p.db.GetWhere(ctx, where, maybeAcct)
|
2021-05-29 18:39:43 +01:00
|
|
|
if err == nil {
|
|
|
|
// we've got it stored locally already!
|
2021-05-29 19:35:03 +01:00
|
|
|
return maybeAcct, nil
|
2021-05-29 18:39:43 +01:00
|
|
|
}
|
|
|
|
|
2021-08-20 11:26:56 +01:00
|
|
|
if err != db.ErrNoEntries {
|
2021-05-29 18:39:43 +01:00
|
|
|
// if it's not errNoEntries there's been a real database error so bail at this point
|
2021-05-29 19:35:03 +01:00
|
|
|
return nil, fmt.Errorf("searchAccountByMention: database error: %s", err)
|
2021-05-29 18:39:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2021-08-25 14:34:33 +01:00
|
|
|
acctURI, err := p.federator.FingerRemoteAccount(ctx, authed.Account.Username, username, domain)
|
2021-05-29 18:39:43 +01:00
|
|
|
if err != nil {
|
|
|
|
// something went wrong doing the webfinger lookup so we can't process the request
|
2021-05-29 19:35:03 +01:00
|
|
|
return nil, fmt.Errorf("searchAccountByMention: error fingering remote account with username %s and domain %s: %s", username, domain, err)
|
2021-05-29 18:39:43 +01:00
|
|
|
}
|
|
|
|
|
2021-08-10 12:32:39 +01:00
|
|
|
// we don't have it locally so try and dereference it
|
2021-08-25 14:34:33 +01:00
|
|
|
account, _, err := p.federator.GetRemoteAccount(ctx, authed.Account.Username, acctURI, true)
|
2021-06-13 17:42:28 +01:00
|
|
|
if err != nil {
|
2021-08-10 12:32:39 +01:00
|
|
|
return nil, fmt.Errorf("searchAccountByMention: error dereferencing account with uri %s: %s", acctURI.String(), err)
|
2021-05-29 18:39:43 +01:00
|
|
|
}
|
2021-08-10 12:32:39 +01:00
|
|
|
return account, nil
|
2021-05-29 18:39:43 +01:00
|
|
|
}
|
|
|
|
|
2021-05-29 19:35:03 +01:00
|
|
|
return nil, nil
|
2021-05-29 18:39:43 +01:00
|
|
|
}
|