added: add context getter
Some checks failed
Deploy to https://wordpress-test.event-federation.eu/ / deploy (push) Failing after 5s
Some checks failed
Deploy to https://wordpress-test.event-federation.eu/ / deploy (push) Failing after 5s
this should be moved to the base class.
This commit is contained in:
parent
cb86454d94
commit
1b2c63ef20
2 changed files with 111 additions and 18 deletions
|
@ -7,6 +7,9 @@
|
||||||
* @package activity-event-transformers
|
* @package activity-event-transformers
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use function Activitypub\snake_to_camel_case;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event is an implementation of one of the
|
* Event is an implementation of one of the
|
||||||
* Activity Streams Event object type
|
* Activity Streams Event object type
|
||||||
|
@ -17,6 +20,9 @@
|
||||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-event
|
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-event
|
||||||
*/
|
*/
|
||||||
class Event extends \Activitypub\Activity\Base_Object {
|
class Event extends \Activitypub\Activity\Base_Object {
|
||||||
|
const REPLIES_MODERATION_OPTION_TYPES = [ 'allow_all', 'closed'];
|
||||||
|
const JOIN_MODE_TYPES = [ 'free', 'restricted', 'external'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event is an implementation of one of the
|
* Event is an implementation of one of the
|
||||||
* Activity Streams
|
* Activity Streams
|
||||||
|
@ -30,74 +36,94 @@ class Event extends \Activitypub\Activity\Base_Object {
|
||||||
* Mobilizon also implemented this as a fallback to their own
|
* Mobilizon also implemented this as a fallback to their own
|
||||||
* repliesModerationOption.
|
* repliesModerationOption.
|
||||||
*
|
*
|
||||||
|
* @context https://joinpeertube.org/commentsEnabled
|
||||||
* @see https://docs.joinpeertube.org/api/activitypub#video
|
* @see https://docs.joinpeertube.org/api/activitypub#video
|
||||||
* @see https://docs.joinmobilizon.org/contribute/activity_pub/
|
* @see https://docs.joinmobilizon.org/contribute/activity_pub/
|
||||||
*
|
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
protected $comments_enabled;
|
protected $comments_enabled;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @context https://joinmobilizon.org/timezone
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $timezone;
|
protected $timezone;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @context https://joinmobilizon/repliesModerationOption
|
* @context https://joinmobilizon.org/repliesModerationOption
|
||||||
* @var enum
|
* @see https://docs.joinmobilizon.org/contribute/activity_pub/#repliesmoderation
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $replies_moderation_option;
|
protected $replies_moderation_option;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @context https://joinmobilizon/anonymousParticipationEnabled
|
||||||
|
* @see https://docs.joinmobilizon.org/contribute/activity_pub/#anonymousparticipationenabled
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
protected $anonymous_participation_enabled;
|
protected $anonymous_participation_enabled;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @context https://schema.org/category
|
||||||
* @var enum
|
* @var enum
|
||||||
*/
|
*/
|
||||||
protected $category;
|
protected $category;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @context https://schema.org/inLanguage
|
||||||
* @var
|
* @var
|
||||||
*/
|
*/
|
||||||
protected $in_language;
|
protected $in_language;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @context https://joinmobilizon.org/isOnline
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
protected $is_online;
|
protected $is_online;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @context http://www.w3.org/2002/12/cal/ical#
|
||||||
* @var enum
|
* @var enum
|
||||||
*/
|
*/
|
||||||
protected $ical_status;
|
protected $ical_status;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @context https://joinmobilizon.org/externalParticipationUrl
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $external_participation_url;
|
protected $external_participation_url;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var enum
|
* @context https://joinmobilizon.org/joinMode
|
||||||
|
* @see https://docs.joinmobilizon.org/contribute/activity_pub/#joinmode
|
||||||
|
* @var
|
||||||
*/
|
*/
|
||||||
protected $join_mode;
|
protected $join_mode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool
|
* @context https://joinmobilizon.org/participantCount
|
||||||
*/
|
|
||||||
protected $draft;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var int
|
* @var int
|
||||||
*/
|
*/
|
||||||
protected $participant_count;
|
protected $participant_count;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @context https://schema.org/maximumAttendeeCapacity
|
||||||
|
* @see https://docs.joinmobilizon.org/contribute/activity_pub/#maximumattendeecapacity
|
||||||
* @var int
|
* @var int
|
||||||
*/
|
*/
|
||||||
protected $maximum_attendee_capacity;
|
protected $maximum_attendee_capacity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setter function that makes sure, we only set valid replies moderation options.
|
||||||
|
* @param string $value
|
||||||
|
*/
|
||||||
|
public function set_replies_moderation_option( $value ) {
|
||||||
|
if ( in_array( $value, self::REPLIES_MODERATION_OPTION_TYPES, true ) ) {
|
||||||
|
$this->replies_moderation_option = $value;
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $array The array version of an object of this class.
|
* @param array $array The array version of an object of this class.
|
||||||
*/
|
*/
|
||||||
|
@ -150,6 +176,66 @@ class Event extends \Activitypub\Activity\Base_Object {
|
||||||
return $context;
|
return $context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static function compact_context( $key_context, $namespace, $abbreviation ) {
|
||||||
|
$abbreviation_added = false;
|
||||||
|
foreach ( $key_context as $key => $value ) {
|
||||||
|
// Check if the key starts with "https://joinpeertube.org/"
|
||||||
|
if ( strpos( $value, $namespace ) === 0 ) {
|
||||||
|
// Replace the key
|
||||||
|
$key_context[ $key ] = $abbreviation . ':' . substr($value, strlen( $namespace ));
|
||||||
|
|
||||||
|
// Add "pt" element only once
|
||||||
|
if ( ! $abbreviation_added ) {
|
||||||
|
$key_context = [ $abbreviation => $namespace . '/ns#' ] + $key_context;
|
||||||
|
$abbreviation_added = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $key_context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function get_context() {
|
||||||
|
$class = self::class;
|
||||||
|
$transient = "activitypub_context_object_{$class}";
|
||||||
|
$context = get_transient($transient);
|
||||||
|
if ( $context ) {
|
||||||
|
return $context;
|
||||||
|
}
|
||||||
|
$reflection_class = new ReflectionClass( self::class );
|
||||||
|
$context = array(
|
||||||
|
'https://www.w3.org/ns/activitystreams',
|
||||||
|
'https://w3id.org/security/v1',
|
||||||
|
);
|
||||||
|
|
||||||
|
$key_context = [];
|
||||||
|
|
||||||
|
foreach ( $reflection_class->getProperties() as $property ) {
|
||||||
|
$doc_omment = $property->getDocComment();
|
||||||
|
|
||||||
|
// Extract context information from the doc comment.
|
||||||
|
preg_match( '/@context\s+([^\s]+)/', $doc_omment, $matches );
|
||||||
|
|
||||||
|
if ( !empty( $matches[1] ) ) {
|
||||||
|
$key_context[ snake_to_camel_case( $property->name ) ] = $matches[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$namespace_abbreviations = array(
|
||||||
|
'https://joinpeertube.org/' => 'pt',
|
||||||
|
'https://joinmobilizon.org/' => 'mz',
|
||||||
|
'https://schema.org/' => 'sc'
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ( $namespace_abbreviations as $namespace => $abbreviation ) {
|
||||||
|
$key_context = self::compact_context( $key_context, $namespace, $abbreviation );
|
||||||
|
}
|
||||||
|
|
||||||
|
$context[] = $key_context;
|
||||||
|
|
||||||
|
set_transient( $transient, $context );
|
||||||
|
return $context;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When using this class we need to add some filters.
|
* When using this class we need to add some filters.
|
||||||
|
|
|
@ -134,17 +134,18 @@ class VS_Event extends \Activitypub\Transformer\Base {
|
||||||
* @return \Activitypub\Activity\Base_Object The ActivityPub Object.
|
* @return \Activitypub\Activity\Base_Object The ActivityPub Object.
|
||||||
*/
|
*/
|
||||||
public function transform() {
|
public function transform() {
|
||||||
|
$context = Event::get_context();
|
||||||
$object = new Event();
|
$object = new Event();
|
||||||
$object
|
$object
|
||||||
->set_id( $this->get_id() )
|
->set_id( $this->get_id() )
|
||||||
->set_url( $this->get_url() )
|
->set_url( $this->get_url() )
|
||||||
->set_type( $this->get_object_type() );
|
->set_type( $this->get_object_type() );
|
||||||
|
|
||||||
$published = \strtotime( $wp_post->post_date_gmt );
|
$published = \strtotime( $this->wp_post->post_date_gmt );
|
||||||
|
|
||||||
$object->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', $published ) );
|
$object->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', $published ) );
|
||||||
|
|
||||||
$updated = \strtotime( $wp_post->post_modified_gmt );
|
$updated = \strtotime( $this->wp_post->post_modified_gmt );
|
||||||
|
|
||||||
if ( $updated > $published ) {
|
if ( $updated > $published ) {
|
||||||
$object->set_updated( \gmdate( 'Y-m-d\TH:i:s\Z', $updated ) );
|
$object->set_updated( \gmdate( 'Y-m-d\TH:i:s\Z', $updated ) );
|
||||||
|
@ -155,23 +156,27 @@ class VS_Event extends \Activitypub\Transformer\Base {
|
||||||
->set_content( $this->get_content() )
|
->set_content( $this->get_content() )
|
||||||
->set_content_map( $this->get_content_map );
|
->set_content_map( $this->get_content_map );
|
||||||
|
|
||||||
$summary = get_post_meta( $wp_post->ID, 'event-summary', true );
|
$summary = get_post_meta( $this->wp_post->ID, 'event-summary', true );
|
||||||
if ( $summary ) {
|
if ( $summary ) {
|
||||||
$object->set_summary( $summary );
|
$object->set_summary( $summary );
|
||||||
} else {
|
} else {
|
||||||
$object->set_summary( $this->content );
|
$object->set_summary( $this->content );
|
||||||
}
|
}
|
||||||
|
|
||||||
$start_time = get_post_meta( $wp_post->ID, 'event-start-date', true );
|
$start_time = get_post_meta( $this->wp_post->ID, 'event-start-date', true );
|
||||||
$object->set_start_time( \gmdate( 'Y-m-d\TH:i:s\Z', $start_time ) );
|
$object->set_start_time( \gmdate( 'Y-m-d\TH:i:s\Z', $start_time ) );
|
||||||
|
|
||||||
$object->set_end_time( $this->get_end_time() );
|
$hide_end_time = get_post_meta( $this->wp_post->ID, 'event-hide-end-time', true);
|
||||||
|
|
||||||
$path = sprintf( 'users/%d/followers', intval( $wp_post->post_author ) );
|
if ( $hide_end_time != 'yes' ) {
|
||||||
|
$object->set_end_time( $this->get_end_time() );
|
||||||
|
}
|
||||||
|
|
||||||
|
$path = sprintf( 'users/%d/followers', intval( $this->wp_post->post_author ) );
|
||||||
|
|
||||||
$object
|
$object
|
||||||
->set_location( $this->get_event_location( $wp_post->ID ) )
|
->set_location( $this->get_event_location( $this->wp_post->ID ) )
|
||||||
->set_comments_enabled( comments_open( $wp_post->ID ) )
|
->set_comments_enabled( comments_open( $this->wp_post->ID ) )
|
||||||
->set_to(
|
->set_to(
|
||||||
array(
|
array(
|
||||||
'https://www.w3.org/ns/activitystreams#Public',
|
'https://www.w3.org/ns/activitystreams#Public',
|
||||||
|
@ -181,7 +186,9 @@ class VS_Event extends \Activitypub\Transformer\Base {
|
||||||
->set_cc( $this->get_cc() )
|
->set_cc( $this->get_cc() )
|
||||||
->set_attachment( $this->get_attachments() )
|
->set_attachment( $this->get_attachments() )
|
||||||
->set_tag( $this->get_tags() )
|
->set_tag( $this->get_tags() )
|
||||||
->set_replies_moderation_option( 'allow_all' );
|
->set_replies_moderation_option( 'allow_all' )
|
||||||
|
->set_join_mode( 'external' )
|
||||||
|
->set_external_participation_url( $this->get_url() );
|
||||||
|
|
||||||
return $object;
|
return $object;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue