diff options
Diffstat (limited to 'internal/federation')
-rw-r--r-- | internal/federation/federatingdb/accept.go | 37 | ||||
-rw-r--r-- | internal/federation/federatingdb/create.go | 24 | ||||
-rw-r--r-- | internal/federation/federatingdb/followers.go | 2 | ||||
-rw-r--r-- | internal/federation/federatingdb/undo.go | 3 | ||||
-rw-r--r-- | internal/federation/federatingdb/util.go | 18 | ||||
-rw-r--r-- | internal/federation/federatingprotocol.go | 7 |
6 files changed, 85 insertions, 6 deletions
diff --git a/internal/federation/federatingdb/accept.go b/internal/federation/federatingdb/accept.go index d08544c99..78cff4094 100644 --- a/internal/federation/federatingdb/accept.go +++ b/internal/federation/federatingdb/accept.go @@ -9,6 +9,7 @@ import ( "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" "github.com/sirupsen/logrus" + "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/util" ) @@ -58,7 +59,43 @@ func (f *federatingDB) Accept(ctx context.Context, accept vocab.ActivityStreamsA } for iter := acceptObject.Begin(); iter != acceptObject.End(); iter = iter.Next() { + // check if the object is an IRI + if iter.IsIRI() { + // we have just the URI of whatever is being accepted, so we need to find out what it is + acceptedObjectIRI := iter.GetIRI() + if util.IsFollowPath(acceptedObjectIRI) { + // ACCEPT FOLLOW + gtsFollowRequest := >smodel.FollowRequest{} + if err := f.db.GetWhere([]db.Where{{Key: "uri", Value: acceptedObjectIRI.String()}}, gtsFollowRequest); err != nil { + return fmt.Errorf("ACCEPT: couldn't get follow request with id %s from the database: %s", acceptedObjectIRI.String(), err) + } + + // make sure the addressee of the original follow is the same as whatever inbox this landed in + if gtsFollowRequest.AccountID != inboxAcct.ID { + return errors.New("ACCEPT: follow object account and inbox account were not the same") + } + follow, err := f.db.AcceptFollowRequest(gtsFollowRequest.AccountID, gtsFollowRequest.TargetAccountID) + if err != nil { + return err + } + + fromFederatorChan <- gtsmodel.FromFederator{ + APObjectType: gtsmodel.ActivityStreamsFollow, + APActivityType: gtsmodel.ActivityStreamsAccept, + GTSModel: follow, + ReceivingAccount: inboxAcct, + } + + return nil + } + } + + // check if iter is an AP object / type + if iter.GetType() == nil { + continue + } switch iter.GetType().GetTypeName() { + // we have the whole object so we can figure out what we're accepting case string(gtsmodel.ActivityStreamsFollow): // ACCEPT FOLLOW asFollow, ok := iter.GetType().(vocab.ActivityStreamsFollow) diff --git a/internal/federation/federatingdb/create.go b/internal/federation/federatingdb/create.go index 026674e43..3ab6e2eca 100644 --- a/internal/federation/federatingdb/create.go +++ b/internal/federation/federatingdb/create.go @@ -29,6 +29,7 @@ import ( "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/id" "github.com/superseriousbusiness/gotosocial/internal/util" ) @@ -99,10 +100,21 @@ func (f *federatingDB) Create(ctx context.Context, asType vocab.Type) error { if err != nil { return fmt.Errorf("error converting note to status: %s", err) } + + // id the status based on the time it was created + statusID, err := id.NewULIDFromTime(status.CreatedAt) + if err != nil { + return err + } + status.ID = statusID + if err := f.db.Put(status); err != nil { if _, ok := err.(db.ErrAlreadyExists); ok { + // the status already exists in the database, which means we've already handled everything else, + // so we can just return nil here and be done with it. return nil } + // an actual error has happened return fmt.Errorf("database error inserting status: %s", err) } @@ -125,6 +137,12 @@ func (f *federatingDB) Create(ctx context.Context, asType vocab.Type) error { return fmt.Errorf("could not convert Follow to follow request: %s", err) } + newID, err := id.NewULID() + if err != nil { + return err + } + followRequest.ID = newID + if err := f.db.Put(followRequest); err != nil { return fmt.Errorf("database error inserting follow request: %s", err) } @@ -146,6 +164,12 @@ func (f *federatingDB) Create(ctx context.Context, asType vocab.Type) error { return fmt.Errorf("could not convert Like to fave: %s", err) } + newID, err := id.NewULID() + if err != nil { + return err + } + fave.ID = newID + if err := f.db.Put(fave); err != nil { return fmt.Errorf("database error inserting fave: %s", err) } diff --git a/internal/federation/federatingdb/followers.go b/internal/federation/federatingdb/followers.go index 7cba101dd..28e6e9d69 100644 --- a/internal/federation/federatingdb/followers.go +++ b/internal/federation/federatingdb/followers.go @@ -43,7 +43,7 @@ func (f *federatingDB) Followers(c context.Context, actorIRI *url.URL) (follower } acctFollowers := []gtsmodel.Follow{} - if err := f.db.GetFollowersByAccountID(acct.ID, &acctFollowers); err != nil { + if err := f.db.GetFollowersByAccountID(acct.ID, &acctFollowers, false); err != nil { return nil, fmt.Errorf("db error getting followers for account id %s: %s", acct.ID, err) } diff --git a/internal/federation/federatingdb/undo.go b/internal/federation/federatingdb/undo.go index 8ca681bb2..3feee6457 100644 --- a/internal/federation/federatingdb/undo.go +++ b/internal/federation/federatingdb/undo.go @@ -48,6 +48,9 @@ func (f *federatingDB) Undo(ctx context.Context, undo vocab.ActivityStreamsUndo) } for iter := undoObject.Begin(); iter != undoObject.End(); iter = iter.Next() { + if iter.GetType() == nil { + continue + } switch iter.GetType().GetTypeName() { case string(gtsmodel.ActivityStreamsFollow): // UNDO FOLLOW diff --git a/internal/federation/federatingdb/util.go b/internal/federation/federatingdb/util.go index ff6ae50a6..ed3c252d9 100644 --- a/internal/federation/federatingdb/util.go +++ b/internal/federation/federatingdb/util.go @@ -27,10 +27,10 @@ import ( "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" - "github.com/google/uuid" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/id" "github.com/superseriousbusiness/gotosocial/internal/util" ) @@ -60,7 +60,7 @@ func sameActor(activityActor vocab.ActivityStreamsActorProperty, followActor voc // // The go-fed library will handle setting the 'id' property on the // activity or object provided with the value returned. -func (f *federatingDB) NewID(c context.Context, t vocab.Type) (id *url.URL, err error) { +func (f *federatingDB) NewID(c context.Context, t vocab.Type) (idURL *url.URL, err error) { l := f.log.WithFields( logrus.Fields{ "func": "NewID", @@ -99,7 +99,11 @@ func (f *federatingDB) NewID(c context.Context, t vocab.Type) (id *url.URL, err if iter.IsIRI() { actorAccount := >smodel.Account{} if err := f.db.GetWhere([]db.Where{{Key: "uri", Value: iter.GetIRI().String()}}, actorAccount); err == nil { // if there's an error here, just use the fallback behavior -- we don't need to return an error here - return url.Parse(util.GenerateURIForFollow(actorAccount.Username, f.config.Protocol, f.config.Host, uuid.NewString())) + newID, err := id.NewRandomULID() + if err != nil { + return nil, err + } + return url.Parse(util.GenerateURIForFollow(actorAccount.Username, f.config.Protocol, f.config.Host, newID)) } } } @@ -158,8 +162,12 @@ func (f *federatingDB) NewID(c context.Context, t vocab.Type) (id *url.URL, err } } - // fallback default behavior: just return a random UUID after our protocol and host - return url.Parse(fmt.Sprintf("%s://%s/%s", f.config.Protocol, f.config.Host, uuid.NewString())) + // fallback default behavior: just return a random ULID after our protocol and host + newID, err := id.NewRandomULID() + if err != nil { + return nil, err + } + return url.Parse(fmt.Sprintf("%s://%s/%s", f.config.Protocol, f.config.Host, newID)) } // ActorForOutbox fetches the actor's IRI for the given outbox IRI. diff --git a/internal/federation/federatingprotocol.go b/internal/federation/federatingprotocol.go index e05bdb7b9..8784c32e2 100644 --- a/internal/federation/federatingprotocol.go +++ b/internal/federation/federatingprotocol.go @@ -31,6 +31,7 @@ import ( "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/id" "github.com/superseriousbusiness/gotosocial/internal/util" ) @@ -142,6 +143,12 @@ func (f *federator) AuthenticatePostInbox(ctx context.Context, w http.ResponseWr return ctx, false, fmt.Errorf("error converting person with public key id %s to account: %s", publicKeyOwnerURI.String(), err) } + aID, err := id.NewRandomULID() + if err != nil { + return ctx, false, err + } + a.ID = aID + if err := f.db.Put(a); err != nil { l.Errorf("error inserting dereferenced remote account: %s", err) } |