redirect to canonical URL if it is not an ActivityPub request

This commit is contained in:
Matthias Pfefferle 2023-05-30 10:22:01 +02:00
parent a617553ddf
commit c95e501f98
5 changed files with 19 additions and 3 deletions

View file

@ -227,7 +227,7 @@ class Activitypub {
); );
\add_rewrite_rule( \add_rewrite_rule(
'^@([\w\-\.]+)', '^@([\w\-\.]+)',
'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/users/$matches[1]&redirect=true', 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/users/$matches[1]',
'top' 'top'
); );
} }

View file

@ -236,7 +236,7 @@ function is_activitypub_request() {
* ActivityPub requests are currently only made for * ActivityPub requests are currently only made for
* author archives, singular posts, and the homepage. * author archives, singular posts, and the homepage.
*/ */
if ( ! \is_author() && ! \is_singular() && ! \is_home() ) { if ( ! \is_author() && ! \is_singular() && ! \is_home() && ! defined( 'REST_REQUEST' ) && ! REST_REQUEST ) {
return false; return false;
} }

View file

@ -55,6 +55,10 @@ class Blog_User extends User {
return \esc_url( \trailingslashit( get_home_url() ) . '@' . $this->get_username() ); return \esc_url( \trailingslashit( get_home_url() ) . '@' . $this->get_username() );
} }
public function get_canonical_url() {
return \get_home_url();
}
public function get_username() { public function get_username() {
return \esc_html( \get_option( 'activitypub_blog_user_identifier', 'feed' ) ); return \esc_html( \get_option( 'activitypub_blog_user_identifier', 'feed' ) );
} }

View file

@ -96,6 +96,10 @@ class User {
return \esc_url( \get_author_posts_url( $this->user_id ) ); return \esc_url( \get_author_posts_url( $this->user_id ) );
} }
public function get_canonical_url() {
return $this->get_url();
}
public function get_username() { public function get_username() {
return \esc_attr( \get_the_author_meta( 'login', $this->user_id ) ); return \esc_attr( \get_the_author_meta( 'login', $this->user_id ) );
} }

View file

@ -6,6 +6,8 @@ use WP_REST_Server;
use WP_REST_Response; use WP_REST_Response;
use Activitypub\User_Factory; use Activitypub\User_Factory;
use function Activitypub\is_activitypub_request;
/** /**
* ActivityPub Followers REST-Class * ActivityPub Followers REST-Class
* *
@ -54,6 +56,12 @@ class Users {
return $user; return $user;
} }
// redirect to canonical URL if it is not an ActivityPub request
if ( ! is_activitypub_request() ) {
header( 'Location: ' . $user->get_canonical_url() );
exit;
}
/* /*
* Action triggerd prior to the ActivityPub profile being created and sent to the client * Action triggerd prior to the ActivityPub profile being created and sent to the client
*/ */
@ -81,7 +89,7 @@ class Users {
$params['user_id'] = array( $params['user_id'] = array(
'required' => true, 'required' => true,
'type' => 'string', 'type' => 'string',
); );
return $params; return $params;