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 ) {
|
foreach ( $inboxes as $inbox => $to ) {
|
||||||
$to = array_unique( $to );
|
$to = array_values( array_unique( $to ) );
|
||||||
$activitypub_activity->set_to( $to );
|
$activitypub_activity->set_to( $to );
|
||||||
$activity = $activitypub_activity->to_json();
|
$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
|
* @param string $the_content the post-content
|
||||||
*
|
*
|
||||||
* @return string the filtered post-content
|
* @return string the filtered post-content
|
||||||
*/
|
*/
|
||||||
public static function the_content( $the_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;
|
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
|
* @param array $result the preg_match results
|
||||||
* @return string the final string
|
* @return string the final string
|
||||||
*/
|
*/
|
||||||
public static function replace_with_links( $result ) {
|
public static function replace_with_links( $result ) {
|
||||||
$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 = $metadata['name'];
|
$username = $metadata['name'];
|
||||||
if ( ! empty( $metadata['preferredUsername'] ) ) {
|
if ( ! empty( $metadata['preferredUsername'] ) ) {
|
||||||
$username = $metadata['preferredUsername'];
|
$username = $metadata['preferredUsername'];
|
||||||
}
|
}
|
||||||
$username = '@<span>' . $username . '</span>';
|
$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];
|
return $result[0];
|
||||||
|
|
|
@ -122,7 +122,8 @@ function get_remote_metadata_by_actor( $actor ) {
|
||||||
return $actor;
|
return $actor;
|
||||||
}
|
}
|
||||||
|
|
||||||
$metadata = \get_transient( 'activitypub_' . $actor );
|
$transient_key = 'activitypub_' . $actor;
|
||||||
|
$metadata = \get_transient( $transient_key );
|
||||||
|
|
||||||
if ( $metadata ) {
|
if ( $metadata ) {
|
||||||
return $metadata;
|
return $metadata;
|
||||||
|
@ -153,10 +154,12 @@ function get_remote_metadata_by_actor( $actor ) {
|
||||||
$metadata = \json_decode( $metadata, true );
|
$metadata = \json_decode( $metadata, true );
|
||||||
|
|
||||||
if ( ! $metadata ) {
|
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;
|
return $metadata;
|
||||||
}
|
}
|
||||||
|
|
|
@ -123,6 +123,12 @@ class Webfinger {
|
||||||
if ( ! preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $account, $m ) ) {
|
if ( ! preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $account, $m ) ) {
|
||||||
return null;
|
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' );
|
$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 ) ) {
|
||||||
|
@ -139,7 +145,9 @@ class Webfinger {
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( \is_wp_error( $response ) ) {
|
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 );
|
$response_code = \wp_remote_retrieve_response_code( $response );
|
||||||
|
@ -148,15 +156,20 @@ class Webfinger {
|
||||||
$body = \json_decode( $body, true );
|
$body = \json_decode( $body, true );
|
||||||
|
|
||||||
if ( ! isset( $body['links'] ) ) {
|
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 ) {
|
foreach ( $body['links'] as $link ) {
|
||||||
if ( 'self' === $link['rel'] && 'application/activity+json' === $link['type'] ) {
|
if ( 'self' === $link['rel'] && 'application/activity+json' === $link['type'] ) {
|
||||||
|
\set_transient( $transient_key, $link['href'], WEEK_IN_SECONDS );
|
||||||
return $link['href'];
|
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_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( '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 );
|
\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 );
|
\Activitypub\safe_remote_post( $inbox, $activity, $user_id );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function activitypub_extract_mentions( $mentions, \ActivityPub\Model\Post $post ) {
|
public function get_possible_mentions() {
|
||||||
$feeds = \Friends\User_Feed::get_by_parser( 'activitypub' );
|
static $users = null;
|
||||||
$users = array();
|
if ( is_null( $users ) ) {
|
||||||
foreach ( $feeds as $feed ) {
|
$feeds = \Friends\User_Feed::get_by_parser( 'activitypub' );
|
||||||
$user = $feed->get_friend_user();
|
$users = array();
|
||||||
$slug = sanitize_title( $user->user_nicename );
|
foreach ( $feeds as $feed ) {
|
||||||
$users[ '@' . $slug ] = $feed->get_url();
|
$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 );
|
preg_match_all( '/@(?:[a-zA-Z0-9_-]+)/', $post->get_content(), $matches );
|
||||||
foreach ( $matches[0] as $match ) {
|
foreach ( $matches[0] as $match ) {
|
||||||
if ( isset( $users[ $match ] ) ) {
|
if ( isset( $users[ $match ] ) ) {
|
||||||
|
@ -426,4 +435,20 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser {
|
||||||
}
|
}
|
||||||
return $mentions;
|
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() {
|
public function the_content_provider() {
|
||||||
return array(
|
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 @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 @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