Account for local urls with an @

This commit is contained in:
Alex Kirk 2022-12-13 11:09:45 +01:00
parent 7ef6cdb7ee
commit 738208b70d
2 changed files with 20 additions and 10 deletions

View file

@ -169,14 +169,14 @@ add_filter(
$username = $m[1]; $username = $m[1];
$domain = $m[2]; $domain = $m[2];
} else { } else {
$p = parse_url( $actor ); $p = wp_parse_url( $actor );
if ( $p ) { if ( $p ) {
if ( isset( $p['host'] ) ) { if ( isset( $p['host'] ) ) {
$domain = $p['host']; $domain = $p['host'];
} }
if ( isset( $p['path'] ) ) { if ( isset( $p['path'] ) ) {
$path_parts = explode( '/', trim( $p['path'], '/' ) ); $path_parts = explode( '/', trim( $p['path'], '/' ) );
$username = array_pop( $path_parts ); $username = ltrim( array_pop( $path_parts ), '@' );
} }
} }
} }

View file

@ -9,20 +9,30 @@ class Test_Functions extends ActivityPub_TestCase_Cache_HTTP {
$this->assertEquals( 'https://notiz.blog/author/matthias-pfefferle/', $metadata['url'] ); $this->assertEquals( 'https://notiz.blog/author/matthias-pfefferle/', $metadata['url'] );
$this->assertEquals( 'pfefferle', $metadata['preferredUsername'] ); $this->assertEquals( 'pfefferle', $metadata['preferredUsername'] );
$this->assertEquals( 'Matthias Pfefferle', $metadata['name'] ); $this->assertEquals( 'Matthias Pfefferle', $metadata['name'] );
}
/**
* @dataProvider example_actors
*/
public function test_get_example_metadata_by_actor( $actor, $domain, $username ) {
add_filter( 'pre_http_request', array( $this, 'invalid_http_response' ), 8, 3 ); add_filter( 'pre_http_request', array( $this, 'invalid_http_response' ), 8, 3 );
$metadata = \ActivityPub\get_remote_metadata_by_actor( $actor );
$this->assertEquals( sprintf( 'https://%s/users/%s/', $domain, $username ), $metadata['url'], $actor );
$this->assertEquals( $username, $metadata['name'], $actor );
remove_filter( 'pre_http_request', array( $this, 'invalid_http_response' ), 8 );
}
public function example_actors() {
$actors = array();
foreach ( array( 'user', 'test' ) as $username ) { foreach ( array( 'user', 'test' ) as $username ) {
foreach ( array( 'example.org', 'example.net', 'example2.com' ) as $domain ) { foreach ( array( 'example.org', 'example.net', 'example2.com' ) as $domain ) {
foreach ( array( '@', '' ) as $leading_at ) { foreach ( array( '@', '' ) as $leading_at ) {
$metadata = \ActivityPub\get_remote_metadata_by_actor( $leading_at . $username . '@' . $domain ); $actors[] = array( $leading_at . $username . '@' . $domain, $domain, $username );
$this->assertEquals( sprintf( 'https://%s/users/%s/', $domain, $username ), $metadata['url'], $username . '@' . $domain );
$this->assertEquals( $username, $metadata['name'], $username . '@' . $domain );
} }
$metadata = \ActivityPub\get_remote_metadata_by_actor( sprintf( 'https://%s/users/%s/', $domain, $username ) ); $actors[] = array( sprintf( 'https://%s/users/%s/', $domain, $username ), $domain, $username );
$this->assertEquals( sprintf( 'https://%s/users/%s/', $domain, $username ), $metadata['url'], $username . '@' . $domain ); $actors[] = array( sprintf( 'https://%s/users/%s', $domain, $username ), $domain, $username );
$this->assertEquals( $username, $metadata['name'], $username . '@' . $domain ); $actors[] = array( sprintf( 'https://%s/@%s', $domain, $username ), $domain, $username );
} }
} }
remove_filter( 'pre_http_request', array( $this, 'invalid_http_response' ), 8 ); return $actors;
} }
} }