[feature] Accept incoming federated Flag activity (#1382)

* start working on handling incoming Flag activity

* interim commit

* federate Flag in successfully
This commit is contained in:
tobi 2023-01-25 11:12:27 +01:00 committed by GitHub
commit 993aae5e48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 560 additions and 50 deletions

View file

@ -78,6 +78,9 @@ func (f *federatingDB) Create(ctx context.Context, asType vocab.Type) error {
case ap.ActivityLike:
// LIKE SOMETHING
return f.activityLike(ctx, asType, receivingAccount, requestingAccount)
case ap.ActivityFlag:
// FLAG / REPORT SOMETHING
return f.activityFlag(ctx, asType, receivingAccount, requestingAccount)
}
return nil
}
@ -314,3 +317,38 @@ func (f *federatingDB) activityLike(ctx context.Context, asType vocab.Type, rece
return nil
}
/*
FLAG HANDLERS
*/
func (f *federatingDB) activityFlag(ctx context.Context, asType vocab.Type, receivingAccount *gtsmodel.Account, requestingAccount *gtsmodel.Account) error {
flag, ok := asType.(vocab.ActivityStreamsFlag)
if !ok {
return errors.New("activityFlag: could not convert type to flag")
}
report, err := f.typeConverter.ASFlagToReport(ctx, flag)
if err != nil {
return fmt.Errorf("activityFlag: could not convert Flag to report: %w", err)
}
newID, err := id.NewULID()
if err != nil {
return err
}
report.ID = newID
if err := f.db.PutReport(ctx, report); err != nil {
return fmt.Errorf("activityFlag: database error inserting report: %w", err)
}
f.fedWorker.Queue(messages.FromFederator{
APObjectType: ap.ActivityFlag,
APActivityType: ap.ActivityCreate,
GTSModel: report,
ReceivingAccount: receivingAccount,
})
return nil
}

View file

@ -20,9 +20,11 @@ package federatingdb_test
import (
"context"
"encoding/json"
"testing"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/activity/streams"
"github.com/superseriousbusiness/gotosocial/internal/ap"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
@ -83,6 +85,50 @@ func (suite *CreateTestSuite) TestCreateNoteForward() {
suite.Equal("http://example.org/users/Some_User/statuses/afaba698-5740-4e32-a702-af61aa543bc1", msg.APIri.String())
}
func (suite *CreateTestSuite) TestCreateFlag1() {
reportedAccount := suite.testAccounts["local_account_1"]
reportingAccount := suite.testAccounts["remote_account_1"]
reportedStatus := suite.testStatuses["local_account_1_status_1"]
raw := `{
"@context": "https://www.w3.org/ns/activitystreams",
"actor": "` + reportingAccount.URI + `",
"content": "Note: ` + reportedStatus.URL + `\n-----\nban this sick filth ⛔",
"id": "http://fossbros-anonymous.io/db22128d-884e-4358-9935-6a7c3940535d",
"object": "` + reportedAccount.URI + `",
"type": "Flag"
}`
m := make(map[string]interface{})
if err := json.Unmarshal([]byte(raw), &m); err != nil {
suite.FailNow(err.Error())
}
t, err := streams.ToType(context.Background(), m)
if err != nil {
suite.FailNow(err.Error())
}
ctx := createTestContext(reportedAccount, reportingAccount)
if err := suite.federatingDB.Create(ctx, t); err != nil {
suite.FailNow(err.Error())
}
// should be a message heading to the processor now, which we can intercept here
msg := <-suite.fromFederator
suite.Equal(ap.ActivityFlag, msg.APObjectType)
suite.Equal(ap.ActivityCreate, msg.APActivityType)
// shiny new report should be defined on the message
suite.NotNil(msg.GTSModel)
report := msg.GTSModel.(*gtsmodel.Report)
// report should be in the database
if _, err := suite.db.GetReportByID(context.Background(), report.ID); err != nil {
suite.FailNow(err.Error())
}
}
func TestCreateTestSuite(t *testing.T) {
suite.Run(t, &CreateTestSuite{})
}