| @@ -2,13 +2,16 @@ | ||
| 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 | |
| 6 | 8 | */ |
| 7 | 9 | |
| 8 | 10 | namespace Activitypub\Activity; |
| 9 | 11 | |
| 10 | -use Activitypub\Activity\Base_Object; | |
| 12 | +use Activitypub\Activity\Extended_Object\Event; | |
| 13 | +use Activitypub\Activity\Extended_Object\Place; | |
| 11 | 14 | |
| 12 | 15 | /** |
| 13 | 16 | * \Activitypub\Activity\Activity implements the common |
| 14 | 17 | * attributes of an Activity. |
| @@ -14,80 +17,79 @@ | ||
| 14 | 17 | * attributes of an Activity. |
| 15 | 18 | * |
| 16 | 19 | * @see https://www.w3.org/TR/activitystreams-core/#activities |
| 17 | 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. | |
| 18 | 36 | */ |
| 19 | 37 | class Activity extends Base_Object { |
| 20 | - const CONTEXT = array( | |
| 38 | + const JSON_LD_CONTEXT = array( | |
| 21 | 39 | 'https://www.w3.org/ns/activitystreams', |
| 22 | - 'https://w3id.org/security/v1', | |
| 23 | - 'https://purl.archive.org/socialweb/webfinger', | |
| 24 | 40 | array( |
| 25 | - 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers', | |
| 26 | - 'PropertyValue' => 'schema:PropertyValue', | |
| 27 | - 'schema' => 'http://schema.org#', | |
| 28 | - 'pt' => 'https://joinpeertube.org/ns#', | |
| 29 | - 'toot' => 'http://joinmastodon.org/ns#', | |
| 30 | - 'litepub' => 'http://litepub.social/ns#', | |
| 31 | - 'lemmy' => 'https://join-lemmy.org/ns#', | |
| 32 | - 'value' => 'schema:value', | |
| 33 | - 'Hashtag' => 'as:Hashtag', | |
| 34 | - 'featured' => array( | |
| 35 | - '@id' => 'toot:featured', | |
| 36 | - '@type' => '@id', | |
| 37 | - ), | |
| 38 | - 'featuredTags' => array( | |
| 39 | - '@id' => 'toot:featuredTags', | |
| 40 | - '@type' => '@id', | |
| 41 | - ), | |
| 42 | - 'alsoKnownAs' => array( | |
| 43 | - '@id' => 'as:alsoKnownAs', | |
| 44 | - '@type' => '@id', | |
| 45 | - ), | |
| 46 | - 'moderators' => array( | |
| 47 | - '@id' => 'lemmy:moderators', | |
| 48 | - '@type' => '@id', | |
| 49 | - ), | |
| 50 | - 'postingRestrictedToMods' => 'lemmy:postingRestrictedToMods', | |
| 51 | - 'discoverable' => 'toot:discoverable', | |
| 52 | - 'indexable' => 'toot:indexable', | |
| 53 | - 'sensitive' => 'as:sensitive', | |
| 41 | + 'toot' => 'http://joinmastodon.org/ns#', | |
| 42 | + 'QuoteRequest' => 'toot:QuoteRequest', | |
| 54 | 43 | ), |
| 55 | 44 | ); |
| 56 | 45 | |
| 57 | 46 | /** |
| 58 | - * The object's unique global identifier | |
| 47 | + * The default types for Activities. | |
| 59 | 48 | * |
| 60 | - * @see https://www.w3.org/TR/activitypub/#obj-id | |
| 49 | + * @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types | |
| 61 | 50 | * |
| 62 | - * @var string | |
| 51 | + * @var array | |
| 63 | 52 | */ |
| 64 | - protected $id; | |
| 53 | + const TYPES = array( | |
| 54 | + 'Accept', | |
| 55 | + 'Add', | |
| 56 | + 'Announce', | |
| 57 | + 'Arrive', | |
| 58 | + 'Block', | |
| 59 | + 'Create', | |
| 60 | + 'Delete', | |
| 61 | + 'Dislike', | |
| 62 | + 'Follow', | |
| 63 | + 'Flag', | |
| 64 | + 'Ignore', | |
| 65 | + 'Invite', | |
| 66 | + 'Join', | |
| 67 | + 'Leave', | |
| 68 | + 'Like', | |
| 69 | + 'Listen', | |
| 70 | + 'Move', | |
| 71 | + 'Offer', | |
| 72 | + 'QuoteRequest', // @see https://codeberg.org/fediverse/fep/src/branch/main/fep/044f/fep-044f.md | |
| 73 | + 'Read', | |
| 74 | + 'Reject', | |
| 75 | + 'Remove', | |
| 76 | + 'TentativeAccept', | |
| 77 | + 'TentativeReject', | |
| 78 | + 'Travel', | |
| 79 | + 'Undo', | |
| 80 | + 'Update', | |
| 81 | + 'View', | |
| 82 | + ); | |
| 65 | 83 | |
| 66 | 84 | /** |
| 85 | + * The type of the object. | |
| 86 | + * | |
| 67 | 87 | * @var string |
| 68 | 88 | */ |
| 69 | 89 | protected $type = 'Activity'; |
| 70 | 90 | |
| 71 | 91 | /** |
| 72 | - * The context within which the object exists or an activity was | |
| 73 | - * performed. | |
| 74 | - * The notion of "context" used is intentionally vague. | |
| 75 | - * The intended function is to serve as a means of grouping objects | |
| 76 | - * and activities that share a common originating context or | |
| 77 | - * purpose. An example could be all activities relating to a common | |
| 78 | - * project or event. | |
| 79 | - * | |
| 80 | - * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-context | |
| 81 | - * | |
| 82 | - * @var string | |
| 83 | - * | ObjectType | |
| 84 | - * | Link | |
| 85 | - * | null | |
| 86 | - */ | |
| 87 | - protected $context = self::CONTEXT; | |
| 88 | - | |
| 89 | - /** | |
| 90 | 92 | * Describes the direct object of the activity. |
| 91 | 93 | * For instance, in the activity "John added a movie to his |
| 92 | 94 | * wishlist", the object of the activity is the movie added. |
| 93 | 95 | * |
| @@ -92,12 +94,9 @@ | ||
| 92 | 94 | * wishlist", the object of the activity is the movie added. |
| 93 | 95 | * |
| 94 | 96 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-object-term |
| 95 | 97 | * |
| 96 | - * @var string | |
| 97 | - * | Base_Objectr | |
| 98 | - * | Link | |
| 99 | - * | null | |
| 98 | + * @var string|Base_Object|array|null | |
| 100 | 99 | */ |
| 101 | 100 | protected $object; |
| 102 | 101 | |
| 103 | 102 | /** |
| @@ -107,13 +106,9 @@ | ||
| 107 | 106 | * The actor MAY be specified using an indirect Link. |
| 108 | 107 | * |
| 109 | 108 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-actor |
| 110 | 109 | * |
| 111 | - * @var string | |
| 112 | - * | \ActivityPhp\Type\Extended\AbstractActor | |
| 113 | - * | array<Actor> | |
| 114 | - * | array<Link> | |
| 115 | - * | Link | |
| 110 | + * @var string|array | |
| 116 | 111 | */ |
| 117 | 112 | protected $actor; |
| 118 | 113 | |
| 119 | 114 | /** |
| @@ -126,13 +121,9 @@ | ||
| 126 | 121 | * An activity can have more than one target. |
| 127 | 122 | * |
| 128 | 123 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-target |
| 129 | 124 | * |
| 130 | - * @var string | |
| 131 | - * | ObjectType | |
| 132 | - * | array<ObjectType> | |
| 133 | - * | Link | |
| 134 | - * | array<Link> | |
| 125 | + * @var string|array|null | |
| 135 | 126 | */ |
| 136 | 127 | protected $target; |
| 137 | 128 | |
| 138 | 129 | /** |
| @@ -142,16 +133,25 @@ | ||
| 142 | 133 | * that new resource. |
| 143 | 134 | * |
| 144 | 135 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-result |
| 145 | 136 | * |
| 146 | - * @var string | |
| 147 | - * | ObjectType | |
| 148 | - * | Link | |
| 149 | - * | null | |
| 137 | + * @var string|Base_Object|null | |
| 150 | 138 | */ |
| 151 | 139 | protected $result; |
| 152 | 140 | |
| 153 | 141 | /** |
| 142 | + * Identifies a Collection containing objects considered to be responses | |
| 143 | + * to this object. | |
| 144 | + * WordPress has a strong core system of approving replies. We only include | |
| 145 | + * approved replies here. | |
| 146 | + * | |
| 147 | + * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-replies | |
| 148 | + * | |
| 149 | + * @var array|null | |
| 150 | + */ | |
| 151 | + protected $replies; | |
| 152 | + | |
| 153 | + /** | |
| 154 | 154 | * An indirect object of the activity from which the |
| 155 | 155 | * activity is directed. |
| 156 | 156 | * The precise meaning of the origin is the object of the English |
| 157 | 157 | * preposition "from". |
| @@ -159,12 +159,9 @@ | ||
| 159 | 159 | * from List A", the origin of the activity is "List A". |
| 160 | 160 | * |
| 161 | 161 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-origin |
| 162 | 162 | * |
| 163 | - * @var string | |
| 164 | - * | ObjectType | |
| 165 | - * | Link | |
| 166 | - * | null | |
| 163 | + * @var string|array|null | |
| 167 | 164 | */ |
| 168 | 165 | protected $origin; |
| 169 | 166 | |
| 170 | 167 | /** |
| @@ -172,12 +169,9 @@ | ||
| 172 | 169 | * Activity. |
| 173 | 170 | * |
| 174 | 171 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-instrument |
| 175 | 172 | * |
| 176 | - * @var string | |
| 177 | - * | ObjectType | |
| 178 | - * | Link | |
| 179 | - * | null | |
| 173 | + * @var string|array|null | |
| 180 | 174 | */ |
| 181 | 175 | protected $instrument; |
| 182 | 176 | |
| 183 | 177 | /** |
| @@ -183,31 +177,53 @@ | ||
| 183 | 177 | /** |
| 184 | 178 | * Set the object and copy Object properties to the Activity. |
| 185 | 179 | * |
| 186 | 180 | * Any to, bto, cc, bcc, and audience properties specified on the object |
| 187 | - * MUST be copied over to the new Create activity by the server. | |
| 181 | + * MUST be copied over to the new "Create" activity by the server. | |
| 188 | 182 | * |
| 189 | 183 | * @see https://www.w3.org/TR/activitypub/#object-without-create |
| 190 | 184 | * |
| 191 | - * @param string|Base_Objectr|Link|null $object | |
| 192 | - * | |
| 193 | - * @return void | |
| 185 | + * @param array|string|Base_Object|Activity|Actor|null $data Activity object. | |
| 194 | 186 | */ |
| 195 | - public function set_object( $object ) { | |
| 196 | - // convert array to object | |
| 197 | - if ( is_array( $object ) ) { | |
| 198 | - $object = self::init_from_array( $object ); | |
| 187 | + public function set_object( $data ) { | |
| 188 | + $object = $data; | |
| 189 | + | |
| 190 | + // Convert array to appropriate object type. | |
| 191 | + if ( is_array( $data ) ) { | |
| 192 | + if ( array_is_list( $data ) ) { | |
| 193 | + $object = array_map( array( $this, 'maybe_convert_to_object' ), $data ); | |
| 194 | + } else { | |
| 195 | + $object = $this->maybe_convert_to_object( $data ); | |
| 196 | + } | |
| 199 | 197 | } |
| 200 | 198 | |
| 201 | - // set object | |
| 202 | 199 | $this->set( 'object', $object ); |
| 200 | + $this->pre_fill_activity_from_object(); | |
| 201 | + } | |
| 203 | 202 | |
| 203 | + /** | |
| 204 | + * Fills the Activity with the specified activity object. | |
| 205 | + */ | |
| 206 | + public function pre_fill_activity_from_object() { | |
| 207 | + $object = $this->get_object(); | |
| 208 | + | |
| 209 | + // Check if `$object` is a URL and use it to generate an ID then. | |
| 210 | + if ( is_string( $object ) && filter_var( $object, FILTER_VALIDATE_URL ) && ! $this->get_id() ) { | |
| 211 | + $this->set( 'id', $object . '#activity-' . strtolower( $this->get_type() ) . '-' . time() ); | |
| 212 | + | |
| 213 | + return; | |
| 214 | + } | |
| 215 | + | |
| 216 | + // Check if `$object` is an object and copy some properties otherwise do nothing. | |
| 204 | 217 | if ( ! is_object( $object ) ) { |
| 205 | 218 | return; |
| 206 | 219 | } |
| 207 | 220 | |
| 208 | 221 | foreach ( array( 'to', 'bto', 'cc', 'bcc', 'audience' ) as $i ) { |
| 209 | - $this->set( $i, $object->get( $i ) ); | |
| 222 | + $value = $object->get( $i ); | |
| 223 | + if ( $value && ! $this->get( $i ) ) { | |
| 224 | + $this->set( $i, $value ); | |
| 225 | + } | |
| 210 | 226 | } |
| 211 | 227 | |
| 212 | 228 | if ( $object->get_published() && ! $this->get_published() ) { |
| 213 | 229 | $this->set( 'published', $object->get_published() ); |
| @@ -220,9 +236,79 @@ | ||
| 220 | 236 | if ( $object->get_attributed_to() && ! $this->get_actor() ) { |
| 221 | 237 | $this->set( 'actor', $object->get_attributed_to() ); |
| 222 | 238 | } |
| 223 | 239 | |
| 240 | + if ( $this->get_type() !== 'Announce' && $object->get_in_reply_to() && ! $this->get_in_reply_to() ) { | |
| 241 | + $this->set( 'in_reply_to', $object->get_in_reply_to() ); | |
| 242 | + } | |
| 243 | + | |
| 244 | + if ( $object->get_interaction_policy() && ! $this->get_interaction_policy() ) { | |
| 245 | + $this->set( 'interaction_policy', $object->get_interaction_policy() ); | |
| 246 | + } | |
| 247 | + | |
| 224 | 248 | if ( $object->get_id() && ! $this->get_id() ) { |
| 225 | - $this->set( 'id', $object->get_id() . '#activity' ); | |
| 249 | + $id = strtok( $object->get_id(), '#' ); | |
| 250 | + if ( $object->get_updated() ) { | |
| 251 | + $updated = $object->get_updated(); | |
| 252 | + } elseif ( $object->get_published() ) { | |
| 253 | + $updated = $object->get_published(); | |
| 254 | + } else { | |
| 255 | + $updated = time(); | |
| 256 | + } | |
| 257 | + $this->set( 'id', $id . '#activity-' . strtolower( $this->get_type() ) . '-' . $updated ); | |
| 226 | 258 | } |
| 259 | + } | |
| 260 | + | |
| 261 | + /** | |
| 262 | + * The context of an Activity is usually just the context of the object it contains. | |
| 263 | + * | |
| 264 | + * @return array $context A compacted JSON-LD context. | |
| 265 | + */ | |
| 266 | + public function get_json_ld_context() { | |
| 267 | + if ( \is_object( $this->object ) ) { | |
| 268 | + $class = get_class( $this->object ); | |
| 269 | + if ( $class && $class::JSON_LD_CONTEXT ) { | |
| 270 | + // Without php 5.6 support this could be just: 'return $this->object::JSON_LD_CONTEXT;'. | |
| 271 | + return $class::JSON_LD_CONTEXT; | |
| 272 | + } | |
| 273 | + } | |
| 274 | + | |
| 275 | + 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; | |
| 227 | 313 | } |
| 228 | 314 | } |