| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |    GoToSocial | 
					
						
							| 
									
										
										
										
											2021-12-20 18:42:19 +01:00
										 |  |  |    Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +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 federatingdb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"net/url" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/sirupsen/logrus" | 
					
						
							| 
									
										
										
										
											2021-12-07 13:31:39 +01:00
										 |  |  | 	"github.com/spf13/viper" | 
					
						
							| 
									
										
										
										
											2021-11-13 17:29:43 +01:00
										 |  |  | 	"github.com/superseriousbusiness/activity/streams" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/activity/streams/vocab" | 
					
						
							| 
									
										
										
										
											2021-08-31 15:59:12 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/ap" | 
					
						
							| 
									
										
										
										
											2021-12-07 13:31:39 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/config" | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/db" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/id" | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/messages" | 
					
						
							| 
									
										
										
										
											2021-12-20 15:19:53 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/uris" | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func sameActor(activityActor vocab.ActivityStreamsActorProperty, followActor vocab.ActivityStreamsActorProperty) bool { | 
					
						
							|  |  |  | 	if activityActor == nil || followActor == nil { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for aIter := activityActor.Begin(); aIter != activityActor.End(); aIter = aIter.Next() { | 
					
						
							|  |  |  | 		for fIter := followActor.Begin(); fIter != followActor.End(); fIter = fIter.Next() { | 
					
						
							|  |  |  | 			if aIter.GetIRI() == nil { | 
					
						
							|  |  |  | 				return false | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if fIter.GetIRI() == nil { | 
					
						
							|  |  |  | 				return false | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if aIter.GetIRI().String() == fIter.GetIRI().String() { | 
					
						
							|  |  |  | 				return true | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	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. | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | func (f *federatingDB) NewID(ctx context.Context, t vocab.Type) (idURL *url.URL, err error) { | 
					
						
							| 
									
										
										
										
											2021-10-11 05:37:33 -07:00
										 |  |  | 	l := logrus.WithFields( | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 		logrus.Fields{ | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 			"func": "NewID", | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 05:37:33 -07:00
										 |  |  | 	if logrus.GetLevel() >= logrus.DebugLevel { | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 		i, err := marshalItem(t) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		l = l.WithField("newID", i) | 
					
						
							|  |  |  | 		l.Debug("entering NewID") | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch t.GetTypeName() { | 
					
						
							| 
									
										
										
										
											2021-08-31 15:59:12 +02:00
										 |  |  | 	case ap.ActivityFollow: | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 		// FOLLOW | 
					
						
							|  |  |  | 		// ID might already be set on a follow we've created, so check it here and return it if it is | 
					
						
							|  |  |  | 		follow, ok := t.(vocab.ActivityStreamsFollow) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return nil, errors.New("newid: follow couldn't be parsed into vocab.ActivityStreamsFollow") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		idProp := follow.GetJSONLDId() | 
					
						
							|  |  |  | 		if idProp != nil { | 
					
						
							|  |  |  | 			if idProp.IsIRI() { | 
					
						
							|  |  |  | 				return idProp.GetIRI(), nil | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// it's not set so create one based on the actor set on the follow (ie., the followER not the followEE) | 
					
						
							|  |  |  | 		actorProp := follow.GetActivityStreamsActor() | 
					
						
							|  |  |  | 		if actorProp != nil { | 
					
						
							|  |  |  | 			for iter := actorProp.Begin(); iter != actorProp.End(); iter = iter.Next() { | 
					
						
							|  |  |  | 				// take the IRI of the first actor we can find (there should only be one) | 
					
						
							|  |  |  | 				if iter.IsIRI() { | 
					
						
							| 
									
										
										
										
											2021-08-20 12:26:56 +02:00
										 |  |  | 					// if there's an error here, just use the fallback behavior -- we don't need to return an error here | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 					if actorAccount, err := f.db.GetAccountByURI(ctx, iter.GetIRI().String()); err == nil { | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 						newID, err := id.NewRandomULID() | 
					
						
							|  |  |  | 						if err != nil { | 
					
						
							|  |  |  | 							return nil, err | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2021-12-20 15:19:53 +01:00
										 |  |  | 						return url.Parse(uris.GenerateURIForFollow(actorAccount.Username, newID)) | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-08-31 15:59:12 +02:00
										 |  |  | 	case ap.ObjectNote: | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 		// NOTE aka STATUS | 
					
						
							|  |  |  | 		// ID might already be set on a note we've created, so check it here and return it if it is | 
					
						
							|  |  |  | 		note, ok := t.(vocab.ActivityStreamsNote) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return nil, errors.New("newid: note couldn't be parsed into vocab.ActivityStreamsNote") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		idProp := note.GetJSONLDId() | 
					
						
							|  |  |  | 		if idProp != nil { | 
					
						
							|  |  |  | 			if idProp.IsIRI() { | 
					
						
							|  |  |  | 				return idProp.GetIRI(), nil | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-08-31 15:59:12 +02:00
										 |  |  | 	case ap.ActivityLike: | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 		// LIKE aka FAVE | 
					
						
							|  |  |  | 		// ID might already be set on a fave we've created, so check it here and return it if it is | 
					
						
							|  |  |  | 		fave, ok := t.(vocab.ActivityStreamsLike) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return nil, errors.New("newid: fave couldn't be parsed into vocab.ActivityStreamsLike") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		idProp := fave.GetJSONLDId() | 
					
						
							|  |  |  | 		if idProp != nil { | 
					
						
							|  |  |  | 			if idProp.IsIRI() { | 
					
						
							|  |  |  | 				return idProp.GetIRI(), nil | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-10-24 11:57:39 +02:00
										 |  |  | 	case ap.ActivityCreate: | 
					
						
							|  |  |  | 		// CREATE | 
					
						
							|  |  |  | 		// ID might already be set on a Create, so check it here and return it if it is | 
					
						
							|  |  |  | 		create, ok := t.(vocab.ActivityStreamsCreate) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return nil, errors.New("newid: create couldn't be parsed into vocab.ActivityStreamsCreate") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		idProp := create.GetJSONLDId() | 
					
						
							|  |  |  | 		if idProp != nil { | 
					
						
							|  |  |  | 			if idProp.IsIRI() { | 
					
						
							|  |  |  | 				return idProp.GetIRI(), nil | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-08-31 15:59:12 +02:00
										 |  |  | 	case ap.ActivityAnnounce: | 
					
						
							| 
									
										
										
										
											2021-05-28 19:57:04 +02:00
										 |  |  | 		// ANNOUNCE aka BOOST | 
					
						
							|  |  |  | 		// ID might already be set on an announce we've created, so check it here and return it if it is | 
					
						
							|  |  |  | 		announce, ok := t.(vocab.ActivityStreamsAnnounce) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | 			return nil, errors.New("newid: announce couldn't be parsed into vocab.ActivityStreamsAnnounce") | 
					
						
							| 
									
										
										
										
											2021-05-28 19:57:04 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		idProp := announce.GetJSONLDId() | 
					
						
							|  |  |  | 		if idProp != nil { | 
					
						
							|  |  |  | 			if idProp.IsIRI() { | 
					
						
							|  |  |  | 				return idProp.GetIRI(), nil | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-08-31 15:59:12 +02:00
										 |  |  | 	case ap.ActivityUpdate: | 
					
						
							| 
									
										
										
										
											2021-05-28 22:47:18 +02:00
										 |  |  | 		// UPDATE | 
					
						
							|  |  |  | 		// ID might already be set on an update we've created, so check it here and return it if it is | 
					
						
							|  |  |  | 		update, ok := t.(vocab.ActivityStreamsUpdate) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | 			return nil, errors.New("newid: update couldn't be parsed into vocab.ActivityStreamsUpdate") | 
					
						
							| 
									
										
										
										
											2021-05-28 22:47:18 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		idProp := update.GetJSONLDId() | 
					
						
							|  |  |  | 		if idProp != nil { | 
					
						
							|  |  |  | 			if idProp.IsIRI() { | 
					
						
							|  |  |  | 				return idProp.GetIRI(), nil | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-08-31 15:59:12 +02:00
										 |  |  | 	case ap.ActivityBlock: | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | 		// BLOCK | 
					
						
							|  |  |  | 		// ID might already be set on a block we've created, so check it here and return it if it is | 
					
						
							|  |  |  | 		block, ok := t.(vocab.ActivityStreamsBlock) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return nil, errors.New("newid: block couldn't be parsed into vocab.ActivityStreamsBlock") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		idProp := block.GetJSONLDId() | 
					
						
							|  |  |  | 		if idProp != nil { | 
					
						
							|  |  |  | 			if idProp.IsIRI() { | 
					
						
							|  |  |  | 				return idProp.GetIRI(), nil | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-08-31 15:59:12 +02:00
										 |  |  | 	case ap.ActivityUndo: | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | 		// UNDO | 
					
						
							|  |  |  | 		// ID might already be set on an undo we've created, so check it here and return it if it is | 
					
						
							|  |  |  | 		undo, ok := t.(vocab.ActivityStreamsUndo) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return nil, errors.New("newid: undo couldn't be parsed into vocab.ActivityStreamsUndo") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		idProp := undo.GetJSONLDId() | 
					
						
							|  |  |  | 		if idProp != nil { | 
					
						
							|  |  |  | 			if idProp.IsIRI() { | 
					
						
							|  |  |  | 				return idProp.GetIRI(), nil | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	// fallback default behavior: just return a random ULID after our protocol and host | 
					
						
							|  |  |  | 	newID, err := id.NewRandomULID() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-12-07 13:31:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	protocol := viper.GetString(config.Keys.Protocol) | 
					
						
							|  |  |  | 	host := viper.GetString(config.Keys.Host) | 
					
						
							|  |  |  | 	return url.Parse(fmt.Sprintf("%s://%s/%s", protocol, host, newID)) | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ActorForOutbox fetches the actor's IRI for the given outbox IRI. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The library makes this call only after acquiring a lock first. | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | func (f *federatingDB) ActorForOutbox(ctx context.Context, outboxIRI *url.URL) (actorIRI *url.URL, err error) { | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 	acct, err := f.getAccountForIRI(ctx, outboxIRI) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return url.Parse(acct.URI) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ActorForInbox fetches the actor's IRI for the given outbox IRI. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The library makes this call only after acquiring a lock first. | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | func (f *federatingDB) ActorForInbox(ctx context.Context, inboxIRI *url.URL) (actorIRI *url.URL, err error) { | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 	acct, err := f.getAccountForIRI(ctx, inboxIRI) | 
					
						
							|  |  |  | 	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) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // getAccountForIRI returns the account that corresponds to or owns the given IRI. | 
					
						
							|  |  |  | func (f *federatingDB) getAccountForIRI(ctx context.Context, iri *url.URL) (account *gtsmodel.Account, err error) { | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	acct := >smodel.Account{} | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 15:19:53 +01:00
										 |  |  | 	if uris.IsInboxPath(iri) { | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 		if err := f.db.GetWhere(ctx, []db.Where{{Key: "inbox_uri", Value: iri.String()}}, acct); err != nil { | 
					
						
							|  |  |  | 			if err == db.ErrNoEntries { | 
					
						
							|  |  |  | 				return nil, fmt.Errorf("no actor found that corresponds to inbox %s", iri.String()) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("db error searching for actor with inbox %s", iri.String()) | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 		return acct, nil | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 15:19:53 +01:00
										 |  |  | 	if uris.IsOutboxPath(iri) { | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 		if err := f.db.GetWhere(ctx, []db.Where{{Key: "outbox_uri", Value: iri.String()}}, acct); err != nil { | 
					
						
							|  |  |  | 			if err == db.ErrNoEntries { | 
					
						
							|  |  |  | 				return nil, fmt.Errorf("no actor found that corresponds to outbox %s", iri.String()) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("db error searching for actor with outbox %s", iri.String()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return acct, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 15:19:53 +01:00
										 |  |  | 	if uris.IsUserPath(iri) { | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 		if err := f.db.GetWhere(ctx, []db.Where{{Key: "uri", Value: iri.String()}}, acct); err != nil { | 
					
						
							|  |  |  | 			if err == db.ErrNoEntries { | 
					
						
							|  |  |  | 				return nil, fmt.Errorf("no actor found that corresponds to uri %s", iri.String()) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("db error searching for actor with uri %s", iri.String()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return acct, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 15:19:53 +01:00
										 |  |  | 	if uris.IsFollowersPath(iri) { | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 		if err := f.db.GetWhere(ctx, []db.Where{{Key: "followers_uri", Value: iri.String()}}, acct); err != nil { | 
					
						
							|  |  |  | 			if err == db.ErrNoEntries { | 
					
						
							|  |  |  | 				return nil, fmt.Errorf("no actor found that corresponds to followers_uri %s", iri.String()) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("db error searching for actor with followers_uri %s", iri.String()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return acct, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 15:19:53 +01:00
										 |  |  | 	if uris.IsFollowingPath(iri) { | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 		if err := f.db.GetWhere(ctx, []db.Where{{Key: "following_uri", Value: iri.String()}}, acct); err != nil { | 
					
						
							|  |  |  | 			if err == db.ErrNoEntries { | 
					
						
							|  |  |  | 				return nil, fmt.Errorf("no actor found that corresponds to following_uri %s", iri.String()) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("db error searching for actor with following_uri %s", iri.String()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return acct, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil, fmt.Errorf("getActorForIRI: iri %s not recognised", iri) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // collectFollows takes a slice of iris and converts them into ActivityStreamsCollection of IRIs. | 
					
						
							|  |  |  | func (f *federatingDB) collectIRIs(ctx context.Context, iris []*url.URL) (vocab.ActivityStreamsCollection, error) { | 
					
						
							|  |  |  | 	collection := streams.NewActivityStreamsCollection() | 
					
						
							|  |  |  | 	items := streams.NewActivityStreamsItemsProperty() | 
					
						
							|  |  |  | 	for _, i := range iris { | 
					
						
							|  |  |  | 		items.AppendIRI(i) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	collection.SetActivityStreamsItems(items) | 
					
						
							|  |  |  | 	return collection, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // extractFromCtx extracts some useful values from a context passed into the federatingDB via the API: | 
					
						
							|  |  |  | //   - The target account that owns the inbox or URI being interacted with. | 
					
						
							| 
									
										
										
										
											2021-10-10 12:39:25 +02:00
										 |  |  | //   - The requesting account that posted to the inbox. | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | //   - A channel that messages for the processor can be placed into. | 
					
						
							| 
									
										
										
										
											2021-10-10 12:39:25 +02:00
										 |  |  | // If a value is not present, nil will be returned for it. It's up to the caller to check this and respond appropriately. | 
					
						
							|  |  |  | func extractFromCtx(ctx context.Context) (receivingAccount, requestingAccount *gtsmodel.Account, fromFederatorChan chan messages.FromFederator) { | 
					
						
							| 
									
										
										
										
											2021-12-20 15:19:53 +01:00
										 |  |  | 	receivingAccountI := ctx.Value(ap.ContextReceivingAccount) | 
					
						
							| 
									
										
										
										
											2021-10-10 12:39:25 +02:00
										 |  |  | 	if receivingAccountI != nil { | 
					
						
							| 
									
										
										
										
											2021-11-22 08:46:19 +01:00
										 |  |  | 		var ok bool | 
					
						
							|  |  |  | 		receivingAccount, ok = receivingAccountI.(*gtsmodel.Account) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							| 
									
										
										
										
											2021-12-20 15:19:53 +01:00
										 |  |  | 			logrus.Panicf("extractFromCtx: context entry with key %s could not be asserted to *gtsmodel.Account", ap.ContextReceivingAccount) | 
					
						
							| 
									
										
										
										
											2021-11-22 08:46:19 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-10-10 12:39:25 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 15:19:53 +01:00
										 |  |  | 	requestingAcctI := ctx.Value(ap.ContextRequestingAccount) | 
					
						
							| 
									
										
										
										
											2021-10-10 12:39:25 +02:00
										 |  |  | 	if requestingAcctI != nil { | 
					
						
							| 
									
										
										
										
											2021-11-22 08:46:19 +01:00
										 |  |  | 		var ok bool | 
					
						
							|  |  |  | 		requestingAccount, ok = requestingAcctI.(*gtsmodel.Account) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							| 
									
										
										
										
											2021-12-20 15:19:53 +01:00
										 |  |  | 			logrus.Panicf("extractFromCtx: context entry with key %s could not be asserted to *gtsmodel.Account", ap.ContextRequestingAccount) | 
					
						
							| 
									
										
										
										
											2021-11-22 08:46:19 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 15:19:53 +01:00
										 |  |  | 	fromFederatorChanI := ctx.Value(ap.ContextFromFederatorChan) | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 	if fromFederatorChanI != nil { | 
					
						
							| 
									
										
										
										
											2021-11-22 08:46:19 +01:00
										 |  |  | 		var ok bool | 
					
						
							|  |  |  | 		fromFederatorChan, ok = fromFederatorChanI.(chan messages.FromFederator) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							| 
									
										
										
										
											2021-12-20 15:19:53 +01:00
										 |  |  | 			logrus.Panicf("extractFromCtx: context entry with key %s could not be asserted to chan messages.FromFederator", ap.ContextFromFederatorChan) | 
					
						
							| 
									
										
										
										
											2021-11-22 08:46:19 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-10 12:39:25 +02:00
										 |  |  | 	return | 
					
						
							| 
									
										
										
										
											2021-10-04 15:24:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func marshalItem(item vocab.Type) (string, error) { | 
					
						
							|  |  |  | 	m, err := streams.Serialize(item) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	b, err := json.Marshal(m) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return string(b), nil | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | } |