2023-04-21 14:56:22 +02:00
< ? php
namespace Activitypub\Collection ;
use WP_Error ;
2023-06-15 11:47:50 +02:00
use WP_Query ;
2023-04-28 11:23:40 +02:00
use Activitypub\Http ;
2023-04-21 14:56:22 +02:00
use Activitypub\Webfinger ;
2023-04-24 20:46:51 +02:00
use Activitypub\Model\Follower ;
2023-04-21 14:56:22 +02:00
2023-05-02 14:39:25 +02:00
use function Activitypub\is_tombstone ;
2023-04-27 09:57:50 +02:00
use function Activitypub\get_remote_metadata_by_actor ;
2023-04-21 14:56:22 +02:00
/**
* ActivityPub Followers Collection
*
2023-06-12 18:38:15 +02:00
* @ author Matt Wiebe
2023-04-21 14:56:22 +02:00
* @ author Matthias Pfefferle
*/
class Followers {
2023-06-20 09:51:13 +02:00
const POST_TYPE = 'ap_follower' ;
2023-05-10 09:04:33 +02:00
const CACHE_KEY_INBOXES = 'follower_inboxes_%s' ;
2023-04-21 14:56:22 +02:00
/**
2023-05-09 10:08:51 +02:00
* Add new Follower
2023-04-21 14:56:22 +02:00
*
2023-04-25 09:09:07 +02:00
* @ param int $user_id The ID of the WordPress User
2023-04-21 15:57:49 +02:00
* @ param string $actor The Actor URL
2023-04-25 09:09:07 +02:00
*
2023-09-05 21:03:25 +02:00
* @ return array | WP_Error The Follower ( WP_Post array ) or an WP_Error
2023-04-21 14:56:22 +02:00
*/
public static function add_follower ( $user_id , $actor ) {
2023-04-27 09:57:50 +02:00
$meta = get_remote_metadata_by_actor ( $actor );
2023-07-05 15:34:22 +02:00
if ( is_tombstone ( $meta ) ) {
2023-05-03 14:50:16 +02:00
return $meta ;
2023-04-27 09:57:50 +02:00
}
2023-09-05 21:03:25 +02:00
if ( empty ( $meta ) || ! is_array ( $meta ) || is_wp_error ( $meta ) ) {
2023-09-21 09:03:24 +02:00
return new WP_Error ( 'activitypub_invalid_follower' , __ ( 'Invalid Follower' , 'activitypub' ), array ( 'status' => 400 ) );
2023-09-05 21:03:25 +02:00
}
2023-07-06 14:42:18 +02:00
$follower = new Follower ();
2023-09-05 21:03:25 +02:00
$follower -> from_array ( $meta );
2023-07-06 14:42:18 +02:00
2023-12-25 21:04:33 +01:00
// Save the follower to the internal post type or update if it's ActivityPub ID is already known.
$follower_id = $follower -> upsert ();
2023-07-06 14:42:18 +02:00
2023-12-25 21:04:33 +01:00
if ( is_wp_error ( $follower_id ) ) {
return $follower_id ;
2023-09-05 21:03:25 +02:00
}
2023-04-21 14:56:22 +02:00
2023-12-25 21:04:33 +01:00
$post_meta = get_post_meta ( $follower_id , 'activitypub_user_id' );
2023-06-15 11:47:50 +02:00
2023-07-13 10:35:15 +02:00
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
2023-12-25 21:04:33 +01:00
// Check if the follow relationship is already present.
2023-10-21 11:23:05 +02:00
if ( is_array ( $post_meta ) && ! in_array ( $user_id , $post_meta ) ) {
2023-12-25 21:04:33 +01:00
// Actually save the follow relationship
add_post_meta ( $follower_id , 'activitypub_user_id' , $user_id );
// Reset the cached inboxes for the followed user
2023-06-19 11:04:45 +02:00
wp_cache_delete ( sprintf ( self :: CACHE_KEY_INBOXES , $user_id ), 'activitypub' );
}
2023-06-15 11:47:50 +02:00
return $follower ;
2023-04-21 14:56:22 +02:00
}
2023-04-24 20:46:51 +02:00
/**
* Remove a Follower
*
2023-04-26 17:24:27 +02:00
* @ param int $user_id The ID of the WordPress User
* @ param string $actor The Actor URL
2023-04-25 09:09:07 +02:00
*
* @ return bool | WP_Error True on success , false or WP_Error on failure .
2023-04-24 20:46:51 +02:00
*/
2023-04-21 16:25:15 +02:00
public static function remove_follower ( $user_id , $actor ) {
2023-05-10 09:04:33 +02:00
wp_cache_delete ( sprintf ( self :: CACHE_KEY_INBOXES , $user_id ), 'activitypub' );
2023-06-15 11:47:50 +02:00
2023-06-16 16:46:49 +02:00
$follower = self :: get_follower ( $user_id , $actor );
if ( ! $follower ) {
return false ;
}
2023-07-06 14:42:18 +02:00
return delete_post_meta ( $follower -> get__id (), 'activitypub_user_id' , $user_id );
2023-04-21 16:25:15 +02:00
}
2023-04-21 15:57:49 +02:00
/**
2023-11-30 11:43:48 +01:00
* Get a Follower .
2023-04-24 20:46:51 +02:00
*
2023-06-15 12:13:30 +02:00
* @ param int $user_id The ID of the WordPress User
* @ param string $actor The Actor URL
2023-04-25 09:09:07 +02:00
*
2023-11-30 11:43:48 +01:00
* @ return \Activitypub\Model\Follower | null The Follower object or null
2023-04-24 20:46:51 +02:00
*/
2023-06-15 12:13:30 +02:00
public static function get_follower ( $user_id , $actor ) {
2023-06-15 11:47:50 +02:00
global $wpdb ;
2023-11-30 11:43:48 +01:00
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
2023-06-15 11:47:50 +02:00
$post_id = $wpdb -> get_var (
$wpdb -> prepare (
2023-07-06 14:42:18 +02:00
" SELECT DISTINCT p.ID FROM $wpdb->posts p INNER JOIN $wpdb->postmeta pm ON p.ID = pm.post_id WHERE p.post_type = %s AND pm.meta_key = 'activitypub_user_id' AND pm.meta_value = %d AND p.guid = %s " ,
2023-06-16 16:46:49 +02:00
array (
esc_sql ( self :: POST_TYPE ),
esc_sql ( $user_id ),
esc_sql ( $actor ),
)
2023-06-15 11:47:50 +02:00
)
);
if ( $post_id ) {
$post = get_post ( $post_id );
2023-07-06 14:42:18 +02:00
return Follower :: init_from_cpt ( $post );
2023-06-15 11:47:50 +02:00
}
return null ;
2023-04-24 20:46:51 +02:00
}
/**
2023-11-30 11:43:48 +01:00
* Get a Follower by Actor indepenent from the User .
2023-04-21 15:57:49 +02:00
*
2023-11-30 11:43:48 +01:00
* @ param string $actor The Actor URL .
2023-04-25 09:09:07 +02:00
*
2023-11-30 11:43:48 +01:00
* @ return \Activitypub\Model\Follower | null The Follower object or null
2023-04-21 15:57:49 +02:00
*/
2023-11-30 11:43:48 +01:00
public static function get_follower_by_actor ( $actor ) {
global $wpdb ;
2023-04-24 20:46:51 +02:00
2023-11-30 11:43:48 +01:00
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$post_id = $wpdb -> get_var (
$wpdb -> prepare (
" SELECT ID FROM $wpdb->posts WHERE guid=%s " ,
esc_sql ( $actor )
2023-08-11 09:24:45 +02:00
)
);
2023-07-06 16:10:48 +02:00
2023-11-30 11:43:48 +01:00
if ( $post_id ) {
$post = get_post ( $post_id );
return Follower :: init_from_cpt ( $post );
}
2023-04-21 14:56:22 +02:00
2023-11-30 11:43:48 +01:00
return null ;
2023-04-21 14:56:22 +02:00
}
2023-04-21 15:57:49 +02:00
/**
* Get the Followers of a given user
*
2023-07-26 22:05:41 +02:00
* @ param int $user_id The ID of the WordPress User .
* @ param int $number Maximum number of results to return .
* @ param int $page Page number .
* @ param array $args The WP_Query arguments .
* @ return array List of `Follower` objects .
2023-04-21 15:57:49 +02:00
*/
2023-07-07 13:43:12 +02:00
public static function get_followers ( $user_id , $number = - 1 , $page = null , $args = array () ) {
2023-07-26 22:05:41 +02:00
$data = self :: get_followers_with_count ( $user_id , $number , $page , $args );
return $data [ 'followers' ];
}
/**
* Get the Followers of a given user , along with a total count for pagination purposes .
*
* @ param int $user_id The ID of the WordPress User .
* @ param int $number Maximum number of results to return .
* @ param int $page Page number .
* @ param array $args The WP_Query arguments .
*
* @ return array
* followers List of `Follower` objects .
* total Total number of followers .
*/
public static function get_followers_with_count ( $user_id , $number = - 1 , $page = null , $args = array () ) {
2023-05-02 14:39:25 +02:00
$defaults = array (
2023-06-15 11:47:50 +02:00
'post_type' => self :: POST_TYPE ,
'posts_per_page' => $number ,
2023-07-06 14:42:18 +02:00
'paged' => $page ,
2023-06-15 11:47:50 +02:00
'orderby' => 'ID' ,
'order' => 'DESC' ,
2023-07-28 10:28:55 +02:00
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
2023-06-15 11:47:50 +02:00
'meta_query' => array (
array (
2023-07-06 14:42:18 +02:00
'key' => 'activitypub_user_id' ,
2023-06-15 11:47:50 +02:00
'value' => $user_id ,
),
),
2023-04-21 14:56:22 +02:00
);
2023-07-26 22:05:41 +02:00
$args = wp_parse_args ( $args , $defaults );
2023-06-12 18:38:15 +02:00
$query = new WP_Query ( $args );
2023-07-26 22:05:41 +02:00
$total = $query -> found_posts ;
$followers = array_map (
2023-12-07 12:30:44 +01:00
function ( $post ) {
2023-07-26 22:05:41 +02:00
return Follower :: init_from_cpt ( $post );
},
$query -> get_posts ()
);
return compact ( 'followers' , 'total' );
2023-04-21 14:56:22 +02:00
}
2023-05-22 10:58:13 +02:00
/**
* Get all Followers
*
2023-06-16 11:40:26 +02:00
* @ param array $args The WP_Query arguments .
2023-05-22 10:58:13 +02:00
*
* @ return array The Term list of Followers .
*/
2023-07-06 14:42:18 +02:00
public static function get_all_followers () {
2023-06-12 18:38:15 +02:00
$args = array (
2023-12-04 10:02:30 +01:00
'nopaging' => true ,
2023-07-28 10:28:55 +02:00
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
2023-09-21 16:26:17 +02:00
'meta_query' => array (
'relation' => 'AND' ,
array (
'key' => 'activitypub_inbox' ,
'compare' => 'EXISTS' ,
),
array (
'key' => 'activitypub_actor_json' ,
'compare' => 'EXISTS' ,
),
),
2023-05-22 10:58:13 +02:00
);
2023-07-06 14:42:18 +02:00
return self :: get_followers ( null , null , null , $args );
2023-05-22 10:58:13 +02:00
}
2023-04-21 15:57:49 +02:00
/**
* Count the total number of followers
*
2023-04-25 09:09:07 +02:00
* @ param int $user_id The ID of the WordPress User
*
* @ return int The number of Followers
2023-04-21 15:57:49 +02:00
*/
2023-06-16 11:40:26 +02:00
public static function count_followers ( $user_id ) {
2023-06-16 16:56:30 +02:00
$query = new WP_Query (
array (
'post_type' => self :: POST_TYPE ,
'fields' => 'ids' ,
2023-07-28 10:28:55 +02:00
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
2023-06-16 16:56:30 +02:00
'meta_query' => array (
2023-09-21 16:26:17 +02:00
'relation' => 'AND' ,
2023-06-16 16:56:30 +02:00
array (
2023-07-06 14:42:18 +02:00
'key' => 'activitypub_user_id' ,
2023-06-16 16:56:30 +02:00
'value' => $user_id ,
),
2023-09-21 16:26:17 +02:00
array (
'key' => 'activitypub_inbox' ,
'compare' => 'EXISTS' ,
),
array (
'key' => 'activitypub_actor_json' ,
'compare' => 'EXISTS' ,
),
2023-06-16 16:56:30 +02:00
),
)
);
return $query -> found_posts ;
2023-04-21 14:56:22 +02:00
}
2023-12-25 21:04:33 +01:00
2023-04-25 09:09:07 +02:00
/**
* Returns all Inboxes fo a Users Followers
*
* @ param int $user_id The ID of the WordPress User
*
* @ return array The list of Inboxes
*/
2023-04-24 20:46:51 +02:00
public static function get_inboxes ( $user_id ) {
2023-05-09 10:28:23 +02:00
$cache_key = sprintf ( self :: CACHE_KEY_INBOXES , $user_id );
2023-12-25 21:04:33 +01:00
// $inboxes = wp_cache_get( $cache_key, 'activitypub' );
2023-05-09 10:28:23 +02:00
2023-12-25 21:04:33 +01:00
// if ( $inboxes ) {
// return $inboxes;
// }
2023-05-09 10:28:23 +02:00
2023-04-25 09:09:07 +02:00
// get all Followers of a ID of the WordPress User
2023-12-25 21:04:33 +01:00
$follower_query = new WP_Query (
2023-04-24 20:46:51 +02:00
array (
2023-12-02 10:58:19 +01:00
'nopaging' => true ,
2023-06-15 11:47:50 +02:00
'post_type' => self :: POST_TYPE ,
2023-04-24 20:46:51 +02:00
'fields' => 'ids' ,
2023-07-28 10:28:55 +02:00
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
2023-05-02 14:39:25 +02:00
'meta_query' => array (
2023-09-21 16:26:17 +02:00
'relation' => 'AND' ,
2023-05-02 14:39:25 +02:00
array (
2023-07-06 14:42:18 +02:00
'key' => 'activitypub_inbox' ,
2023-05-02 14:39:25 +02:00
'compare' => 'EXISTS' ,
),
2023-06-15 11:53:07 +02:00
array (
2023-07-06 14:42:18 +02:00
'key' => 'activitypub_user_id' ,
2023-06-15 11:53:07 +02:00
'value' => $user_id ,
),
2023-09-21 16:26:17 +02:00
array (
'key' => 'activitypub_inbox' ,
'value' => '' ,
'compare' => '!=' ,
),
2023-05-02 14:39:25 +02:00
),
2023-04-24 20:46:51 +02:00
)
);
2023-12-25 21:04:33 +01:00
$follower_ids = $follower_query -> get_posts ();
2023-04-24 20:46:51 +02:00
2023-12-25 21:04:33 +01:00
if ( ! $follower_ids ) {
2023-04-25 09:31:28 +02:00
return array ();
}
2023-12-25 21:04:33 +01:00
$user = Users :: get_by_id ( $user_id );
if ( $user -> get_manually_approved_followers () ) {
$accepted_follow_requests_query = new WP_Query (
array (
'nopaging' => true ,
'post_type' => 'ap_follow_request' ,
'fields' => 'id=>parent' ,
'post_status' => 'publish' ,
'post_parent__in' => $follower_ids ,
// phpcs:ign ore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
'meta_query' => array (
'relation' => 'AND' ,
array (
'key' => 'activitypub_user_id' ,
'value' => $user_id ,
),
),
)
);
}
$accepted_follow_requests = $accepted_follow_requests_query -> get_posts ();
2023-04-24 20:46:51 +02:00
global $wpdb ;
2023-07-28 10:28:55 +02:00
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
2023-04-24 20:46:51 +02:00
$results = $wpdb -> get_col (
$wpdb -> prepare (
2023-06-16 11:40:26 +02:00
" SELECT DISTINCT meta_value FROM { $wpdb -> postmeta }
2023-12-25 21:04:33 +01:00
WHERE post_id IN ( " . implode( ', ', array_fill( 0, count( $follower_ids ), '%d' ) ) . " )
2023-07-06 14:42:18 +02:00
AND meta_key = 'activitypub_inbox'
2023-04-24 20:46:51 +02:00
AND meta_value IS NOT NULL " ,
2023-12-25 21:04:33 +01:00
$follower_ids
2023-04-24 20:46:51 +02:00
)
);
2023-12-25 21:04:33 +01:00
2023-05-09 10:28:23 +02:00
$inboxes = array_filter ( $results );
2023-05-10 09:04:33 +02:00
wp_cache_set ( $cache_key , $inboxes , 'activitypub' );
2023-05-09 10:28:23 +02:00
return $inboxes ;
2023-04-24 20:46:51 +02:00
}
2023-05-10 14:18:56 +02:00
/**
2023-05-10 14:55:10 +02:00
* Get all Followers that have not been updated for a given time
2023-05-10 14:18:56 +02:00
*
2023-05-10 14:55:10 +02:00
* @ param enum $output The output format , supported ARRAY_N , OBJECT and ACTIVITYPUB_OBJECT .
* @ param int $number Limits the result .
* @ param int $older_than The time in seconds .
*
* @ return mixed The Term list of Followers , the format depends on $output .
2023-05-10 14:18:56 +02:00
*/
2023-07-28 10:34:10 +02:00
public static function get_outdated_followers ( $number = 50 , $older_than = 86400 ) {
2023-05-10 14:18:56 +02:00
$args = array (
2023-06-15 12:17:48 +02:00
'post_type' => self :: POST_TYPE ,
'posts_per_page' => $number ,
'orderby' => 'modified' ,
2023-09-21 16:26:17 +02:00
'order' => 'ASC' ,
2023-06-16 11:40:26 +02:00
'post_status' => 'any' , // 'any' includes 'trash
'date_query' => array (
2023-05-10 14:18:56 +02:00
array (
2023-06-15 12:17:48 +02:00
'column' => 'post_modified_gmt' ,
2023-06-16 11:40:26 +02:00
'before' => gmdate ( 'Y-m-d' , \time () - $older_than ),
2023-05-10 14:18:56 +02:00
),
),
);
2023-06-15 12:17:48 +02:00
$posts = new WP_Query ( $args );
2023-05-10 14:18:56 +02:00
$items = array ();
2023-06-15 12:17:48 +02:00
foreach ( $posts -> get_posts () as $follower ) {
2023-07-06 14:42:18 +02:00
$items [] = Follower :: init_from_cpt ( $follower ); // phpcs:ignore
2023-05-10 14:18:56 +02:00
}
2023-05-11 09:46:26 +02:00
return $items ;
2023-05-10 14:18:56 +02:00
}
2023-05-10 14:55:10 +02:00
/**
* Get all Followers that had errors
*
* @ param enum $output The output format , supported ARRAY_N , OBJECT and ACTIVITYPUB_OBJECT
* @ param integer $number The number of Followers to return .
*
* @ return mixed The Term list of Followers , the format depends on $output .
*/
2023-09-21 16:26:17 +02:00
public static function get_faulty_followers ( $number = 20 ) {
2023-05-10 14:18:56 +02:00
$args = array (
2023-06-15 11:47:50 +02:00
'post_type' => self :: POST_TYPE ,
'posts_per_page' => $number ,
2023-07-28 10:28:55 +02:00
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
2023-06-15 11:47:50 +02:00
'meta_query' => array (
2023-09-21 16:26:17 +02:00
'relation' => 'OR' ,
2023-05-10 14:18:56 +02:00
array (
2023-07-06 14:42:18 +02:00
'key' => 'activitypub_errors' ,
2023-06-15 12:17:48 +02:00
'compare' => 'EXISTS' ,
2023-05-10 14:18:56 +02:00
),
2023-09-21 16:26:17 +02:00
array (
'key' => 'activitypub_inbox' ,
'compare' => 'NOT EXISTS' ,
),
array (
'key' => 'activitypub_actor_json' ,
'compare' => 'NOT EXISTS' ,
),
array (
'key' => 'activitypub_inbox' ,
'value' => '' ,
'compare' => '=' ,
),
array (
'key' => 'activitypub_actor_json' ,
'value' => '' ,
'compare' => '=' ,
),
2023-05-10 14:18:56 +02:00
),
);
2023-06-15 12:17:48 +02:00
$posts = new WP_Query ( $args );
2023-05-10 14:18:56 +02:00
$items = array ();
2023-06-15 12:17:48 +02:00
foreach ( $posts -> get_posts () as $follower ) {
2023-07-06 14:42:18 +02:00
$items [] = Follower :: init_from_cpt ( $follower ); // phpcs:ignore
2023-05-10 14:18:56 +02:00
}
2023-05-11 09:46:26 +02:00
return $items ;
2023-05-10 14:18:56 +02:00
}
2023-07-06 14:42:18 +02:00
/**
2023-07-11 08:53:18 +02:00
* This function is used to store errors that occur when
* sending an ActivityPub message to a Follower .
2023-07-06 14:42:18 +02:00
*
2023-07-11 08:53:18 +02:00
* The error will be stored in the
* post meta .
*
* @ param int $post_id The ID of the WordPress Custom - Post - Type .
* @ param mixed $error The error message . Can be a string or a WP_Error .
*
* @ return int | false The meta ID on success , false on failure .
2023-07-06 14:42:18 +02:00
*/
public static function add_error ( $post_id , $error ) {
if ( is_string ( $error ) ) {
$error_message = $error ;
} elseif ( is_wp_error ( $error ) ) {
$error_message = $error -> get_error_message ();
} else {
2023-07-11 08:53:18 +02:00
$error_message = __ (
'Unknown Error or misconfigured Error-Message' ,
'activitypub'
);
2023-07-06 14:42:18 +02:00
}
2023-07-11 08:53:18 +02:00
return add_post_meta (
$post_id ,
'activitypub_errors' ,
$error_message
);
2023-07-06 14:42:18 +02:00
}
2023-04-21 14:56:22 +02:00
}