From 129915e4051d89e68293e8fa8e0005111b5f8190 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Thu, 13 Feb 2025 13:07:46 +0100 Subject: [PATCH] Fix unfollowing in Enable Mastodon Apps (#471) --- includes/class-user.php | 18 ++++++++++++++-- integrations/class-enable-mastodon-apps.php | 24 +++++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/includes/class-user.php b/includes/class-user.php index 0ab6345f..9ef68fd6 100644 --- a/includes/class-user.php +++ b/includes/class-user.php @@ -1469,13 +1469,27 @@ public static function mastodon_api_get_posts_query_args( $args ) { } public static function mastodon_entity_relationship( $relationship, $user_id ) { - $user = Feed_Parser_ActivityPub::determine_mastodon_api_user( $user_id ); + if ( ! class_exists( 'Friends\Feed_Parser_ActivityPub' ) ) { + if ( ! is_wp_error( $user_id ) ) { + $user = User::get_user_by_id( $user_id ); + if ( ! $user ) { + $user = User::get_user_by_id( 'friends-virtual-user-' . $user_id ); + } + } + } else { + $user = Feed_Parser_ActivityPub::determine_mastodon_api_user( $user_id ); + } if ( $user instanceof self ) { if ( ! $relationship instanceof \Enable_Mastodon_Apps\Entity\Relationship ) { $relationship = new \Enable_Mastodon_Apps\Entity\Relationship(); } + foreach ( $user->get_active_feeds() as $feed ) { + if ( Feed_Parser_ActivityPub::SLUG === $feed->get_parser() ) { + $relationship->following = true; + break; + } + } - $relationship->following = true; if ( $user->has_cap( 'friend' ) ) { $relationship->followed_by = true; } diff --git a/integrations/class-enable-mastodon-apps.php b/integrations/class-enable-mastodon-apps.php index acd21c61..a21d4025 100644 --- a/integrations/class-enable-mastodon-apps.php +++ b/integrations/class-enable-mastodon-apps.php @@ -16,14 +16,34 @@ public static function init() { add_filter( 'mastodon_api_account', array( 'Friends\User', 'mastodon_api_account' ), 8, 4 ); add_filter( 'mastodon_api_get_posts_query_args', array( 'Friends\User', 'mastodon_api_get_posts_query_args' ) ); add_filter( 'mastodon_entity_relationship', array( 'Friends\User', 'mastodon_entity_relationship' ), 10, 2 ); - add_action( 'mastodon_api_account_follow', array( get_called_class(), 'mastodon_api_account_follow' ), 10, 1 ); + add_filter( 'mastodon_api_account_follow', array( get_called_class(), 'mastodon_api_account_follow' ), 10, 1 ); + add_action( 'mastodon_api_account_unfollow', array( get_called_class(), 'mastodon_api_account_unfollow' ), 10, 1 ); add_filter( 'mastodon_api_timelines_args', array( get_called_class(), 'mastodon_api_timelines_args' ) ); add_filter( 'mastodon_api_account_statuses_args', array( get_called_class(), 'mastodon_api_timelines_args' ) ); add_filter( 'mastodon_api_view_post_types', array( get_called_class(), 'mastodon_api_view_post_types' ) ); } public static function mastodon_api_account_follow( $user_id ) { - return apply_filters( 'friends_create_and_follow', null, $user_id ); + $user = User::get_user_by_id( $user_id ); + if ( ! $user ) { + return apply_filters( 'friends_create_and_follow', null, $user_id ); + } + foreach ( $user->get_feeds() as $feed ) { + if ( $feed->is_active() ) { + continue; + } + if ( class_exists( 'Friends\Feed_Parser_ActivityPub' ) && Feed_Parser_ActivityPub::SLUG === $feed->get_parser() ) { + $feed->activate(); + } + } + return $user_id; + } + + public static function mastodon_api_account_unfollow( $user_id ) { + $user = User::get_user_by_id( $user_id ); + foreach ( $user->get_active_feeds() as $feed ) { + $feed->deactivate(); + } } public static function mastodon_api_timelines_args( $args ) {