Skip to content

Commit

Permalink
Send the ActivityPub follow not to the shared inbox but the individua…
Browse files Browse the repository at this point in the history
…ls inbox (#291)
  • Loading branch information
akirk authored Feb 21, 2024
1 parent d18e821 commit 14b60d3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 36 deletions.
53 changes: 17 additions & 36 deletions feed-parsers/class-feed-parser-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,16 @@ public function friends_webfinger_resolve( $url, $incoming_url ) {
* Get the inbox URL for an actor
*
* @param string $url The URL of the actor.
* @param string $type The type of the activity.
* @return string|WP_Error
*/
public static function get_inbox_by_actor( $url ) {
public static function get_inbox_by_actor( $url, $type ) {
$metadata = self::get_metadata( $url );
if ( \is_wp_error( $metadata ) ) {
return $metadata;
}

if ( isset( $metadata['endpoints'] ) && isset( $metadata['endpoints']['sharedInbox'] ) ) {
if ( ! in_array( $type, array( 'Follow' ) ) && isset( $metadata['endpoints'] ) && isset( $metadata['endpoints']['sharedInbox'] ) ) {
return $metadata['endpoints']['sharedInbox'];
}

Expand Down Expand Up @@ -1021,14 +1022,15 @@ public function activitypub_follow_user( $url, $user_id = null ) {
return $meta;
}
$to = $meta['id'];
$inbox = self::get_inbox_by_actor( $to );
$type = 'Follow';
$inbox = self::get_inbox_by_actor( $to, $type );
if ( is_wp_error( $inbox ) ) {
return $inbox;
}
$actor = \get_author_posts_url( $user_id );

$activity = new \Activitypub\Activity\Activity();
$activity->set_type( 'Follow' );
$activity->set_type( $type );
$activity->set_to( null );
$activity->set_cc( null );
$activity->set_actor( $actor );
Expand Down Expand Up @@ -1098,7 +1100,8 @@ public function activitypub_unfollow_user( $url, $user_id = null ) {
return $meta;
}
$to = $meta['id'];
$inbox = self::get_inbox_by_actor( $to );
$type = 'Follow';
$inbox = self::get_inbox_by_actor( $to, $type );
if ( is_wp_error( $inbox ) ) {
return $inbox;
}
Expand All @@ -1111,7 +1114,7 @@ public function activitypub_unfollow_user( $url, $user_id = null ) {
$activity->set_actor( $actor );
$activity->set_object(
array(
'type' => 'Follow',
'type' => $type,
'actor' => $actor,
'object' => $to,
'id' => $to,
Expand Down Expand Up @@ -1412,14 +1415,15 @@ public function queue_like_post( \WP_Post $post, $author_url ) {
* @return void|WP_Error
*/
public function activitypub_like_post( $url, $external_post_id, $user_id ) {
$inbox = self::get_inbox_by_actor( $url );
$type = 'Like';
$inbox = self::get_inbox_by_actor( $url, $type );
if ( is_wp_error( $inbox ) ) {
return $inbox;
}
$actor = \get_author_posts_url( $user_id );

$activity = new \Activitypub\Activity\Activity();
$activity->set_type( 'Like' );
$activity->set_type( $type );
$activity->set_to( null );
$activity->set_cc( null );
$activity->set_actor( $actor );
Expand Down Expand Up @@ -1493,7 +1497,8 @@ public function queue_unlike_post( \WP_Post $post, $author_url ) {
* @return void|WP_Error
*/
public function activitypub_unlike_post( $url, $external_post_id, $user_id ) {
$inbox = self::get_inbox_by_actor( $url );
$type = 'Like';
$inbox = self::get_inbox_by_actor( $url, $type );
if ( is_wp_error( $inbox ) ) {
return $inbox;
}
Expand All @@ -1506,7 +1511,7 @@ public function activitypub_unlike_post( $url, $external_post_id, $user_id ) {
$activity->set_actor( $actor );
$activity->set_object(
array(
'type' => 'Like',
'type' => $type,
'actor' => $actor,
'object' => $external_post_id,
'id' => $actor . '#like-' . \preg_replace( '~^https?://~', '', $external_post_id ),
Expand Down Expand Up @@ -1612,30 +1617,6 @@ public function frontend_boost_form( $args ) {
}

public function friends_search_autocomplete( $results, $q ) {
function url_truncate( $url, $max_length = 30 ) {
$p = wp_parse_url( $url );
$parts = array( $p['host'] );
if ( trim( $p['path'] ) ) {
$parts = array_merge( $parts, explode( '/', $p['path'] ) );
}

$url = join( '/', $parts );
$reduce = 4;
while ( strlen( $url ) > $max_length ) {
$last_part = array_pop( $parts );
$last_part = substr( $last_part, strlen( $last_part ) - $reduce );
foreach ( $parts as $k => $part ) {
$parts[ $k ] = substr( $part, 0, strlen( $part ) - $reduce );
}
$url = join( '../', array_filter( $parts ) ) . '../..' . $last_part;
array_push( $parts, $last_part );
$reduce = 1;

}

return $url;
}

$url = preg_match( '#^(?:https?:\/\/)?(?:w{3}\.)?[\w-]+(?:\.[\w-]+)+((?:\/[^\s\/]*)*)#i', $q, $m );
$url_with_path = isset( $m[1] ) && $m[1];

Expand All @@ -1654,7 +1635,7 @@ function url_truncate( $url, $max_length = 30 ) {
$result .= '<span class="ab-icon dashicons dashicons-controls-repeat"></span>';
$result .= 'Boost ';
$result .= ' <small>';
$result .= esc_html( url_truncate( $q ) );
$result .= esc_html( Friends::url_truncate( $q ) );
$result .= '</small></a>';
$results[] = $result;
}
Expand All @@ -1664,7 +1645,7 @@ function url_truncate( $url, $max_length = 30 ) {
$result .= '<span class="ab-icon dashicons dashicons-admin-comments"></span>';
$result .= 'Reply to ';
$result .= ' <small>';
$result .= esc_html( url_truncate( $q ) );
$result .= esc_html( Friends::url_truncate( $q ) );
$result .= '</small></a>';
$results[] = $result;
}
Expand Down
24 changes: 24 additions & 0 deletions includes/class-friends.php
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,30 @@ public static function check_url( $url ) {
return wp_http_validate_url( $url );
}

public static function url_truncate( $url, $max_length = 50 ) {
$p = wp_parse_url( untrailingslashit( $url ) );
$parts = array( $p['host'] );
if ( trim( $p['path'] ) ) {
$parts = array_merge( $parts, explode( '/', $p['path'] ) );
}

$url = join( '/', $parts );
$reduce = 4;
while ( strlen( $url ) > $max_length ) {
$last_part = array_pop( $parts );
$last_part = substr( $last_part, strlen( $last_part ) - $reduce );
foreach ( $parts as $k => $part ) {
$parts[ $k ] = substr( $part, 0, strlen( $part ) - $reduce );
}
$url = join( '../', array_filter( $parts ) ) . '../..' . $last_part;
array_push( $parts, $last_part );
$reduce = 1;

}

return $url;
}

/**
* Delete all the data the plugin has stored in WordPress
*/
Expand Down

0 comments on commit 14b60d3

Please sign in to comment.