| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |    GoToSocial | 
					
						
							| 
									
										
										
										
											2023-01-05 12:43:00 +01:00
										 |  |  |    Copyright (C) 2021-2023 GoToSocial Authors admin@gotosocial.org | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02: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/>. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package dereferencing | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2022-01-16 18:52:55 +01:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	"net/url" | 
					
						
							| 
									
										
										
										
											2022-01-24 18:12:04 +01:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-13 17:29:43 +01:00
										 |  |  | 	"github.com/superseriousbusiness/activity/streams" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/activity/streams/vocab" | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/ap" | 
					
						
							| 
									
										
										
										
											2022-08-20 22:47:19 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/config" | 
					
						
							| 
									
										
										
										
											2022-06-11 11:01:34 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/db" | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/id" | 
					
						
							| 
									
										
										
										
											2022-07-19 09:47:55 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/log" | 
					
						
							| 
									
										
										
										
											2022-01-09 18:41:22 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/media" | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | func (d *deref) GetAccountByURI(ctx context.Context, requestUser string, uri *url.URL, block bool) (*gtsmodel.Account, error) { | 
					
						
							|  |  |  | 	var ( | 
					
						
							|  |  |  | 		account *gtsmodel.Account | 
					
						
							|  |  |  | 		uriStr  = uri.String() | 
					
						
							|  |  |  | 		err     error | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2022-11-29 10:24:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	// Search the database for existing account with ID URI. | 
					
						
							|  |  |  | 	account, err = d.db.GetAccountByURI(ctx, uriStr) | 
					
						
							|  |  |  | 	if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("GetAccountByURI: error checking database for account %s by uri: %w", uriStr, err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-11-29 10:24:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	if account == nil { | 
					
						
							|  |  |  | 		// Else, search the database for existing by ID URL. | 
					
						
							|  |  |  | 		account, err = d.db.GetAccountByURL(ctx, uriStr) | 
					
						
							|  |  |  | 		if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("GetAccountByURI: error checking database for account %s by url: %w", uriStr, err) | 
					
						
							| 
									
										
										
										
											2022-11-29 10:24:55 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	if account == nil { | 
					
						
							|  |  |  | 		// Ensure that this is isn't a search for a local account. | 
					
						
							|  |  |  | 		if uri.Host == config.GetHost() || uri.Host == config.GetAccountDomain() { | 
					
						
							|  |  |  | 			return nil, NewErrNotRetrievable(err) // this will be db.ErrNoEntries | 
					
						
							| 
									
										
										
										
											2022-08-20 22:47:19 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 		// Create and pass-through a new bare-bones model for dereferencing. | 
					
						
							|  |  |  | 		return d.enrichAccount(ctx, requestUser, uri, >smodel.Account{ | 
					
						
							|  |  |  | 			ID:     id.NewULID(), | 
					
						
							|  |  |  | 			Domain: uri.Host, | 
					
						
							|  |  |  | 			URI:    uriStr, | 
					
						
							|  |  |  | 		}, false, true) | 
					
						
							| 
									
										
										
										
											2022-06-11 11:01:34 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	// Try to update existing account model | 
					
						
							|  |  |  | 	enriched, err := d.enrichAccount(ctx, requestUser, uri, account, false, block) | 
					
						
							| 
									
										
										
										
											2022-06-11 11:01:34 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 		log.Errorf("error enriching remote account: %v", err) | 
					
						
							|  |  |  | 		return account, nil // fall back to returning existing | 
					
						
							| 
									
										
										
										
											2022-06-11 11:01:34 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	return enriched, nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-06-11 11:01:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | func (d *deref) GetAccountByUsernameDomain(ctx context.Context, requestUser string, username string, domain string, block bool) (*gtsmodel.Account, error) { | 
					
						
							|  |  |  | 	if domain == config.GetHost() || domain == config.GetAccountDomain() { | 
					
						
							|  |  |  | 		// We do local lookups using an empty domain, | 
					
						
							|  |  |  | 		// else it will fail the db search below. | 
					
						
							|  |  |  | 		domain = "" | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-11-29 10:24:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	// Search the database for existing account with USERNAME@DOMAIN | 
					
						
							|  |  |  | 	account, err := d.db.GetAccountByUsernameDomain(ctx, username, domain) | 
					
						
							|  |  |  | 	if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("GetAccountByUsernameDomain: error checking database for account %s@%s: %w", username, domain, err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-01-24 18:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	if account == nil { | 
					
						
							|  |  |  | 		// Check for failed local lookup. | 
					
						
							|  |  |  | 		if domain == "" { | 
					
						
							|  |  |  | 			return nil, NewErrNotRetrievable(err) // will be db.ErrNoEntries | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Create and pass-through a new bare-bones model for dereferencing. | 
					
						
							|  |  |  | 		return d.enrichAccount(ctx, requestUser, nil, >smodel.Account{ | 
					
						
							|  |  |  | 			ID:       id.NewULID(), | 
					
						
							|  |  |  | 			Username: username, | 
					
						
							|  |  |  | 			Domain:   domain, | 
					
						
							|  |  |  | 		}, false, true) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	// Try to update existing account model | 
					
						
							|  |  |  | 	enriched, err := d.enrichAccount(ctx, requestUser, nil, account, false, block) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Errorf("GetAccountByUsernameDomain: error enriching account from remote: %v", err) | 
					
						
							|  |  |  | 		return account, nil // fall back to returning unchanged existing account model | 
					
						
							| 
									
										
										
										
											2022-06-11 11:01:34 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	return enriched, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (d *deref) UpdateAccount(ctx context.Context, requestUser string, account *gtsmodel.Account, force bool) (*gtsmodel.Account, error) { | 
					
						
							|  |  |  | 	return d.enrichAccount(ctx, requestUser, nil, account, force, false) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // enrichAccount will ensure the given account is the most up-to-date model of the account, re-webfingering and re-dereferencing if necessary. | 
					
						
							|  |  |  | func (d *deref) enrichAccount(ctx context.Context, requestUser string, uri *url.URL, account *gtsmodel.Account, force, block bool) (*gtsmodel.Account, error) { | 
					
						
							|  |  |  | 	if account.IsLocal() { | 
					
						
							|  |  |  | 		// Can't update local accounts. | 
					
						
							|  |  |  | 		return account, nil | 
					
						
							| 
									
										
										
										
											2022-11-29 10:24:55 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	if !account.CreatedAt.IsZero() && account.IsInstance() { | 
					
						
							|  |  |  | 		// Existing instance account. No need for update. | 
					
						
							|  |  |  | 		return account, nil | 
					
						
							| 
									
										
										
										
											2022-06-11 11:01:34 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	if !force { | 
					
						
							|  |  |  | 		const interval = time.Hour * 48 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// If this account was updated recently (last interval), we return as-is. | 
					
						
							|  |  |  | 		if next := account.FetchedAt.Add(interval); time.Now().Before(next) { | 
					
						
							|  |  |  | 			return account, nil | 
					
						
							| 
									
										
										
										
											2022-06-11 11:01:34 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	if account.Username != "" { | 
					
						
							|  |  |  | 		// A username was provided so we can attempt a webfinger, this ensures up-to-date accountdomain info. | 
					
						
							|  |  |  | 		accDomain, accURI, err := d.fingerRemoteAccount(ctx, requestUser, account.Username, account.Domain) | 
					
						
							| 
									
										
										
										
											2022-11-29 10:24:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 		if err != nil && account.URI == "" { | 
					
						
							|  |  |  | 			// this is a new account (to us) with username@domain but failed | 
					
						
							|  |  |  | 			// webfinger, there is nothing more we can do in this situation. | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("enrichAccount: error webfingering account: %w", err) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 		if err == nil { | 
					
						
							|  |  |  | 			// Update account with latest info. | 
					
						
							|  |  |  | 			account.URI = accURI.String() | 
					
						
							|  |  |  | 			account.Domain = accDomain | 
					
						
							|  |  |  | 			uri = accURI | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	if uri == nil { | 
					
						
							|  |  |  | 		var err error | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 		// No URI provided / found, must parse from account. | 
					
						
							|  |  |  | 		uri, err = url.Parse(account.URI) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("enrichAccount: invalid uri %q: %w", account.URI, err) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	// Check whether this account URI is a blocked domain / subdomain | 
					
						
							|  |  |  | 	if blocked, err := d.db.IsDomainBlocked(ctx, uri.Host); err != nil { | 
					
						
							|  |  |  | 		return nil, newErrDB(fmt.Errorf("enrichAccount: error checking blocked domain: %w", err)) | 
					
						
							|  |  |  | 	} else if blocked { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("enrichAccount: %s is blocked", uri.Host) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-06-11 11:01:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	// Mark deref+update handshake start | 
					
						
							|  |  |  | 	d.startHandshake(requestUser, uri) | 
					
						
							|  |  |  | 	defer d.stopHandshake(requestUser, uri) | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	// Dereference this account to get the latest available. | 
					
						
							|  |  |  | 	apubAcc, err := d.dereferenceAccountable(ctx, requestUser, uri) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("enrichAccount: error dereferencing account %s: %w", uri, err) | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	// Convert the dereferenced AP account object to our GTS model. | 
					
						
							|  |  |  | 	latestAcc, err := d.typeConverter.ASRepresentationToAccount( | 
					
						
							|  |  |  | 		ctx, apubAcc, account.Domain, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("enrichAccount: error converting accountable to gts model for account %s: %w", uri, err) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	if account.Username == "" { | 
					
						
							|  |  |  | 		// No username was provided, so no webfinger was attempted earlier. | 
					
						
							|  |  |  | 		// | 
					
						
							|  |  |  | 		// Now we have a username we can attempt it now, this ensures up-to-date accountdomain info. | 
					
						
							|  |  |  | 		accDomain, _, err := d.fingerRemoteAccount(ctx, requestUser, latestAcc.Username, uri.Host) | 
					
						
							| 
									
										
										
										
											2022-09-23 21:27:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 		if err == nil { | 
					
						
							|  |  |  | 			// Update account with latest info. | 
					
						
							|  |  |  | 			latestAcc.Domain = accDomain | 
					
						
							| 
									
										
										
										
											2022-09-23 21:27:35 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	// Ensure ID is set and update fetch time. | 
					
						
							|  |  |  | 	latestAcc.ID = account.ID | 
					
						
							|  |  |  | 	latestAcc.FetchedAt = time.Now() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Fetch latest account media (TODO: check for changed URI to previous). | 
					
						
							|  |  |  | 	if err = d.fetchRemoteAccountMedia(ctx, latestAcc, requestUser, block); err != nil { | 
					
						
							|  |  |  | 		log.Errorf("error fetching remote media for account %s: %v", uri, err) | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	// Fetch the latest remote account emoji IDs used in account display name/bio. | 
					
						
							|  |  |  | 	_, err = d.fetchRemoteAccountEmojis(ctx, latestAcc, requestUser) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Errorf("error fetching remote emojis for account %s: %v", uri, err) | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	if account.CreatedAt.IsZero() { | 
					
						
							|  |  |  | 		// CreatedAt will be zero if no local copy was | 
					
						
							|  |  |  | 		// found in one of the GetAccountBy___() functions. | 
					
						
							|  |  |  | 		// | 
					
						
							|  |  |  | 		// Set time of creation from the last-fetched date. | 
					
						
							|  |  |  | 		latestAcc.CreatedAt = latestAcc.FetchedAt | 
					
						
							|  |  |  | 		latestAcc.UpdatedAt = latestAcc.FetchedAt | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// This is a new account, we need to place it in the database. | 
					
						
							|  |  |  | 		if err := d.db.PutAccount(ctx, latestAcc); err != nil { | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("enrichAccount: error putting in database: %w", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		// Set time of update from the last-fetched date. | 
					
						
							|  |  |  | 		latestAcc.UpdatedAt = latestAcc.FetchedAt | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Use existing account values. | 
					
						
							|  |  |  | 		latestAcc.CreatedAt = account.CreatedAt | 
					
						
							|  |  |  | 		latestAcc.Language = account.Language | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// This is an existing account, update the model in the database. | 
					
						
							|  |  |  | 		if err := d.db.UpdateAccount(ctx, latestAcc); err != nil { | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("enrichAccount: error updating database: %w", err) | 
					
						
							| 
									
										
										
										
											2022-01-25 11:21:22 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	return latestAcc, nil | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // dereferenceAccountable calls remoteAccountID with a GET request, and tries to parse whatever | 
					
						
							|  |  |  | // it finds as something that an account model can be constructed out of. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Will work for Person, Application, or Service models. | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | func (d *deref) dereferenceAccountable(ctx context.Context, username string, remoteAccountID *url.URL) (ap.Accountable, error) { | 
					
						
							|  |  |  | 	transport, err := d.transportController.NewTransportForUsername(ctx, username) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-11-29 10:24:55 +01:00
										 |  |  | 		return nil, fmt.Errorf("DereferenceAccountable: transport err: %w", err) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 	b, err := transport.Dereference(ctx, remoteAccountID) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-11-29 10:24:55 +01:00
										 |  |  | 		return nil, fmt.Errorf("DereferenceAccountable: error deferencing %s: %w", remoteAccountID.String(), err) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m := make(map[string]interface{}) | 
					
						
							|  |  |  | 	if err := json.Unmarshal(b, &m); err != nil { | 
					
						
							| 
									
										
										
										
											2022-11-29 10:24:55 +01:00
										 |  |  | 		return nil, fmt.Errorf("DereferenceAccountable: error unmarshalling bytes into json: %w", err) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 	t, err := streams.ToType(ctx, m) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-11-29 10:24:55 +01:00
										 |  |  | 		return nil, fmt.Errorf("DereferenceAccountable: error resolving json into ap vocab type: %w", err) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	//nolint shutup linter | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	switch t.GetTypeName() { | 
					
						
							| 
									
										
										
										
											2021-09-03 10:30:40 +02:00
										 |  |  | 	case ap.ActorApplication: | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 		return t.(vocab.ActivityStreamsApplication), nil | 
					
						
							| 
									
										
										
										
											2021-09-30 12:27:42 +02:00
										 |  |  | 	case ap.ActorGroup: | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 		return t.(vocab.ActivityStreamsGroup), nil | 
					
						
							| 
									
										
										
										
											2021-09-30 12:27:42 +02:00
										 |  |  | 	case ap.ActorOrganization: | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 		return t.(vocab.ActivityStreamsOrganization), nil | 
					
						
							| 
									
										
										
										
											2021-09-30 12:27:42 +02:00
										 |  |  | 	case ap.ActorPerson: | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 		return t.(vocab.ActivityStreamsPerson), nil | 
					
						
							| 
									
										
										
										
											2021-09-03 10:30:40 +02:00
										 |  |  | 	case ap.ActorService: | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 		return t.(vocab.ActivityStreamsService), nil | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 10:24:55 +01:00
										 |  |  | 	return nil, newErrWrongType(fmt.Errorf("DereferenceAccountable: type name %s not supported as Accountable", t.GetTypeName())) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | // fetchRemoteAccountMedia fetches and stores the header and avatar for a remote account, | 
					
						
							|  |  |  | // using a transport on behalf of requestingUsername. | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | // | 
					
						
							| 
									
										
										
										
											2022-01-25 11:21:22 +01:00
										 |  |  | // The returned boolean indicates whether anything changed -- in other words, whether the | 
					
						
							|  |  |  | // account should be updated in the database. | 
					
						
							|  |  |  | // | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | // targetAccount's AvatarMediaAttachmentID and HeaderMediaAttachmentID will be updated as necessary. | 
					
						
							|  |  |  | // | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | // If refresh is true, then the media will be fetched again even if it's already been fetched before. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // If blocking is true, then the calls to the media manager made by this function will be blocking: | 
					
						
							|  |  |  | // in other words, the function won't return until the header and the avatar have been fully processed. | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | func (d *deref) fetchRemoteAccountMedia(ctx context.Context, targetAccount *gtsmodel.Account, requestingUsername string, blocking bool) error { | 
					
						
							|  |  |  | 	// Fetch a transport beforehand for either(or both) avatar / header dereferencing. | 
					
						
							|  |  |  | 	tsport, err := d.transportController.NewTransportForUsername(ctx, requestingUsername) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return fmt.Errorf("fetchRemoteAccountMedia: error getting transport for user: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	if targetAccount.AvatarRemoteURL != "" { | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		var processingMedia *media.ProcessingMedia | 
					
						
							| 
									
										
										
										
											2022-01-08 17:17:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 		// Parse the target account's avatar URL into URL object. | 
					
						
							|  |  |  | 		avatarIRI, err := url.Parse(targetAccount.AvatarRemoteURL) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-08 13:17:10 +01:00
										 |  |  | 		d.dereferencingAvatarsLock.Lock() // LOCK HERE | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		// first check if we're already processing this media | 
					
						
							|  |  |  | 		if alreadyProcessing, ok := d.dereferencingAvatars[targetAccount.ID]; ok { | 
					
						
							|  |  |  | 			// we're already on it, no worries | 
					
						
							|  |  |  | 			processingMedia = alreadyProcessing | 
					
						
							| 
									
										
										
										
											2022-11-11 20:27:37 +01:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2022-11-03 15:03:12 +01:00
										 |  |  | 			data := func(innerCtx context.Context) (io.ReadCloser, int64, error) { | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 				return tsport.DereferenceMedia(innerCtx, avatarIRI) | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			avatar := true | 
					
						
							| 
									
										
										
										
											2022-02-22 13:50:33 +01:00
										 |  |  | 			newProcessing, err := d.mediaManager.ProcessMedia(ctx, data, nil, targetAccount.ID, &media.AdditionalMediaInfo{ | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 				RemoteURL: &targetAccount.AvatarRemoteURL, | 
					
						
							|  |  |  | 				Avatar:    &avatar, | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							| 
									
										
										
										
											2022-02-08 13:17:10 +01:00
										 |  |  | 				d.dereferencingAvatarsLock.Unlock() | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 				return err | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// store it in our map to indicate it's in process | 
					
						
							|  |  |  | 			d.dereferencingAvatars[targetAccount.ID] = newProcessing | 
					
						
							|  |  |  | 			processingMedia = newProcessing | 
					
						
							| 
									
										
										
										
											2022-01-08 17:17:01 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-02-08 13:17:10 +01:00
										 |  |  | 		d.dereferencingAvatarsLock.Unlock() // UNLOCK HERE | 
					
						
							| 
									
										
										
										
											2022-01-08 17:17:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-11 20:27:37 +01:00
										 |  |  | 		load := func(innerCtx context.Context) error { | 
					
						
							|  |  |  | 			_, err := processingMedia.LoadAttachment(innerCtx) | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		cleanup := func() { | 
					
						
							|  |  |  | 			d.dereferencingAvatarsLock.Lock() | 
					
						
							|  |  |  | 			delete(d.dereferencingAvatars, targetAccount.ID) | 
					
						
							|  |  |  | 			d.dereferencingAvatarsLock.Unlock() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		// block until loaded if required... | 
					
						
							|  |  |  | 		if blocking { | 
					
						
							| 
									
										
										
										
											2022-11-11 20:27:37 +01:00
										 |  |  | 			if err := loadAndCleanup(ctx, load, cleanup); err != nil { | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 				return err | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			// ...otherwise do it async | 
					
						
							|  |  |  | 			go func() { | 
					
						
							| 
									
										
										
										
											2022-01-24 18:12:04 +01:00
										 |  |  | 				dlCtx, done := context.WithDeadline(context.Background(), time.Now().Add(1*time.Minute)) | 
					
						
							| 
									
										
										
										
											2022-11-11 20:27:37 +01:00
										 |  |  | 				if err := loadAndCleanup(dlCtx, load, cleanup); err != nil { | 
					
						
							| 
									
										
										
										
											2022-07-19 09:47:55 +01:00
										 |  |  | 					log.Errorf("fetchRemoteAccountMedia: error during async lock and load of avatar: %s", err) | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2022-01-24 18:12:04 +01:00
										 |  |  | 				done() | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 			}() | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-01-24 18:12:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		targetAccount.AvatarMediaAttachmentID = processingMedia.AttachmentID() | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	if targetAccount.HeaderRemoteURL != "" { | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		var processingMedia *media.ProcessingMedia | 
					
						
							| 
									
										
										
										
											2022-01-08 17:17:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 		// Parse the target account's header URL into URL object. | 
					
						
							|  |  |  | 		headerIRI, err := url.Parse(targetAccount.HeaderRemoteURL) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-08 13:17:10 +01:00
										 |  |  | 		d.dereferencingHeadersLock.Lock() // LOCK HERE | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		// first check if we're already processing this media | 
					
						
							|  |  |  | 		if alreadyProcessing, ok := d.dereferencingHeaders[targetAccount.ID]; ok { | 
					
						
							|  |  |  | 			// we're already on it, no worries | 
					
						
							|  |  |  | 			processingMedia = alreadyProcessing | 
					
						
							| 
									
										
										
										
											2022-11-11 20:27:37 +01:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2022-11-03 15:03:12 +01:00
										 |  |  | 			data := func(innerCtx context.Context) (io.ReadCloser, int64, error) { | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 				return tsport.DereferenceMedia(innerCtx, headerIRI) | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			header := true | 
					
						
							| 
									
										
										
										
											2022-02-22 13:50:33 +01:00
										 |  |  | 			newProcessing, err := d.mediaManager.ProcessMedia(ctx, data, nil, targetAccount.ID, &media.AdditionalMediaInfo{ | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 				RemoteURL: &targetAccount.HeaderRemoteURL, | 
					
						
							|  |  |  | 				Header:    &header, | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							| 
									
										
										
										
											2022-02-08 13:17:10 +01:00
										 |  |  | 				d.dereferencingAvatarsLock.Unlock() | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 				return err | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// store it in our map to indicate it's in process | 
					
						
							|  |  |  | 			d.dereferencingHeaders[targetAccount.ID] = newProcessing | 
					
						
							|  |  |  | 			processingMedia = newProcessing | 
					
						
							| 
									
										
										
										
											2022-01-08 17:17:01 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-02-08 13:17:10 +01:00
										 |  |  | 		d.dereferencingHeadersLock.Unlock() // UNLOCK HERE | 
					
						
							| 
									
										
										
										
											2022-01-08 17:17:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-11 20:27:37 +01:00
										 |  |  | 		load := func(innerCtx context.Context) error { | 
					
						
							|  |  |  | 			_, err := processingMedia.LoadAttachment(innerCtx) | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		cleanup := func() { | 
					
						
							|  |  |  | 			d.dereferencingHeadersLock.Lock() | 
					
						
							|  |  |  | 			delete(d.dereferencingHeaders, targetAccount.ID) | 
					
						
							|  |  |  | 			d.dereferencingHeadersLock.Unlock() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		// block until loaded if required... | 
					
						
							|  |  |  | 		if blocking { | 
					
						
							| 
									
										
										
										
											2022-11-11 20:27:37 +01:00
										 |  |  | 			if err := loadAndCleanup(ctx, load, cleanup); err != nil { | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 				return err | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			// ...otherwise do it async | 
					
						
							|  |  |  | 			go func() { | 
					
						
							| 
									
										
										
										
											2022-01-24 18:12:04 +01:00
										 |  |  | 				dlCtx, done := context.WithDeadline(context.Background(), time.Now().Add(1*time.Minute)) | 
					
						
							| 
									
										
										
										
											2022-11-11 20:27:37 +01:00
										 |  |  | 				if err := loadAndCleanup(dlCtx, load, cleanup); err != nil { | 
					
						
							| 
									
										
										
										
											2022-07-19 09:47:55 +01:00
										 |  |  | 					log.Errorf("fetchRemoteAccountMedia: error during async lock and load of header: %s", err) | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2022-01-24 18:12:04 +01:00
										 |  |  | 				done() | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 			}() | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-01-24 18:12:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		targetAccount.HeaderMediaAttachmentID = processingMedia.AttachmentID() | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-03 20:03:05 +00:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-26 11:56:01 +02:00
										 |  |  | func (d *deref) fetchRemoteAccountEmojis(ctx context.Context, targetAccount *gtsmodel.Account, requestingUsername string) (bool, error) { | 
					
						
							|  |  |  | 	maybeEmojis := targetAccount.Emojis | 
					
						
							|  |  |  | 	maybeEmojiIDs := targetAccount.EmojiIDs | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// It's possible that the account had emoji IDs set on it, but not Emojis | 
					
						
							|  |  |  | 	// themselves, depending on how it was fetched before being passed to us. | 
					
						
							|  |  |  | 	// | 
					
						
							|  |  |  | 	// If we only have IDs, fetch the emojis from the db. We know they're in | 
					
						
							|  |  |  | 	// there or else they wouldn't have IDs. | 
					
						
							|  |  |  | 	if len(maybeEmojiIDs) > len(maybeEmojis) { | 
					
						
							| 
									
										
										
										
											2022-11-11 20:27:37 +01:00
										 |  |  | 		maybeEmojis = make([]*gtsmodel.Emoji, 0, len(maybeEmojiIDs)) | 
					
						
							| 
									
										
										
										
											2022-09-26 11:56:01 +02:00
										 |  |  | 		for _, emojiID := range maybeEmojiIDs { | 
					
						
							|  |  |  | 			maybeEmoji, err := d.db.GetEmojiByID(ctx, emojiID) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return false, err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			maybeEmojis = append(maybeEmojis, maybeEmoji) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// For all the maybe emojis we have, we either fetch them from the database | 
					
						
							|  |  |  | 	// (if we haven't already), or dereference them from the remote instance. | 
					
						
							|  |  |  | 	gotEmojis, err := d.populateEmojis(ctx, maybeEmojis, requestingUsername) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return false, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Extract the ID of each fetched or dereferenced emoji, so we can attach | 
					
						
							|  |  |  | 	// this to the account if necessary. | 
					
						
							|  |  |  | 	gotEmojiIDs := make([]string, 0, len(gotEmojis)) | 
					
						
							|  |  |  | 	for _, e := range gotEmojis { | 
					
						
							|  |  |  | 		gotEmojiIDs = append(gotEmojiIDs, e.ID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var ( | 
					
						
							|  |  |  | 		changed  = false // have the emojis for this account changed? | 
					
						
							|  |  |  | 		maybeLen = len(maybeEmojis) | 
					
						
							|  |  |  | 		gotLen   = len(gotEmojis) | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// if the length of everything is zero, this is simple: | 
					
						
							|  |  |  | 	// nothing has changed and there's nothing to do | 
					
						
							|  |  |  | 	if maybeLen == 0 && gotLen == 0 { | 
					
						
							|  |  |  | 		return changed, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// if the *amount* of emojis on the account has changed, then the got emojis | 
					
						
							|  |  |  | 	// are definitely different from the previous ones (if there were any) -- | 
					
						
							|  |  |  | 	// the account has either more or fewer emojis set on it now, so take the | 
					
						
							|  |  |  | 	// discovered emojis as the new correct ones. | 
					
						
							|  |  |  | 	if maybeLen != gotLen { | 
					
						
							|  |  |  | 		changed = true | 
					
						
							|  |  |  | 		targetAccount.Emojis = gotEmojis | 
					
						
							|  |  |  | 		targetAccount.EmojiIDs = gotEmojiIDs | 
					
						
							|  |  |  | 		return changed, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// if the lengths are the same but not all of the slices are | 
					
						
							|  |  |  | 	// zero, something *might* have changed, so we have to check | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 1. did we have emojis before that we don't have now? | 
					
						
							|  |  |  | 	for _, maybeEmoji := range maybeEmojis { | 
					
						
							|  |  |  | 		var stillPresent bool | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for _, gotEmoji := range gotEmojis { | 
					
						
							|  |  |  | 			if maybeEmoji.URI == gotEmoji.URI { | 
					
						
							|  |  |  | 				// the emoji we maybe had is still present now, | 
					
						
							|  |  |  | 				// so we can stop checking gotEmojis | 
					
						
							|  |  |  | 				stillPresent = true | 
					
						
							|  |  |  | 				break | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if !stillPresent { | 
					
						
							|  |  |  | 			// at least one maybeEmoji is no longer present in | 
					
						
							|  |  |  | 			// the got emojis, so we can stop checking now | 
					
						
							|  |  |  | 			changed = true | 
					
						
							|  |  |  | 			targetAccount.Emojis = gotEmojis | 
					
						
							|  |  |  | 			targetAccount.EmojiIDs = gotEmojiIDs | 
					
						
							|  |  |  | 			return changed, nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 2. do we have emojis now that we didn't have before? | 
					
						
							|  |  |  | 	for _, gotEmoji := range gotEmojis { | 
					
						
							|  |  |  | 		var wasPresent bool | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for _, maybeEmoji := range maybeEmojis { | 
					
						
							|  |  |  | 			// check emoji IDs here as well, because unreferenced | 
					
						
							|  |  |  | 			// maybe emojis we didn't already have would not have | 
					
						
							|  |  |  | 			// had IDs set on them yet | 
					
						
							|  |  |  | 			if gotEmoji.URI == maybeEmoji.URI && gotEmoji.ID == maybeEmoji.ID { | 
					
						
							|  |  |  | 				// this got emoji was present already in the maybeEmoji, | 
					
						
							|  |  |  | 				// so we can stop checking through maybeEmojis | 
					
						
							|  |  |  | 				wasPresent = true | 
					
						
							|  |  |  | 				break | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if !wasPresent { | 
					
						
							|  |  |  | 			// at least one gotEmojis was not present in | 
					
						
							|  |  |  | 			// the maybeEmojis, so we can stop checking now | 
					
						
							|  |  |  | 			changed = true | 
					
						
							|  |  |  | 			targetAccount.Emojis = gotEmojis | 
					
						
							|  |  |  | 			targetAccount.EmojiIDs = gotEmojiIDs | 
					
						
							|  |  |  | 			return changed, nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return changed, nil | 
					
						
							|  |  |  | } |