diff options
author | 2023-05-09 12:16:10 +0200 | |
---|---|---|
committer | 2023-05-09 11:16:10 +0100 | |
commit | 0e29f1f5bb68a48d9b837d7f4e0a16370734955b (patch) | |
tree | f08d203ec8ca8aeea728e5251b1dc3956524b4f4 /internal/ap | |
parent | [chore/performance] Make sender multiplier configurable (#1750) (diff) | |
download | gotosocial-0e29f1f5bb68a48d9b837d7f4e0a16370734955b.tar.xz |
[feature] Enable federation in/out of profile PropertyValue fields (#1722)
Co-authored-by: kim <grufwub@gmail.com>
Co-authored-by: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>
Diffstat (limited to 'internal/ap')
-rw-r--r-- | internal/ap/extract.go | 53 | ||||
-rw-r--r-- | internal/ap/interfaces.go | 1 | ||||
-rw-r--r-- | internal/ap/normalize.go | 110 | ||||
-rw-r--r-- | internal/ap/normalize_test.go | 16 | ||||
-rw-r--r-- | internal/ap/resolve.go | 10 | ||||
-rw-r--r-- | internal/ap/serialize.go | 168 |
6 files changed, 294 insertions, 64 deletions
diff --git a/internal/ap/extract.go b/internal/ap/extract.go index 2742d27ac..ce7c03901 100644 --- a/internal/ap/extract.go +++ b/internal/ap/extract.go @@ -266,6 +266,59 @@ func ExtractSummary(i WithSummary) string { return "" } +func ExtractFields(i WithAttachment) []*gtsmodel.Field { + attachmentProp := i.GetActivityStreamsAttachment() + if attachmentProp == nil { + // Nothing to do. + return nil + } + + l := attachmentProp.Len() + if l == 0 { + // Nothing to do. + return nil + } + + fields := make([]*gtsmodel.Field, 0, l) + for iter := attachmentProp.Begin(); iter != attachmentProp.End(); iter = iter.Next() { + if !iter.IsSchemaPropertyValue() { + continue + } + + propertyValue := iter.GetSchemaPropertyValue() + if propertyValue == nil { + continue + } + + nameProp := propertyValue.GetActivityStreamsName() + if nameProp == nil || nameProp.Len() != 1 { + continue + } + + name := nameProp.At(0).GetXMLSchemaString() + if name == "" { + continue + } + + valueProp := propertyValue.GetSchemaValue() + if valueProp == nil || !valueProp.IsXMLSchemaString() { + continue + } + + value := valueProp.Get() + if value == "" { + continue + } + + fields = append(fields, >smodel.Field{ + Name: name, + Value: value, + }) + } + + return fields +} + // ExtractDiscoverable extracts the Discoverable boolean of an interface. func ExtractDiscoverable(i WithDiscoverable) (bool, error) { if i.GetTootDiscoverable() == nil { diff --git a/internal/ap/interfaces.go b/internal/ap/interfaces.go index 674791229..611f4bde0 100644 --- a/internal/ap/interfaces.go +++ b/internal/ap/interfaces.go @@ -30,6 +30,7 @@ type Accountable interface { WithName WithImage WithSummary + WithAttachment WithSetSummary WithDiscoverable WithURL diff --git a/internal/ap/normalize.go b/internal/ap/normalize.go index 52e297080..38861a1b9 100644 --- a/internal/ap/normalize.go +++ b/internal/ap/normalize.go @@ -22,14 +22,23 @@ import ( "github.com/superseriousbusiness/activity/streams" ) -// NormalizeActivityObject normalizes the 'object'.'content' field of the given Activity. +/* + NORMALIZE INCOMING + The below functions should be called to normalize the content + of messages *COMING INTO* GoToSocial via the federation API, + either as the result of delivery from a remote instance to this + instance, or as a result of this instance doing an http call to + another instance to dereference something. +*/ + +// NormalizeIncomingActivityObject normalizes the 'object'.'content' field of the given Activity. // // The rawActivity map should the freshly deserialized json representation of the Activity. // -// This function is a noop if the type passed in is anything except a Create with a Statusable as its Object. -func NormalizeActivityObject(activity pub.Activity, rawJSON map[string]interface{}) { - if activity.GetTypeName() != ActivityCreate { - // Only interested in Create right now. +// This function is a noop if the type passed in is anything except a Create or Update with a Statusable or Accountable as its Object. +func NormalizeIncomingActivityObject(activity pub.Activity, rawJSON map[string]interface{}) { + if typeName := activity.GetTypeName(); typeName != ActivityCreate && typeName != ActivityUpdate { + // Only interested in Create or Update right now. return } @@ -51,8 +60,8 @@ func NormalizeActivityObject(activity pub.Activity, rawJSON map[string]interface } // We now know length is 1 so get the first - // item from the iter. We need this to be - // a Statusable if we're to continue. + // item from the iter. We need this to be + // a Statusable or Accountable if we're to continue. i := createObject.At(0) if i == nil { // This is awkward. @@ -65,38 +74,63 @@ func NormalizeActivityObject(activity pub.Activity, rawJSON map[string]interface return } - statusable, ok := t.(Statusable) - if !ok { - // Object is not Statusable; - // we're not interested. - return - } + switch t.GetTypeName() { + case ObjectArticle, ObjectDocument, ObjectImage, ObjectVideo, ObjectNote, ObjectPage, ObjectEvent, ObjectPlace, ObjectProfile: + statusable, ok := t.(Statusable) + if !ok { + // Object is not Statusable; + // we're not interested. + return + } - rawObject, ok := rawJSON["object"] - if !ok { - // No object in raw map. - return - } + rawObject, ok := rawJSON["object"] + if !ok { + // No object in raw map. + return + } - rawStatusableJSON, ok := rawObject.(map[string]interface{}) - if !ok { - // Object wasn't a json object. - return - } + rawStatusableJSON, ok := rawObject.(map[string]interface{}) + if !ok { + // Object wasn't a json object. + return + } - // Normalize everything we can on the statusable. - NormalizeContent(statusable, rawStatusableJSON) - NormalizeAttachments(statusable, rawStatusableJSON) - NormalizeSummary(statusable, rawStatusableJSON) - NormalizeName(statusable, rawStatusableJSON) + // Normalize everything we can on the statusable. + NormalizeIncomingContent(statusable, rawStatusableJSON) + NormalizeIncomingAttachments(statusable, rawStatusableJSON) + NormalizeIncomingSummary(statusable, rawStatusableJSON) + NormalizeIncomingName(statusable, rawStatusableJSON) + case ActorApplication, ActorGroup, ActorOrganization, ActorPerson, ActorService: + accountable, ok := t.(Accountable) + if !ok { + // Object is not Accountable; + // we're not interested. + return + } + + rawObject, ok := rawJSON["object"] + if !ok { + // No object in raw map. + return + } + + rawAccountableJSON, ok := rawObject.(map[string]interface{}) + if !ok { + // Object wasn't a json object. + return + } + + // Normalize everything we can on the accountable. + NormalizeIncomingSummary(accountable, rawAccountableJSON) + } } -// NormalizeContent replaces the Content of the given item +// NormalizeIncomingContent replaces the Content of the given item // with the raw 'content' value from the raw json object map. // // noop if there was no content in the json object map or the // content was not a plain string. -func NormalizeContent(item WithSetContent, rawJSON map[string]interface{}) { +func NormalizeIncomingContent(item WithSetContent, rawJSON map[string]interface{}) { rawContent, ok := rawJSON["content"] if !ok { // No content in rawJSON. @@ -118,13 +152,13 @@ func NormalizeContent(item WithSetContent, rawJSON map[string]interface{}) { item.SetActivityStreamsContent(contentProp) } -// NormalizeAttachments normalizes all attachments (if any) of the given -// itm, replacing the 'name' (aka content warning) field of each attachment +// NormalizeIncomingAttachments normalizes all attachments (if any) of the given +// item, replacing the 'name' (aka content warning) field of each attachment // with the raw 'name' value from the raw json object map. // // noop if there are no attachments; noop if attachment is not a format // we can understand. -func NormalizeAttachments(item WithAttachment, rawJSON map[string]interface{}) { +func NormalizeIncomingAttachments(item WithAttachment, rawJSON map[string]interface{}) { rawAttachments, ok := rawJSON["attachment"] if !ok { // No attachments in rawJSON. @@ -173,16 +207,16 @@ func NormalizeAttachments(item WithAttachment, rawJSON map[string]interface{}) { continue } - NormalizeName(attachmentable, rawAttachment) + NormalizeIncomingName(attachmentable, rawAttachment) } } -// NormalizeSummary replaces the Summary of the given item +// NormalizeIncomingSummary replaces the Summary of the given item // with the raw 'summary' value from the raw json object map. // // noop if there was no summary in the json object map or the // summary was not a plain string. -func NormalizeSummary(item WithSetSummary, rawJSON map[string]interface{}) { +func NormalizeIncomingSummary(item WithSetSummary, rawJSON map[string]interface{}) { rawSummary, ok := rawJSON["summary"] if !ok { // No summary in rawJSON. @@ -202,12 +236,12 @@ func NormalizeSummary(item WithSetSummary, rawJSON map[string]interface{}) { item.SetActivityStreamsSummary(summaryProp) } -// NormalizeName replaces the Name of the given item +// NormalizeIncomingName replaces the Name of the given item // with the raw 'name' value from the raw json object map. // // noop if there was no name in the json object map or the // name was not a plain string. -func NormalizeName(item WithSetName, rawJSON map[string]interface{}) { +func NormalizeIncomingName(item WithSetName, rawJSON map[string]interface{}) { rawName, ok := rawJSON["name"] if !ok { // No name in rawJSON. diff --git a/internal/ap/normalize_test.go b/internal/ap/normalize_test.go index c265b02f5..2c9a1907a 100644 --- a/internal/ap/normalize_test.go +++ b/internal/ap/normalize_test.go @@ -49,7 +49,7 @@ func (suite *NormalizeTestSuite) jsonToType(rawJson string) (vocab.Type, map[str } func (suite *NormalizeTestSuite) typeToJson(t vocab.Type) string { - m, err := streams.Serialize(t) + m, err := ap.Serialize(t) if err != nil { suite.FailNow(err.Error()) } @@ -223,7 +223,7 @@ func (suite *NormalizeTestSuite) TestNormalizeActivityObject() { note, ) - ap.NormalizeActivityObject(create, map[string]interface{}{"object": rawNote}) + ap.NormalizeIncomingActivityObject(create, map[string]interface{}{"object": rawNote}) suite.Equal(`UPDATE: As of this morning there are now more than 7 million Mastodon users, most from the <a class="hashtag" data-tag="twittermigration" href="https://example.org/tag/twittermigration" rel="tag ugc">#TwitterMigration</a>.<br><br>In fact, 100,000 new accounts have been created since last night.<br><br>Since last night's spike 8,000-12,000 new accounts are being created every hour.<br><br>Yesterday, I estimated that Mastodon would have 8 million users by the end of the week. That might happen a lot sooner if this trend continues.`, ap.ExtractContent(note)) } @@ -248,7 +248,7 @@ func (suite *NormalizeTestSuite) TestNormalizeStatusableAttachmentsOneAttachment }`, suite.typeToJson(note)) // Normalize it! - ap.NormalizeAttachments(note, rawNote) + ap.NormalizeIncomingAttachments(note, rawNote) // After normalization, the 'name' field of the // attachment should no longer be all jacked up. @@ -289,7 +289,7 @@ func (suite *NormalizeTestSuite) TestNormalizeStatusableAttachmentsOneAttachment }`, suite.typeToJson(note)) // Normalize it! - ap.NormalizeAttachments(note, rawNote) + ap.NormalizeIncomingAttachments(note, rawNote) // After normalization, the 'name' field of the // attachment should no longer be all jacked up. @@ -349,7 +349,7 @@ func (suite *NormalizeTestSuite) TestNormalizeStatusableAttachmentsMultipleAttac }`, suite.typeToJson(note)) // Normalize it! - ap.NormalizeAttachments(note, rawNote) + ap.NormalizeIncomingAttachments(note, rawNote) // After normalization, the 'name' field of the // attachment should no longer be all jacked up. @@ -392,7 +392,7 @@ func (suite *NormalizeTestSuite) TestNormalizeAccountableSummary() { accountable, rawAccount := suite.getAccountable() suite.Equal(`about: I'm a #Barbie%20%23girl%20in%20a%20%23Barbie%20%23world%0ALife%20in%20plastic,%20it%27s%20fantastic%0AYou%20can%20brush%20my%20hair,%20undress%20me%20everywhere%0AImagination,%20life%20is%20your%20creation%0AI%27m%20a%20blonde%20bimbo%20girl%0AIn%20a%20fantasy%20world%0ADress%20me%20up,%20make%20it%20tight%0AI%27m%20your%20dolly%0AYou%27re%20my%20doll,%20rock%20and%20roll%0AFeel%20the%20glamour%20in%20pink%0AKiss%20me%20here,%20touch%20me%20there%0AHanky%20panky`, ap.ExtractSummary(accountable)) - ap.NormalizeSummary(accountable, rawAccount) + ap.NormalizeIncomingSummary(accountable, rawAccount) suite.Equal(`about: I'm a #Barbie #girl in a #Barbie #world Life in plastic, it's fantastic You can brush my hair, undress me everywhere @@ -411,7 +411,7 @@ func (suite *NormalizeTestSuite) TestNormalizeStatusableSummary() { statusable, rawAccount := suite.getStatusableWithWeirdSummaryAndName() suite.Equal(`warning: #WEIRD%20%23SUMMARY%20;;;;a;;a;asv%20%20%20%20khop8273987(*%5E&%5E)`, ap.ExtractSummary(statusable)) - ap.NormalizeSummary(statusable, rawAccount) + ap.NormalizeIncomingSummary(statusable, rawAccount) suite.Equal(`warning: #WEIRD #SUMMARY ;;;;a;;a;asv khop8273987(*^&^)`, ap.ExtractSummary(statusable)) } @@ -419,7 +419,7 @@ func (suite *NormalizeTestSuite) TestNormalizeStatusableName() { statusable, rawAccount := suite.getStatusableWithWeirdSummaryAndName() suite.Equal(`warning: #WEIRD%20%23nameEE%20;;;;a;;a;asv%20%20%20%20khop8273987(*%5E&%5E)`, ap.ExtractName(statusable)) - ap.NormalizeName(statusable, rawAccount) + ap.NormalizeIncomingName(statusable, rawAccount) suite.Equal(`WARNING: #WEIRD #nameEE ;;;;a;;a;asv khop8273987(*^&^)`, ap.ExtractName(statusable)) } diff --git a/internal/ap/resolve.go b/internal/ap/resolve.go index 8d116751c..ef4d0b50f 100644 --- a/internal/ap/resolve.go +++ b/internal/ap/resolve.go @@ -72,10 +72,10 @@ func ResolveStatusable(ctx context.Context, b []byte) (Statusable, error) { return nil, newErrWrongType(err) } - NormalizeContent(statusable, rawStatusable) - NormalizeAttachments(statusable, rawStatusable) - NormalizeSummary(statusable, rawStatusable) - NormalizeName(statusable, rawStatusable) + NormalizeIncomingContent(statusable, rawStatusable) + NormalizeIncomingAttachments(statusable, rawStatusable) + NormalizeIncomingSummary(statusable, rawStatusable) + NormalizeIncomingName(statusable, rawStatusable) return statusable, nil } @@ -118,7 +118,7 @@ func ResolveAccountable(ctx context.Context, b []byte) (Accountable, error) { return nil, newErrWrongType(err) } - NormalizeSummary(accountable, rawAccountable) + NormalizeIncomingSummary(accountable, rawAccountable) return accountable, nil } diff --git a/internal/ap/serialize.go b/internal/ap/serialize.go index 471a7f41e..368d7f9a2 100644 --- a/internal/ap/serialize.go +++ b/internal/ap/serialize.go @@ -18,13 +18,41 @@ package ap import ( - "errors" + "fmt" "github.com/superseriousbusiness/activity/streams" "github.com/superseriousbusiness/activity/streams/vocab" ) -// SerializeOrderedCollection is a custom serializer for an ActivityStreamsOrderedCollection. +// Serialize is a custom serializer for ActivityStreams types. +// +// In most cases, it will simply call the go-fed streams.Serialize function under the hood. +// However, if custom serialization is required on a specific type (eg for inter-implementation +// compatibility), it can be inserted into the switch as necessary. +// +// Callers should always call this function instead of streams.Serialize, unless there's a +// very good reason to do otherwise. +// +// Currently, the following things will be custom serialized: +// +// - OrderedCollection: 'orderedItems' property will always be made into an array. +// - Any Accountable type: 'attachment' property will always be made into an array. +// - Update: any Accountable 'object's set on an update will be custom serialized as above. +func Serialize(t vocab.Type) (m map[string]interface{}, e error) { + switch t.GetTypeName() { + case ObjectOrderedCollection: + return serializeOrderedCollection(t) + case ActorApplication, ActorGroup, ActorOrganization, ActorPerson, ActorService: + return serializeAccountable(t, true) + case ActivityUpdate: + return serializeWithObject(t) + default: + // No custom serializer necessary. + return streams.Serialize(t) + } +} + +// serializeOrderedCollection is a custom serializer for an ActivityStreamsOrderedCollection. // Unlike the standard streams.Serialize function, this serializer normalizes the orderedItems // value to always be an array/slice, regardless of how many items are contained therein. // @@ -33,32 +61,146 @@ import ( // See: // - https://github.com/go-fed/activity/issues/139 // - https://github.com/mastodon/mastodon/issues/24225 -func SerializeOrderedCollection(orderedCollection vocab.ActivityStreamsOrderedCollection) (map[string]interface{}, error) { +func serializeOrderedCollection(orderedCollection vocab.Type) (map[string]interface{}, error) { data, err := streams.Serialize(orderedCollection) if err != nil { return nil, err } - return data, normalizeOrderedCollectionData(data) + orderedItems, ok := data["orderedItems"] + if !ok { + // No 'orderedItems', nothing to change. + return data, nil + } + + if _, ok := orderedItems.([]interface{}); ok { + // Already slice. + return data, nil + } + + // Coerce single-object to slice. + data["orderedItems"] = []interface{}{orderedItems} + + return data, nil } -func normalizeOrderedCollectionData(rawOrderedCollection map[string]interface{}) error { - orderedItems, ok := rawOrderedCollection["orderedItems"] +// SerializeAccountable is a custom serializer for any Accountable type. +// This serializer rewrites the 'attachment' value of the Accountable, if +// present, to always be an array/slice. +// +// While this is not strictly necessary in json-ld terms, most other fedi +// implementations look for attachment to be an array of PropertyValue (field) +// entries, and will not parse single-entry, non-array attachments on accounts +// properly. +// +// If the accountable is being serialized as a top-level object (eg., for serving +// in response to an account dereference request), then includeContext should be +// set to true, so as to include the json-ld '@context' entries in the data. +// If the accountable is being serialized as part of another object (eg., as the +// object of an activity), then includeContext should be set to false, as the +// @context entry should be included on the top-level/wrapping activity/object. +func serializeAccountable(accountable vocab.Type, includeContext bool) (map[string]interface{}, error) { + var ( + data map[string]interface{} + err error + ) + + if includeContext { + data, err = streams.Serialize(accountable) + } else { + data, err = accountable.Serialize() + } + + if err != nil { + return nil, err + } + + attachment, ok := data["attachment"] if !ok { - return errors.New("no orderedItems set on OrderedCollection") + // No 'attachment', nothing to change. + return data, nil } - if _, ok := orderedItems.([]interface{}); ok { + if _, ok := attachment.([]interface{}); ok { // Already slice. - return nil + return data, nil } - orderedItemsString, ok := orderedItems.(string) + // Coerce single-object to slice. + data["attachment"] = []interface{}{attachment} + + return data, nil +} + +func serializeWithObject(t vocab.Type) (map[string]interface{}, error) { + withObject, ok := t.(WithObject) if !ok { - return errors.New("orderedItems was neither slice nor string") + return nil, fmt.Errorf("serializeWithObject: could not resolve %T to WithObject", t) + } + + data, err := streams.Serialize(t) + if err != nil { + return nil, err + } + + object := withObject.GetActivityStreamsObject() + if object == nil { + // Nothing to do, bail early. + return data, nil + } + + objectLen := object.Len() + if objectLen == 0 { + // Nothing to do, bail early. + return data, nil } - rawOrderedCollection["orderedItems"] = []string{orderedItemsString} + // The thing we already serialized has objects + // on it, so we should see if we need to custom + // serialize any of those objects, and replace + // them on the data map as necessary. + objects := make([]interface{}, 0, objectLen) + for iter := object.Begin(); iter != object.End(); iter = iter.Next() { + if iter.IsIRI() { + // Plain IRIs don't need custom serialization. + objects = append(objects, iter.GetIRI().String()) + continue + } + + var ( + objectType = iter.GetType() + objectSer map[string]interface{} + ) + + if objectType == nil { + // This is awkward. + return nil, fmt.Errorf("serializeWithObject: could not resolve object iter %T to vocab.Type", iter) + } + + switch objectType.GetTypeName() { + case ActorApplication, ActorGroup, ActorOrganization, ActorPerson, ActorService: + // @context will be included in wrapping type already, + // we don't need to include it in the object itself. + objectSer, err = serializeAccountable(objectType, false) + default: + // No custom serializer for this type; serialize as normal. + objectSer, err = objectType.Serialize() + } + + if err != nil { + return nil, err + } + + objects = append(objects, objectSer) + } + + if objectLen == 1 { + // Unnest single object. + data["object"] = objects[0] + } else { + // Array of objects. + data["object"] = objects + } - return nil + return data, nil } |