| 
									
										
										
										
											2024-01-16 17:22:44 +01:00
										 |  |  | // GoToSocial | 
					
						
							|  |  |  | // Copyright (C) GoToSocial Authors admin@gotosocial.org | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // 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 account | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"net/url" | 
					
						
							|  |  |  | 	"slices" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtserror" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							| 
									
										
										
										
											2024-11-11 15:45:19 +00:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/util/xslices" | 
					
						
							| 
									
										
										
										
											2024-01-16 17:22:44 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (p *Processor) Alias( | 
					
						
							|  |  |  | 	ctx context.Context, | 
					
						
							|  |  |  | 	account *gtsmodel.Account, | 
					
						
							|  |  |  | 	newAKAURIStrs []string, | 
					
						
							|  |  |  | ) (*apimodel.Account, gtserror.WithCode) { | 
					
						
							|  |  |  | 	if slices.Equal( | 
					
						
							|  |  |  | 		newAKAURIStrs, | 
					
						
							|  |  |  | 		account.AlsoKnownAsURIs, | 
					
						
							|  |  |  | 	) { | 
					
						
							|  |  |  | 		// No changes to do | 
					
						
							|  |  |  | 		// here. Return early. | 
					
						
							|  |  |  | 		return p.c.GetAPIAccountSensitive(ctx, account) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	newLen := len(newAKAURIStrs) | 
					
						
							|  |  |  | 	if newLen == 0 { | 
					
						
							|  |  |  | 		// Simply unset existing | 
					
						
							|  |  |  | 		// aliases and return early. | 
					
						
							|  |  |  | 		account.AlsoKnownAsURIs = nil | 
					
						
							|  |  |  | 		account.AlsoKnownAs = nil | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		err := p.state.DB.UpdateAccount(ctx, account, "also_known_as_uris") | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			err := gtserror.Newf("db error updating also_known_as_uri: %w", err) | 
					
						
							|  |  |  | 			return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return p.c.GetAPIAccountSensitive(ctx, account) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// We need to set new AKA URIs! | 
					
						
							|  |  |  | 	// | 
					
						
							|  |  |  | 	// First parse them to URI ptrs and | 
					
						
							|  |  |  | 	// normalized string representations. | 
					
						
							|  |  |  | 	// | 
					
						
							|  |  |  | 	// Use this cheeky type to avoid | 
					
						
							|  |  |  | 	// repeatedly calling uri.String(). | 
					
						
							|  |  |  | 	type uri struct { | 
					
						
							|  |  |  | 		uri *url.URL // Parsed URI. | 
					
						
							|  |  |  | 		str string   // uri.String(). | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	newAKAs := make([]uri, newLen) | 
					
						
							|  |  |  | 	for i, newAKAURIStr := range newAKAURIStrs { | 
					
						
							|  |  |  | 		newAKAURI, err := url.Parse(newAKAURIStr) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			err := fmt.Errorf( | 
					
						
							|  |  |  | 				"invalid also_known_as_uri (%s) provided in account alias request: %w", | 
					
						
							|  |  |  | 				newAKAURIStr, err, | 
					
						
							|  |  |  | 			) | 
					
						
							|  |  |  | 			return nil, gtserror.NewErrorBadRequest(err, err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// We only deref http or https, so check this. | 
					
						
							|  |  |  | 		if newAKAURI.Scheme != "https" && newAKAURI.Scheme != "http" { | 
					
						
							|  |  |  | 			err := fmt.Errorf( | 
					
						
							|  |  |  | 				"invalid also_known_as_uri (%s) provided in account alias request: %w", | 
					
						
							|  |  |  | 				newAKAURIStr, errors.New("uri must not be empty and scheme must be http or https"), | 
					
						
							|  |  |  | 			) | 
					
						
							|  |  |  | 			return nil, gtserror.NewErrorBadRequest(err, err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		newAKAs[i].uri = newAKAURI | 
					
						
							|  |  |  | 		newAKAs[i].str = newAKAURI.String() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// For each deduped entry, get and | 
					
						
							|  |  |  | 	// check the target account, and set. | 
					
						
							|  |  |  | 	for _, newAKA := range newAKAs { | 
					
						
							|  |  |  | 		// Don't let account do anything | 
					
						
							|  |  |  | 		// daft by aliasing to itself. | 
					
						
							| 
									
										
										
										
											2024-01-20 12:45:43 +01:00
										 |  |  | 		if newAKA.str == account.URI || | 
					
						
							|  |  |  | 			newAKA.str == account.URL { | 
					
						
							| 
									
										
										
										
											2024-01-16 17:22:44 +01:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-20 12:45:43 +01:00
										 |  |  | 		// Ensure we have account dereferenced. | 
					
						
							| 
									
										
										
										
											2025-04-06 14:39:40 +02:00
										 |  |  | 		// | 
					
						
							|  |  |  | 		// As this comes from user input, allow checking | 
					
						
							|  |  |  | 		// by URL to make things easier, not just to an | 
					
						
							|  |  |  | 		// exact AP URI (which a user might not even know). | 
					
						
							| 
									
										
										
										
											2024-01-20 12:45:43 +01:00
										 |  |  | 		targetAccount, _, err := p.federator.GetAccountByURI(ctx, | 
					
						
							|  |  |  | 			account.Username, | 
					
						
							|  |  |  | 			newAKA.uri, | 
					
						
							| 
									
										
										
										
											2025-04-06 14:39:40 +02:00
										 |  |  | 			true, | 
					
						
							| 
									
										
										
										
											2024-01-20 12:45:43 +01:00
										 |  |  | 		) | 
					
						
							| 
									
										
										
										
											2024-01-16 17:22:44 +01:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			err := fmt.Errorf( | 
					
						
							|  |  |  | 				"error dereferencing also_known_as_uri (%s) account: %w", | 
					
						
							|  |  |  | 				newAKA.str, err, | 
					
						
							|  |  |  | 			) | 
					
						
							|  |  |  | 			return nil, gtserror.NewErrorUnprocessableEntity(err, err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-20 12:45:43 +01:00
										 |  |  | 		// Target must not be suspended. | 
					
						
							| 
									
										
										
										
											2024-01-16 17:22:44 +01:00
										 |  |  | 		if !targetAccount.SuspendedAt.IsZero() { | 
					
						
							|  |  |  | 			err := fmt.Errorf( | 
					
						
							|  |  |  | 				"target account %s is suspended from this instance; "+ | 
					
						
							|  |  |  | 					"you will not be able to set alsoKnownAs to that account", | 
					
						
							|  |  |  | 				newAKA.str, | 
					
						
							|  |  |  | 			) | 
					
						
							|  |  |  | 			return nil, gtserror.NewErrorUnprocessableEntity(err, err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Alrighty-roo, looks good, add this one. | 
					
						
							| 
									
										
										
										
											2024-01-20 12:45:43 +01:00
										 |  |  | 		account.AlsoKnownAsURIs = append(account.AlsoKnownAsURIs, targetAccount.URI) | 
					
						
							| 
									
										
										
										
											2024-01-16 17:22:44 +01:00
										 |  |  | 		account.AlsoKnownAs = append(account.AlsoKnownAs, targetAccount) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-20 12:45:43 +01:00
										 |  |  | 	// Dedupe URIs + accounts, in case someone | 
					
						
							|  |  |  | 	// provided both an account URL and an | 
					
						
							|  |  |  | 	// account URI above, for the same account. | 
					
						
							| 
									
										
										
										
											2024-11-11 15:45:19 +00:00
										 |  |  | 	account.AlsoKnownAsURIs = xslices.Deduplicate(account.AlsoKnownAsURIs) | 
					
						
							|  |  |  | 	account.AlsoKnownAs = xslices.DeduplicateFunc( | 
					
						
							| 
									
										
										
										
											2024-01-20 12:45:43 +01:00
										 |  |  | 		account.AlsoKnownAs, | 
					
						
							|  |  |  | 		func(a *gtsmodel.Account) string { | 
					
						
							|  |  |  | 			return a.URI | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-16 17:22:44 +01:00
										 |  |  | 	err := p.state.DB.UpdateAccount(ctx, account, "also_known_as_uris") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		err := gtserror.Newf("db error updating also_known_as_uri: %w", err) | 
					
						
							|  |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return p.c.GetAPIAccountSensitive(ctx, account) | 
					
						
							|  |  |  | } |