| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |    GoToSocial | 
					
						
							|  |  |  |    Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    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 13:12:00 +02:00
										 |  |  | package processing | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 	"net/url" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 	"github.com/sirupsen/logrus" | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/db" | 
					
						
							| 
									
										
										
										
											2021-06-04 14:16:29 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtserror" | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/oauth" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/util" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-04 14:16:29 +02:00
										 |  |  | func (p *processor) SearchGet(authed *oauth.Auth, searchQuery *apimodel.SearchQuery) (*apimodel.SearchResult, gtserror.WithCode) { | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 	l := p.log.WithFields(logrus.Fields{ | 
					
						
							|  |  |  | 		"func":  "SearchGet", | 
					
						
							|  |  |  | 		"query": searchQuery.Query, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02: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 20:35:03 +02:00
										 |  |  | 	var foundOne bool | 
					
						
							|  |  |  | 	// check if the query is something like @whatever_username@example.org -- this means it's a remote account | 
					
						
							|  |  |  | 	if !foundOne && util.IsMention(searchQuery.Query) { | 
					
						
							|  |  |  | 		l.Debug("search term is a mention, looking it up...") | 
					
						
							|  |  |  | 		foundAccount, err := p.searchAccountByMention(authed, searchQuery.Query, searchQuery.Resolve) | 
					
						
							|  |  |  | 		if err == nil && foundAccount != nil { | 
					
						
							|  |  |  | 			foundAccounts = append(foundAccounts, foundAccount) | 
					
						
							|  |  |  | 			foundOne = true | 
					
						
							|  |  |  | 			l.Debug("got an account by searching by mention") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 	// check if the query is a URI and just do a lookup for that, straight up | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 	if uri, err := url.Parse(query); err == nil && !foundOne { | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 		// 1. check if it's a status | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 		if foundStatus, err := p.searchStatusByURI(authed, uri, searchQuery.Resolve); err == nil && foundStatus != nil { | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 			foundStatuses = append(foundStatuses, foundStatus) | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 			foundOne = true | 
					
						
							|  |  |  | 			l.Debug("got a status by searching by URI") | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// 2. check if it's an account | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 		if foundAccount, err := p.searchAccountByURI(authed, uri, searchQuery.Resolve); err == nil && foundAccount != nil { | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 			foundAccounts = append(foundAccounts, foundAccount) | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 			foundOne = true | 
					
						
							|  |  |  | 			l.Debug("got an account by searching by URI") | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 	if !foundOne { | 
					
						
							|  |  |  | 		// we haven't found anything yet so search for text now | 
					
						
							|  |  |  | 		l.Debug("nothing found by mention or by URI, will fall back to searching by text now") | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02: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 | 
					
						
							|  |  |  | 		if blocked, err := p.db.Blocked(authed.Account.ID, foundAccount.ID); err == nil && !blocked { | 
					
						
							|  |  |  | 			// all good, convert it and add it to the results | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 			if acctMasto, err := p.tc.AccountToMastoPublic(foundAccount); err == nil && acctMasto != nil { | 
					
						
							|  |  |  | 				results.Accounts = append(results.Accounts, *acctMasto) | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, foundStatus := range foundStatuses { | 
					
						
							|  |  |  | 		statusOwner := >smodel.Account{} | 
					
						
							|  |  |  | 		if err := p.db.GetByID(foundStatus.AccountID, statusOwner); err != nil { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		relevantAccounts, err := p.db.PullRelevantAccountsFromStatus(foundStatus) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-06-04 16:35:58 +02:00
										 |  |  | 		if visible, err := p.db.StatusVisible(foundStatus, authed.Account, relevantAccounts); !visible || err != nil { | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		statusMasto, err := p.tc.StatusToMasto(foundStatus, statusOwner, authed.Account, relevantAccounts.BoostedAccount, relevantAccounts.ReplyToAccount, nil) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		results.Statuses = append(results.Statuses, *statusMasto) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return results, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | func (p *processor) searchStatusByURI(authed *oauth.Auth, uri *url.URL, resolve bool) (*gtsmodel.Status, error) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 	maybeStatus := >smodel.Status{} | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 	if err := p.db.GetWhere([]db.Where{{Key: "uri", Value: uri.String(), CaseInsensitive: true}}, maybeStatus); err == nil { | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 		// we have it and it's a status | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 		return maybeStatus, nil | 
					
						
							|  |  |  | 	} else if err := p.db.GetWhere([]db.Where{{Key: "url", Value: uri.String(), CaseInsensitive: true}}, maybeStatus); err == nil { | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 		// we have it and it's a status | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 		return maybeStatus, nil | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// we don't have it locally so dereference it if we're allowed to | 
					
						
							|  |  |  | 	if resolve { | 
					
						
							|  |  |  | 		statusable, err := p.federator.DereferenceRemoteStatus(authed.Account.Username, uri) | 
					
						
							|  |  |  | 		if err == nil { | 
					
						
							|  |  |  | 			// it IS a status! | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// extract the status owner's IRI from the statusable | 
					
						
							|  |  |  | 			var statusOwnerURI *url.URL | 
					
						
							|  |  |  | 			statusAttributedTo := statusable.GetActivityStreamsAttributedTo() | 
					
						
							|  |  |  | 			for i := statusAttributedTo.Begin(); i != statusAttributedTo.End(); i = i.Next() { | 
					
						
							|  |  |  | 				if i.IsIRI() { | 
					
						
							|  |  |  | 					statusOwnerURI = i.GetIRI() | 
					
						
							|  |  |  | 					break | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if statusOwnerURI == nil { | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 				return nil, errors.New("couldn't extract ownerAccountURI from statusable") | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// make sure the status owner exists in the db by searching for it | 
					
						
							|  |  |  | 			_, err := p.searchAccountByURI(authed, statusOwnerURI, resolve) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return nil, err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// we have the status owner, we have the dereferenced status, so now we should finish dereferencing the status properly | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// first turn it into a gtsmodel.Status | 
					
						
							|  |  |  | 			status, err := p.tc.ASStatusToStatus(statusable) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-04 14:16:29 +02:00
										 |  |  | 				return nil, gtserror.NewErrorInternalError(err) | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// put it in the DB so it gets a UUID | 
					
						
							|  |  |  | 			if err := p.db.Put(status); err != nil { | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 				return nil, fmt.Errorf("error putting status in the db: %s", err) | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// properly dereference everything in the status (media attachments etc) | 
					
						
							|  |  |  | 			if err := p.dereferenceStatusFields(status, authed.Account.Username); err != nil { | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 				return nil, fmt.Errorf("error dereferencing status fields: %s", err) | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// update with the nicely dereferenced status | 
					
						
							|  |  |  | 			if err := p.db.UpdateByID(status.ID, status); err != nil { | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 				return nil, fmt.Errorf("error updating status in the db: %s", err) | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 			return status, nil | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 	return nil, nil | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | func (p *processor) searchAccountByURI(authed *oauth.Auth, uri *url.URL, resolve bool) (*gtsmodel.Account, error) { | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 	maybeAccount := >smodel.Account{} | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 	if err := p.db.GetWhere([]db.Where{{Key: "uri", Value: uri.String(), CaseInsensitive: true}}, maybeAccount); err == nil { | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 		// we have it and it's an account | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 		return maybeAccount, nil | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 	} else if err = p.db.GetWhere([]db.Where{{Key: "url", Value: uri.String(), CaseInsensitive: true}}, maybeAccount); err == nil { | 
					
						
							|  |  |  | 		// we have it and it's an account | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 		return maybeAccount, nil | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if resolve { | 
					
						
							|  |  |  | 		// we don't have it locally so try and dereference it | 
					
						
							|  |  |  | 		accountable, err := p.federator.DereferenceRemoteAccount(authed.Account.Username, uri) | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("searchAccountByURI: error dereferencing account with uri %s: %s", uri.String(), err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 		// it IS an account! | 
					
						
							|  |  |  | 		account, err := p.tc.ASRepresentationToAccount(accountable, false) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("searchAccountByURI: error dereferencing account with uri %s: %s", uri.String(), err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 		if err := p.db.Put(account); err != nil { | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("searchAccountByURI: error inserting account with uri %s: %s", uri.String(), err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 		if err := p.dereferenceAccountFields(account, authed.Account.Username, false); err != nil { | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("searchAccountByURI: error further dereferencing account with uri %s: %s", uri.String(), err) | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return account, nil | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 	return nil, nil | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | func (p *processor) searchAccountByMention(authed *oauth.Auth, mention string, resolve bool) (*gtsmodel.Account, error) { | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 	// query is for a remote account | 
					
						
							|  |  |  | 	username, domain, err := util.ExtractMentionParts(mention) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 		return nil, fmt.Errorf("searchAccountByMention: error extracting mention parts: %s", err) | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// if it's a local account we can skip a whole bunch of stuff | 
					
						
							|  |  |  | 	maybeAcct := >smodel.Account{} | 
					
						
							|  |  |  | 	if domain == p.config.Host { | 
					
						
							|  |  |  | 		if err = p.db.GetLocalAccountByUsername(username, maybeAcct); err != nil { | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 			return nil, fmt.Errorf("searchAccountByMention: error getting local account by username: %s", err) | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 		return maybeAcct, nil | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02: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}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = p.db.GetWhere(where, maybeAcct) | 
					
						
							|  |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		// we've got it stored locally already! | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 		return maybeAcct, nil | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if _, ok := err.(db.ErrNoEntries); !ok { | 
					
						
							|  |  |  | 		// if it's  not errNoEntries there's been a real database error so bail at this point | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 		return nil, fmt.Errorf("searchAccountByMention: database error: %s", err) | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02: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 | 
					
						
							|  |  |  | 		acctURI, err := p.federator.FingerRemoteAccount(authed.Account.Username, username, domain) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			// something went wrong doing the webfinger lookup so we can't process the request | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 			return nil, fmt.Errorf("searchAccountByMention: error fingering remote account with username %s and domain %s: %s", username, domain, err) | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// dereference the account based on the URI we retrieved from the webfinger lookup | 
					
						
							|  |  |  | 		accountable, err := p.federator.DereferenceRemoteAccount(authed.Account.Username, acctURI) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			// something went wrong doing the dereferencing so we can't process the request | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 			return nil, fmt.Errorf("searchAccountByMention: error dereferencing remote account with uri %s: %s", acctURI.String(), err) | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// convert the dereferenced account to the gts model of that account | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 		foundAccount, err := p.tc.ASRepresentationToAccount(accountable, false) | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			// something went wrong doing the conversion to a gtsmodel.Account so we can't process the request | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 			return nil, fmt.Errorf("searchAccountByMention: error converting account with uri %s: %s", acctURI.String(), err) | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// put this new account in our database | 
					
						
							|  |  |  | 		if err := p.db.Put(foundAccount); err != nil { | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 			return nil, fmt.Errorf("searchAccountByMention: error inserting account with uri %s: %s", acctURI.String(), err) | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// properly dereference all the fields on the account immediately | 
					
						
							|  |  |  | 		if err := p.dereferenceAccountFields(foundAccount, authed.Account.Username, true); err != nil { | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 			return nil, fmt.Errorf("searchAccountByMention: error dereferencing fields on account with uri %s: %s", acctURI.String(), err) | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 20:35:03 +02:00
										 |  |  | 	return nil, nil | 
					
						
							| 
									
										
										
										
											2021-05-29 19:39:43 +02:00
										 |  |  | } |