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