2021-05-29 17:39:43 +00:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2021-12-20 17:42:19 +00:00
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
2021-05-29 17:39:43 +00: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 11:12:00 +00:00
|
|
|
package processing
|
2021-05-29 17:39:43 +00:00
|
|
|
|
|
|
|
import (
|
2021-08-25 13:34:33 +00:00
|
|
|
"context"
|
2022-06-11 09:01:34 +00:00
|
|
|
"errors"
|
2021-05-29 18:35:03 +00:00
|
|
|
"fmt"
|
2021-05-29 17:39:43 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
2022-07-19 08:47:55 +00:00
|
|
|
"codeberg.org/gruf/go-kv"
|
2021-05-29 17:39:43 +00: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 17:39:43 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2022-06-11 09:01:34 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/federation/dereferencing"
|
2021-06-13 16:42:28 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2021-05-29 17:39:43 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2022-07-19 08:47:55 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2021-05-29 17:39:43 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
|
|
|
)
|
|
|
|
|
2022-06-11 09:01:34 +00:00
|
|
|
func (p *processor) SearchGet(ctx context.Context, authed *oauth.Auth, search *apimodel.SearchQuery) (*apimodel.SearchResult, gtserror.WithCode) {
|
2022-07-19 08:47:55 +00:00
|
|
|
l := log.WithFields(kv.Fields{
|
|
|
|
{"query", search.Query},
|
|
|
|
}...)
|
2021-05-29 18:35:03 +00:00
|
|
|
|
2022-06-11 09:01:34 +00:00
|
|
|
// 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{
|
2021-05-29 17:39:43 +00:00
|
|
|
Accounts: []apimodel.Account{},
|
|
|
|
Statuses: []apimodel.Status{},
|
|
|
|
Hashtags: []apimodel.Tag{},
|
|
|
|
}
|
2022-08-31 07:57:50 +00:00
|
|
|
|
|
|
|
// currently the search will only ever return one result,
|
|
|
|
// so return nothing if the offset is greater than 0
|
|
|
|
if search.Offset > 0 {
|
|
|
|
return searchResult, nil
|
|
|
|
}
|
|
|
|
|
2021-05-29 17:39:43 +00:00
|
|
|
foundAccounts := []*gtsmodel.Account{}
|
|
|
|
foundStatuses := []*gtsmodel.Status{}
|
|
|
|
|
2021-05-29 18:35:03 +00:00
|
|
|
var foundOne bool
|
2022-06-11 09:01:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
SEARCH BY MENTION
|
2022-08-20 20:47:19 +00:00
|
|
|
check if the query is something like @whatever_username@example.org -- this means it's likely a remote account
|
2022-06-11 09:01:34 +00:00
|
|
|
*/
|
|
|
|
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 {
|
2021-05-29 18:35:03 +00:00
|
|
|
foundAccounts = append(foundAccounts, foundAccount)
|
|
|
|
foundOne = true
|
|
|
|
l.Debug("got an account by searching by mention")
|
2022-06-11 09:01:34 +00:00
|
|
|
} else if err != nil {
|
|
|
|
l.Debugf("error looking up account %s: %s", maybeNamestring, err)
|
2021-05-29 18:35:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 09:01:34 +00:00
|
|
|
/*
|
|
|
|
SEARCH BY URI
|
|
|
|
check if the query is a URI with a recognizable scheme and dereference it
|
|
|
|
*/
|
2021-10-17 12:19:49 +00:00
|
|
|
if !foundOne {
|
2022-05-24 11:38:11 +00:00
|
|
|
if uri, err := url.Parse(query); err == nil && (uri.Scheme == "https" || uri.Scheme == "http") {
|
2022-06-20 12:11:36 +00:00
|
|
|
// don't attempt to resolve (ie., dereference) local accounts/statuses
|
|
|
|
resolve := search.Resolve
|
|
|
|
if uri.Host == config.GetHost() || uri.Host == config.GetAccountDomain() {
|
|
|
|
resolve = false
|
2021-10-17 12:19:49 +00:00
|
|
|
}
|
2021-05-29 17:39:43 +00:00
|
|
|
|
2022-06-20 12:11:36 +00:00
|
|
|
// check if it's a status or an account
|
|
|
|
if foundStatus, err := p.searchStatusByURI(ctx, authed, uri, resolve); err == nil && foundStatus != nil {
|
|
|
|
foundStatuses = append(foundStatuses, foundStatus)
|
|
|
|
l.Debug("got a status by searching by URI")
|
|
|
|
} else if foundAccount, err := p.searchAccountByURI(ctx, authed, uri, resolve); err == nil && foundAccount != nil {
|
2021-10-17 12:19:49 +00:00
|
|
|
foundAccounts = append(foundAccounts, foundAccount)
|
|
|
|
l.Debug("got an account by searching by URI")
|
|
|
|
}
|
2021-05-29 17:39:43 +00: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 13:34:33 +00:00
|
|
|
if blocked, err := p.db.IsBlocked(ctx, authed.Account.ID, foundAccount.ID, true); err == nil && !blocked {
|
2021-05-29 17:39:43 +00:00
|
|
|
// all good, convert it and add it to the results
|
2021-10-04 13:24:19 +00:00
|
|
|
if apiAcct, err := p.tc.AccountToAPIAccountPublic(ctx, foundAccount); err == nil && apiAcct != nil {
|
2022-06-11 09:01:34 +00:00
|
|
|
searchResult.Accounts = append(searchResult.Accounts, *apiAcct)
|
2021-05-29 17:39:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, foundStatus := range foundStatuses {
|
2021-08-25 13:34:33 +00:00
|
|
|
if visible, err := p.filter.StatusVisible(ctx, foundStatus, authed.Account); !visible || err != nil {
|
2021-05-29 17:39:43 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-04 13:24:19 +00:00
|
|
|
apiStatus, err := p.tc.StatusToAPIStatus(ctx, foundStatus, authed.Account)
|
2021-05-29 17:39:43 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-11 09:01:34 +00:00
|
|
|
searchResult.Statuses = append(searchResult.Statuses, *apiStatus)
|
2021-05-29 17:39:43 +00:00
|
|
|
}
|
|
|
|
|
2022-06-11 09:01:34 +00:00
|
|
|
return searchResult, nil
|
2021-05-29 17:39:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (p *processor) searchStatusByURI(ctx context.Context, authed *oauth.Auth, uri *url.URL, resolve bool) (*gtsmodel.Status, error) {
|
2022-09-25 11:09:41 +00:00
|
|
|
// Calculate URI string once
|
|
|
|
uriStr := uri.String()
|
|
|
|
|
|
|
|
// Look for status locally (by URI), we only accept "not found" errors.
|
|
|
|
status, err := p.db.GetStatusByURI(ctx, uriStr)
|
|
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
return nil, fmt.Errorf("searchStatusByURI: error fetching status %q: %v", uriStr, err)
|
|
|
|
} else if err == nil {
|
|
|
|
return status, nil
|
|
|
|
}
|
2021-05-29 18:35:03 +00:00
|
|
|
|
2022-09-25 11:09:41 +00:00
|
|
|
// Again, look for status locally (by URL), we only accept "not found" errors.
|
|
|
|
status, err = p.db.GetStatusByURL(ctx, uriStr)
|
|
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
return nil, fmt.Errorf("searchStatusByURI: error fetching status %q: %v", uriStr, err)
|
|
|
|
} else if err == nil {
|
|
|
|
return status, nil
|
2021-05-29 17:39:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if resolve {
|
2022-09-25 11:09:41 +00:00
|
|
|
// This is a non-local status and we're allowed to resolve, so dereference it
|
|
|
|
status, statusable, err := p.federator.GetRemoteStatus(ctx, authed.Account.Username, uri, true, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("searchStatusByURI: error fetching remote status %q: %v", uriStr, err)
|
2021-05-29 17:39:43 +00:00
|
|
|
}
|
2022-09-25 11:09:41 +00:00
|
|
|
|
|
|
|
// Attempt to dereference the status thread while we are here
|
|
|
|
p.federator.DereferenceRemoteThread(ctx, authed.Account.Username, uri, status, statusable)
|
2021-05-29 17:39:43 +00:00
|
|
|
}
|
2022-09-25 11:09:41 +00:00
|
|
|
|
2021-05-29 18:35:03 +00:00
|
|
|
return nil, nil
|
2021-05-29 17:39:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (p *processor) searchAccountByURI(ctx context.Context, authed *oauth.Auth, uri *url.URL, resolve bool) (*gtsmodel.Account, error) {
|
2022-08-20 20:47:19 +00:00
|
|
|
// it might be a web url like http://example.org/@user instead
|
|
|
|
// of an AP uri like http://example.org/users/user, check first
|
|
|
|
if maybeAccount, err := p.db.GetAccountByURL(ctx, uri.String()); err == nil {
|
2021-05-29 18:35:03 +00:00
|
|
|
return maybeAccount, nil
|
2021-05-29 17:39:43 +00:00
|
|
|
}
|
2021-08-20 10:26:56 +00:00
|
|
|
|
2022-08-20 20:47:19 +00:00
|
|
|
if uri.Host == config.GetHost() || uri.Host == config.GetAccountDomain() {
|
|
|
|
// this is a local account; if we don't have it now then
|
|
|
|
// we should just bail instead of trying to get it remote
|
|
|
|
if maybeAccount, err := p.db.GetAccountByURI(ctx, uri.String()); err == nil {
|
|
|
|
return maybeAccount, nil
|
2021-05-29 18:35:03 +00:00
|
|
|
}
|
2022-08-20 20:47:19 +00:00
|
|
|
return nil, nil
|
2021-05-29 17:39:43 +00:00
|
|
|
}
|
2022-08-20 20:47:19 +00:00
|
|
|
|
|
|
|
// we don't have it yet, try to find it remotely
|
|
|
|
return p.federator.GetRemoteAccount(ctx, dereferencing.GetRemoteAccountParams{
|
|
|
|
RequestingUsername: authed.Account.Username,
|
|
|
|
RemoteAccountID: uri,
|
|
|
|
Blocking: true,
|
|
|
|
SkipResolve: !resolve,
|
|
|
|
})
|
2021-05-29 17:39:43 +00:00
|
|
|
}
|
|
|
|
|
2022-06-11 09:01:34 +00:00
|
|
|
func (p *processor) searchAccountByMention(ctx context.Context, authed *oauth.Auth, username string, domain string, resolve bool) (*gtsmodel.Account, error) {
|
2021-05-29 17:39:43 +00:00
|
|
|
// if it's a local account we can skip a whole bunch of stuff
|
2022-06-11 09:01:34 +00:00
|
|
|
if domain == config.GetHost() || domain == config.GetAccountDomain() || domain == "" {
|
2022-09-02 09:56:33 +00:00
|
|
|
maybeAcct, err := p.db.GetAccountByUsernameDomain(ctx, username, "")
|
2022-08-20 20:47:19 +00:00
|
|
|
if err == nil || err == db.ErrNoEntries {
|
|
|
|
return maybeAcct, nil
|
2021-05-29 17:39:43 +00:00
|
|
|
}
|
2022-08-20 20:47:19 +00:00
|
|
|
return nil, fmt.Errorf("searchAccountByMention: error getting local account by username: %s", err)
|
2021-05-29 17:39:43 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 20:47:19 +00:00
|
|
|
// we don't have it yet, try to find it remotely
|
|
|
|
return p.federator.GetRemoteAccount(ctx, dereferencing.GetRemoteAccountParams{
|
|
|
|
RequestingUsername: authed.Account.Username,
|
|
|
|
RemoteAccountUsername: username,
|
|
|
|
RemoteAccountHost: domain,
|
|
|
|
Blocking: true,
|
|
|
|
SkipResolve: !resolve,
|
|
|
|
})
|
2021-05-29 17:39:43 +00:00
|
|
|
}
|