some simplifications
This commit is contained in:
parent
b07e756927
commit
a9c1862369
7 changed files with 75 additions and 77 deletions
|
@ -33,12 +33,12 @@ class Activity_Dispatcher {
|
||||||
/**
|
/**
|
||||||
* Send Activities to followers and mentioned users or `Announce` (boost) a blog post.
|
* Send Activities to followers and mentioned users or `Announce` (boost) a blog post.
|
||||||
*
|
*
|
||||||
* @param WP_Post $wp_post The ActivityPub Post.
|
* @param mixed $wp_object The ActivityPub Post.
|
||||||
* @param string $type The Activity-Type.
|
* @param string $type The Activity-Type.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function send_activity_or_announce( WP_Post $wp_post, $type ) {
|
public static function send_activity_or_announce( $wp_object, $type ) {
|
||||||
// check if a migration is needed before sending new posts
|
// check if a migration is needed before sending new posts
|
||||||
Migration::maybe_migrate();
|
Migration::maybe_migrate();
|
||||||
|
|
||||||
|
@ -46,35 +46,35 @@ class Activity_Dispatcher {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$wp_post->post_author = Users::BLOG_USER_ID;
|
$wp_object->post_author = Users::BLOG_USER_ID;
|
||||||
|
|
||||||
if ( is_single_user() ) {
|
if ( is_single_user() ) {
|
||||||
self::send_activity( $wp_post, $type );
|
self::send_activity( $wp_object, $type );
|
||||||
} else {
|
} else {
|
||||||
self::send_announce( $wp_post, $type );
|
self::send_announce( $wp_object, $type );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send Activities to followers and mentioned users.
|
* Send Activities to followers and mentioned users.
|
||||||
*
|
*
|
||||||
* @param WP_Post $wp_post The ActivityPub Post.
|
* @param mixed $wp_object The ActivityPub Post.
|
||||||
* @param string $type The Activity-Type.
|
* @param string $type The Activity-Type.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function send_activity( WP_Post $wp_post, $type ) {
|
public static function send_activity( $wp_object, $type ) {
|
||||||
if ( is_user_disabled( $wp_post->post_author ) ) {
|
if ( is_user_disabled( $wp_object->post_author ) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$object = Post::transform( $wp_post )->to_object();
|
$object = Factory::get_transformer( $wp_object )->to_object();
|
||||||
|
|
||||||
$activity = new Activity();
|
$activity = new Activity();
|
||||||
$activity->set_type( $type );
|
$activity->set_type( $type );
|
||||||
$activity->set_object( $object );
|
$activity->set_object( $object );
|
||||||
|
|
||||||
$follower_inboxes = Followers::get_inboxes( $wp_post->post_author );
|
$follower_inboxes = Followers::get_inboxes( $wp_object->post_author );
|
||||||
$mentioned_inboxes = Mention::get_inboxes( $activity->get_cc() );
|
$mentioned_inboxes = Mention::get_inboxes( $activity->get_cc() );
|
||||||
|
|
||||||
$inboxes = array_merge( $follower_inboxes, $mentioned_inboxes );
|
$inboxes = array_merge( $follower_inboxes, $mentioned_inboxes );
|
||||||
|
@ -83,19 +83,19 @@ class Activity_Dispatcher {
|
||||||
$json = $activity->to_json();
|
$json = $activity->to_json();
|
||||||
|
|
||||||
foreach ( $inboxes as $inbox ) {
|
foreach ( $inboxes as $inbox ) {
|
||||||
safe_remote_post( $inbox, $json, $wp_post->post_author );
|
safe_remote_post( $inbox, $json, $wp_object->post_author );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send Announces to followers and mentioned users.
|
* Send Announces to followers and mentioned users.
|
||||||
*
|
*
|
||||||
* @param WP_Post $wp_post The ActivityPub Post.
|
* @param mixed $wp_object The ActivityPub Post.
|
||||||
* @param string $type The Activity-Type.
|
* @param string $type The Activity-Type.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function send_announce( WP_Post $wp_post, $type ) {
|
public static function send_announce( $wp_object, $type ) {
|
||||||
if ( ! in_array( $type, array( 'Create', 'Update' ), true ) ) {
|
if ( ! in_array( $type, array( 'Create', 'Update' ), true ) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -104,7 +104,7 @@ class Activity_Dispatcher {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$object = Post::transform( $wp_post )->to_object();
|
$object = Factory::get_transformer( $wp_object )->to_object();
|
||||||
|
|
||||||
$activity = new Activity();
|
$activity = new Activity();
|
||||||
$activity->set_type( 'Announce' );
|
$activity->set_type( 'Announce' );
|
||||||
|
@ -113,7 +113,7 @@ class Activity_Dispatcher {
|
||||||
// send only the id
|
// send only the id
|
||||||
$activity->set_object( $object->get_id() );
|
$activity->set_object( $object->get_id() );
|
||||||
|
|
||||||
$follower_inboxes = Followers::get_inboxes( $wp_post->post_author );
|
$follower_inboxes = Followers::get_inboxes( $wp_object->post_author );
|
||||||
$mentioned_inboxes = Mention::get_inboxes( $activity->get_cc() );
|
$mentioned_inboxes = Mention::get_inboxes( $activity->get_cc() );
|
||||||
|
|
||||||
$inboxes = array_merge( $follower_inboxes, $mentioned_inboxes );
|
$inboxes = array_merge( $follower_inboxes, $mentioned_inboxes );
|
||||||
|
@ -122,34 +122,7 @@ class Activity_Dispatcher {
|
||||||
$json = $activity->to_json();
|
$json = $activity->to_json();
|
||||||
|
|
||||||
foreach ( $inboxes as $inbox ) {
|
foreach ( $inboxes as $inbox ) {
|
||||||
safe_remote_post( $inbox, $json, $wp_post->post_author );
|
safe_remote_post( $inbox, $json, $wp_object->post_author );
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Send Activities to followers and mentioned users.
|
|
||||||
*
|
|
||||||
* @param $wp_comment The ActivityPub Comment.
|
|
||||||
* @param string $type The Activity-Type.
|
|
||||||
*/
|
|
||||||
public static function send_comment_activity( $wp_comment, $type ) {
|
|
||||||
|
|
||||||
$object = Comment::transform( $wp_comment )->to_object();
|
|
||||||
|
|
||||||
$activity = new Activity();
|
|
||||||
$activity->set_type( $type );
|
|
||||||
$activity->set_object( $object );
|
|
||||||
|
|
||||||
$follower_inboxes = Followers::get_inboxes( $wp_comment->user_id );
|
|
||||||
$mentioned_inboxes = Mention::get_inboxes( $activity->get_cc() );
|
|
||||||
|
|
||||||
$inboxes = array_merge( $follower_inboxes, $mentioned_inboxes );
|
|
||||||
$inboxes = array_unique( $inboxes );
|
|
||||||
|
|
||||||
$json = $activity->to_json();
|
|
||||||
|
|
||||||
foreach ( $inboxes as $inbox ) {
|
|
||||||
safe_remote_post( $inbox, $json, $wp_comment->user_id );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,9 @@ namespace Activitypub;
|
||||||
use Activitypub\Signature;
|
use Activitypub\Signature;
|
||||||
use Activitypub\Collection\Users;
|
use Activitypub\Collection\Users;
|
||||||
|
|
||||||
|
use function Activitypub\is_comment;
|
||||||
|
use function Activitypub\is_replies;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ActivityPub Class
|
* ActivityPub Class
|
||||||
*
|
*
|
||||||
|
@ -92,9 +95,9 @@ class Activitypub {
|
||||||
|
|
||||||
if ( \is_author() ) {
|
if ( \is_author() ) {
|
||||||
$json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/author-json.php';
|
$json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/author-json.php';
|
||||||
} elseif ( \Activitypub\is_ap_replies() ) {
|
} elseif ( is_replies() ) {
|
||||||
$json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/replies-json.php';
|
$json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/replies-json.php';
|
||||||
} elseif ( \Activitypub\is_ap_comment() ) {
|
} elseif ( is_comment() ) {
|
||||||
$json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/comment-json.php';
|
$json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/comment-json.php';
|
||||||
} elseif ( \is_singular() ) {
|
} elseif ( \is_singular() ) {
|
||||||
$json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/post-json.php';
|
$json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/post-json.php';
|
||||||
|
|
|
@ -18,7 +18,6 @@ class Comments {
|
||||||
\add_filter( 'comment_post', array( self::class, 'postprocess_comment' ), 10, 3 );
|
\add_filter( 'comment_post', array( self::class, 'postprocess_comment' ), 10, 3 );
|
||||||
\add_filter( 'comment_reply_link', array( self::class, 'comment_reply_link' ), 10, 4 );
|
\add_filter( 'comment_reply_link', array( self::class, 'comment_reply_link' ), 10, 4 );
|
||||||
\add_action( 'edit_comment', array( self::class, 'edit_comment' ), 20, 2 ); //schedule_admin_comment_activity
|
\add_action( 'edit_comment', array( self::class, 'edit_comment' ), 20, 2 ); //schedule_admin_comment_activity
|
||||||
\add_action( 'transition_comment_status', array( self::class, 'schedule_comment_activity' ), 20, 3 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -133,28 +132,4 @@ class Comments {
|
||||||
\wp_schedule_single_event( \time(), 'activitypub_send_comment_activity', array( $wp_comment, 'Update' ) );
|
\wp_schedule_single_event( \time(), 'activitypub_send_comment_activity', array( $wp_comment, 'Update' ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Schedule Comment Activities
|
|
||||||
*
|
|
||||||
* transition_comment_status()
|
|
||||||
* @param int $comment
|
|
||||||
*/
|
|
||||||
public static function schedule_comment_activity( $new_status, $old_status, $activitypub_comment ) {
|
|
||||||
if ( 'approved' === $new_status && 'approved' !== $old_status ) {
|
|
||||||
//should only federate replies from local actors
|
|
||||||
//should only federate replies to federated actors
|
|
||||||
|
|
||||||
$ap_object = unserialize( \get_comment_meta( $activitypub_comment->comment_ID, 'ap_object', true ) );
|
|
||||||
if ( empty( $ap_object ) ) {
|
|
||||||
\wp_schedule_single_event( \time(), 'activitypub_send_comment_activity', array( $activitypub_comment, 'Create' ) );
|
|
||||||
}
|
|
||||||
} elseif ( 'trash' === $new_status ) {
|
|
||||||
\wp_schedule_single_event( \time(), 'activitypub_send_comment_activity', array( $activitypub_comment, 'Delete' ) );
|
|
||||||
} elseif ( $old_status === $new_status ) {
|
|
||||||
//TODO Test with non-admin user
|
|
||||||
\wp_schedule_single_event( \time(), 'activitypub_send_comment_activity', array( $activitypub_comment, 'Update' ) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ class Scheduler {
|
||||||
*/
|
*/
|
||||||
public static function init() {
|
public static function init() {
|
||||||
\add_action( 'transition_post_status', array( self::class, 'schedule_post_activity' ), 33, 3 );
|
\add_action( 'transition_post_status', array( self::class, 'schedule_post_activity' ), 33, 3 );
|
||||||
|
\add_action( 'transition_comment_status', array( self::class, 'schedule_comment_activity' ), 33, 3 );
|
||||||
|
|
||||||
\add_action( 'activitypub_update_followers', array( self::class, 'update_followers' ) );
|
\add_action( 'activitypub_update_followers', array( self::class, 'update_followers' ) );
|
||||||
\add_action( 'activitypub_cleanup_followers', array( self::class, 'cleanup_followers' ) );
|
\add_action( 'activitypub_cleanup_followers', array( self::class, 'cleanup_followers' ) );
|
||||||
|
@ -99,6 +100,30 @@ class Scheduler {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Schedule Comment Activities
|
||||||
|
*
|
||||||
|
* transition_comment_status()
|
||||||
|
*
|
||||||
|
* @param int $comment
|
||||||
|
*/
|
||||||
|
public static function schedule_comment_activity( $new_status, $old_status, $comment ) {
|
||||||
|
$comment = get_comment( $comment );
|
||||||
|
|
||||||
|
if ( ! $comment->user_id ) {
|
||||||
|
// Registered comment author
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( 'approved' === $new_status && 'approved' !== $old_status ) {
|
||||||
|
\wp_schedule_single_event( \time(), 'activitypub_send_activity', array( $comment, 'Create' ) );
|
||||||
|
} elseif ( 'trash' === $new_status ) {
|
||||||
|
\wp_schedule_single_event( \time(), 'activitypub_send_activity', array( $comment, 'Delete' ) );
|
||||||
|
} elseif ( $old_status === $new_status ) {
|
||||||
|
\wp_schedule_single_event( \time(), 'activitypub_send_activity', array( $comment, 'Update' ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update followers
|
* Update followers
|
||||||
*
|
*
|
||||||
|
|
|
@ -358,9 +358,10 @@ function get_source_id( $comment ) {
|
||||||
/**
|
/**
|
||||||
* Verify if url is a wp_ap_comment,
|
* Verify if url is a wp_ap_comment,
|
||||||
* Or if it is a previously received remote comment
|
* Or if it is a previously received remote comment
|
||||||
|
*
|
||||||
* @return int comment_id
|
* @return int comment_id
|
||||||
*/
|
*/
|
||||||
function is_ap_comment() {
|
function is_comment() {
|
||||||
$comment_id = get_query_var( 'replytocom', null );
|
$comment_id = get_query_var( 'replytocom', null );
|
||||||
if ( ! is_null( $comment_id ) ) {
|
if ( ! is_null( $comment_id ) ) {
|
||||||
$comment = \get_comment( $comment_id );
|
$comment = \get_comment( $comment_id );
|
||||||
|
@ -376,7 +377,7 @@ function is_ap_comment() {
|
||||||
* Verify if url has a replies query,
|
* Verify if url has a replies query,
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
function is_ap_replies() {
|
function is_replies() {
|
||||||
$replies = get_query_var( 'replies' );
|
$replies = get_query_var( 'replies' );
|
||||||
if ( $replies ) {
|
if ( $replies ) {
|
||||||
return $replies;
|
return $replies;
|
||||||
|
|
21
includes/transformer/class-factory.php
Normal file
21
includes/transformer/class-factory.php
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
<?php
|
||||||
|
namespace Activitypub\Transformer;
|
||||||
|
|
||||||
|
use Activitypub\Transformer\Post;
|
||||||
|
use Activitypub\Transformer\Comment;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transformer Factory
|
||||||
|
*/
|
||||||
|
class Factor {
|
||||||
|
public static function get_transformer( $object ) {
|
||||||
|
switch ( get_class( $object ) ) {
|
||||||
|
case 'WP_Post':
|
||||||
|
return new Post( $object );
|
||||||
|
case 'WP_Comment':
|
||||||
|
return new Comment( $object );
|
||||||
|
default:
|
||||||
|
return apply_filters( 'activitypub_transformer', null, $object, get_class( $object ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
$comment = \get_comment( \get_query_var( 'replytocom' ) ); // phpcs:ignore
|
$comment = \get_comment( \get_query_var( 'replytocom' ) ); // phpcs:ignore
|
||||||
|
|
||||||
$activitypub_comment = new \Activitypub\Model\Comment( $comment );
|
$object = new \Activitypub\Transformer\Comment( $comment );
|
||||||
$json = \array_merge( array( '@context' => \Activitypub\get_context() ), $activitypub_comment->to_array() );
|
$json = \array_merge( array( '@context' => \Activitypub\get_context() ), $object->to_object()->to_array() );
|
||||||
|
|
||||||
// filter output
|
// filter output
|
||||||
$json = \apply_filters( 'activitypub_json_comment_array', $json );
|
$json = \apply_filters( 'activitypub_json_comment_array', $json );
|
||||||
|
|
Loading…
Reference in a new issue