| @@ -17,31 +17,12 @@ | ||
| 17 | 17 | * attributes of an Activity. |
| 18 | 18 | * |
| 19 | 19 | * @see https://www.w3.org/TR/activitystreams-core/#activities |
| 20 | 20 | * @see https://www.w3.org/TR/activitystreams-core/#intransitiveactivities |
| 21 | - * | |
| 22 | - * @method string|array get_actor() Gets one or more entities that performed or are expected to perform the activity. | |
| 23 | - * @method string|array|null get_instrument() Gets one or more objects used in the completion of an Activity. | |
| 24 | - * @method Base_Object|string|array|null get_object() Gets the direct object of the activity. | |
| 25 | - * @method string|string[]|null get_origin() Gets the origin property of the activity. | |
| 26 | - * @method array|null get_replies() Gets the collection of responses to this activity. | |
| 27 | - * @method string|null get_result() Gets the result property of the activity. | |
| 28 | - * @method string|string[]|null get_target() Gets the target property of the activity. | |
| 29 | - * | |
| 30 | - * @method Activity set_actor( string|array $actor ) Sets one or more entities that performed the activity. | |
| 31 | - * @method Activity set_instrument( string|array $instrument ) Sets one or more objects used in the completion of an Activity. | |
| 32 | - * @method Activity set_origin( string|array|null $origin ) Sets the origin property of the activity. | |
| 33 | - * @method Activity set_replies( array $replies ) Sets the collection of responses to this activity. | |
| 34 | - * @method Activity set_result( string|null $result ) Sets the result property of the activity. | |
| 35 | - * @method Activity set_target( string|array|null $target ) Sets the target property of the activity. | |
| 36 | 21 | */ |
| 37 | 22 | class Activity extends Base_Object { |
| 38 | 23 | const JSON_LD_CONTEXT = array( |
| 39 | 24 | 'https://www.w3.org/ns/activitystreams', |
| 40 | - array( | |
| 41 | - 'toot' => 'http://joinmastodon.org/ns#', | |
| 42 | - 'QuoteRequest' => 'toot:QuoteRequest', | |
| 43 | - ), | |
| 44 | 25 | ); |
| 45 | 26 | |
| 46 | 27 | /** |
| 47 | 28 | * The default types for Activities. |
| @@ -68,9 +49,8 @@ | ||
| 68 | 49 | 'Like', |
| 69 | 50 | 'Listen', |
| 70 | 51 | 'Move', |
| 71 | 52 | 'Offer', |
| 72 | - 'QuoteRequest', // @see https://codeberg.org/fediverse/fep/src/branch/main/fep/044f/fep-044f.md | |
| 73 | 53 | 'Read', |
| 74 | 54 | 'Reject', |
| 75 | 55 | 'Remove', |
| 76 | 56 | 'TentativeAccept', |
| @@ -94,9 +74,9 @@ | ||
| 94 | 74 | * wishlist", the object of the activity is the movie added. |
| 95 | 75 | * |
| 96 | 76 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-object-term |
| 97 | 77 | * |
| 98 | - * @var string|Base_Object|array|null | |
| 78 | + * @var string|Base_Object|null | |
| 99 | 79 | */ |
| 100 | 80 | protected $object; |
| 101 | 81 | |
| 102 | 82 | /** |
| @@ -121,9 +101,9 @@ | ||
| 121 | 101 | * An activity can have more than one target. |
| 122 | 102 | * |
| 123 | 103 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-target |
| 124 | 104 | * |
| 125 | - * @var string|array|null | |
| 105 | + * @var string|array | |
| 126 | 106 | */ |
| 127 | 107 | protected $target; |
| 128 | 108 | |
| 129 | 109 | /** |
| @@ -133,9 +113,9 @@ | ||
| 133 | 113 | * that new resource. |
| 134 | 114 | * |
| 135 | 115 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-result |
| 136 | 116 | * |
| 137 | - * @var string|Base_Object|null | |
| 117 | + * @var string|Base_Object | |
| 138 | 118 | */ |
| 139 | 119 | protected $result; |
| 140 | 120 | |
| 141 | 121 | /** |
| @@ -145,9 +125,9 @@ | ||
| 145 | 125 | * approved replies here. |
| 146 | 126 | * |
| 147 | 127 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-replies |
| 148 | 128 | * |
| 149 | - * @var array|null | |
| 129 | + * @var array | |
| 150 | 130 | */ |
| 151 | 131 | protected $replies; |
| 152 | 132 | |
| 153 | 133 | /** |
| @@ -159,9 +139,9 @@ | ||
| 159 | 139 | * from List A", the origin of the activity is "List A". |
| 160 | 140 | * |
| 161 | 141 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-origin |
| 162 | 142 | * |
| 163 | - * @var string|array|null | |
| 143 | + * @var string|array | |
| 164 | 144 | */ |
| 165 | 145 | protected $origin; |
| 166 | 146 | |
| 167 | 147 | /** |
| @@ -169,9 +149,9 @@ | ||
| 169 | 149 | * Activity. |
| 170 | 150 | * |
| 171 | 151 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-instrument |
| 172 | 152 | * |
| 173 | - * @var string|array|null | |
| 153 | + * @var string|array | |
| 174 | 154 | */ |
| 175 | 155 | protected $instrument; |
| 176 | 156 | |
| 177 | 157 | /** |
| @@ -181,9 +161,9 @@ | ||
| 181 | 161 | * MUST be copied over to the new "Create" activity by the server. |
| 182 | 162 | * |
| 183 | 163 | * @see https://www.w3.org/TR/activitypub/#object-without-create |
| 184 | 164 | * |
| 185 | - * @param array|string|Base_Object|Activity|Actor|null $data Activity object. | |
| 165 | + * @param array|string|Base_Object|Activity|Actor|null $data Activity object. | |
| 186 | 166 | */ |
| 187 | 167 | public function set_object( $data ) { |
| 188 | 168 | $object = $data; |
| 189 | 169 | |
| @@ -188,12 +168,28 @@ | ||
| 188 | 168 | $object = $data; |
| 189 | 169 | |
| 190 | 170 | // Convert array to appropriate object type. |
| 191 | 171 | if ( is_array( $data ) ) { |
| 192 | - if ( array_is_list( $data ) ) { | |
| 193 | - $object = array_map( array( $this, 'maybe_convert_to_object' ), $data ); | |
| 172 | + $type = $data['type'] ?? null; | |
| 173 | + | |
| 174 | + if ( in_array( $type, self::TYPES, true ) ) { | |
| 175 | + $object = self::init_from_array( $data ); | |
| 176 | + } elseif ( in_array( $type, Actor::TYPES, true ) ) { | |
| 177 | + $object = Actor::init_from_array( $data ); | |
| 178 | + } elseif ( in_array( $type, Base_Object::TYPES, true ) ) { | |
| 179 | + switch ( $type ) { | |
| 180 | + case 'Event': | |
| 181 | + $object = Event::init_from_array( $data ); | |
| 182 | + break; | |
| 183 | + case 'Place': | |
| 184 | + $object = Place::init_from_array( $data ); | |
| 185 | + break; | |
| 186 | + default: | |
| 187 | + $object = Base_Object::init_from_array( $data ); | |
| 188 | + break; | |
| 189 | + } | |
| 194 | 190 | } else { |
| 195 | - $object = $this->maybe_convert_to_object( $data ); | |
| 191 | + $object = Generic_Object::init_from_array( $data ); | |
| 196 | 192 | } |
| 197 | 193 | } |
| 198 | 194 | |
| 199 | 195 | $this->set( 'object', $object ); |
| @@ -205,9 +201,9 @@ | ||
| 205 | 201 | */ |
| 206 | 202 | public function pre_fill_activity_from_object() { |
| 207 | 203 | $object = $this->get_object(); |
| 208 | 204 | |
| 209 | - // Check if `$object` is a URL and use it to generate an ID then. | |
| 205 | + // Check if `$data` is a URL and use it to generate an ID then. | |
| 210 | 206 | if ( is_string( $object ) && filter_var( $object, FILTER_VALIDATE_URL ) && ! $this->get_id() ) { |
| 211 | 207 | $this->set( 'id', $object . '#activity-' . strtolower( $this->get_type() ) . '-' . time() ); |
| 212 | 208 | |
| 213 | 209 | return; |
| @@ -212,9 +208,9 @@ | ||
| 212 | 208 | |
| 213 | 209 | return; |
| 214 | 210 | } |
| 215 | 211 | |
| 216 | - // Check if `$object` is an object and copy some properties otherwise do nothing. | |
| 212 | + // Check if `$data` is an object and copy some properties otherwise do nothing. | |
| 217 | 213 | if ( ! is_object( $object ) ) { |
| 218 | 214 | return; |
| 219 | 215 | } |
| 220 | 216 | |
| @@ -240,12 +236,8 @@ | ||
| 240 | 236 | if ( $this->get_type() !== 'Announce' && $object->get_in_reply_to() && ! $this->get_in_reply_to() ) { |
| 241 | 237 | $this->set( 'in_reply_to', $object->get_in_reply_to() ); |
| 242 | 238 | } |
| 243 | 239 | |
| 244 | - if ( $object->get_interaction_policy() && ! $this->get_interaction_policy() ) { | |
| 245 | - $this->set( 'interaction_policy', $object->get_interaction_policy() ); | |
| 246 | - } | |
| 247 | - | |
| 248 | 240 | if ( $object->get_id() && ! $this->get_id() ) { |
| 249 | 241 | $id = strtok( $object->get_id(), '#' ); |
| 250 | 242 | if ( $object->get_updated() ) { |
| 251 | 243 | $updated = $object->get_updated(); |
| @@ -263,9 +255,9 @@ | ||
| 263 | 255 | * |
| 264 | 256 | * @return array $context A compacted JSON-LD context. |
| 265 | 257 | */ |
| 266 | 258 | public function get_json_ld_context() { |
| 267 | - if ( \is_object( $this->object ) ) { | |
| 259 | + if ( $this->object instanceof Base_Object ) { | |
| 268 | 260 | $class = get_class( $this->object ); |
| 269 | 261 | if ( $class && $class::JSON_LD_CONTEXT ) { |
| 270 | 262 | // Without php 5.6 support this could be just: 'return $this->object::JSON_LD_CONTEXT;'. |
| 271 | 263 | return $class::JSON_LD_CONTEXT; |
| @@ -272,43 +264,6 @@ | ||
| 272 | 264 | } |
| 273 | 265 | } |
| 274 | 266 | |
| 275 | 267 | return static::JSON_LD_CONTEXT; |
| 276 | - } | |
| 277 | - | |
| 278 | - /** | |
| 279 | - * Convert data to the appropriate object type if it has an ActivityPub type. | |
| 280 | - * | |
| 281 | - * @param array|string|Base_Object|Activity|Actor|null $data The data to convert. | |
| 282 | - * | |
| 283 | - * @return Activity|Actor|Base_Object|Generic_Object|string|\WP_Error|null The converted object or original data. | |
| 284 | - */ | |
| 285 | - private function maybe_convert_to_object( $data ) { | |
| 286 | - if ( ! is_array( $data ) ) { | |
| 287 | - return $data; | |
| 288 | - } | |
| 289 | - | |
| 290 | - $type = $data['type'] ?? null; | |
| 291 | - | |
| 292 | - if ( in_array( $type, self::TYPES, true ) ) { | |
| 293 | - $object = self::init_from_array( $data ); | |
| 294 | - } elseif ( in_array( $type, Actor::TYPES, true ) ) { | |
| 295 | - $object = Actor::init_from_array( $data ); | |
| 296 | - } elseif ( in_array( $type, Base_Object::TYPES, true ) ) { | |
| 297 | - switch ( $type ) { | |
| 298 | - case 'Event': | |
| 299 | - $object = Event::init_from_array( $data ); | |
| 300 | - break; | |
| 301 | - case 'Place': | |
| 302 | - $object = Place::init_from_array( $data ); | |
| 303 | - break; | |
| 304 | - default: | |
| 305 | - $object = Base_Object::init_from_array( $data ); | |
| 306 | - break; | |
| 307 | - } | |
| 308 | - } else { | |
| 309 | - $object = Generic_Object::init_from_array( $data ); | |
| 310 | - } | |
| 311 | - | |
| 312 | - return $object; | |
| 313 | 268 | } |
| 314 | 269 | } |