Skip to content

Commit

Permalink
Add local users to potential mentions so that it won't be trashed (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk authored Mar 15, 2024
1 parent 821f83d commit 1a17dc2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
43 changes: 32 additions & 11 deletions feed-parsers/class-feed-parser-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,9 @@ private function disable_polling( User_Feed $user_feed ) {
*/
public function get_external_mentions_user() {
$external_mentions_username = apply_filters( 'friends_external_mentions_username', self::EXTERNAL_MENTIONS_USERNAME );
$user = get_user_by( 'login', $external_mentions_username );
if ( ! $user ) {
$user = User::get_by_username( $external_mentions_username );
if ( ! $user || is_wp_error( $user ) ) {
$user = Subscription::create( $external_mentions_username, 'subscription', home_url(), __( 'External Mentions', 'friends' ) );
} else {
$user = User::get_by_username( $external_mentions_username );
}

if ( $user instanceof \WP_User && ! ( $user instanceof Subscription ) && ! is_user_member_of_blog( $user->ID, get_current_blog_id() ) ) {
Expand Down Expand Up @@ -1136,7 +1134,7 @@ public function activitypub_unfollow_user( $url, $user_id = null ) {
}
}

public function get_possible_mentions() {
public static function get_possible_mentions() {
static $users = null;
if ( ! method_exists( '\Friends\User_Feed', 'get_by_parser' ) ) {
return array();
Expand All @@ -1147,9 +1145,28 @@ public function get_possible_mentions() {
$users = array();
foreach ( $feeds as $feed ) {
$user = $feed->get_friend_user();
$slug = sanitize_title( $user->user_nicename );
$slug = $user->user_nicename;
if ( ! $slug ) {
$slug = $user->user_login;
}
$slug = sanitize_title( $slug );
$users[ '@' . $slug ] = $feed->get_url();
}

$local_users = get_users(
array(
'fields' => array( 'ID', 'user_nicename', 'user_login' ),
)
);
foreach ( $local_users as $local_user ) {
$slug = $local_user->user_nicename;
if ( ! $slug ) {
$slug = $local_user->user_login;
}
$slug = sanitize_title( $slug );
$users[ '@' . $slug ] = get_author_posts_url( $local_user->ID );
}
$users[ '@' . sanitize_title( get_bloginfo( 'name' ) ) ] = get_bloginfo( 'url' );
}
return $users;
}
Expand All @@ -1162,7 +1179,7 @@ public function get_possible_mentions() {
* @return mixed The discovered mentions.
*/
public function activitypub_extract_mentions( $mentions, $post_content ) {
$users = $this->get_possible_mentions();
$users = self::get_possible_mentions();
preg_match_all( '/@(?:[a-zA-Z0-9_-]+)/', $post_content, $matches );
foreach ( $matches[0] as $match ) {
if ( isset( $users[ $match ] ) ) {
Expand Down Expand Up @@ -1200,7 +1217,7 @@ function ( $m ) use ( &$protected_tags ) {
* @return string The replaced username.
*/
public function replace_with_links( array $result ) {
$users = $this->get_possible_mentions();
$users = self::get_possible_mentions();
if ( ! isset( $users[ $result[0] ] ) ) {
return $result[0];
}
Expand Down Expand Up @@ -1273,13 +1290,17 @@ public function activitypub_settings( User $friend ) {
* @return Feed_Item The modified feed item.
*/
public function modify_incoming_item( Feed_Item $item, User_Feed $feed = null, User $friend_user = null ) {
if ( ! $feed || 'activitypub' !== $feed->get_parser() ) {
if ( ! $feed || 'activitypub' !== $feed->get_parser() || ! $friend_user ) {
return $item;
}

if ( ! $friend_user->get_user_option( 'activitypub_friends_show_replies' ) ) {
$external_mentions_user = $this->get_external_mentions_user();
if (
$external_mentions_user->get_object_id() !== $friend_user->get_object_id() && // Don't hide mentions for the external mentions user.
! $friend_user->get_user_option( 'activitypub_friends_show_replies' )
) {
$plain_text_content = \wp_strip_all_tags( $item->post_content );
$possible_mentions = $this->get_possible_mentions();
$possible_mentions = self::get_possible_mentions();

$no_known_user_found = true;
if ( preg_match( '/^@(?:[a-zA-Z0-9_.-]+)/i', $plain_text_content, $m ) ) {
Expand Down
9 changes: 9 additions & 0 deletions tests/test-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ public function test_incoming_announce() {
$this->assertEquals( 'Matthias Pfefferle', get_post_meta( $posts[0]->ID, 'author', true ) );
}

public function test_possible_mentions() {
add_filter( 'activitypub_cache_possible_friend_mentions', '__return_false' );
$mentions = \Friends\Feed_Parser_ActivityPub::get_possible_mentions();
$this->assertContains( \get_author_posts_url( get_current_user_id() ), $mentions );

remove_all_filters( 'activitypub_from_post_object' );
remove_all_filters( 'activitypub_cache_possible_friend_mentions' );
}

public function test_friend_mentions() {
add_filter( 'activitypub_cache_possible_friend_mentions', '__return_false' );
$post_id = \wp_insert_post(
Expand Down

0 comments on commit 1a17dc2

Please sign in to comment.