Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outbox Collection/Scheduler #593

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
22 changes: 21 additions & 1 deletion includes/class-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Exception;
use Activitypub\Signature;
use Activitypub\Collection\Users;
use Activitypub\Collection\Outbox;
use Activitypub\Collection\Followers;

use function Activitypub\sanitize_url;
Expand Down Expand Up @@ -384,11 +385,12 @@ public static function plugin_update_message( $data ) {
}

/**
* Register the "Followers" Taxonomy
* Register Custom Post Types
*
* @return void
*/
private static function register_post_types() {
// register Followers Post-Type
register_post_type(
Followers::POST_TYPE,
array(
Expand Down Expand Up @@ -457,6 +459,24 @@ private static function register_post_types() {
);

do_action( 'activitypub_after_register_post_type' );

// register Outbox Post-Type
register_post_type(
Outbox::POST_TYPE,
array(
'labels' => array(
'name' => _x( 'Outbox', 'post_type plural name', 'activitypub' ),
'singular_name' => _x( 'Outbox Item', 'post_type single name', 'activitypub' ),
),
'public' => false,
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'delete_with_user' => false,
'can_export' => true,
'supports' => array(),
)
);
}

/**
Expand Down
55 changes: 53 additions & 2 deletions includes/class-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ class Handler {
* Initialize the class, registering WordPress hooks
*/
public static function init() {
self::register_handlers();
self::register_inbox_handlers();

\add_action( 'transition_post_status', array( self::class, 'schedule_post_activity' ), 33, 3 );
}

/**
* Register handlers.
*/
public static function register_handlers() {
public static function register_inbox_handlers() {
Create::init();
Delete::init();
Follow::init();
Expand All @@ -30,4 +32,53 @@ public static function register_handlers() {

do_action( 'activitypub_register_handlers' );
}

/**
* Schedule Activities.
*
* @param string $new_status New post status.
* @param string $old_status Old post status.
* @param WP_Post $post Post object.
*/
public static function schedule_post_activity( $new_status, $old_status, $post ) {
// Do not send activities if post is password protected.
if ( \post_password_required( $post ) ) {
return;
}

// Check if post-type supports ActivityPub.
$post_types = \get_post_types_by_support( 'activitypub' );
if ( ! \in_array( $post->post_type, $post_types, true ) ) {
return;
}

$type = false;

if ( 'publish' === $new_status && 'publish' !== $old_status ) {
$type = 'Create';
} elseif ( 'publish' === $new_status ) {
$type = 'Update';
} elseif ( 'trash' === $new_status ) {
$type = 'Delete';
}

if ( ! $type ) {
return;
}

\wp_schedule_single_event(
\time(),
'activitypub_send_activity',
array( $post, $type )
);

\wp_schedule_single_event(
\time(),
sprintf(
'activitypub_send_%s_activity',
\strtolower( $type )
),
array( $post )
);
}
}
1 change: 0 additions & 1 deletion includes/class-scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public static function deregister_schedules() {
wp_unschedule_hook( 'activitypub_cleanup_followers' );
}


/**
* Schedule Activities.
*
Expand Down
9 changes: 9 additions & 0 deletions includes/collection/class-outbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Activitypub\Collection;

/**
* ActivityPub Outbox Collection
*/
class Outbox {
const POST_TYPE = 'ap_outbox';
}
4 changes: 2 additions & 2 deletions includes/handler/class-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public static function init() {
/**
* Handle "Update" requests
*
* @param array $array The activity-object
* @param int $user_id The id of the local blog-user
* @param array $array The activity-object
* @param int $user_id The id of the local blog-user
*/
public static function handle_update( $array, $user_id ) {
$object_type = isset( $array['object']['type'] ) ? $array['object']['type'] : '';
Expand Down
18 changes: 18 additions & 0 deletions includes/transformer/class-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Activitypub\Model\Blog_User;
use Activitypub\Transformer\Base;
use Activitypub\Collection\Users;
use Activitypub\Activity\Activity;
use Activitypub\Activity\Base_Object;

use function Activitypub\esc_hashtag;
Expand Down Expand Up @@ -82,6 +83,23 @@ public function to_object() {
return $object;
}

/**
* Transforms the ActivityPub Object to an Activity
*
* @param string $type The Activity-Type.
*
* @return \Activitypub\Activity\Activity The Activity.
*/
public function to_activity( $type ) {
$object = $this->to_object();

$activity = new Activity();
$activity->set_type( $type );
$activity->set_object( $object );

return $activity;
}

/**
* Returns the ID of the Post.
*
Expand Down
Loading