Rework inboxes for cc
This commit is contained in:
parent
05575fe6e7
commit
99b316db34
6 changed files with 61 additions and 21 deletions
|
@ -49,7 +49,7 @@ class Activity_Dispatcher {
|
|||
}
|
||||
|
||||
foreach ( $inboxes as $inbox => $to ) {
|
||||
$to = array_unique( $to );
|
||||
$to = array_values( array_unique( $to ) );
|
||||
$activitypub_activity->set_to( $to );
|
||||
$activity = $activitypub_activity->to_json();
|
||||
|
||||
|
|
|
@ -16,34 +16,33 @@ class Mention {
|
|||
}
|
||||
|
||||
/**
|
||||
* Filter to replace the #tags in the content with links
|
||||
* Filter to replace the mentions in the content with links
|
||||
*
|
||||
* @param string $the_content the post-content
|
||||
*
|
||||
* @return string the filtered post-content
|
||||
*/
|
||||
public static function the_content( $the_content ) {
|
||||
$the_content = \preg_replace_callback( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/i', array( '\Activitypub\Mention', 'replace_with_links' ), $the_content );
|
||||
$the_content = \preg_replace_callback( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/', array( '\Activitypub\Mention', 'replace_with_links' ), $the_content );
|
||||
|
||||
return $the_content;
|
||||
}
|
||||
|
||||
/**
|
||||
* A callback for preg_replace to build the term links
|
||||
* A callback for preg_replace to build the user links
|
||||
*
|
||||
* @param array $result the preg_match results
|
||||
* @return string the final string
|
||||
*/
|
||||
public static function replace_with_links( $result ) {
|
||||
$metadata = \ActivityPub\get_remote_metadata_by_actor( $result[0] );
|
||||
|
||||
if ( ! is_wp_error( $metadata ) ) {
|
||||
$username = $metadata['name'];
|
||||
if ( ! empty( $metadata['preferredUsername'] ) ) {
|
||||
$username = $metadata['preferredUsername'];
|
||||
}
|
||||
$username = '@<span>' . $username . '</span>';
|
||||
return \sprintf( '<a rel="mention" class="u-url mention href="%s">%s</a>', $metadata['url'], $username );
|
||||
return \sprintf( '<a rel="mention" class="u-url mention" href="%s">%s</a>', $metadata['url'], $username );
|
||||
}
|
||||
|
||||
return $result[0];
|
||||
|
|
|
@ -122,7 +122,8 @@ function get_remote_metadata_by_actor( $actor ) {
|
|||
return $actor;
|
||||
}
|
||||
|
||||
$metadata = \get_transient( 'activitypub_' . $actor );
|
||||
$transient_key = 'activitypub_' . $actor;
|
||||
$metadata = \get_transient( $transient_key );
|
||||
|
||||
if ( $metadata ) {
|
||||
return $metadata;
|
||||
|
@ -153,10 +154,12 @@ function get_remote_metadata_by_actor( $actor ) {
|
|||
$metadata = \json_decode( $metadata, true );
|
||||
|
||||
if ( ! $metadata ) {
|
||||
return new \WP_Error( 'activitypub_invalid_json', \__( 'No valid JSON data', 'activitypub' ), $actor );
|
||||
$metadata = new \WP_Error( 'activitypub_invalid_json', \__( 'No valid JSON data', 'activitypub' ), $actor );
|
||||
\set_transient( $transient_key, $metadata, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
|
||||
return $metadata;
|
||||
}
|
||||
|
||||
\set_transient( 'activitypub_' . $actor, $metadata, WEEK_IN_SECONDS );
|
||||
\set_transient( $transient_key, $metadata, WEEK_IN_SECONDS );
|
||||
|
||||
return $metadata;
|
||||
}
|
||||
|
|
|
@ -123,6 +123,12 @@ class Webfinger {
|
|||
if ( ! preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $account, $m ) ) {
|
||||
return null;
|
||||
}
|
||||
$transient_key = 'activitypub_resolve_' . ltrim( $account, '@' );
|
||||
|
||||
$link = \get_transient( $transient_key );
|
||||
if ( $link ) {
|
||||
return $link;
|
||||
}
|
||||
|
||||
$url = \add_query_arg( 'resource', 'acct:' . ltrim( $account, '@' ), 'https://' . $m[1] . '/.well-known/webfinger' );
|
||||
if ( ! \wp_http_validate_url( $url ) ) {
|
||||
|
@ -139,7 +145,9 @@ class Webfinger {
|
|||
);
|
||||
|
||||
if ( \is_wp_error( $response ) ) {
|
||||
return new \WP_Error( 'webfinger_url_not_accessible', null, $url );
|
||||
$link = new \WP_Error( 'webfinger_url_not_accessible', null, $url );
|
||||
\set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
|
||||
return $link;
|
||||
}
|
||||
|
||||
$response_code = \wp_remote_retrieve_response_code( $response );
|
||||
|
@ -148,15 +156,20 @@ class Webfinger {
|
|||
$body = \json_decode( $body, true );
|
||||
|
||||
if ( ! isset( $body['links'] ) ) {
|
||||
return 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.
|
||||
return $link;
|
||||
}
|
||||
|
||||
foreach ( $body['links'] as $link ) {
|
||||
if ( 'self' === $link['rel'] && 'application/activity+json' === $link['type'] ) {
|
||||
\set_transient( $transient_key, $link['href'], WEEK_IN_SECONDS );
|
||||
return $link['href'];
|
||||
}
|
||||
}
|
||||
|
||||
return new \WP_Error( 'webfinger_url_no_activity_pub', null, $body );
|
||||
$link = new \WP_Error( 'webfinger_url_no_activity_pub', null, $body );
|
||||
\set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
|
||||
return $link;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser {
|
|||
\add_action( 'friends_feed_parser_activitypub_unfollow', array( $this, 'unfollow_user' ), 10, 2 );
|
||||
\add_filter( 'friends_rewrite_incoming_url', array( $this, 'friends_rewrite_incoming_url' ), 10, 2 );
|
||||
|
||||
\add_filter( 'the_content', array( $this, 'the_content' ), 99, 2 );
|
||||
\add_filter( 'activitypub_extract_mentions', array( $this, 'activitypub_extract_mentions' ), 10, 2 );
|
||||
}
|
||||
|
||||
|
@ -410,14 +411,22 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser {
|
|||
\Activitypub\safe_remote_post( $inbox, $activity, $user_id );
|
||||
}
|
||||
|
||||
public function activitypub_extract_mentions( $mentions, \ActivityPub\Model\Post $post ) {
|
||||
$feeds = \Friends\User_Feed::get_by_parser( 'activitypub' );
|
||||
$users = array();
|
||||
foreach ( $feeds as $feed ) {
|
||||
$user = $feed->get_friend_user();
|
||||
$slug = sanitize_title( $user->user_nicename );
|
||||
$users[ '@' . $slug ] = $feed->get_url();
|
||||
public function get_possible_mentions() {
|
||||
static $users = null;
|
||||
if ( is_null( $users ) ) {
|
||||
$feeds = \Friends\User_Feed::get_by_parser( 'activitypub' );
|
||||
$users = array();
|
||||
foreach ( $feeds as $feed ) {
|
||||
$user = $feed->get_friend_user();
|
||||
$slug = sanitize_title( $user->user_nicename );
|
||||
$users[ '@' . $slug ] = $feed->get_url();
|
||||
}
|
||||
}
|
||||
return $users;
|
||||
}
|
||||
|
||||
public function activitypub_extract_mentions( $mentions, \ActivityPub\Model\Post $post ) {
|
||||
$users = $this->get_possible_mentions();
|
||||
preg_match_all( '/@(?:[a-zA-Z0-9_-]+)/', $post->get_content(), $matches );
|
||||
foreach ( $matches[0] as $match ) {
|
||||
if ( isset( $users[ $match ] ) ) {
|
||||
|
@ -426,4 +435,20 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser {
|
|||
}
|
||||
return $mentions;
|
||||
}
|
||||
|
||||
|
||||
public function the_content( $the_content ) {
|
||||
$the_content = \preg_replace_callback( '/@(?:[a-zA-Z0-9_-]+)/', array( $this, 'replace_with_links' ), $the_content );
|
||||
|
||||
return $the_content;
|
||||
}
|
||||
|
||||
public function replace_with_links( $result ) {
|
||||
$users = $this->get_possible_mentions();
|
||||
if ( isset( $users[ $result[0] ] ) ) {
|
||||
return \Activitypub\Mention::replace_with_links( array( $users[ $result[0] ] ) );
|
||||
}
|
||||
|
||||
return $result[0];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,8 +19,8 @@ class Test_Activitypub_Mention extends ActivityPub_TestCase_Cache_HTTP {
|
|||
|
||||
public function the_content_provider() {
|
||||
return array(
|
||||
array( 'hallo @username@example.org test', 'hallo <a rel="mention" class="u-url mention href="https://example.org/users/username">@<span>username</span></a> test' ),
|
||||
array( 'hallo @pfefferle@notiz.blog test', 'hallo <a rel="mention" class="u-url mention href="https://notiz.blog/author/matthias-pfefferle/">@<span>pfefferle</span></a> test' ),
|
||||
array( 'hallo @username@example.org test', 'hallo <a rel="mention" class="u-url mention" href="https://example.org/users/username">@<span>username</span></a> test' ),
|
||||
array( 'hallo @pfefferle@notiz.blog test', 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@<span>pfefferle</span></a> test' ),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue