bleep bloop

This commit is contained in:
tsmethurst 2021-05-14 13:07:17 +02:00
commit 2453957a6e
5 changed files with 122 additions and 19 deletions

View file

@ -359,16 +359,12 @@ func (f *federatingDB) Create(c context.Context, asType vocab.Type) error {
) )
l.Debugf("received CREATE asType %+v", asType) l.Debugf("received CREATE asType %+v", asType)
switch gtsmodel.ActivityStreamsActivity(asType.GetTypeName()) { switch gtsmodel.ActivityStreamsActivity(asType.GetTypeName()) {
case gtsmodel.ActivityStreamsCreate: case gtsmodel.ActivityStreamsCreate:
create, ok := asType.(vocab.ActivityStreamsCreate) create, ok := asType.(vocab.ActivityStreamsCreate)
if !ok { if !ok {
return errors.New("could not convert type to create") return errors.New("could not convert type to create")
} }
object := create.GetActivityStreamsObject() object := create.GetActivityStreamsObject()
for objectIter := object.Begin(); objectIter != object.End(); objectIter = objectIter.Next() { for objectIter := object.Begin(); objectIter != object.End(); objectIter = objectIter.Next() {
switch gtsmodel.ActivityStreamsObject(objectIter.GetType().GetTypeName()) { switch gtsmodel.ActivityStreamsObject(objectIter.GetType().GetTypeName()) {
@ -383,6 +379,12 @@ func (f *federatingDB) Create(c context.Context, asType vocab.Type) error {
} }
} }
} }
case gtsmodel.ActivityStreamsFollow:
follow, ok := asType.(vocab.ActivityStreamsFollow)
if !ok {
return errors.New("could not convert type to follow")
}
} }
return nil return nil
} }

View file

@ -72,8 +72,49 @@ func (f *federator) PostInboxRequestBodyHook(ctx context.Context, r *http.Reques
return nil, err return nil, err
} }
ctxWithActivity := context.WithValue(ctx, util.APActivity, activity) // derefence the actor of the activity already
return ctxWithActivity, nil // var requestingActorIRI *url.URL
// actorProp := activity.GetActivityStreamsActor()
// if actorProp != nil {
// for i := actorProp.Begin(); i != actorProp.End(); i = i.Next() {
// if i.IsIRI() {
// requestingActorIRI = i.GetIRI()
// break
// }
// }
// }
// if requestingActorIRI != nil {
// requestedAccountI := ctx.Value(util.APAccount)
// requestedAccount, ok := requestedAccountI.(*gtsmodel.Account)
// if !ok {
// return nil, errors.New("requested account was not set on request context")
// }
// requestingActor := &gtsmodel.Account{}
// if err := f.db.GetWhere("uri", requestingActorIRI.String(), requestingActor); err != nil {
// // there's been a proper error so return it
// if _, ok := err.(db.ErrNoEntries); !ok {
// return nil, fmt.Errorf("error getting requesting actor with id %s: %s", requestingActorIRI.String(), err)
// }
// // we don't know this account (yet) so let's dereference it right now
// person, err := f.DereferenceRemoteAccount(requestedAccount.Username, publicKeyOwnerURI)
// if err != nil {
// return ctx, false, fmt.Errorf("error dereferencing account with public key id %s: %s", publicKeyOwnerURI.String(), err)
// }
// a, err := f.typeConverter.ASRepresentationToAccount(person)
// if err != nil {
// return ctx, false, fmt.Errorf("error converting person with public key id %s to account: %s", publicKeyOwnerURI.String(), err)
// }
// requestingAccount = a
// }
// }
// set the activity on the context for use later on
return context.WithValue(ctx, util.APActivity, activity), nil
} }
// AuthenticatePostInbox delegates the authentication of a POST to an // AuthenticatePostInbox delegates the authentication of a POST to an
@ -141,6 +182,11 @@ func (f *federator) AuthenticatePostInbox(ctx context.Context, w http.ResponseWr
if err != nil { if err != nil {
return ctx, false, fmt.Errorf("error converting person with public key id %s to account: %s", publicKeyOwnerURI.String(), err) return ctx, false, fmt.Errorf("error converting person with public key id %s to account: %s", publicKeyOwnerURI.String(), err)
} }
if err := f.db.Put(a); err != nil {
l.Errorf("error inserting dereferenced remote account: %s", err)
}
requestingAccount = a requestingAccount = a
} }

View file

@ -82,9 +82,6 @@ func extractTos(i withTo) ([]*url.URL, error) {
} }
} }
} }
if len(to) == 0 {
return nil, errors.New("found no to entries")
}
return to, nil return to, nil
} }
@ -98,9 +95,6 @@ func extractCCs(i withCC) ([]*url.URL, error) {
} }
} }
} }
if len(cc) == 0 {
return nil, errors.New("found no cc entries")
}
return cc, nil return cc, nil
} }

View file

@ -21,6 +21,8 @@ package typeutils
import ( import (
"errors" "errors"
"fmt" "fmt"
"net/url"
"strings"
"github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
@ -218,10 +220,11 @@ func (c *converter) ASStatusToStatus(statusable Statusable) (*gtsmodel.Status, e
status.APStatusOwnerURI = attributedTo.String() status.APStatusOwnerURI = attributedTo.String()
statusOwner := &gtsmodel.Account{} statusOwner := &gtsmodel.Account{}
if err := c.db.GetWhere("uri", attributedTo.String(), statusOwner); err == nil { if err := c.db.GetWhere("uri", attributedTo.String(), statusOwner); err != nil {
status.AccountID = statusOwner.ID return nil, fmt.Errorf("couldn't get status owner from db: %s", err)
status.GTSAccount = statusOwner
} }
status.AccountID = statusOwner.ID
status.GTSAccount = statusOwner
// check if there's a post that this is a reply to // check if there's a post that this is a reply to
inReplyToURI, err := extractInReplyToURI(statusable) inReplyToURI, err := extractInReplyToURI(statusable)
@ -248,10 +251,45 @@ func (c *converter) ASStatusToStatus(statusable Statusable) (*gtsmodel.Status, e
} }
// visibility entry for this status // visibility entry for this status
// TODO: if it's just got followers in TO and it's not CC'ed to public, it's followers only var visibility gtsmodel.Visibility
// TODO: if it's CC'ed to public, it's public or unlocked
// TODO: if it's a DM then it's addressed to SPECIFIC ACCOUNTS and not followers or public to, err := extractTos(statusable)
// TODO: mentioned SPECIFIC ACCOUNTS also get added to CC'es if it's not a direct message if err != nil {
return nil, fmt.Errorf("error extracting TO values: %s", err)
}
cc, err := extractCCs(statusable)
if err != nil {
return nil, fmt.Errorf("error extracting CC values: %s", err)
}
if len(to) == 0 && len(cc) == 0 {
return nil, errors.New("message wasn't TO or CC anyone")
}
// for visibility derivation, we start by assuming most restrictive, and work our way to least restrictive
// if it's a DM then it's addressed to SPECIFIC ACCOUNTS and not followers or public
if len(to) != 0 && len(cc) == 0 {
visibility = gtsmodel.VisibilityDirect
}
// if it's just got followers in TO and it's not also CC'ed to public, it's followers only
if isFollowers(to, statusOwner.FollowersURI) {
visibility = gtsmodel.VisibilityFollowersOnly
}
// if it's CC'ed to public, it's public or unlocked
// mentioned SPECIFIC ACCOUNTS also get added to CC'es if it's not a direct message
if isPublic(to) {
visibility = gtsmodel.VisibilityPublic
}
// we should have a visibility by now
if visibility == "" {
return nil, errors.New("couldn't derive visibility")
}
status.Visibility = visibility
// advanced visibility for this status // advanced visibility for this status
// TODO: a lot of work to be done here -- a new type needs to be created for this in go-fed/activity using ASTOOL // TODO: a lot of work to be done here -- a new type needs to be created for this in go-fed/activity using ASTOOL
@ -262,7 +300,26 @@ func (c *converter) ASStatusToStatus(statusable Statusable) (*gtsmodel.Status, e
// language // language
// we might be able to extract this from the contentMap field // we might be able to extract this from the contentMap field
// ActivityStreamsType
status.ActivityStreamsType = gtsmodel.ActivityStreamsObject(statusable.GetTypeName()) status.ActivityStreamsType = gtsmodel.ActivityStreamsObject(statusable.GetTypeName())
return status, nil return status, nil
} }
func isPublic(tos []*url.URL) bool {
for _, entry := range tos {
if strings.EqualFold(entry.String(), "https://www.w3.org/ns/activitystreams#Public") {
return true
}
}
return false
}
func isFollowers(ccs []*url.URL, followersURI string) bool {
for _, entry := range ccs {
if strings.EqualFold(entry.String(), followersURI) {
return true
}
}
return false
}

View file

@ -58,7 +58,11 @@ const (
// APAccount can be used the set and retrieve the account being interacted with // APAccount can be used the set and retrieve the account being interacted with
APAccount APContextKey = "account" APAccount APContextKey = "account"
// APRequestingAccount can be used to set and retrieve the account of an incoming federation request. // APRequestingAccount can be used to set and retrieve the account of an incoming federation request.
// This will often be the actor of the instance that's posting the request.
APRequestingAccount APContextKey = "requestingAccount" APRequestingAccount APContextKey = "requestingAccount"
// APRequestingActorIRI can be used to set and retrieve the actor of an incoming federation request.
// This will usually be the owner of whatever activity is being posted.
APRequestingActorIRI APContextKey = "requestingActorIRI"
// APRequestingPublicKeyID can be used to set and retrieve the public key ID of an incoming federation request. // APRequestingPublicKeyID can be used to set and retrieve the public key ID of an incoming federation request.
APRequestingPublicKeyID APContextKey = "requestingPublicKeyID" APRequestingPublicKeyID APContextKey = "requestingPublicKeyID"
// APFromFederatorChanKey can be used to pass a pointer to the fromFederator channel into the federator for use in callbacks. // APFromFederatorChanKey can be used to pass a pointer to the fromFederator channel into the federator for use in callbacks.