| @@ -2,16 +2,13 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * Inspired by the PHP ActivityPub Library by @Landrok |
| 4 | 4 | * |
| 5 | 5 | * @link https://github.com/landrok/activitypub |
| 6 | - * | |
| 7 | - * @package Activitypub | |
| 8 | 6 | */ |
| 9 | 7 | |
| 10 | 8 | namespace Activitypub\Activity; |
| 11 | 9 | |
| 12 | -use Activitypub\Activity\Extended_Object\Event; | |
| 13 | -use Activitypub\Activity\Extended_Object\Place; | |
| 10 | +use Activitypub\Activity\Base_Object; | |
| 14 | 11 | |
| 15 | 12 | /** |
| 16 | 13 | * \Activitypub\Activity\Activity implements the common |
| 17 | 14 | * attributes of an Activity. |
| @@ -17,77 +14,15 @@ | ||
| 17 | 14 | * attributes of an Activity. |
| 18 | 15 | * |
| 19 | 16 | * @see https://www.w3.org/TR/activitystreams-core/#activities |
| 20 | 17 | * @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 | 18 | */ |
| 37 | 19 | class Activity extends Base_Object { |
| 38 | 20 | const JSON_LD_CONTEXT = array( |
| 39 | 21 | '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 | 22 | ); |
| 47 | 23 | |
| 48 | 24 | /** |
| 49 | - * The default types for Activities. | |
| 50 | - * | |
| 51 | - * @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types | |
| 52 | - * | |
| 53 | - * @var array | |
| 54 | - */ | |
| 55 | - const TYPES = array( | |
| 56 | - 'Accept', | |
| 57 | - 'Add', | |
| 58 | - 'Announce', | |
| 59 | - 'Arrive', | |
| 60 | - 'Block', | |
| 61 | - 'Create', | |
| 62 | - 'Delete', | |
| 63 | - 'Dislike', | |
| 64 | - 'Follow', | |
| 65 | - 'Flag', | |
| 66 | - 'Ignore', | |
| 67 | - 'Invite', | |
| 68 | - 'Join', | |
| 69 | - 'Leave', | |
| 70 | - 'Like', | |
| 71 | - 'Listen', | |
| 72 | - 'Move', | |
| 73 | - '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 | - 'Read', | |
| 77 | - 'Reject', | |
| 78 | - 'Remove', | |
| 79 | - 'TentativeAccept', | |
| 80 | - 'TentativeReject', | |
| 81 | - 'Travel', | |
| 82 | - 'Undo', | |
| 83 | - 'Update', | |
| 84 | - 'View', | |
| 85 | - ); | |
| 86 | - | |
| 87 | - /** | |
| 88 | - * The type of the object. | |
| 89 | - * | |
| 90 | 25 | * @var string |
| 91 | 26 | */ |
| 92 | 27 | protected $type = 'Activity'; |
| 93 | 28 | |
| @@ -97,9 +32,12 @@ | ||
| 97 | 32 | * wishlist", the object of the activity is the movie added. |
| 98 | 33 | * |
| 99 | 34 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-object-term |
| 100 | 35 | * |
| 101 | - * @var string|Base_Object|array|null | |
| 36 | + * @var string | |
| 37 | + * | Base_Object | |
| 38 | + * | Link | |
| 39 | + * | null | |
| 102 | 40 | */ |
| 103 | 41 | protected $object; |
| 104 | 42 | |
| 105 | 43 | /** |
| @@ -109,9 +47,13 @@ | ||
| 109 | 47 | * The actor MAY be specified using an indirect Link. |
| 110 | 48 | * |
| 111 | 49 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-actor |
| 112 | 50 | * |
| 113 | - * @var string|array | |
| 51 | + * @var string | |
| 52 | + * | \ActivityPhp\Type\Extended\AbstractActor | |
| 53 | + * | array<Actor> | |
| 54 | + * | array<Link> | |
| 55 | + * | Link | |
| 114 | 56 | */ |
| 115 | 57 | protected $actor; |
| 116 | 58 | |
| 117 | 59 | /** |
| @@ -124,9 +66,13 @@ | ||
| 124 | 66 | * An activity can have more than one target. |
| 125 | 67 | * |
| 126 | 68 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-target |
| 127 | 69 | * |
| 128 | - * @var string|array|null | |
| 70 | + * @var string | |
| 71 | + * | ObjectType | |
| 72 | + * | array<ObjectType> | |
| 73 | + * | Link | |
| 74 | + * | array<Link> | |
| 129 | 75 | */ |
| 130 | 76 | protected $target; |
| 131 | 77 | |
| 132 | 78 | /** |
| @@ -136,25 +82,16 @@ | ||
| 136 | 82 | * that new resource. |
| 137 | 83 | * |
| 138 | 84 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-result |
| 139 | 85 | * |
| 140 | - * @var string|Base_Object|null | |
| 86 | + * @var string | |
| 87 | + * | ObjectType | |
| 88 | + * | Link | |
| 89 | + * | null | |
| 141 | 90 | */ |
| 142 | 91 | protected $result; |
| 143 | 92 | |
| 144 | 93 | /** |
| 145 | - * Identifies a Collection containing objects considered to be responses | |
| 146 | - * to this object. | |
| 147 | - * WordPress has a strong core system of approving replies. We only include | |
| 148 | - * approved replies here. | |
| 149 | - * | |
| 150 | - * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-replies | |
| 151 | - * | |
| 152 | - * @var array|null | |
| 153 | - */ | |
| 154 | - protected $replies; | |
| 155 | - | |
| 156 | - /** | |
| 157 | 94 | * An indirect object of the activity from which the |
| 158 | 95 | * activity is directed. |
| 159 | 96 | * The precise meaning of the origin is the object of the English |
| 160 | 97 | * preposition "from". |
| @@ -162,9 +99,12 @@ | ||
| 162 | 99 | * from List A", the origin of the activity is "List A". |
| 163 | 100 | * |
| 164 | 101 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-origin |
| 165 | 102 | * |
| 166 | - * @var string|array|null | |
| 103 | + * @var string | |
| 104 | + * | ObjectType | |
| 105 | + * | Link | |
| 106 | + * | null | |
| 167 | 107 | */ |
| 168 | 108 | protected $origin; |
| 169 | 109 | |
| 170 | 110 | /** |
| @@ -172,9 +112,12 @@ | ||
| 172 | 112 | * Activity. |
| 173 | 113 | * |
| 174 | 114 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-instrument |
| 175 | 115 | * |
| 176 | - * @var string|array|null | |
| 116 | + * @var string | |
| 117 | + * | ObjectType | |
| 118 | + * | Link | |
| 119 | + * | null | |
| 177 | 120 | */ |
| 178 | 121 | protected $instrument; |
| 179 | 122 | |
| 180 | 123 | /** |
| @@ -180,53 +123,31 @@ | ||
| 180 | 123 | /** |
| 181 | 124 | * Set the object and copy Object properties to the Activity. |
| 182 | 125 | * |
| 183 | 126 | * Any to, bto, cc, bcc, and audience properties specified on the object |
| 184 | - * MUST be copied over to the new "Create" activity by the server. | |
| 127 | + * MUST be copied over to the new Create activity by the server. | |
| 185 | 128 | * |
| 186 | 129 | * @see https://www.w3.org/TR/activitypub/#object-without-create |
| 187 | 130 | * |
| 188 | - * @param array|string|Base_Object|Activity|Actor|null $data Activity object. | |
| 131 | + * @param string|Base_Objectr|Link|null $object | |
| 132 | + * | |
| 133 | + * @return void | |
| 189 | 134 | */ |
| 190 | - public function set_object( $data ) { | |
| 191 | - $object = $data; | |
| 192 | - | |
| 193 | - // 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 ); | |
| 197 | - } else { | |
| 198 | - $object = $this->maybe_convert_to_object( $data ); | |
| 199 | - } | |
| 135 | + public function set_object( $object ) { | |
| 136 | + // convert array to object | |
| 137 | + if ( is_array( $object ) ) { | |
| 138 | + $object = self::init_from_array( $object ); | |
| 200 | 139 | } |
| 201 | 140 | |
| 141 | + // set object | |
| 202 | 142 | $this->set( 'object', $object ); |
| 203 | - $this->pre_fill_activity_from_object(); | |
| 204 | - } | |
| 205 | 143 | |
| 206 | - /** | |
| 207 | - * Fills the Activity with the specified activity object. | |
| 208 | - */ | |
| 209 | - public function pre_fill_activity_from_object() { | |
| 210 | - $object = $this->get_object(); | |
| 211 | - | |
| 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() ); | |
| 215 | - | |
| 144 | + if ( ! is_object( $object ) ) { | |
| 216 | 145 | return; |
| 217 | 146 | } |
| 218 | 147 | |
| 219 | - // Check if `$object` is an object and copy some properties otherwise do nothing. | |
| 220 | - if ( ! \is_object( $object ) ) { | |
| 221 | - return; | |
| 222 | - } | |
| 223 | - | |
| 224 | 148 | foreach ( array( 'to', 'bto', 'cc', 'bcc', 'audience' ) as $i ) { |
| 225 | - $value = $object->get( $i ); | |
| 226 | - if ( $value && ! $this->get( $i ) ) { | |
| 227 | - $this->set( $i, $value ); | |
| 228 | - } | |
| 149 | + $this->set( $i, $object->get( $i ) ); | |
| 229 | 150 | } |
| 230 | 151 | |
| 231 | 152 | if ( $object->get_published() && ! $this->get_published() ) { |
| 232 | 153 | $this->set( 'published', $object->get_published() ); |
| @@ -239,26 +160,20 @@ | ||
| 239 | 160 | if ( $object->get_attributed_to() && ! $this->get_actor() ) { |
| 240 | 161 | $this->set( 'actor', $object->get_attributed_to() ); |
| 241 | 162 | } |
| 242 | 163 | |
| 243 | - if ( $this->get_type() !== 'Announce' && $object->get_in_reply_to() && ! $this->get_in_reply_to() ) { | |
| 164 | + if ( $object->get_in_reply_to() ) { | |
| 244 | 165 | $this->set( 'in_reply_to', $object->get_in_reply_to() ); |
| 245 | 166 | } |
| 246 | 167 | |
| 247 | - if ( $object->get_interaction_policy() && ! $this->get_interaction_policy() ) { | |
| 248 | - $this->set( 'interaction_policy', $object->get_interaction_policy() ); | |
| 249 | - } | |
| 250 | - | |
| 251 | 168 | if ( $object->get_id() && ! $this->get_id() ) { |
| 252 | - $id = \strtok( $object->get_id(), '#' ); | |
| 169 | + $id = strtok( $object->get_id(), '#' ); | |
| 253 | 170 | if ( $object->get_updated() ) { |
| 254 | 171 | $updated = $object->get_updated(); |
| 255 | - } elseif ( $object->get_published() ) { | |
| 172 | + } else { | |
| 256 | 173 | $updated = $object->get_published(); |
| 257 | - } else { | |
| 258 | - $updated = \time(); | |
| 259 | 174 | } |
| 260 | - $this->set( 'id', $id . '#activity-' . \strtolower( $this->get_type() ) . '-' . $updated ); | |
| 175 | + $this->set( 'id', $id . '#activity-' . strtolower( $this->get_type() ) . '-' . $updated ); | |
| 261 | 176 | } |
| 262 | 177 | } |
| 263 | 178 | |
| 264 | 179 | /** |
| @@ -266,52 +181,15 @@ | ||
| 266 | 181 | * |
| 267 | 182 | * @return array $context A compacted JSON-LD context. |
| 268 | 183 | */ |
| 269 | 184 | public function get_json_ld_context() { |
| 270 | - if ( \is_object( $this->object ) ) { | |
| 271 | - $class = \get_class( $this->object ); | |
| 185 | + if ( $this->object instanceof Base_Object ) { | |
| 186 | + $class = get_class( $this->object ); | |
| 272 | 187 | if ( $class && $class::JSON_LD_CONTEXT ) { |
| 273 | - // Without php 5.6 support this could be just: 'return $this->object::JSON_LD_CONTEXT;'. | |
| 188 | + // Without php 5.6 support this could be just: 'return $this->object::JSON_LD_CONTEXT;' | |
| 274 | 189 | return $class::JSON_LD_CONTEXT; |
| 275 | 190 | } |
| 276 | 191 | } |
| 277 | 192 | |
| 278 | 193 | 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 | 194 | } |
| 317 | 195 | } |