| @@ -2,14 +2,19 @@ | ||
| 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\Link; | |
| 11 | 13 | |
| 14 | +use function Activitypub\is_actor; | |
| 15 | +use function Activitypub\is_activity; | |
| 16 | + | |
| 12 | 17 | /** |
| 13 | 18 | * \Activitypub\Activity\Activity implements the common |
| 14 | 19 | * attributes of an Activity. |
| 15 | 20 | * |
| @@ -14,80 +19,55 @@ | ||
| 14 | 19 | * attributes of an Activity. |
| 15 | 20 | * |
| 16 | 21 | * @see https://www.w3.org/TR/activitystreams-core/#activities |
| 17 | 22 | * @see https://www.w3.org/TR/activitystreams-core/#intransitiveactivities |
| 23 | + * | |
| 24 | + * @method string|array|null get_actor() Gets one or more entities that performed or are expected to perform the activity. | |
| 25 | + * @method string|null get_id() Gets the object's unique global identifier. | |
| 26 | + * @method string get_type() Gets the type of the object. | |
| 27 | + * @method string|null get_name() Gets the natural language name of the object. | |
| 28 | + * @method string|null get_url() Gets the URL of the object. | |
| 29 | + * @method string|null get_summary() Gets the natural language summary of the object. | |
| 30 | + * @method string|null get_published() Gets the date and time the object was published in ISO 8601 format. | |
| 31 | + * @method string|null get_updated() Gets the date and time the object was updated in ISO 8601 format. | |
| 32 | + * @method string|null get_attributed_to() Gets the entity attributed as the original author. | |
| 33 | + * @method array|string|null get_cc() Gets the secondary recipients of the object. | |
| 34 | + * @method array|string|null get_to() Gets the primary recipients of the object. | |
| 35 | + * @method array|null get_attachment() Gets the attachment property of the object. | |
| 36 | + * @method array|null get_icon() Gets the icon property of the object. | |
| 37 | + * @method array|null get_image() Gets the image property of the object. | |
| 38 | + * @method Base_Object|string|null get_object() Gets the direct object of the activity. | |
| 39 | + * @method array|string|null get_in_reply_to() Gets the objects this object is in reply to. | |
| 40 | + * | |
| 41 | + * @method Activity set_actor( string|array $actor ) Sets one or more entities that performed the activity. | |
| 42 | + * @method Activity set_id( string $id ) Sets the object's unique global identifier. | |
| 43 | + * @method Activity set_type( string $type ) Sets the type of the object. | |
| 44 | + * @method Activity set_name( string $name ) Sets the natural language name of the object. | |
| 45 | + * @method Activity set_url( string $url ) Sets the URL of the object. | |
| 46 | + * @method Activity set_summary( string $summary ) Sets the natural language summary of the object. | |
| 47 | + * @method Activity set_published( string $published ) Sets the date and time the object was published in ISO 8601 format. | |
| 48 | + * @method Activity set_updated( string $updated ) Sets the date and time the object was updated in ISO 8601 format. | |
| 49 | + * @method Activity set_attributed_to( string $attributed_to ) Sets the entity attributed as the original author. | |
| 50 | + * @method Activity set_cc( array|string $cc ) Sets the secondary recipients of the object. | |
| 51 | + * @method Activity set_to( array|string $to ) Sets the primary recipients of the object. | |
| 52 | + * @method Activity set_attachment( array $attachment ) Sets the attachment property of the object. | |
| 53 | + * @method Activity set_icon( array $icon ) Sets the icon property of the object. | |
| 54 | + * @method Activity set_image( array $image ) Sets the image property of the object. | |
| 55 | + * @method Activity set_content( string $content ) Sets the content property of the object. | |
| 18 | 56 | */ |
| 19 | 57 | class Activity extends Base_Object { |
| 20 | - const CONTEXT = array( | |
| 58 | + const JSON_LD_CONTEXT = array( | |
| 21 | 59 | 'https://www.w3.org/ns/activitystreams', |
| 22 | - 'https://w3id.org/security/v1', | |
| 23 | - 'https://purl.archive.org/socialweb/webfinger', | |
| 24 | - 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', | |
| 54 | - ), | |
| 55 | 60 | ); |
| 56 | 61 | |
| 57 | 62 | /** |
| 58 | - * The object's unique global identifier | |
| 63 | + * The type of the object. | |
| 59 | 64 | * |
| 60 | - * @see https://www.w3.org/TR/activitypub/#obj-id | |
| 61 | - * | |
| 62 | 65 | * @var string |
| 63 | 66 | */ |
| 64 | - protected $id; | |
| 65 | - | |
| 66 | - /** | |
| 67 | - * @var string | |
| 68 | - */ | |
| 69 | 67 | protected $type = 'Activity'; |
| 70 | 68 | |
| 71 | 69 | /** |
| 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 | 70 | * Describes the direct object of the activity. |
| 91 | 71 | * For instance, in the activity "John added a movie to his |
| 92 | 72 | * wishlist", the object of the activity is the movie added. |
| 93 | 73 | * |
| @@ -92,12 +72,9 @@ | ||
| 92 | 72 | * wishlist", the object of the activity is the movie added. |
| 93 | 73 | * |
| 94 | 74 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-object-term |
| 95 | 75 | * |
| 96 | - * @var string | |
| 97 | - * | Base_Objectr | |
| 98 | - * | Link | |
| 99 | - * | null | |
| 76 | + * @var string|Base_Object|null | |
| 100 | 77 | */ |
| 101 | 78 | protected $object; |
| 102 | 79 | |
| 103 | 80 | /** |
| @@ -107,13 +84,9 @@ | ||
| 107 | 84 | * The actor MAY be specified using an indirect Link. |
| 108 | 85 | * |
| 109 | 86 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-actor |
| 110 | 87 | * |
| 111 | - * @var string | |
| 112 | - * | \ActivityPhp\Type\Extended\AbstractActor | |
| 113 | - * | array<Actor> | |
| 114 | - * | array<Link> | |
| 115 | - * | Link | |
| 88 | + * @var string|array | |
| 116 | 89 | */ |
| 117 | 90 | protected $actor; |
| 118 | 91 | |
| 119 | 92 | /** |
| @@ -126,13 +99,9 @@ | ||
| 126 | 99 | * An activity can have more than one target. |
| 127 | 100 | * |
| 128 | 101 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-target |
| 129 | 102 | * |
| 130 | - * @var string | |
| 131 | - * | ObjectType | |
| 132 | - * | array<ObjectType> | |
| 133 | - * | Link | |
| 134 | - * | array<Link> | |
| 103 | + * @var string|array | |
| 135 | 104 | */ |
| 136 | 105 | protected $target; |
| 137 | 106 | |
| 138 | 107 | /** |
| @@ -142,16 +111,25 @@ | ||
| 142 | 111 | * that new resource. |
| 143 | 112 | * |
| 144 | 113 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-result |
| 145 | 114 | * |
| 146 | - * @var string | |
| 147 | - * | ObjectType | |
| 148 | - * | Link | |
| 149 | - * | null | |
| 115 | + * @var string|Base_Object | |
| 150 | 116 | */ |
| 151 | 117 | protected $result; |
| 152 | 118 | |
| 153 | 119 | /** |
| 120 | + * Identifies a Collection containing objects considered to be responses | |
| 121 | + * to this object. | |
| 122 | + * WordPress has a strong core system of approving replies. We only include | |
| 123 | + * approved replies here. | |
| 124 | + * | |
| 125 | + * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-replies | |
| 126 | + * | |
| 127 | + * @var array | |
| 128 | + */ | |
| 129 | + protected $replies; | |
| 130 | + | |
| 131 | + /** | |
| 154 | 132 | * An indirect object of the activity from which the |
| 155 | 133 | * activity is directed. |
| 156 | 134 | * The precise meaning of the origin is the object of the English |
| 157 | 135 | * preposition "from". |
| @@ -159,12 +137,9 @@ | ||
| 159 | 137 | * from List A", the origin of the activity is "List A". |
| 160 | 138 | * |
| 161 | 139 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-origin |
| 162 | 140 | * |
| 163 | - * @var string | |
| 164 | - * | ObjectType | |
| 165 | - * | Link | |
| 166 | - * | null | |
| 141 | + * @var string|array | |
| 167 | 142 | */ |
| 168 | 143 | protected $origin; |
| 169 | 144 | |
| 170 | 145 | /** |
| @@ -172,12 +147,9 @@ | ||
| 172 | 147 | * Activity. |
| 173 | 148 | * |
| 174 | 149 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-instrument |
| 175 | 150 | * |
| 176 | - * @var string | |
| 177 | - * | ObjectType | |
| 178 | - * | Link | |
| 179 | - * | null | |
| 151 | + * @var string|array | |
| 180 | 152 | */ |
| 181 | 153 | protected $instrument; |
| 182 | 154 | |
| 183 | 155 | /** |
| @@ -187,42 +159,75 @@ | ||
| 187 | 159 | * MUST be copied over to the new Create activity by the server. |
| 188 | 160 | * |
| 189 | 161 | * @see https://www.w3.org/TR/activitypub/#object-without-create |
| 190 | 162 | * |
| 191 | - * @param string|Base_Objectr|Link|null $object | |
| 192 | - * | |
| 193 | - * @return void | |
| 163 | + * @param array|string|Base_Object|Link|null $data Activity object. | |
| 194 | 164 | */ |
| 195 | - public function set_object( $object ) { | |
| 196 | - // convert array to object | |
| 197 | - if ( is_array( $object ) ) { | |
| 198 | - $object = self::init_from_array( $object ); | |
| 165 | + public function set_object( $data ) { | |
| 166 | + // Convert array to object. | |
| 167 | + if ( is_array( $data ) ) { | |
| 168 | + $data = Generic_Object::init_from_array( $data ); | |
| 199 | 169 | } |
| 200 | 170 | |
| 201 | - // set object | |
| 202 | - $this->set( 'object', $object ); | |
| 171 | + // Set object. | |
| 172 | + $this->set( 'object', $data ); | |
| 203 | 173 | |
| 204 | - if ( ! is_object( $object ) ) { | |
| 174 | + // Check if `$data` is a URL and use it to generate an ID then. | |
| 175 | + if ( is_string( $data ) && filter_var( $data, FILTER_VALIDATE_URL ) && ! $this->get_id() ) { | |
| 176 | + $this->set( 'id', $data . '#activity-' . strtolower( $this->get_type() ) . '-' . time() ); | |
| 177 | + | |
| 205 | 178 | return; |
| 206 | 179 | } |
| 207 | 180 | |
| 181 | + // Check if `$data` is an object and copy some properties otherwise do nothing. | |
| 182 | + if ( ! is_object( $data ) ) { | |
| 183 | + return; | |
| 184 | + } | |
| 185 | + | |
| 208 | 186 | foreach ( array( 'to', 'bto', 'cc', 'bcc', 'audience' ) as $i ) { |
| 209 | - $this->set( $i, $object->get( $i ) ); | |
| 187 | + $this->set( $i, $data->get( $i ) ); | |
| 210 | 188 | } |
| 211 | 189 | |
| 212 | - if ( $object->get_published() && ! $this->get_published() ) { | |
| 213 | - $this->set( 'published', $object->get_published() ); | |
| 190 | + if ( $data->get_published() && ! $this->get_published() ) { | |
| 191 | + $this->set( 'published', $data->get_published() ); | |
| 214 | 192 | } |
| 215 | 193 | |
| 216 | - if ( $object->get_updated() && ! $this->get_updated() ) { | |
| 217 | - $this->set( 'updated', $object->get_updated() ); | |
| 194 | + if ( $data->get_updated() && ! $this->get_updated() ) { | |
| 195 | + $this->set( 'updated', $data->get_updated() ); | |
| 218 | 196 | } |
| 219 | 197 | |
| 220 | - if ( $object->get_attributed_to() && ! $this->get_actor() ) { | |
| 221 | - $this->set( 'actor', $object->get_attributed_to() ); | |
| 198 | + if ( $data->get_attributed_to() && ! $this->get_actor() ) { | |
| 199 | + $this->set( 'actor', $data->get_attributed_to() ); | |
| 222 | 200 | } |
| 223 | 201 | |
| 224 | - if ( $object->get_id() && ! $this->get_id() ) { | |
| 225 | - $this->set( 'id', $object->get_id() . '#activity' ); | |
| 202 | + if ( $data->get_in_reply_to() ) { | |
| 203 | + $this->set( 'in_reply_to', $data->get_in_reply_to() ); | |
| 226 | 204 | } |
| 205 | + | |
| 206 | + if ( $data->get_id() && ! $this->get_id() ) { | |
| 207 | + $id = strtok( $data->get_id(), '#' ); | |
| 208 | + if ( $data->get_updated() ) { | |
| 209 | + $updated = $data->get_updated(); | |
| 210 | + } else { | |
| 211 | + $updated = $data->get_published(); | |
| 212 | + } | |
| 213 | + $this->set( 'id', $id . '#activity-' . strtolower( $this->get_type() ) . '-' . $updated ); | |
| 214 | + } | |
| 215 | + } | |
| 216 | + | |
| 217 | + /** | |
| 218 | + * The context of an Activity is usually just the context of the object it contains. | |
| 219 | + * | |
| 220 | + * @return array $context A compacted JSON-LD context. | |
| 221 | + */ | |
| 222 | + public function get_json_ld_context() { | |
| 223 | + if ( $this->object instanceof Base_Object ) { | |
| 224 | + $class = get_class( $this->object ); | |
| 225 | + if ( $class && $class::JSON_LD_CONTEXT ) { | |
| 226 | + // Without php 5.6 support this could be just: 'return $this->object::JSON_LD_CONTEXT;'. | |
| 227 | + return $class::JSON_LD_CONTEXT; | |
| 228 | + } | |
| 229 | + } | |
| 230 | + | |
| 231 | + return static::JSON_LD_CONTEXT; | |
| 227 | 232 | } |
| 228 | 233 | } |