From cb86454d943787fd73f7eef3ea886eaa793daa3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Sat, 2 Dec 2023 16:03:12 +0100 Subject: [PATCH] wip: add event boilerplate with third party context --- activitypub-event-transformers.php | 6 +- activitypub/object/class-event.php | 129 +++++++++++++++++++- activitypub/object/class-place.php | 79 ++++++++++++ activitypub/object/class-postal-address.php | 57 +++++++++ activitypub/transformer/class-vs-event.php | 89 +++++++++----- 5 files changed, 323 insertions(+), 37 deletions(-) create mode 100644 activitypub/object/class-place.php create mode 100644 activitypub/object/class-postal-address.php diff --git a/activitypub-event-transformers.php b/activitypub-event-transformers.php index 4fca954..9fcb533 100644 --- a/activitypub-event-transformers.php +++ b/activitypub-event-transformers.php @@ -23,12 +23,12 @@ if ( ! defined( 'ABSPATH' ) ) { * Include fransformer file and register transformer class. * * @since 1.0.0 - * @param \Activitypub\Transformer\Transformers_Manager $transformers_manager ActivtiyPub transformers manager. + * @param \Activitypub\Transformer\Transformer_Factory $transformers_manager ActivtiyPub transformers manager. * @return void */ function register_event_transformers( $transformers_manager ) { - require_once __DIR__ . '/activitypub/transformer/class-tribe.php'; - $transformers_manager->register( new \Tribe() ); + // require_once __DIR__ . '/activitypub/transformer/class-tribe.php'; + // $transformers_manager->register( new \Tribe() ); require_once __DIR__ . '/activitypub/transformer/class-vs-event.php'; $transformers_manager->register( new \VS_Event() ); diff --git a/activitypub/object/class-event.php b/activitypub/object/class-event.php index 49e175f..f869a5e 100644 --- a/activitypub/object/class-event.php +++ b/activitypub/object/class-event.php @@ -1,7 +1,8 @@ hasProperty( $property ) ) { + $reflection_property = $reflection_class->getProperty( $property ); + $doc_omment = $reflection_property->getDocComment(); + + // Extract context information from the doc comment. + preg_match( '/@context\s+([^\s]+)/', $doc_omment, $matches ); + + if ( !empty( $matches[1] ) ) { + return $matches[1]; + } else { + return 'https://www.w3.org/ns/activitystreams'; + } + } + + return null; + } + + public function filter_context( $context ) { + if ( isset( $this->replies_moderation_option ) ) { + $replies_moderation_option_context = $this->get_property_context( 'replies_moderation_option' ); + } + return $context; + } + + + /** + * When using this class we need to add some filters. + */ + public function __construct() { + $class = get_class( $this ); + $class = 'event'; + add_filter( "activitypub_activity_{$class}_object_array", [ $this, 'rename_array_keys' ] ); + add_filter( 'activitypub_json_context', [ $this, 'filter_context' ] ); + } } diff --git a/activitypub/object/class-place.php b/activitypub/object/class-place.php new file mode 100644 index 0000000..1aa5412 --- /dev/null +++ b/activitypub/object/class-place.php @@ -0,0 +1,79 @@ + + * Mobilizon also implemented this as a fallback to their own + * repliesModerationOption. + * + * @see https://docs.joinpeertube.org/api/activitypub#video + * @see https://docs.joinmobilizon.org/contribute/activity_pub/ + * + * @var bool + */ + protected $comments_enabled; + + /** + * @var Postal_Address|string + */ + protected $address; +} diff --git a/activitypub/object/class-postal-address.php b/activitypub/object/class-postal-address.php new file mode 100644 index 0000000..0c974fe --- /dev/null +++ b/activitypub/object/class-postal-address.php @@ -0,0 +1,57 @@ +set_type( 'Place' ); $object->set_name( get_post_meta( $post_id, 'event-location', true ) ); - $array = $object->to_array(); - return $array; + return $object; } /** @@ -96,19 +95,50 @@ class VS_Event extends \Activitypub\Transformer\Base { } /** - * Transforms the VS Event WP_Post object to an ActivityPub Event Object + * Get the end time from the events metadata. + */ + private function get_end_time() { + $end_time = get_post_meta( $this->wp_post->ID, 'event-date', true ); + return \gmdate( 'Y-m-d\TH:i:s\Z', $end_time ); + } + + /** + * Get the event link from the events metadata. + */ + private function get_event_link() { + $event_link = get_post_meta( $this->wp_post->ID, 'event-link', true ); + if ( $event_link ) { + return array( + 'type' => 'Link', + 'name' => 'Website', + 'href' => \esc_url( get_post_meta( $post_id, 'event-location', true ) ), + 'mediaType' => 'text/html', + ); + } + } + + /** + * Extends the get_attachments function to also add the event Link. + */ + protected function get_attachments() { + $attachments = parent::get_attachments(); + $attachments[] = $this->get_event_link(); + return $attachments; + } + + /** + * Transforms the VS Event WP_Post object to an ActivityPub Event Object. * * @see \Activitypub\Activity\Base_Object * - * @return \Activitypub\Activity\Base_Object The ActivityPub Object + * @return \Activitypub\Activity\Base_Object The ActivityPub Object. */ - public function to_object() { - $wp_post = $this->wp_post; + public function transform() { $object = new Event(); - - $object->set_id( $this->get_id() ); - $object->set_url( $this->get_url() ); - $object->set_type( $this->get_object_type() ); + $object + ->set_id( $this->get_id() ) + ->set_url( $this->get_url() ) + ->set_type( $this->get_object_type() ); $published = \strtotime( $wp_post->post_date_gmt ); @@ -120,9 +150,10 @@ class VS_Event extends \Activitypub\Transformer\Base { $object->set_updated( \gmdate( 'Y-m-d\TH:i:s\Z', $updated ) ); } - $object->set_attributed_to( $this->get_attributed_to() ); - $object->set_content( $this->get_content() ); - $object->set_content_map( $this->get_content_map ); + $object + ->set_attributed_to( $this->get_attributed_to() ) + ->set_content( $this->get_content() ) + ->set_content_map( $this->get_content_map ); $summary = get_post_meta( $wp_post->ID, 'event-summary', true ); if ( $summary ) { @@ -134,29 +165,23 @@ class VS_Event extends \Activitypub\Transformer\Base { $start_time = get_post_meta( $wp_post->ID, 'event-start-date', true ); $object->set_start_time( \gmdate( 'Y-m-d\TH:i:s\Z', $start_time ) ); - $end_time = get_post_meta( $wp_post->ID, 'event-date', true ); - $object->set_end_time( \gmdate( 'Y-m-d\TH:i:s\Z', $end_time ) ); + $object->set_end_time( $this->get_end_time() ); $path = sprintf( 'users/%d/followers', intval( $wp_post->post_author ) ); - $location = get_post_meta( $wp_post->ID, 'event-link', true ); - $object->set_location( $this->get_event_location( $wp_post->ID ) ); - - $is_open_for_comments = comments_open( $wp_post->ID ); - $object->set_comments_enabled( $is_open_for_comments ); - - $object->set_to( - array( - 'https://www.w3.org/ns/activitystreams#Public', - get_rest_url_by_path( $path ), + $object + ->set_location( $this->get_event_location( $wp_post->ID ) ) + ->set_comments_enabled( comments_open( $wp_post->ID ) ) + ->set_to( + array( + 'https://www.w3.org/ns/activitystreams#Public', + get_rest_url_by_path( $path ), + ) ) - ); - $object->set_cc( $this->get_cc() ); - - $attachments = $this->get_attachments(); - - $object->set_attachment( $this->get_attachments() ); - $object->set_tag( $this->get_tags() ); + ->set_cc( $this->get_cc() ) + ->set_attachment( $this->get_attachments() ) + ->set_tag( $this->get_tags() ) + ->set_replies_moderation_option( 'allow_all' ); return $object; }