| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |    GoToSocial | 
					
						
							| 
									
										
										
										
											2021-12-20 18:42:19 +01:00
										 |  |  |    Copyright (C) 2021-2022 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" | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2022-01-24 18:12:04 +01:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/sirupsen/logrus" | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/id" | 
					
						
							| 
									
										
										
										
											2022-01-09 18:41:22 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/media" | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/transport" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | func instanceAccount(account *gtsmodel.Account) bool { | 
					
						
							|  |  |  | 	return strings.EqualFold(account.Username, account.Domain) || | 
					
						
							|  |  |  | 		account.FollowersURI == "" || | 
					
						
							|  |  |  | 		account.FollowingURI == "" || | 
					
						
							|  |  |  | 		(account.Username == "internal.fetch" && strings.Contains(account.Note, "internal service actor")) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | // GetRemoteAccount completely dereferences a remote account, converts it to a GtS model account, | 
					
						
							|  |  |  | // puts it in the database, and returns it to a caller. The boolean indicates whether the account is new | 
					
						
							|  |  |  | // to us or not. If we haven't seen the account before, bool will be true. If we have seen the account before, | 
					
						
							|  |  |  | // it will be false. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Refresh indicates whether--if the account exists in our db already--it should be refreshed by calling | 
					
						
							|  |  |  | // the remote instance again. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // SIDE EFFECTS: remote account will be stored in the database, or updated if it already exists (and refresh is true). | 
					
						
							| 
									
										
										
										
											2022-01-24 18:12:04 +01:00
										 |  |  | func (d *deref) GetRemoteAccount(ctx context.Context, username string, remoteAccountID *url.URL, blocking bool, refresh bool) (*gtsmodel.Account, error) { | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	new := true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 	// check if we already have the account in our db, and just return it unless we'd doing a refresh | 
					
						
							|  |  |  | 	remoteAccount, err := d.db.GetAccountByURI(ctx, remoteAccountID.String()) | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 	if err == nil { | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 		new = false | 
					
						
							|  |  |  | 		if !refresh { | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 			// make sure the account fields are populated before returning: | 
					
						
							|  |  |  | 			// even if we're not doing a refresh, the caller might want to block | 
					
						
							|  |  |  | 			// until everything is loaded | 
					
						
							| 
									
										
										
										
											2022-01-24 18:12:04 +01:00
										 |  |  | 			if err := d.populateAccountFields(ctx, remoteAccount, username, refresh, blocking); err != nil { | 
					
						
							|  |  |  | 				return nil, fmt.Errorf("GetRemoteAccount: error populating remoteAccount fields: %s", err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			updatedAccount, err := d.db.UpdateAccount(ctx, remoteAccount) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return nil, fmt.Errorf("GetRemoteAccount: error updating remoteAccount: %s", err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			return updatedAccount, err | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if new { | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		// we haven't seen this account before: dereference it from remote | 
					
						
							|  |  |  | 		accountable, err := d.dereferenceAccountable(ctx, username, remoteAccountID) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 			return nil, fmt.Errorf("GetRemoteAccount: error dereferencing accountable: %s", err) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		newAccount, err := d.typeConverter.ASRepresentationToAccount(ctx, accountable, refresh) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("GetRemoteAccount: error converting accountable to account: %s", err) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		ulid, err := id.NewRandomULID() | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("GetRemoteAccount: error generating new id for account: %s", err) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		newAccount.ID = ulid | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err := d.populateAccountFields(ctx, newAccount, username, refresh, blocking); err != nil { | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("GetRemoteAccount: error populating further account fields: %s", err) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		if err := d.db.Put(ctx, newAccount); err != nil { | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("GetRemoteAccount: error putting new account: %s", err) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return newAccount, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// we have seen this account before, but we have to refresh it | 
					
						
							|  |  |  | 	refreshedAccountable, err := d.dereferenceAccountable(ctx, username, remoteAccountID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("GetRemoteAccount: error dereferencing refreshedAccountable: %s", err) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 	refreshedAccount, err := d.typeConverter.ASRepresentationToAccount(ctx, refreshedAccountable, refresh) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("GetRemoteAccount: error converting refreshedAccountable to refreshedAccount: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	refreshedAccount.ID = remoteAccount.ID | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := d.populateAccountFields(ctx, refreshedAccount, username, refresh, blocking); err != nil { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("GetRemoteAccount: error populating further refreshedAccount fields: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	updatedAccount, err := d.db.UpdateAccount(ctx, refreshedAccount) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("GetRemoteAccount: error updating refreshedAccount: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return updatedAccount, 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) { | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	d.startHandshake(username, remoteAccountID) | 
					
						
							|  |  |  | 	defer d.stopHandshake(username, remoteAccountID) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	if blocked, err := d.db.IsDomainBlocked(ctx, remoteAccountID.Host); blocked || err != nil { | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 		return nil, fmt.Errorf("DereferenceAccountable: domain %s is blocked", remoteAccountID.Host) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	transport, err := d.transportController.NewTransportForUsername(ctx, username) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("DereferenceAccountable: transport err: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("DereferenceAccountable: error deferencing %s: %s", remoteAccountID.String(), err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m := make(map[string]interface{}) | 
					
						
							|  |  |  | 	if err := json.Unmarshal(b, &m); err != nil { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("DereferenceAccountable: error unmarshalling bytes into json: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("DereferenceAccountable: error resolving json into ap vocab type: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch t.GetTypeName() { | 
					
						
							| 
									
										
										
										
											2021-09-03 10:30:40 +02:00
										 |  |  | 	case ap.ActorApplication: | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 		p, ok := t.(vocab.ActivityStreamsApplication) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return nil, errors.New("DereferenceAccountable: error resolving type as activitystreams application") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return p, nil | 
					
						
							| 
									
										
										
										
											2021-09-30 12:27:42 +02:00
										 |  |  | 	case ap.ActorGroup: | 
					
						
							|  |  |  | 		p, ok := t.(vocab.ActivityStreamsGroup) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return nil, errors.New("DereferenceAccountable: error resolving type as activitystreams group") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return p, nil | 
					
						
							|  |  |  | 	case ap.ActorOrganization: | 
					
						
							|  |  |  | 		p, ok := t.(vocab.ActivityStreamsOrganization) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return nil, errors.New("DereferenceAccountable: error resolving type as activitystreams organization") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return p, nil | 
					
						
							|  |  |  | 	case ap.ActorPerson: | 
					
						
							|  |  |  | 		p, ok := t.(vocab.ActivityStreamsPerson) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return nil, errors.New("DereferenceAccountable: error resolving type as activitystreams person") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return p, nil | 
					
						
							| 
									
										
										
										
											2021-09-03 10:30:40 +02:00
										 |  |  | 	case ap.ActorService: | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 		p, ok := t.(vocab.ActivityStreamsService) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return nil, errors.New("DereferenceAccountable: error resolving type as activitystreams service") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return p, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil, fmt.Errorf("DereferenceAccountable: type name %s not supported", t.GetTypeName()) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | // populateAccountFields populates any fields on the given account that weren't populated by the initial | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | // dereferencing. This includes things like header and avatar etc. | 
					
						
							| 
									
										
										
										
											2022-01-24 18:12:04 +01:00
										 |  |  | func (d *deref) populateAccountFields(ctx context.Context, account *gtsmodel.Account, requestingUsername string, blocking bool, refresh bool) error { | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 	// if we're dealing with an instance account, just bail, we don't need to do anything | 
					
						
							|  |  |  | 	if instanceAccount(account) { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	accountURI, err := url.Parse(account.URI) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		return fmt.Errorf("populateAccountFields: couldn't parse account URI %s: %s", account.URI, err) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	if blocked, err := d.db.IsDomainBlocked(ctx, accountURI.Host); blocked || err != nil { | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		return fmt.Errorf("populateAccountFields: domain %s is blocked", accountURI.Host) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	t, err := d.transportController.NewTransportForUsername(ctx, requestingUsername) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		return fmt.Errorf("populateAccountFields: error getting transport for user: %s", err) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// fetch the header and avatar | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 	if err := d.fetchRemoteAccountMedia(ctx, account, t, refresh, blocking); err != nil { | 
					
						
							|  |  |  | 		return fmt.Errorf("populateAccountFields: error fetching header/avi for account: %s", err) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | // | 
					
						
							|  |  |  | // 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. | 
					
						
							| 
									
										
										
										
											2022-01-24 18:12:04 +01:00
										 |  |  | func (d *deref) fetchRemoteAccountMedia(ctx context.Context, targetAccount *gtsmodel.Account, t transport.Transport, blocking bool, refresh bool) error { | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	accountURI, err := url.Parse(targetAccount.URI) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		return fmt.Errorf("fetchRemoteAccountMedia: couldn't parse account URI %s: %s", targetAccount.URI, err) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	if blocked, err := d.db.IsDomainBlocked(ctx, accountURI.Host); blocked || err != nil { | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		return fmt.Errorf("fetchRemoteAccountMedia: domain %s is blocked", accountURI.Host) | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if targetAccount.AvatarRemoteURL != "" && (targetAccount.AvatarMediaAttachmentID == "" || refresh) { | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		var processingMedia *media.ProcessingMedia | 
					
						
							| 
									
										
										
										
											2022-01-08 17:17:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		// first check if we're already processing this media | 
					
						
							|  |  |  | 		d.dereferencingAvatarsLock.Lock() | 
					
						
							|  |  |  | 		if alreadyProcessing, ok := d.dereferencingAvatars[targetAccount.ID]; ok { | 
					
						
							|  |  |  | 			// we're already on it, no worries | 
					
						
							|  |  |  | 			processingMedia = alreadyProcessing | 
					
						
							| 
									
										
										
										
											2022-01-08 17:17:01 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		d.dereferencingAvatarsLock.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if processingMedia == nil { | 
					
						
							|  |  |  | 			// we're not already processing it so start now | 
					
						
							|  |  |  | 			avatarIRI, err := url.Parse(targetAccount.AvatarRemoteURL) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			data := func(innerCtx context.Context) (io.Reader, int, error) { | 
					
						
							|  |  |  | 				return t.DereferenceMedia(innerCtx, avatarIRI) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			avatar := true | 
					
						
							|  |  |  | 			newProcessing, err := d.mediaManager.ProcessMedia(ctx, data, targetAccount.ID, &media.AdditionalMediaInfo{ | 
					
						
							|  |  |  | 				RemoteURL: &targetAccount.AvatarRemoteURL, | 
					
						
							|  |  |  | 				Avatar:    &avatar, | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// store it in our map to indicate it's in process | 
					
						
							|  |  |  | 			d.dereferencingAvatarsLock.Lock() | 
					
						
							|  |  |  | 			d.dereferencingAvatars[targetAccount.ID] = newProcessing | 
					
						
							|  |  |  | 			d.dereferencingAvatarsLock.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			processingMedia = newProcessing | 
					
						
							| 
									
										
										
										
											2022-01-08 17:17:01 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		// block until loaded if required... | 
					
						
							|  |  |  | 		if blocking { | 
					
						
							|  |  |  | 			if err := lockAndLoad(ctx, d.dereferencingAvatarsLock, processingMedia, d.dereferencingAvatars, targetAccount.ID); err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} 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)) | 
					
						
							|  |  |  | 				if err := lockAndLoad(dlCtx, d.dereferencingAvatarsLock, processingMedia, d.dereferencingAvatars, targetAccount.ID); err != nil { | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 					logrus.Errorf("fetchRemoteAccountMedia: error during async lock and load of avatar: %s", err) | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if targetAccount.HeaderRemoteURL != "" && (targetAccount.HeaderMediaAttachmentID == "" || refresh) { | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		var processingMedia *media.ProcessingMedia | 
					
						
							| 
									
										
										
										
											2022-01-08 17:17:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		// first check if we're already processing this media | 
					
						
							|  |  |  | 		d.dereferencingHeadersLock.Lock() | 
					
						
							|  |  |  | 		if alreadyProcessing, ok := d.dereferencingHeaders[targetAccount.ID]; ok { | 
					
						
							|  |  |  | 			// we're already on it, no worries | 
					
						
							|  |  |  | 			processingMedia = alreadyProcessing | 
					
						
							| 
									
										
										
										
											2022-01-08 17:17:01 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		d.dereferencingHeadersLock.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if processingMedia == nil { | 
					
						
							|  |  |  | 			// we're not already processing it so start now | 
					
						
							|  |  |  | 			headerIRI, err := url.Parse(targetAccount.HeaderRemoteURL) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			data := func(innerCtx context.Context) (io.Reader, int, error) { | 
					
						
							|  |  |  | 				return t.DereferenceMedia(innerCtx, headerIRI) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			header := true | 
					
						
							|  |  |  | 			newProcessing, err := d.mediaManager.ProcessMedia(ctx, data, targetAccount.ID, &media.AdditionalMediaInfo{ | 
					
						
							|  |  |  | 				RemoteURL: &targetAccount.HeaderRemoteURL, | 
					
						
							|  |  |  | 				Header:    &header, | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// store it in our map to indicate it's in process | 
					
						
							|  |  |  | 			d.dereferencingHeadersLock.Lock() | 
					
						
							|  |  |  | 			d.dereferencingHeaders[targetAccount.ID] = newProcessing | 
					
						
							|  |  |  | 			d.dereferencingHeadersLock.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			processingMedia = newProcessing | 
					
						
							| 
									
										
										
										
											2022-01-08 17:17:01 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 		// block until loaded if required... | 
					
						
							|  |  |  | 		if blocking { | 
					
						
							|  |  |  | 			if err := lockAndLoad(ctx, d.dereferencingHeadersLock, processingMedia, d.dereferencingHeaders, targetAccount.ID); err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} 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)) | 
					
						
							|  |  |  | 				if err := lockAndLoad(dlCtx, d.dereferencingHeadersLock, processingMedia, d.dereferencingHeaders, targetAccount.ID); err != nil { | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 					logrus.Errorf("fetchRemoteAccountMedia: error during async lock and load of header: %s", err) | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-10 13:32:39 +02:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-01-24 13:12:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | func lockAndLoad(ctx context.Context, lock *sync.Mutex, processing *media.ProcessingMedia, processingMap map[string]*media.ProcessingMedia, accountID string) error { | 
					
						
							|  |  |  | 	// whatever happens, remove the in-process media from the map | 
					
						
							|  |  |  | 	defer func() { | 
					
						
							|  |  |  | 		lock.Lock() | 
					
						
							|  |  |  | 		delete(processingMap, accountID) | 
					
						
							|  |  |  | 		lock.Unlock() | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// try and load it | 
					
						
							|  |  |  | 	_, err := processing.LoadAttachment(ctx) | 
					
						
							|  |  |  | 	return err | 
					
						
							|  |  |  | } |