summaryrefslogtreecommitdiff
path: root/internal/typeutils
diff options
context:
space:
mode:
Diffstat (limited to 'internal/typeutils')
-rw-r--r--internal/typeutils/converter.go2
-rw-r--r--internal/typeutils/internaltoas.go50
-rw-r--r--internal/typeutils/internaltoas_test.go34
3 files changed, 86 insertions, 0 deletions
diff --git a/internal/typeutils/converter.go b/internal/typeutils/converter.go
index 33ed617e0..ec7b09f27 100644
--- a/internal/typeutils/converter.go
+++ b/internal/typeutils/converter.go
@@ -188,6 +188,8 @@ type TypeConverter interface {
//
// Appropriate 'next' and 'prev' fields will be created based on the highest and lowest IDs present in the statuses slice.
StatusesToASOutboxPage(ctx context.Context, outboxID string, maxID string, minID string, statuses []*gtsmodel.Status) (vocab.ActivityStreamsOrderedCollectionPage, error)
+ // ReportToASFlag converts a gts model report into an activitystreams FLAG, suitable for federation.
+ ReportToASFlag(ctx context.Context, r *gtsmodel.Report) (vocab.ActivityStreamsFlag, error)
/*
INTERNAL (gts) MODEL TO INTERNAL MODEL
diff --git a/internal/typeutils/internaltoas.go b/internal/typeutils/internaltoas.go
index bf4dc7e18..2ae58b317 100644
--- a/internal/typeutils/internaltoas.go
+++ b/internal/typeutils/internaltoas.go
@@ -1295,3 +1295,53 @@ func (c *converter) OutboxToASCollection(ctx context.Context, outboxID string) (
return collection, nil
}
+
+func (c *converter) ReportToASFlag(ctx context.Context, r *gtsmodel.Report) (vocab.ActivityStreamsFlag, error) {
+ flag := streams.NewActivityStreamsFlag()
+
+ flagIDProp := streams.NewJSONLDIdProperty()
+ idURI, err := url.Parse(r.URI)
+ if err != nil {
+ return nil, fmt.Errorf("error parsing url %s: %w", r.URI, err)
+ }
+ flagIDProp.SetIRI(idURI)
+ flag.SetJSONLDId(flagIDProp)
+
+ // for privacy, set the actor as the INSTANCE ACTOR,
+ // not as the actor who created the report
+ instanceAccount, err := c.db.GetInstanceAccount(ctx, "")
+ if err != nil {
+ return nil, fmt.Errorf("error getting instance account: %w", err)
+ }
+ instanceAccountIRI, err := url.Parse(instanceAccount.URI)
+ if err != nil {
+ return nil, fmt.Errorf("error parsing url %s: %w", instanceAccount.URI, err)
+ }
+ flagActorProp := streams.NewActivityStreamsActorProperty()
+ flagActorProp.AppendIRI(instanceAccountIRI)
+ flag.SetActivityStreamsActor(flagActorProp)
+
+ // content should be the comment submitted when the report was created
+ contentProp := streams.NewActivityStreamsContentProperty()
+ contentProp.AppendXMLSchemaString(r.Comment)
+ flag.SetActivityStreamsContent(contentProp)
+
+ // set at least the target account uri as the object of the flag
+ objectProp := streams.NewActivityStreamsObjectProperty()
+ targetAccountURI, err := url.Parse(r.TargetAccount.URI)
+ if err != nil {
+ return nil, fmt.Errorf("error parsing url %s: %w", r.TargetAccount.URI, err)
+ }
+ objectProp.AppendIRI(targetAccountURI)
+ // also set status URIs if they were provided with the report
+ for _, s := range r.Statuses {
+ statusURI, err := url.Parse(s.URI)
+ if err != nil {
+ return nil, fmt.Errorf("error parsing url %s: %w", s.URI, err)
+ }
+ objectProp.AppendIRI(statusURI)
+ }
+ flag.SetActivityStreamsObject(objectProp)
+
+ return flag, nil
+}
diff --git a/internal/typeutils/internaltoas_test.go b/internal/typeutils/internaltoas_test.go
index d9a91b736..0bbb80dac 100644
--- a/internal/typeutils/internaltoas_test.go
+++ b/internal/typeutils/internaltoas_test.go
@@ -510,6 +510,40 @@ func (suite *InternalToASTestSuite) TestSelfBoostFollowersOnlyToAS() {
}`, string(bytes))
}
+func (suite *InternalToASTestSuite) TestReportToAS() {
+ ctx := context.Background()
+
+ testReport := suite.testReports["local_account_2_report_remote_account_1"]
+ account := suite.testAccounts["local_account_2"]
+ targetAccount := suite.testAccounts["remote_account_1"]
+ statuses := []*gtsmodel.Status{suite.testStatuses["remote_account_1_status_1"]}
+
+ testReport.Account = account
+ testReport.TargetAccount = targetAccount
+ testReport.Statuses = statuses
+
+ flag, err := suite.typeconverter.ReportToASFlag(ctx, testReport)
+ suite.NoError(err)
+
+ ser, err := streams.Serialize(flag)
+ suite.NoError(err)
+
+ bytes, err := json.MarshalIndent(ser, "", " ")
+ suite.NoError(err)
+
+ suite.Equal(`{
+ "@context": "https://www.w3.org/ns/activitystreams",
+ "actor": "http://localhost:8080/users/localhost:8080",
+ "content": "dark souls sucks, please yeet this nerd",
+ "id": "http://localhost:8080/reports/01GP3AWY4CRDVRNZKW0TEAMB5R",
+ "object": [
+ "http://fossbros-anonymous.io/users/foss_satan",
+ "http://fossbros-anonymous.io/users/foss_satan/statuses/01FVW7JHQFSFK166WWKR8CBA6M"
+ ],
+ "type": "Flag"
+}`, string(bytes))
+}
+
func TestInternalToASTestSuite(t *testing.T) {
suite.Run(t, new(InternalToASTestSuite))
}