Skip to content

Commit

Permalink
fix #23
Browse files Browse the repository at this point in the history
  • Loading branch information
pfefferle committed Feb 17, 2019
1 parent 0fa7fc6 commit 13bbfe0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
5 changes: 2 additions & 3 deletions activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function activitypub_init() {
require_once dirname( __FILE__ ) . '/includes/class-rest-activitypub-outbox.php';
add_action( 'rest_api_init', array( 'Rest_Activitypub_Outbox', 'register_routes' ) );
add_action( 'activitypub_send_post_activity', array( 'Rest_Activitypub_Outbox', 'send_post_activity' ) );
add_action( 'activitypub_send_update_activity', array( 'Rest_Activitypub_Outbox', 'send_update_activity' ) );

require_once dirname( __FILE__ ) . '/includes/class-rest-activitypub-inbox.php';
add_action( 'rest_api_init', array( 'Rest_Activitypub_Inbox', 'register_routes' ) );
Expand All @@ -62,9 +63,7 @@ function activitypub_init() {
add_post_type_support( 'page', 'activitypub' );

$post_types = get_post_types_by_support( 'activitypub' );
foreach ( $post_types as $post_type ) {
add_action( 'publish_' . $post_type, array( 'Activitypub', 'schedule_post_activity' ) );
}
add_action( 'transition_post_status', array( 'Activitypub', 'schedule_post_activity' ), 10, 3 );

require_once dirname( __FILE__ ) . '/includes/class-activitypub-admin.php';
add_action( 'admin_menu', array( 'Activitypub_Admin', 'admin_menu' ) );
Expand Down
8 changes: 6 additions & 2 deletions includes/class-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ public static function add_rewrite_endpoint() {
*
* @param int $post_id
*/
public static function schedule_post_activity( $post_id ) {
wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_post_activity', array( $post_id ) );
public static function schedule_post_activity( $new_status, $old_status, $post ) {
if ( 'publish' === $new_status && 'publish' !== $old_status ) {
wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_post_activity', array( $post->ID ) );
} elseif ( 'publish' === $new_status ) {
wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_update_activity', array( $post->ID ) );
}
}
}
17 changes: 17 additions & 0 deletions includes/class-rest-activitypub-outbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,21 @@ public static function send_post_activity( $post_id ) {
$response = activitypub_safe_remote_post( $inbox, $activity, $user_id );
}
}

public static function send_update_activity( $post_id ) {
$post = get_post( $post_id );
$user_id = $post->post_author;

$activitypub_post = new Activitypub_Post( $post );
$activitypub_activity = new Activitypub_Activity( 'Update', Activitypub_Activity::TYPE_FULL );
$activitypub_activity->from_post( $activitypub_post->to_array() );

$activity = $activitypub_activity->to_json(); // phpcs:ignore

$followers = Db_Activitypub_Followers::get_followers( $user_id );

foreach ( activitypub_get_follower_inboxes( $user_id, $followers ) as $inbox ) {
$response = activitypub_safe_remote_post( $inbox, $activity, $user_id );
}
}
}

0 comments on commit 13bbfe0

Please sign in to comment.