serialize.go (6510B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package ap 19 20 import ( 21 "fmt" 22 23 "github.com/superseriousbusiness/activity/streams" 24 "github.com/superseriousbusiness/activity/streams/vocab" 25 ) 26 27 // Serialize is a custom serializer for ActivityStreams types. 28 // 29 // In most cases, it will simply call the go-fed streams.Serialize function under the hood. 30 // However, if custom serialization is required on a specific type (eg for inter-implementation 31 // compatibility), it can be inserted into the switch as necessary. 32 // 33 // Callers should always call this function instead of streams.Serialize, unless there's a 34 // very good reason to do otherwise. 35 // 36 // Currently, the following things will be custom serialized: 37 // 38 // - OrderedCollection: 'orderedItems' property will always be made into an array. 39 // - Any Accountable type: 'attachment' property will always be made into an array. 40 // - Update: any Accountable 'object's set on an update will be custom serialized as above. 41 func Serialize(t vocab.Type) (m map[string]interface{}, e error) { 42 switch t.GetTypeName() { 43 case ObjectOrderedCollection: 44 return serializeOrderedCollection(t) 45 case ActorApplication, ActorGroup, ActorOrganization, ActorPerson, ActorService: 46 return serializeAccountable(t, true) 47 case ActivityUpdate: 48 return serializeWithObject(t) 49 default: 50 // No custom serializer necessary. 51 return streams.Serialize(t) 52 } 53 } 54 55 // serializeOrderedCollection is a custom serializer for an ActivityStreamsOrderedCollection. 56 // Unlike the standard streams.Serialize function, this serializer normalizes the orderedItems 57 // value to always be an array/slice, regardless of how many items are contained therein. 58 // 59 // TODO: Remove this function if we can fix the underlying issue in Go-Fed. 60 // 61 // See: 62 // - https://github.com/go-fed/activity/issues/139 63 // - https://github.com/mastodon/mastodon/issues/24225 64 func serializeOrderedCollection(orderedCollection vocab.Type) (map[string]interface{}, error) { 65 data, err := streams.Serialize(orderedCollection) 66 if err != nil { 67 return nil, err 68 } 69 70 orderedItems, ok := data["orderedItems"] 71 if !ok { 72 // No 'orderedItems', nothing to change. 73 return data, nil 74 } 75 76 if _, ok := orderedItems.([]interface{}); ok { 77 // Already slice. 78 return data, nil 79 } 80 81 // Coerce single-object to slice. 82 data["orderedItems"] = []interface{}{orderedItems} 83 84 return data, nil 85 } 86 87 // SerializeAccountable is a custom serializer for any Accountable type. 88 // This serializer rewrites the 'attachment' value of the Accountable, if 89 // present, to always be an array/slice. 90 // 91 // While this is not strictly necessary in json-ld terms, most other fedi 92 // implementations look for attachment to be an array of PropertyValue (field) 93 // entries, and will not parse single-entry, non-array attachments on accounts 94 // properly. 95 // 96 // If the accountable is being serialized as a top-level object (eg., for serving 97 // in response to an account dereference request), then includeContext should be 98 // set to true, so as to include the json-ld '@context' entries in the data. 99 // If the accountable is being serialized as part of another object (eg., as the 100 // object of an activity), then includeContext should be set to false, as the 101 // @context entry should be included on the top-level/wrapping activity/object. 102 func serializeAccountable(accountable vocab.Type, includeContext bool) (map[string]interface{}, error) { 103 var ( 104 data map[string]interface{} 105 err error 106 ) 107 108 if includeContext { 109 data, err = streams.Serialize(accountable) 110 } else { 111 data, err = accountable.Serialize() 112 } 113 114 if err != nil { 115 return nil, err 116 } 117 118 attachment, ok := data["attachment"] 119 if !ok { 120 // No 'attachment', nothing to change. 121 return data, nil 122 } 123 124 if _, ok := attachment.([]interface{}); ok { 125 // Already slice. 126 return data, nil 127 } 128 129 // Coerce single-object to slice. 130 data["attachment"] = []interface{}{attachment} 131 132 return data, nil 133 } 134 135 func serializeWithObject(t vocab.Type) (map[string]interface{}, error) { 136 withObject, ok := t.(WithObject) 137 if !ok { 138 return nil, fmt.Errorf("serializeWithObject: could not resolve %T to WithObject", t) 139 } 140 141 data, err := streams.Serialize(t) 142 if err != nil { 143 return nil, err 144 } 145 146 object := withObject.GetActivityStreamsObject() 147 if object == nil { 148 // Nothing to do, bail early. 149 return data, nil 150 } 151 152 objectLen := object.Len() 153 if objectLen == 0 { 154 // Nothing to do, bail early. 155 return data, nil 156 } 157 158 // The thing we already serialized has objects 159 // on it, so we should see if we need to custom 160 // serialize any of those objects, and replace 161 // them on the data map as necessary. 162 objects := make([]interface{}, 0, objectLen) 163 for iter := object.Begin(); iter != object.End(); iter = iter.Next() { 164 if iter.IsIRI() { 165 // Plain IRIs don't need custom serialization. 166 objects = append(objects, iter.GetIRI().String()) 167 continue 168 } 169 170 var ( 171 objectType = iter.GetType() 172 objectSer map[string]interface{} 173 ) 174 175 if objectType == nil { 176 // This is awkward. 177 return nil, fmt.Errorf("serializeWithObject: could not resolve object iter %T to vocab.Type", iter) 178 } 179 180 switch objectType.GetTypeName() { 181 case ActorApplication, ActorGroup, ActorOrganization, ActorPerson, ActorService: 182 // @context will be included in wrapping type already, 183 // we don't need to include it in the object itself. 184 objectSer, err = serializeAccountable(objectType, false) 185 default: 186 // No custom serializer for this type; serialize as normal. 187 objectSer, err = objectType.Serialize() 188 } 189 190 if err != nil { 191 return nil, err 192 } 193 194 objects = append(objects, objectSer) 195 } 196 197 if objectLen == 1 { 198 // Unnest single object. 199 data["object"] = objects[0] 200 } else { 201 // Array of objects. 202 data["object"] = objects 203 } 204 205 return data, nil 206 }