diff --git a/internal/federation/federatingprotocol.go b/internal/federation/federatingprotocol.go index 22769317a..d549a157a 100644 --- a/internal/federation/federatingprotocol.go +++ b/internal/federation/federatingprotocol.go @@ -72,7 +72,6 @@ func (f *federator) PostInboxRequestBodyHook(ctx context.Context, r *http.Reques return nil, err } - ctxWithActivity := context.WithValue(ctx, util.APActivity, activity) return ctxWithActivity, nil } @@ -145,8 +144,9 @@ func (f *federator) AuthenticatePostInbox(ctx context.Context, w http.ResponseWr requestingAccount = a } - contextWithRequestingAccount := context.WithValue(ctx, util.APRequestingAccount, requestingAccount) - return contextWithRequestingAccount, true, nil + withRequester := context.WithValue(ctx, util.APRequestingAccount, requestingAccount) + withRequested := context.WithValue(withRequester, util.APAccount, requestedAccount) + return withRequested, true, nil } // Blocked should determine whether to permit a set of actors given by @@ -163,8 +163,40 @@ func (f *federator) AuthenticatePostInbox(ctx context.Context, w http.ResponseWr // Finally, if the authentication and authorization succeeds, then // blocked must be false and error nil. The request will continue // to be processed. +// +// TODO: implement domain block checking here as well func (f *federator) Blocked(ctx context.Context, actorIRIs []*url.URL) (bool, error) { - // TODO + l := f.log.WithFields(logrus.Fields{ + "func": "Blocked", + }) + l.Debugf("entering BLOCKED function with IRI list: %+v", actorIRIs) + + requestedAccountI := ctx.Value(util.APAccount) + requestedAccount, ok := requestedAccountI.(*gtsmodel.Account) + if !ok { + f.log.Errorf("requested account not set on request context") + return false, errors.New("requested account not set on request context, so couldn't determine blocks") + } + + for _, uri := range actorIRIs { + a := >smodel.Account{} + if err := f.db.GetWhere("uri", uri.String(), a); err != nil { + _, ok := err.(db.ErrNoEntries) + if ok { + // we don't have an entry for this account so it's not blocked + // TODO: allow a different default to be set for this behavior + continue + } + return false, fmt.Errorf("error getting account with uri %s: %s", uri.String(), err) + } + blocked, err := f.db.Blocked(requestedAccount.ID, a.ID) + if err != nil { + return false, fmt.Errorf("error checking account blocks: %s", err) + } + if blocked { + return true, nil + } + } return false, nil } diff --git a/internal/message/processor.go b/internal/message/processor.go index 9d7f0c6c9..44e30e562 100644 --- a/internal/message/processor.go +++ b/internal/message/processor.go @@ -20,7 +20,10 @@ package message import ( "context" + "errors" + "fmt" "net/http" + "net/url" "github.com/sirupsen/logrus" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" @@ -194,6 +197,9 @@ func (p *processor) Start() error { p.log.Infof("received message TO client API: %+v", clientMsg) case clientMsg := <-p.fromClientAPI: p.log.Infof("received message FROM client API: %+v", clientMsg) + if err := p.processFromClientAPI(clientMsg); err != nil { + p.log.Error(err) + } case federatorMsg := <-p.toFederator: p.log.Infof("received message TO federator: %+v", federatorMsg) case federatorMsg := <-p.fromFederator: @@ -240,3 +246,54 @@ type FromFederator struct { APActivityType gtsmodel.ActivityStreamsActivity Activity interface{} } + +func (p *processor) processFromClientAPI(clientMsg FromClientAPI) error { + switch clientMsg.APObjectType { + case gtsmodel.ActivityStreamsNote: + status, ok := clientMsg.Activity.(*gtsmodel.Status) + if !ok { + return errors.New("note was not parseable as *gtsmodel.Status") + } + + if err := p.notifyStatus(status); err != nil { + return err + } + + if status.VisibilityAdvanced.Federated { + return p.federateStatus(status) + } + return nil + } + return fmt.Errorf("message type unprocessable: %+v", clientMsg) +} + +func (p *processor) federateStatus(status *gtsmodel.Status) error { + // derive the sending account -- it might be attached to the status already + sendingAcct := >smodel.Account{} + if status.GTSAccount != nil { + sendingAcct = status.GTSAccount + } else { + // it wasn't attached so get it from the db instead + if err := p.db.GetByID(status.AccountID, sendingAcct); err != nil { + return err + } + } + + outboxURI, err := url.Parse(sendingAcct.OutboxURI) + if err != nil { + return err + } + + // convert the status to AS format Note + note, err := p.tc.StatusToAS(status) + if err != nil { + return err + } + + _, err = p.federator.FederatingActor().Send(context.Background(), outboxURI, note) + return err +} + +func (p *processor) notifyStatus(status *gtsmodel.Status) error { + return nil +} diff --git a/internal/message/statusprocess.go b/internal/message/statusprocess.go index bd4329082..d9d115aec 100644 --- a/internal/message/statusprocess.go +++ b/internal/message/statusprocess.go @@ -81,6 +81,13 @@ func (p *processor) StatusCreate(auth *oauth.Auth, form *apimodel.AdvancedStatus } } + // put the new status in the appropriate channel for async processing + p.fromClientAPI <- FromClientAPI{ + APObjectType: newStatus.ActivityStreamsType, + APActivityType: gtsmodel.ActivityStreamsCreate, + Activity: newStatus, + } + // return the frontend representation of the new status to the submitter return p.tc.StatusToMasto(newStatus, auth.Account, auth.Account, nil, newStatus.GTSReplyToAccount, nil) }