Cache more metadata and webfinger results
This commit is contained in:
parent
7c0b101be1
commit
8391e713c9
3 changed files with 21 additions and 10 deletions
|
@ -35,9 +35,13 @@ class Mention {
|
||||||
* @return string the final string
|
* @return string the final string
|
||||||
*/
|
*/
|
||||||
public static function replace_with_links( $result ) {
|
public static function replace_with_links( $result ) {
|
||||||
|
error_log($result[0]);
|
||||||
$metadata = \ActivityPub\get_remote_metadata_by_actor( $result[0] );
|
$metadata = \ActivityPub\get_remote_metadata_by_actor( $result[0] );
|
||||||
if ( ! is_wp_error( $metadata ) ) {
|
if ( ! is_wp_error( $metadata ) ) {
|
||||||
|
$username = ltrim( $result[0], '@');
|
||||||
|
if ( ! empty( $metadata['name'] ) ) {
|
||||||
$username = $metadata['name'];
|
$username = $metadata['name'];
|
||||||
|
}
|
||||||
if ( ! empty( $metadata['preferredUsername'] ) ) {
|
if ( ! empty( $metadata['preferredUsername'] ) ) {
|
||||||
$username = $metadata['preferredUsername'];
|
$username = $metadata['preferredUsername'];
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,9 @@ class Webfinger {
|
||||||
|
|
||||||
$url = \add_query_arg( 'resource', 'acct:' . ltrim( $account, '@' ), 'https://' . $m[1] . '/.well-known/webfinger' );
|
$url = \add_query_arg( 'resource', 'acct:' . ltrim( $account, '@' ), 'https://' . $m[1] . '/.well-known/webfinger' );
|
||||||
if ( ! \wp_http_validate_url( $url ) ) {
|
if ( ! \wp_http_validate_url( $url ) ) {
|
||||||
return new \WP_Error( 'invalid_webfinger_url', null, $url );
|
$response = new \WP_Error( 'invalid_webfinger_url', null, $url );
|
||||||
|
\set_transient( $transient_key, $response, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
|
||||||
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to access author URL
|
// try to access author URL
|
||||||
|
@ -49,6 +51,7 @@ class Webfinger {
|
||||||
array(
|
array(
|
||||||
'headers' => array( 'Accept' => 'application/activity+json' ),
|
'headers' => array( 'Accept' => 'application/activity+json' ),
|
||||||
'redirection' => 0,
|
'redirection' => 0,
|
||||||
|
'timeout' => 2,
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -58,12 +61,10 @@ class Webfinger {
|
||||||
return $link;
|
return $link;
|
||||||
}
|
}
|
||||||
|
|
||||||
$response_code = \wp_remote_retrieve_response_code( $response );
|
|
||||||
|
|
||||||
$body = \wp_remote_retrieve_body( $response );
|
$body = \wp_remote_retrieve_body( $response );
|
||||||
$body = \json_decode( $body, true );
|
$body = \json_decode( $body, true );
|
||||||
|
|
||||||
if ( ! isset( $body['links'] ) ) {
|
if ( empty( $body['links'] ) ) {
|
||||||
$link = new \WP_Error( 'webfinger_url_invalid_response', null, $url );
|
$link = new \WP_Error( 'webfinger_url_invalid_response', null, $url );
|
||||||
\set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
|
\set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
|
||||||
return $link;
|
return $link;
|
||||||
|
|
|
@ -61,14 +61,14 @@ function safe_remote_post( $url, $body, $user_id ) {
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
function safe_remote_get( $url, $user_id ) {
|
function safe_remote_get( $url, $user_id, $override_args = array() ) {
|
||||||
$date = \gmdate( 'D, d M Y H:i:s T' );
|
$date = \gmdate( 'D, d M Y H:i:s T' );
|
||||||
$signature = \Activitypub\Signature::generate_signature( $user_id, 'get', $url, $date );
|
$signature = \Activitypub\Signature::generate_signature( $user_id, 'get', $url, $date );
|
||||||
|
|
||||||
$wp_version = \get_bloginfo( 'version' );
|
$wp_version = \get_bloginfo( 'version' );
|
||||||
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
|
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
|
||||||
$args = array(
|
$args = array(
|
||||||
'timeout' => 100,
|
'timeout' => apply_filters( 'activitypub_remote_get_timeout', 100 ),
|
||||||
'limit_response_size' => 1048576,
|
'limit_response_size' => 1048576,
|
||||||
'redirection' => 3,
|
'redirection' => 3,
|
||||||
'user-agent' => "$user_agent; ActivityPub",
|
'user-agent' => "$user_agent; ActivityPub",
|
||||||
|
@ -130,7 +130,9 @@ function get_remote_metadata_by_actor( $actor ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! \wp_http_validate_url( $actor ) ) {
|
if ( ! \wp_http_validate_url( $actor ) ) {
|
||||||
return new \WP_Error( 'activitypub_no_valid_actor_url', \__( 'The "actor" is no valid URL', 'activitypub' ), $actor );
|
$metadata = new \WP_Error( 'activitypub_no_valid_actor_url', \__( 'The "actor" is no valid URL', 'activitypub' ), $actor );
|
||||||
|
\set_transient( $transient_key, $metadata, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
|
||||||
|
return $metadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
$user = \get_users(
|
$user = \get_users(
|
||||||
|
@ -143,10 +145,14 @@ function get_remote_metadata_by_actor( $actor ) {
|
||||||
|
|
||||||
// we just need any user to generate a request signature
|
// we just need any user to generate a request signature
|
||||||
$user_id = \reset( $user );
|
$user_id = \reset( $user );
|
||||||
|
$short_timeout = function() {
|
||||||
|
return 3;
|
||||||
|
};
|
||||||
|
add_filter( 'activitypub_remote_get_timeout', $short_timeout );
|
||||||
$response = \Activitypub\safe_remote_get( $actor, $user_id );
|
$response = \Activitypub\safe_remote_get( $actor, $user_id );
|
||||||
|
remove_filter( 'activitypub_remote_get_timeout', $short_timeout );
|
||||||
if ( \is_wp_error( $response ) ) {
|
if ( \is_wp_error( $response ) ) {
|
||||||
|
\set_transient( $transient_key, $response, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue