| 
									
										
										
										
											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" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-02 12:48:00 +01:00
										 |  |  | 	"codeberg.org/superseriousbusiness/activity/streams/vocab" | 
					
						
							| 
									
										
										
										
											2021-08-31 15:59:12 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/ap" | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/db" | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtscontext" | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtserror" | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							| 
									
										
										
										
											2022-07-19 09:47:55 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/log" | 
					
						
							| 
									
										
										
										
											2025-01-24 16:36:34 +00:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/messages" | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (f *federatingDB) Undo(ctx context.Context, undo vocab.ActivityStreamsUndo) error { | 
					
						
							| 
									
										
										
										
											2024-09-17 19:35:47 +00:00
										 |  |  | 	log.DebugKV(ctx, "undo", serialize{undo}) | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-02-17 15:20:39 +01:00
										 |  |  | 	requestingAcct := activityContext.requestingAcct | 
					
						
							|  |  |  | 	receivingAcct := activityContext.receivingAcct | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-04 13:09:42 +01:00
										 |  |  | 	for _, object := range ap.ExtractObjects(undo) { | 
					
						
							|  |  |  | 		// Try to get object as vocab.Type, | 
					
						
							|  |  |  | 		// else skip handling (likely) IRI. | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 		asType := object.GetType() | 
					
						
							|  |  |  | 		if asType == nil { | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-17 19:35:47 +00:00
										 |  |  | 		// Check and handle any vocab.Type objects. | 
					
						
							|  |  |  | 		switch name := asType.GetTypeName(); name { | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// UNDO FOLLOW | 
					
						
							| 
									
										
										
										
											2021-09-03 10:30:40 +02:00
										 |  |  | 		case ap.ActivityFollow: | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 			if err := f.undoFollow( | 
					
						
							|  |  |  | 				ctx, | 
					
						
							|  |  |  | 				receivingAcct, | 
					
						
							|  |  |  | 				requestingAcct, | 
					
						
							|  |  |  | 				undo, | 
					
						
							|  |  |  | 				asType, | 
					
						
							|  |  |  | 			); err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// UNDO LIKE | 
					
						
							| 
									
										
										
										
											2021-09-03 10:30:40 +02:00
										 |  |  | 		case ap.ActivityLike: | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 			if err := f.undoLike( | 
					
						
							|  |  |  | 				ctx, | 
					
						
							|  |  |  | 				receivingAcct, | 
					
						
							|  |  |  | 				requestingAcct, | 
					
						
							|  |  |  | 				undo, | 
					
						
							|  |  |  | 				asType, | 
					
						
							|  |  |  | 			); err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// UNDO BLOCK | 
					
						
							| 
									
										
										
										
											2021-09-03 10:30:40 +02:00
										 |  |  | 		case ap.ActivityBlock: | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 			if err := f.undoBlock( | 
					
						
							|  |  |  | 				ctx, | 
					
						
							|  |  |  | 				receivingAcct, | 
					
						
							|  |  |  | 				requestingAcct, | 
					
						
							|  |  |  | 				undo, | 
					
						
							|  |  |  | 				asType, | 
					
						
							|  |  |  | 			); err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-24 16:36:34 +00:00
										 |  |  | 		// UNDO ANNOUNCE | 
					
						
							|  |  |  | 		case ap.ActivityAnnounce: | 
					
						
							|  |  |  | 			if err := f.undoAnnounce( | 
					
						
							|  |  |  | 				ctx, | 
					
						
							|  |  |  | 				receivingAcct, | 
					
						
							|  |  |  | 				requestingAcct, | 
					
						
							|  |  |  | 				undo, | 
					
						
							|  |  |  | 				asType, | 
					
						
							|  |  |  | 			); err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-17 19:35:47 +00:00
										 |  |  | 		// UNHANDLED | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			log.Debugf(ctx, "unhandled object type: %s", name) | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (f *federatingDB) undoFollow( | 
					
						
							|  |  |  | 	ctx context.Context, | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	receivingAcct *gtsmodel.Account, | 
					
						
							|  |  |  | 	requestingAcct *gtsmodel.Account, | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	undo vocab.ActivityStreamsUndo, | 
					
						
							|  |  |  | 	t vocab.Type, | 
					
						
							|  |  |  | ) error { | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	asFollow, ok := t.(vocab.ActivityStreamsFollow) | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	if !ok { | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 		err := fmt.Errorf("%T not parseable as vocab.ActivityStreamsFollow", t) | 
					
						
							|  |  |  | 		return gtserror.SetMalformed(err) | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	// Make sure the Undo | 
					
						
							|  |  |  | 	// actor owns the target. | 
					
						
							|  |  |  | 	if !sameActor( | 
					
						
							|  |  |  | 		undo.GetActivityStreamsActor(), | 
					
						
							|  |  |  | 		asFollow.GetActivityStreamsActor(), | 
					
						
							|  |  |  | 	) { | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 		// Ignore this Activity. | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	// Convert AS Follow to barebones *gtsmodel.Follow, | 
					
						
							|  |  |  | 	// retrieving origin + target accts from the db. | 
					
						
							|  |  |  | 	follow, err := f.converter.ASFollowToFollow( | 
					
						
							|  |  |  | 		gtscontext.SetBarebones(ctx), | 
					
						
							|  |  |  | 		asFollow, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							|  |  |  | 		err := gtserror.Newf("error converting AS Follow to follow: %w", err) | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	// We were missing origin or | 
					
						
							|  |  |  | 	// target for this Follow, so | 
					
						
							|  |  |  | 	// we cannot Undo anything. | 
					
						
							|  |  |  | 	if follow == nil { | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	// Ensure addressee is follow target. | 
					
						
							|  |  |  | 	if follow.TargetAccountID != receivingAcct.ID { | 
					
						
							|  |  |  | 		const text = "receivingAcct was not Follow target" | 
					
						
							|  |  |  | 		return gtserror.NewErrorForbidden(errors.New(text), text) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 12:59:37 +01:00
										 |  |  | 	// Ensure requester is follow origin. | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	if follow.AccountID != requestingAcct.ID { | 
					
						
							|  |  |  | 		const text = "requestingAcct was not Follow origin" | 
					
						
							|  |  |  | 		return gtserror.NewErrorForbidden(errors.New(text), text) | 
					
						
							| 
									
										
										
										
											2024-02-06 12:59:37 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	// Delete any existing follow with this URI. | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	err = f.state.DB.DeleteFollowByURI(ctx, follow.URI) | 
					
						
							|  |  |  | 	if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							|  |  |  | 		err := gtserror.Newf("db error deleting follow: %w", err) | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Delete any existing follow request with this URI. | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	err = f.state.DB.DeleteFollowRequestByURI(ctx, follow.URI) | 
					
						
							|  |  |  | 	if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							|  |  |  | 		err := gtserror.Newf("db error deleting follow request: %w", err) | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	log.Debug(ctx, "Follow undone") | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (f *federatingDB) undoLike( | 
					
						
							|  |  |  | 	ctx context.Context, | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	receivingAcct *gtsmodel.Account, | 
					
						
							|  |  |  | 	requestingAcct *gtsmodel.Account, | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	undo vocab.ActivityStreamsUndo, | 
					
						
							|  |  |  | 	t vocab.Type, | 
					
						
							|  |  |  | ) error { | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	asLike, ok := t.(vocab.ActivityStreamsLike) | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	if !ok { | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 		err := fmt.Errorf("%T not parseable as vocab.ActivityStreamsLike", t) | 
					
						
							|  |  |  | 		return gtserror.SetMalformed(err) | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	// Make sure the Undo | 
					
						
							|  |  |  | 	// actor owns the target. | 
					
						
							|  |  |  | 	if !sameActor( | 
					
						
							|  |  |  | 		undo.GetActivityStreamsActor(), | 
					
						
							|  |  |  | 		asLike.GetActivityStreamsActor(), | 
					
						
							|  |  |  | 	) { | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 		// Ignore this Activity. | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	// Convert AS Like to barebones *gtsmodel.StatusFave, | 
					
						
							|  |  |  | 	// retrieving liking acct and target status from the DB. | 
					
						
							|  |  |  | 	fave, err := f.converter.ASLikeToFave( | 
					
						
							|  |  |  | 		gtscontext.SetBarebones(ctx), | 
					
						
							|  |  |  | 		asLike, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							|  |  |  | 		err := gtserror.Newf("error converting AS Like to fave: %w", err) | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	// We were missing status, account, | 
					
						
							|  |  |  | 	// or other for this Like, so we | 
					
						
							|  |  |  | 	// cannot Undo anything. | 
					
						
							|  |  |  | 	if fave == nil { | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	// Ensure addressee is fave target. | 
					
						
							|  |  |  | 	if fave.TargetAccountID != receivingAcct.ID { | 
					
						
							|  |  |  | 		const text = "receivingAcct was not Fave target" | 
					
						
							|  |  |  | 		return gtserror.NewErrorForbidden(errors.New(text), text) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 12:59:37 +01:00
										 |  |  | 	// Ensure requester is fave origin. | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	if fave.AccountID != requestingAcct.ID { | 
					
						
							|  |  |  | 		const text = "requestingAcct was not Fave origin" | 
					
						
							|  |  |  | 		return gtserror.NewErrorForbidden(errors.New(text), text) | 
					
						
							| 
									
										
										
										
											2024-02-06 12:59:37 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	// Fetch fave from the DB so we know the ID to delete it. | 
					
						
							|  |  |  | 	// | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	// Ignore URI on Likes, since we often get multiple Likes | 
					
						
							|  |  |  | 	// with the same target and account ID, but differing URIs. | 
					
						
							|  |  |  | 	// Instead, we'll select using account and target status. | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	// | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	// Regardless of the URI, we can read an Undo Like to mean | 
					
						
							|  |  |  | 	// "I don't want to fave this post anymore". | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	fave, err = f.state.DB.GetStatusFave( | 
					
						
							|  |  |  | 		gtscontext.SetBarebones(ctx), | 
					
						
							|  |  |  | 		fave.AccountID, | 
					
						
							|  |  |  | 		fave.StatusID, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							|  |  |  | 		err := gtserror.Newf( | 
					
						
							|  |  |  | 			"db error getting fave from %s targeting %s: %w", | 
					
						
							|  |  |  | 			fave.AccountID, fave.StatusID, err, | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	if fave == nil { | 
					
						
							|  |  |  | 		// We didn't have this fave | 
					
						
							|  |  |  | 		// stored anyway, so we can't | 
					
						
							|  |  |  | 		// Undo it, just ignore. | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Delete the fave. | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	if err := f.state.DB.DeleteStatusFaveByID(ctx, fave.ID); err != nil { | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 		err := gtserror.Newf("db error deleting fave %s: %w", fave.ID, err) | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	log.Debug(ctx, "Like undone") | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (f *federatingDB) undoBlock( | 
					
						
							|  |  |  | 	ctx context.Context, | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	receivingAcct *gtsmodel.Account, | 
					
						
							|  |  |  | 	requestingAcct *gtsmodel.Account, | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	undo vocab.ActivityStreamsUndo, | 
					
						
							|  |  |  | 	t vocab.Type, | 
					
						
							|  |  |  | ) error { | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	asBlock, ok := t.(vocab.ActivityStreamsBlock) | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	if !ok { | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 		err := fmt.Errorf("%T not parseable as vocab.ActivityStreamsBlock", t) | 
					
						
							|  |  |  | 		return gtserror.SetMalformed(err) | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	// Make sure the Undo | 
					
						
							|  |  |  | 	// actor owns the target. | 
					
						
							|  |  |  | 	if !sameActor( | 
					
						
							|  |  |  | 		undo.GetActivityStreamsActor(), | 
					
						
							|  |  |  | 		asBlock.GetActivityStreamsActor(), | 
					
						
							|  |  |  | 	) { | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 		// Ignore this Activity. | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	// Convert AS Block to barebones *gtsmodel.Block, | 
					
						
							|  |  |  | 	// retrieving origin + target accts from the DB. | 
					
						
							|  |  |  | 	block, err := f.converter.ASBlockToBlock( | 
					
						
							|  |  |  | 		gtscontext.SetBarebones(ctx), | 
					
						
							|  |  |  | 		asBlock, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							|  |  |  | 		err := gtserror.Newf("error converting AS Block to block: %w", err) | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	// We were missing origin or | 
					
						
							|  |  |  | 	// target for this Block, so | 
					
						
							|  |  |  | 	// we cannot Undo anything. | 
					
						
							|  |  |  | 	if block == nil { | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	// Ensure addressee is block target. | 
					
						
							|  |  |  | 	if block.TargetAccountID != receivingAcct.ID { | 
					
						
							|  |  |  | 		const text = "receivingAcct was not Block target" | 
					
						
							|  |  |  | 		return gtserror.NewErrorForbidden(errors.New(text), text) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 12:59:37 +01:00
										 |  |  | 	// Ensure requester is block origin. | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	if block.AccountID != requestingAcct.ID { | 
					
						
							|  |  |  | 		const text = "requestingAcct was not Block origin" | 
					
						
							|  |  |  | 		return gtserror.NewErrorForbidden(errors.New(text), text) | 
					
						
							| 
									
										
										
										
											2024-02-06 12:59:37 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 17:49:40 +02:00
										 |  |  | 	// Delete any existing block. | 
					
						
							|  |  |  | 	err = f.state.DB.DeleteBlockByURI(ctx, block.URI) | 
					
						
							|  |  |  | 	if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							|  |  |  | 		err := gtserror.Newf("db error deleting block: %w", err) | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 	log.Debug(ctx, "Block undone") | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-01-24 16:36:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (f *federatingDB) undoAnnounce( | 
					
						
							|  |  |  | 	ctx context.Context, | 
					
						
							|  |  |  | 	receivingAcct *gtsmodel.Account, | 
					
						
							|  |  |  | 	requestingAcct *gtsmodel.Account, | 
					
						
							|  |  |  | 	undo vocab.ActivityStreamsUndo, | 
					
						
							|  |  |  | 	t vocab.Type, | 
					
						
							|  |  |  | ) error { | 
					
						
							|  |  |  | 	asAnnounce, ok := t.(vocab.ActivityStreamsAnnounce) | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							|  |  |  | 		err := fmt.Errorf("%T not parseable as vocab.ActivityStreamsAnnounce", t) | 
					
						
							|  |  |  | 		return gtserror.SetMalformed(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Make sure the Undo actor owns the | 
					
						
							|  |  |  | 	// Announce they're trying to undo. | 
					
						
							|  |  |  | 	if !sameActor( | 
					
						
							|  |  |  | 		undo.GetActivityStreamsActor(), | 
					
						
							|  |  |  | 		asAnnounce.GetActivityStreamsActor(), | 
					
						
							|  |  |  | 	) { | 
					
						
							|  |  |  | 		// Ignore this Activity. | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Convert AS Announce to *gtsmodel.Status, | 
					
						
							|  |  |  | 	// retrieving origin account + target status. | 
					
						
							|  |  |  | 	boost, isNew, err := f.converter.ASAnnounceToStatus( | 
					
						
							|  |  |  | 		// Use barebones as we don't | 
					
						
							|  |  |  | 		// need to populate attachments | 
					
						
							|  |  |  | 		// on boosted status, mentions, etc. | 
					
						
							|  |  |  | 		gtscontext.SetBarebones(ctx), | 
					
						
							|  |  |  | 		asAnnounce, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							|  |  |  | 		err := gtserror.Newf("error converting AS Announce to boost: %w", err) | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if boost == nil { | 
					
						
							|  |  |  | 		// We were missing origin or | 
					
						
							|  |  |  | 		// target(s) for this Announce, | 
					
						
							|  |  |  | 		// so we cannot Undo anything. | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if isNew { | 
					
						
							|  |  |  | 		// We hadn't seen this boost | 
					
						
							|  |  |  | 		// before anyway, so there's | 
					
						
							|  |  |  | 		// nothing to Undo. | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Ensure requester == announcer. | 
					
						
							|  |  |  | 	if boost.AccountID != requestingAcct.ID { | 
					
						
							|  |  |  | 		const text = "requestingAcct was not Block origin" | 
					
						
							|  |  |  | 		return gtserror.NewErrorForbidden(errors.New(text), text) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Looks valid. Process side effects asynchronously. | 
					
						
							|  |  |  | 	f.state.Workers.Federator.Queue.Push(&messages.FromFediAPI{ | 
					
						
							|  |  |  | 		APObjectType:   ap.ActivityAnnounce, | 
					
						
							|  |  |  | 		APActivityType: ap.ActivityUndo, | 
					
						
							|  |  |  | 		GTSModel:       boost, | 
					
						
							|  |  |  | 		Receiving:      receivingAcct, | 
					
						
							|  |  |  | 		Requesting:     requestingAcct, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |