wip
Some checks failed
PHP Code Checker / PHP Code Checker (pull_request) Failing after 49s
PHPUnit / PHPUnit – PHP 7.4 (pull_request) Failing after 57s
PHPUnit / PHPUnit – PHP 8.0 (pull_request) Successful in 1m5s
PHPUnit / PHPUnit – PHP 8.1 (pull_request) Successful in 1m2s
PHPUnit / PHPUnit – PHP 8.2 (pull_request) Successful in 1m4s
PHPUnit / PHPUnit – PHP 8.3 (pull_request) Successful in 1m3s
PHPUnit / PHPUnit – PHP 8.4 (pull_request) Successful in 1m6s

This commit is contained in:
André Menrath 2024-12-11 23:09:12 +01:00
parent 16762b2b31
commit 3f5c57134e
6 changed files with 198 additions and 13 deletions

View file

@ -34,6 +34,8 @@ class Event_Sources {
\add_action( 'event_bridge_for_activitypub_unfollow', array( self::class, 'activitypub_unfollow_actor' ), 10, 2 ); \add_action( 'event_bridge_for_activitypub_unfollow', array( self::class, 'activitypub_unfollow_actor' ), 10, 2 );
} }
/** /**
* Register the post type used to store the external event sources (i.e., followed ActivityPub actors). * Register the post type used to store the external event sources (i.e., followed ActivityPub actors).
*/ */

View file

@ -41,7 +41,7 @@ class Create {
* @param int $user_id The id of the local blog-user. * @param int $user_id The id of the local blog-user.
*/ */
public static function handle_create( $activity, $user_id ) { public static function handle_create( $activity, $user_id ) {
// We only process activities that are target the application user. // We only process activities that are target to the application user.
if ( Actors::APPLICATION_USER_ID !== $user_id ) { if ( Actors::APPLICATION_USER_ID !== $user_id ) {
return; return;
} }
@ -80,6 +80,8 @@ class Create {
if ( isset( $json_params['object']['type'] ) && 'Event' === $json_params['object']['type'] ) { if ( isset( $json_params['object']['type'] ) && 'Event' === $json_params['object']['type'] ) {
$valid = true; $valid = true;
} else {
return $valid;
} }
if ( empty( $json_params['type'] ) ) { if ( empty( $json_params['type'] ) ) {
@ -87,7 +89,7 @@ class Create {
} }
if ( if (
'Create' !== $json_params['type'] || 'Create' !== $json_params['type'] || 'Update' !== $json_params['type'] ||
is_wp_error( $request ) is_wp_error( $request )
) { ) {
return $valid; return $valid;
@ -103,10 +105,22 @@ class Create {
'id', 'id',
); );
// Limit this as a safety measure.
add_filter( 'wp_revisions_to_keep', array( 'revisions_to_keep' ) );
if ( array_intersect( $required, array_keys( $object ) ) !== $required ) { if ( array_intersect( $required, array_keys( $object ) ) !== $required ) {
return false; return false;
} }
return $valid; return $valid;
} }
/**
* Return the number of revisions to keep.
*
* @return int The number of revisions to keep.
*/
public static function revisions_to_keep() {
return 3;
}
} }

View file

@ -10,6 +10,9 @@ namespace Event_Bridge_For_ActivityPub\ActivityPub\Handler;
use Activitypub\Notification; use Activitypub\Notification;
use Activitypub\Collection\Actors; use Activitypub\Collection\Actors;
use Activitypub\Http; use Activitypub\Http;
use Event_Bridge_For_ActivityPub\Setup;
use function Activitypub\is_activity_public;
/** /**
* Handle Update requests. * Handle Update requests.
@ -21,30 +24,41 @@ class Update {
public static function init() { public static function init() {
\add_action( \add_action(
'activitypub_inbox_update', 'activitypub_inbox_update',
array( self::class, 'handle_update' ) array( self::class, 'handle_update' ),
15,
2
); );
} }
/** /**
* Handle "Follow" requests. * Handle "Follow" requests.
* *
* @param array $activity The activity object. * @param array $activity The activity-object.
* @param int $user_id The id of the local blog-user.
*/ */
public static function handle_update( $activity ) { public static function handle_update( $activity, $user_id ) {
if ( ! isset( $activity['object'] ) ) { // We only process activities that are target the application user.
if ( Actors::APPLICATION_USER_ID !== $user_id ) {
return; return;
} }
$object = Actors::get_by_resource( $activity['object'] ); // Check if Activity is public or not.
if ( ! is_activity_public( $activity ) ) {
if ( ! $object || is_wp_error( $object ) ) {
// If we can not find a actor, we handle the `Update` activity.
return; return;
} }
// We only expect `Update` activities being answers to follow requests by the application actor. // Check if an object is set.
if ( Actors::APPLICATION_USER_ID !== $object->get__id() ) { if ( ! isset( $activity['object']['type'] ) || 'Event' !== $activity['object']['type'] ) {
return; return;
} }
$transmogrifier_class = Setup::get_transmogrifier();
if ( ! $transmogrifier_class ) {
return;
}
$transmogrifier = new $transmogrifier_class( $activity['object'] );
$transmogrifier->update();
} }
} }

View file

@ -52,6 +52,15 @@ class GatherPress {
$this->activitypub_event = $activitypub_event; $this->activitypub_event = $activitypub_event;
} }
/**
* Get post.
*/
private function get_post_id_from_activitypub_id() {
global $wpdb;
$id = $this->activitypub_event->get_id();
return $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid=%s AND post_type=%s", $id, 'gatherpress_event' ) );
}
/** /**
* Save the ActivityPub event object as GatherPress Event. * Save the ActivityPub event object as GatherPress Event.
*/ */
@ -64,6 +73,39 @@ class GatherPress {
'post_content' => $this->activitypub_event->get_content(), 'post_content' => $this->activitypub_event->get_content(),
'post_excerpt' => $this->activitypub_event->get_summary(), 'post_excerpt' => $this->activitypub_event->get_summary(),
'post_status' => 'publish', 'post_status' => 'publish',
'guid' => $this->activitypub_event->get_id(),
)
);
if ( ! $post_id || is_wp_error( $post_id ) ) {
return;
}
$event = new \GatherPress\Core\Event( $post_id );
$params = array(
'datetime_start' => $this->activitypub_event->get_start_time(),
'datetime_end' => $this->activitypub_event->get_end_time(),
'timezone' => $this->activitypub_event->get_timezone(),
);
$event->save_datetimes( $params );
}
/**
* Save the ActivityPub event object as GatherPress Event.
*/
public function update() {
$post_id = $this->get_post_id_from_activitypub_id();
// Insert new GatherPress Event post.
$post_id = wp_update_post(
array(
'ID' => $post_id,
'post_title' => $this->activitypub_event->get_name(),
'post_type' => 'gatherpress_event',
'post_content' => $this->activitypub_event->get_content(),
'post_excerpt' => $this->activitypub_event->get_summary(),
'post_status' => 'publish',
'guid' => $this->activitypub_event->get_id(),
) )
); );

View file

@ -0,0 +1,111 @@
<?php
/**
* Class responsible for User Interface additions in the Admin UI.
*
* @package Event_Bridge_For_ActivityPub
* @since 1.0.0
* @license AGPL-3.0-or-later
*/
namespace Event_Bridge_For_ActivityPub\Admin;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
use Event_Bridge_For_ActivityPub\Integrations\Event_Plugin;
/**
* Class responsible for Event Plugin related admin notices.
*
* Notices for guiding to proper configuration of ActivityPub with event plugins.
*
* @since 1.0.0
*/
class User_Interface {
/**
* Init.
*/
public static function init() {
\add_filter( 'page_row_actions', array( self::class, 'row_actions' ), 10, 2 );
\add_filter( 'post_row_actions', array( self::class, 'row_actions' ), 10, 2 );
\add_action(
'admin_init',
\add_filter(
'map_meta_cap',
array( self::class, 'disable_editing_for_external_events' ),
10,
4
)
);
}
/**
* Add an column that shows the origin of an external event.
*
* @param array $columns The current columns.
* @return array
*/
public static function add_origin_column( $columns ) {
// Add a new column after the title column.
$columns['activitypub_origin'] = __( 'ActivityPub origin', 'event-bridge-for-activitypub' );
return $columns;
}
/**
* Add a "⁂ Preview" link to the row actions.
*
* @param array $actions The existing actions.
* @param \WP_Post $post The post object.
*
* @return array The modified actions.
*/
public static function row_actions( $actions, $post ) {
// check if the post is enabled for ActivityPub.
if ( ! self::post_is_external_event_post( $post ) ) {
return $actions;
}
$actions['view_origin'] = sprintf(
'<a href="%s" target="_blank">%s</a>',
\esc_url( $post->guid ),
\esc_html__( 'Open original page', 'event-bridge-for-activitypub' )
);
return $actions;
}
/**
* Check if a post is both an event post and external (from ActivityPub federation).
*
* @param WP_Post $post The post.
* @return bool
*/
private static function post_is_external_event_post( $post ) {
if ( 'gatherpress_event' !== $post->post_type ) {
return false;
}
return str_starts_with( $post->guid, 'https://ga.lan' ) ? true : false;
}
/**
* Modify the user capabilities so that nobody can edit external events.
*
* @param array $caps Concerned user's capabilities.
* @param array $cap Required primitive capabilities for the requested capability.
* @param array $user_id The WordPress user ID.
* @param array $args Additional args.
*
* @return array
*/
public static function disable_editing_for_external_events( $caps, $cap, $user_id, $args ) {
if ( 'edit_post' === $cap && isset( $args[0] ) ) {
$post_id = $args[0];
$post = get_post( $post_id );
if ( $post && self::post_is_external_event_post( $post ) ) {
// Deny editing by returning 'do_not_allow'.
return array( 'do_not_allow' );
}
}
return $caps;
}
}

View file

@ -21,6 +21,7 @@ use Event_Bridge_For_ActivityPub\Admin\Event_Plugin_Admin_Notices;
use Event_Bridge_For_ActivityPub\Admin\General_Admin_Notices; use Event_Bridge_For_ActivityPub\Admin\General_Admin_Notices;
use Event_Bridge_For_ActivityPub\Admin\Health_Check; use Event_Bridge_For_ActivityPub\Admin\Health_Check;
use Event_Bridge_For_ActivityPub\Admin\Settings_Page; use Event_Bridge_For_ActivityPub\Admin\Settings_Page;
use Event_Bridge_For_ActivityPub\Admin\User_Interface;
use Event_Bridge_For_ActivityPub\Integrations\Event_Plugin; use Event_Bridge_For_ActivityPub\Integrations\Event_Plugin;
require_once ABSPATH . 'wp-admin/includes/plugin.php'; require_once ABSPATH . 'wp-admin/includes/plugin.php';
@ -212,7 +213,8 @@ class Setup {
if ( get_option( 'event_bridge_for_activitypub_event_sources_active' ) ) { if ( get_option( 'event_bridge_for_activitypub_event_sources_active' ) ) {
add_action( 'init', array( Event_Sources_Collection::class, 'init' ) ); add_action( 'init', array( Event_Sources_Collection::class, 'init' ) );
add_action( 'init', array( Handler::class, 'register_handlers' ) ); add_action( 'activitypub_register_handlers', array( Handler::class, 'register_handlers' ) );
add_action( 'admin_init', array( User_Interface::class, 'init' ) );
} }
add_filter( 'activitypub_transformer', array( $this, 'register_activitypub_event_transformer' ), 10, 3 ); add_filter( 'activitypub_transformer', array( $this, 'register_activitypub_event_transformer' ), 10, 3 );