Skip to content

Commit

Permalink
Workaround for 5.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk committed Feb 20, 2025
1 parent 87be212 commit 56d7740
Showing 1 changed file with 92 additions and 30 deletions.
122 changes: 92 additions & 30 deletions feed-parsers/class-feed-parser-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1923,7 +1923,7 @@ public function post_reaction( $post_id ) {
}

/**
* Prepare to follow the user via a scheduled event.
* Prepare to like the post via a scheduled event.
*
* @param \WP_Post $post The post.
* @param string $author_url The author url.
Expand All @@ -1935,13 +1935,32 @@ public function queue_like_post( \WP_Post $post, $author_url ) {
if ( ! $external_post_id ) {
$external_post_id = $post->guid;
}
$user_feed = User_Feed::get_by_url( $author_url );
if ( version_compare( ACTIVITYPUB_PLUGIN_VERSION, '5.3.0', '>=' ) ) {
$outbox_activity_id = \Activitypub\add_to_outbox( $external_post_id, 'Like', get_current_user_id(), ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC );
if ( ! $outbox_activity_id ) {
if ( $user_feed instanceof User_Feed ) {
$user_feed->update_last_log( __( 'Like failed.', 'friends' ) );
}
return false;
}
if ( $user_feed instanceof User_Feed ) {
$user_feed->update_last_log( __( 'Sent like.', 'friends' ) );
}
update_post_meta( $post->ID, 'ap_outbox_like_id', $outbox_activity_id );
return true;
}

$queued = $this->queue(
'friends_feed_parser_activitypub_like',
array( $author_url, $external_post_id, get_current_user_id() ),
'friends_feed_parser_activitypub_unlike'
);

if ( $queued && $user_feed instanceof User_Feed ) {
$user_feed->update_last_log( __( 'Queued like request.', 'friends' ) );
}

return $queued;
}

Expand All @@ -1967,8 +1986,14 @@ public function activitypub_like_post( $url, $external_post_id, $user_id ) {
$activity->set_id( $actor . '#like-' . \preg_replace( '~^https?://~', '', $external_post_id ) );
$activity->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', time() ) );

$inboxes = apply_filters( 'activitypub_send_to_inboxes', array(), $user_id, $activity );
$inboxes = array_unique( $inboxes );
$json = $activity->to_json();

if ( version_compare( ACTIVITYPUB_PLUGIN_VERSION, '5.2.0', '>=' ) ) {
$inboxes = \Activitypub\Followers::get_inboxes_for_activity( $json, $actor->get__id(), 500 );
} else {
$inboxes = apply_filters( 'activitypub_send_to_inboxes', array(), $user_id, $activity );
$inboxes = array_unique( $inboxes );
}

if ( empty( $inboxes ) ) {
$message = sprintf(
Expand All @@ -1986,8 +2011,6 @@ public function activitypub_like_post( $url, $external_post_id, $user_id ) {
return;
}

$json = $activity->to_json();

$report = array();
foreach ( $inboxes as $inbox ) {
$response = \Activitypub\safe_remote_post( $inbox, $json, $user_id );
Expand Down Expand Up @@ -2049,18 +2072,29 @@ public function queue_unlike_post( \WP_Post $post, $author_url ) {
if ( ! $external_post_id ) {
$external_post_id = $post->guid;
}

$user_feed = User_Feed::get_by_url( $author_url );
if ( version_compare( ACTIVITYPUB_PLUGIN_VERSION, '5.3.0', '>=' ) ) {
$outbox_activity_id = get_post_meta( $post->ID, 'ap_outbox_like_id', true );
if ( ! $outbox_activity_id ) {
if ( $user_feed instanceof User_Feed ) {
$user_feed->update_last_log( __( 'Unlike failed.', 'friends' ) );
}
return false;
}
if ( $user_feed instanceof User_Feed ) {
$user_feed->update_last_log( __( 'Sent unlike request.', 'friends' ) );
}
\Activitypub\Outbox::undo( $outbox_activity_id );
return true;
}
$queued = $this->queue(
'friends_feed_parser_activitypub_unlike',
array( $author_url, $external_post_id, get_current_user_id() ),
'friends_feed_parser_activitypub_like'
);

if ( $queued ) {
$user_feed = User_Feed::get_by_url( $author_url );
if ( $user_feed instanceof User_Feed ) {
$user_feed->update_last_log( __( 'Queued unlike request.', 'friends' ) );
}
if ( $queued && $user_feed instanceof User_Feed ) {
$user_feed->update_last_log( __( 'Queued unlike request.', 'friends' ) );
}

return $queued;
Expand Down Expand Up @@ -2094,8 +2128,13 @@ public function activitypub_unlike_post( $url, $external_post_id, $user_id ) {
);
$activity->set_id( $actor . '#unlike-' . \preg_replace( '~^https?://~', '', $external_post_id ) );

$inboxes = apply_filters( 'activitypub_send_to_inboxes', array(), $user_id, $activity );
$inboxes = array_unique( $inboxes );
$json = $activity->to_json();
if ( version_compare( ACTIVITYPUB_PLUGIN_VERSION, '5.2.0', '>=' ) ) {
$inboxes = \Activitypub\Followers::get_inboxes_for_activity( $json, $actor->get__id(), 500 );
} else {
$inboxes = apply_filters( 'activitypub_send_to_inboxes', array(), $user_id, $activity );
$inboxes = array_unique( $inboxes );
}

if ( empty( $inboxes ) ) {
$message = sprintf(
Expand All @@ -2113,8 +2152,6 @@ public function activitypub_unlike_post( $url, $external_post_id, $user_id ) {
return;
}

$json = $activity->to_json();

$report = array();
foreach ( $inboxes as $inbox ) {
$response = \Activitypub\safe_remote_post( $inbox, $json, $user_id );
Expand Down Expand Up @@ -2244,11 +2281,11 @@ public function ajax_boost() {
}

public function mastodon_api_reblog( $post_id ) {
$this->queue_announce( get_permalink( $post_id ) );
$this->queue_announce( get_post( $post_id ) );
}

public function mastodon_api_unreblog( $post_id ) {
$this->queue_unannounce( get_permalink( $post_id ) );
$this->queue_unannounce( get_post( $post_id ) );
}

/**
Expand All @@ -2274,7 +2311,7 @@ public function reblog( $ret, $post ) {
return $ret;
}
if ( User_Feed::get_parser_for_post_id( $post->ID ) === 'activitypub' ) {
$this->queue_announce( $post->guid );
$this->queue_announce( $post );
\update_post_meta( $post->ID, 'reblogged', 'activitypub' );
\update_post_meta( $post->ID, 'reblogged_by', get_current_user_id() );
return true;
Expand All @@ -2288,7 +2325,7 @@ public function unreblog( $ret, $post ) {
return $ret;
}
if ( get_post_meta( $post->ID, 'reblogged', true ) === 'activitypub' ) {
$this->queue_unannounce( $post->guid );
$this->queue_unannounce( $post );
\delete_post_meta( $post->ID, 'reblogged', 'activitypub' );
\delete_post_meta( $post->ID, 'reblogged_by', get_current_user_id() );
return true;
Expand All @@ -2299,11 +2336,20 @@ public function unreblog( $ret, $post ) {
/**
* Prepare to announce the post via a scheduled event.
*
* @param string $url The url to announce.
* @param \WP_Post $post The post.
*
* @return bool|WP_Error Whether the event was queued.
*/
public function queue_announce( $url ) {
public function queue_announce( \WP_Post $post ) {
$url = get_permalink( $post );
if ( version_compare( ACTIVITYPUB_PLUGIN_VERSION, '5.3.0', '>=' ) ) {
$outbox_activity_id = \Activitypub\add_to_outbox( $url, 'Announce', get_current_user_id(), ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC );
if ( ! $outbox_activity_id ) {
return false;
}
update_post_meta( $post->ID, 'ap_outbox_announce_id', $outbox_activity_id );
return true;
}
$queued = $this->queue(
'friends_feed_parser_activitypub_announce',
array( $url, get_current_user_id() ),
Expand Down Expand Up @@ -2331,8 +2377,13 @@ public function activitypub_announce( $url, $user_id ) {
$activity->set_to( array( 'https://www.w3.org/ns/activitystreams#Public' ) );
$activity->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', time() ) );

$inboxes = apply_filters( 'activitypub_send_to_inboxes', array(), $user_id, $activity );
$inboxes = array_unique( $inboxes );
$json = $activity->to_json();
if ( version_compare( ACTIVITYPUB_PLUGIN_VERSION, '5.2.0', '>=' ) ) {
$inboxes = \Activitypub\Followers::get_inboxes_for_activity( $json, $actor->get__id(), 500 );
} else {
$inboxes = apply_filters( 'activitypub_send_to_inboxes', array(), $user_id, $activity );
$inboxes = array_unique( $inboxes );
}

if ( empty( $inboxes ) ) {
$message = sprintf(
Expand All @@ -2350,7 +2401,6 @@ public function activitypub_announce( $url, $user_id ) {
return;
}

$json = $activity->to_json();
$report = array();
foreach ( $inboxes as $inbox ) {
$response = \Activitypub\safe_remote_post( $inbox, $json, $user_id );
Expand All @@ -2374,11 +2424,20 @@ public function activitypub_announce( $url, $user_id ) {
/**
* Prepare to announce the post via a scheduled event.
*
* @param string $url The url to announce.
* @param \WP_Post $post The post.
*
* @return bool|WP_Error Whether the event was queued.
*/
public function queue_unannounce( $url ) {
public function queue_unannounce( \WP_Post $post ) {
$url = get_permalink( $post );
if ( version_compare( ACTIVITYPUB_PLUGIN_VERSION, '5.3.0', '>=' ) ) {
$outbox_activity_id = get_post_meta( $post->ID, 'ap_outbox_announce_id', true );
if ( ! $outbox_activity_id ) {
return false;
}
\Activitypub\Outbox::undo( $outbox_activity_id );
return true;
}
$queued = $this->queue(
'friends_feed_parser_activitypub_unannounce',
array( $url, get_current_user_id() ),
Expand Down Expand Up @@ -2414,8 +2473,13 @@ public function activitypub_unannounce( $url, $user_id ) {
);
$activity->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', time() ) );

$inboxes = apply_filters( 'activitypub_send_to_inboxes', array(), $user_id, $activity );
$inboxes = array_unique( $inboxes );
$json = $activity->to_json();
if ( version_compare( ACTIVITYPUB_PLUGIN_VERSION, '5.2.0', '>=' ) ) {
$inboxes = \Activitypub\Followers::get_inboxes_for_activity( $json, $actor->get__id(), 500 );
} else {
$inboxes = apply_filters( 'activitypub_send_to_inboxes', array(), $user_id, $activity );
$inboxes = array_unique( $inboxes );
}

if ( empty( $inboxes ) ) {
$message = sprintf(
Expand All @@ -2433,8 +2497,6 @@ public function activitypub_unannounce( $url, $user_id ) {
return;
}

$json = $activity->to_json();

$report = array();
foreach ( $inboxes as $inbox ) {
$response = \Activitypub\safe_remote_post( $inbox, $json, $user_id );
Expand Down

0 comments on commit 56d7740

Please sign in to comment.