| 
									
										
										
										
											2023-03-12 16:00:57 +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/>. | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | package federatingdb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2024-04-26 13:50:46 +01:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	"net/url" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-31 15:59:12 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/ap" | 
					
						
							| 
									
										
										
										
											2024-04-26 13:50:46 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/db" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtserror" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							| 
									
										
										
										
											2022-07-19 09:47:55 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/log" | 
					
						
							| 
									
										
										
										
											2021-08-31 15:59:12 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/messages" | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Delete removes the entry with the given id. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Delete is only called for federated objects. Deletes from the Social | 
					
						
							|  |  |  | // Protocol instead call Update to create a Tombstone. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The library makes this call only after acquiring a lock first. | 
					
						
							|  |  |  | func (f *federatingDB) Delete(ctx context.Context, id *url.URL) error { | 
					
						
							| 
									
										
										
										
											2024-09-17 19:35:47 +00:00
										 |  |  | 	log.DebugKV(ctx, "id", id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-17 15:20:39 +01:00
										 |  |  | 	activityContext := getActivityContext(ctx) | 
					
						
							|  |  |  | 	if activityContext.internal { | 
					
						
							| 
									
										
										
										
											2023-06-13 16:47:56 +02:00
										 |  |  | 		return nil // Already processed. | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-26 13:50:46 +01:00
										 |  |  | 	// Extract receiving / requesting accounts. | 
					
						
							|  |  |  | 	requesting := activityContext.requestingAcct | 
					
						
							|  |  |  | 	receiving := activityContext.receivingAcct | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Serialize deleted ID URI. | 
					
						
							|  |  |  | 	// (may be status OR account) | 
					
						
							|  |  |  | 	uriStr := id.String() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var ( | 
					
						
							|  |  |  | 		ok  bool | 
					
						
							|  |  |  | 		err error | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Try delete as an account URI. | 
					
						
							|  |  |  | 	ok, err = f.deleteAccount(ctx, | 
					
						
							|  |  |  | 		requesting, | 
					
						
							|  |  |  | 		receiving, | 
					
						
							|  |  |  | 		uriStr, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} else if ok { | 
					
						
							|  |  |  | 		// success! | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Try delete as a status URI. | 
					
						
							|  |  |  | 	ok, err = f.deleteStatus(ctx, | 
					
						
							|  |  |  | 		requesting, | 
					
						
							|  |  |  | 		receiving, | 
					
						
							|  |  |  | 		uriStr, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} else if ok { | 
					
						
							|  |  |  | 		// success! | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-17 19:35:47 +00:00
										 |  |  | 	log.Debugf(ctx, "unknown iri: %s", uriStr) | 
					
						
							| 
									
										
										
										
											2024-04-26 13:50:46 +01:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (f *federatingDB) deleteAccount( | 
					
						
							|  |  |  | 	ctx context.Context, | 
					
						
							|  |  |  | 	requesting *gtsmodel.Account, | 
					
						
							|  |  |  | 	receiving *gtsmodel.Account, | 
					
						
							|  |  |  | 	uri string, // target account | 
					
						
							|  |  |  | ) ( | 
					
						
							|  |  |  | 	bool, // success? | 
					
						
							|  |  |  | 	error, // any error | 
					
						
							|  |  |  | ) { | 
					
						
							|  |  |  | 	account, err := f.state.DB.GetAccountByURI(ctx, uri) | 
					
						
							|  |  |  | 	if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							|  |  |  | 		return false, gtserror.Newf("error getting account: %w", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if account != nil { | 
					
						
							|  |  |  | 		// Ensure requesting account is | 
					
						
							|  |  |  | 		// only trying to delete itself. | 
					
						
							|  |  |  | 		if account.ID != requesting.ID { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// TODO: handled forwarded deletes, | 
					
						
							|  |  |  | 			// for now we silently drop this. | 
					
						
							|  |  |  | 			return true, nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		log.Debugf(ctx, "deleting account: %s", account.URI) | 
					
						
							|  |  |  | 		f.state.Workers.Federator.Queue.Push(&messages.FromFediAPI{ | 
					
						
							| 
									
										
										
										
											2024-06-06 15:43:25 +02:00
										 |  |  | 			APObjectType:   ap.ActorPerson, | 
					
						
							| 
									
										
										
										
											2024-04-26 13:50:46 +01:00
										 |  |  | 			APActivityType: ap.ActivityDelete, | 
					
						
							|  |  |  | 			GTSModel:       account, | 
					
						
							|  |  |  | 			Receiving:      receiving, | 
					
						
							|  |  |  | 			Requesting:     requesting, | 
					
						
							| 
									
										
										
										
											2022-04-28 13:23:11 +01:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2024-04-26 13:50:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return true, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return false, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (f *federatingDB) deleteStatus( | 
					
						
							|  |  |  | 	ctx context.Context, | 
					
						
							|  |  |  | 	requesting *gtsmodel.Account, | 
					
						
							|  |  |  | 	receiving *gtsmodel.Account, | 
					
						
							|  |  |  | 	uri string, // target status | 
					
						
							|  |  |  | ) ( | 
					
						
							|  |  |  | 	bool, // success? | 
					
						
							|  |  |  | 	error, // any error | 
					
						
							|  |  |  | ) { | 
					
						
							|  |  |  | 	status, err := f.state.DB.GetStatusByURI(ctx, uri) | 
					
						
							|  |  |  | 	if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							|  |  |  | 		return false, gtserror.Newf("error getting status: %w", err) | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-26 13:50:46 +01:00
										 |  |  | 	if status != nil { | 
					
						
							|  |  |  | 		// Ensure requesting account is only | 
					
						
							|  |  |  | 		// trying to delete its own statuses. | 
					
						
							|  |  |  | 		if status.AccountID != requesting.ID { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// TODO: handled forwarded deletes, | 
					
						
							|  |  |  | 			// for now we silently drop this. | 
					
						
							|  |  |  | 			return true, nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		log.Debugf(ctx, "deleting status: %s", status.URI) | 
					
						
							|  |  |  | 		f.state.Workers.Federator.Queue.Push(&messages.FromFediAPI{ | 
					
						
							|  |  |  | 			APObjectType:   ap.ObjectNote, | 
					
						
							|  |  |  | 			APActivityType: ap.ActivityDelete, | 
					
						
							|  |  |  | 			GTSModel:       status, | 
					
						
							|  |  |  | 			Receiving:      receiving, | 
					
						
							|  |  |  | 			Requesting:     requesting, | 
					
						
							| 
									
										
										
										
											2022-04-28 13:23:11 +01:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2024-04-26 13:50:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return true, nil | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-26 13:50:46 +01:00
										 |  |  | 	return false, nil | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | } |