| 
									
										
										
										
											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-05-27 16:06:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | package federatingdb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2025-08-09 16:23:00 +02:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	"net/url" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-25 15:15:36 +02:00
										 |  |  | 	"code.superseriousbusiness.org/activity/streams" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/activity/streams/vocab" | 
					
						
							| 
									
										
										
										
											2025-04-26 15:34:10 +02:00
										 |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/ap" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/config" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/gtscontext" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/gtsmodel" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/id" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/log" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/uris" | 
					
						
							| 
									
										
										
										
											2024-09-17 19:35:47 +00:00
										 |  |  | 	"codeberg.org/gruf/go-byteutil" | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-04 20:21:20 +00:00
										 |  |  | func typeNames(objects []ap.TypeOrIRI) []string { | 
					
						
							|  |  |  | 	names := make([]string, len(objects)) | 
					
						
							|  |  |  | 	for i, object := range objects { | 
					
						
							|  |  |  | 		if object.IsIRI() { | 
					
						
							|  |  |  | 			names[i] = "IRI" | 
					
						
							|  |  |  | 		} else if t := object.GetType(); t != nil { | 
					
						
							|  |  |  | 			names[i] = t.GetTypeName() | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			names[i] = "nil" | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return names | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // isSender returns whether an object with AttributedTo property comes from the given requesting account. | 
					
						
							|  |  |  | func isSender(with ap.WithAttributedTo, requester *gtsmodel.Account) bool { | 
					
						
							|  |  |  | 	for _, uri := range ap.GetAttributedTo(with) { | 
					
						
							|  |  |  | 		if uri.String() == requester.URI { | 
					
						
							|  |  |  | 			return true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | func sameActor(actor1 vocab.ActivityStreamsActorProperty, actor2 vocab.ActivityStreamsActorProperty) bool { | 
					
						
							|  |  |  | 	if actor1 == nil || actor2 == nil { | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for a1Iter := actor1.Begin(); a1Iter != actor1.End(); a1Iter = a1Iter.Next() { | 
					
						
							| 
									
										
										
										
											2025-05-15 09:40:48 +00:00
										 |  |  | 		a1IRI := a1Iter.GetIRI() | 
					
						
							|  |  |  | 		if a1IRI == nil { | 
					
						
							|  |  |  | 			return false | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-15 09:40:48 +00:00
										 |  |  | 		a1IRIStr := a1IRI.String() | 
					
						
							|  |  |  | 		for a2Iter := actor2.Begin(); a2Iter != actor2.End(); a2Iter = a2Iter.Next() { | 
					
						
							|  |  |  | 			a2IRI := a2Iter.GetIRI() | 
					
						
							|  |  |  | 			if a2IRI == nil { | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 				return false | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-15 09:40:48 +00:00
										 |  |  | 			a2IRIStr := a2IRI.String() | 
					
						
							|  |  |  | 			if a1IRIStr == a2IRIStr { | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 				return true | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-04-05 20:10:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	return false | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NewID creates a new IRI id for the provided activity or object. The | 
					
						
							|  |  |  | // implementation does not need to set the 'id' property and simply | 
					
						
							|  |  |  | // needs to determine the value. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The go-fed library will handle setting the 'id' property on the | 
					
						
							|  |  |  | // activity or object provided with the value returned. | 
					
						
							| 
									
										
										
										
											2025-05-15 09:40:48 +00:00
										 |  |  | func (f *DB) NewID(ctx context.Context, t vocab.Type) (idURL *url.URL, err error) { | 
					
						
							| 
									
										
										
										
											2024-09-17 19:35:47 +00:00
										 |  |  | 	log.DebugKV(ctx, "newID", serialize{t}) | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-04 20:21:20 +00:00
										 |  |  | 	// Most of our types set an ID already | 
					
						
							|  |  |  | 	// by this point, return this if found. | 
					
						
							|  |  |  | 	idProp := t.GetJSONLDId() | 
					
						
							|  |  |  | 	if idProp != nil && idProp.IsIRI() { | 
					
						
							|  |  |  | 		return idProp.GetIRI(), nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if t.GetTypeName() == ap.ActivityFollow { | 
					
						
							|  |  |  | 		follow, _ := t.(vocab.ActivityStreamsFollow) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// If an actor URI has been set, create a new ID | 
					
						
							|  |  |  | 		// based on actor (i.e. followER not the followEE). | 
					
						
							| 
									
										
										
										
											2023-11-30 16:22:34 +00:00
										 |  |  | 		if uri := ap.GetActorIRIs(follow); len(uri) == 1 { | 
					
						
							| 
									
										
										
										
											2023-11-04 20:21:20 +00:00
										 |  |  | 			if actorAccount, err := f.state.DB.GetAccountByURI(ctx, uri[0].String()); err == nil { | 
					
						
							|  |  |  | 				newID, err := id.NewRandomULID() | 
					
						
							|  |  |  | 				if err != nil { | 
					
						
							|  |  |  | 					return nil, err | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2023-11-04 20:21:20 +00:00
										 |  |  | 				return url.Parse(uris.GenerateURIForFollow(actorAccount.Username, newID)) | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-04 20:21:20 +00:00
										 |  |  | 	// Default fallback behaviour: | 
					
						
							| 
									
										
										
										
											2025-05-15 09:40:48 +00:00
										 |  |  | 	// {proto}://{host}/{newULID} | 
					
						
							|  |  |  | 	return &url.URL{ | 
					
						
							|  |  |  | 		Scheme: config.GetProtocol(), | 
					
						
							|  |  |  | 		Host:   config.GetHost(), | 
					
						
							|  |  |  | 		Path:   "/" + id.NewULID(), | 
					
						
							|  |  |  | 	}, nil | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-06 14:39:40 +02:00
										 |  |  | // ActorForOutbox fetches the local actor's IRI for the given outbox IRI. | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | // | 
					
						
							|  |  |  | // The library makes this call only after acquiring a lock first. | 
					
						
							| 
									
										
										
										
											2025-05-15 09:40:48 +00:00
										 |  |  | func (f *DB) ActorForOutbox(ctx context.Context, outboxIRI *url.URL) (actorIRI *url.URL, err error) { | 
					
						
							| 
									
										
										
										
											2025-04-06 14:39:40 +02:00
										 |  |  | 	acct, err := f.state.DB.GetOneAccountByOutboxURI(ctx, outboxIRI.String()) | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return url.Parse(acct.URI) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-06 14:39:40 +02:00
										 |  |  | // ActorForInbox fetches the local actor's IRI for the given inbox IRI. | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | // | 
					
						
							|  |  |  | // The library makes this call only after acquiring a lock first. | 
					
						
							| 
									
										
										
										
											2025-05-15 09:40:48 +00:00
										 |  |  | func (f *DB) ActorForInbox(ctx context.Context, inboxIRI *url.URL) (actorIRI *url.URL, err error) { | 
					
						
							| 
									
										
										
										
											2025-04-06 14:39:40 +02:00
										 |  |  | 	acct, err := f.state.DB.GetOneAccountByInboxURI(ctx, inboxIRI.String()) | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 	return url.Parse(acct.URI) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // collectFollows takes a slice of iris and converts them into ActivityStreamsCollection of IRIs. | 
					
						
							| 
									
										
										
										
											2025-05-15 09:40:48 +00:00
										 |  |  | func (f *DB) collectIRIs(_ context.Context, iris []*url.URL) (vocab.ActivityStreamsCollection, error) { | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 	collection := streams.NewActivityStreamsCollection() | 
					
						
							|  |  |  | 	items := streams.NewActivityStreamsItemsProperty() | 
					
						
							|  |  |  | 	for _, i := range iris { | 
					
						
							|  |  |  | 		items.AppendIRI(i) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	collection.SetActivityStreamsItems(items) | 
					
						
							|  |  |  | 	return collection, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-17 15:20:39 +01:00
										 |  |  | // activityContext represents the context in | 
					
						
							|  |  |  | // which a call to one of the federatingdb | 
					
						
							|  |  |  | // functions is taking place, including the | 
					
						
							|  |  |  | // account who initiated the request via POST | 
					
						
							|  |  |  | // to an inbox, and the account who received | 
					
						
							|  |  |  | // the request in their inbox. | 
					
						
							|  |  |  | type activityContext struct { | 
					
						
							|  |  |  | 	// The account that owns the inbox | 
					
						
							|  |  |  | 	// or URI being interacted with. | 
					
						
							|  |  |  | 	receivingAcct *gtsmodel.Account | 
					
						
							| 
									
										
										
										
											2021-10-10 12:39:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-17 15:20:39 +01:00
										 |  |  | 	// The account whose keyId was used | 
					
						
							|  |  |  | 	// to POST a request to the inbox. | 
					
						
							|  |  |  | 	requestingAcct *gtsmodel.Account | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Whether this is an internal request, | 
					
						
							|  |  |  | 	// ie., one originating not from the | 
					
						
							|  |  |  | 	// API but from inside the instance. | 
					
						
							|  |  |  | 	// | 
					
						
							|  |  |  | 	// If the request is internal, it's | 
					
						
							|  |  |  | 	// safe to assume that the activity | 
					
						
							|  |  |  | 	// has already been processed elsewhere, | 
					
						
							|  |  |  | 	// and we can return with no action. | 
					
						
							|  |  |  | 	internal bool | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-17 15:20:39 +01:00
										 |  |  | // getActivityContext extracts the context in | 
					
						
							|  |  |  | // which an Activity is taking place from the | 
					
						
							|  |  |  | // context.Context passed in to one of the | 
					
						
							|  |  |  | // federatingdb functions. | 
					
						
							|  |  |  | func getActivityContext(ctx context.Context) activityContext { | 
					
						
							|  |  |  | 	receivingAcct := gtscontext.ReceivingAccount(ctx) | 
					
						
							|  |  |  | 	requestingAcct := gtscontext.RequestingAccount(ctx) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// If the receiving account wasn't set on | 
					
						
							|  |  |  | 	// the context, that means this request | 
					
						
							|  |  |  | 	// didn't pass through the fedi API, but | 
					
						
							|  |  |  | 	// came from inside the instance as the | 
					
						
							|  |  |  | 	// result of a local activity. | 
					
						
							|  |  |  | 	internal := receivingAcct == nil | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return activityContext{ | 
					
						
							|  |  |  | 		receivingAcct:  receivingAcct, | 
					
						
							|  |  |  | 		requestingAcct: requestingAcct, | 
					
						
							|  |  |  | 		internal:       internal, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-17 19:35:47 +00:00
										 |  |  | // serialize wraps a vocab.Type to provide | 
					
						
							|  |  |  | // lazy-serialization along with error output. | 
					
						
							|  |  |  | type serialize struct{ item vocab.Type } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-09 16:23:00 +02:00
										 |  |  | func (s serialize) MarshalJSON() ([]byte, error) { | 
					
						
							|  |  |  | 	m, err := ap.Serialize(s.item) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("error serializing: %w", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return json.Marshal(m) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-17 19:35:47 +00:00
										 |  |  | func (s serialize) String() string { | 
					
						
							|  |  |  | 	m, err := ap.Serialize(s.item) | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2025-08-09 16:23:00 +02:00
										 |  |  | 		return "!(error serializing: " + err.Error() + ")" | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-13 16:47:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 	b, err := json.Marshal(m) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2025-08-09 16:23:00 +02:00
										 |  |  | 		return "!(error marshaling: " + err.Error() + ")" | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-13 16:47:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-17 19:35:47 +00:00
										 |  |  | 	return byteutil.B2S(b) | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | } |