From 7769d76849d6aa1a72d5ca51868679d0c1f6a2f2 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 21 Apr 2023 14:56:22 +0200 Subject: [PATCH] use a taxonomy to save the list of followers --- activitypub.php | 5 +- includes/class-admin.php | 2 +- includes/collection/class-followers.php | 244 ++++++++++++++++++++++++ includes/rest/class-inbox.php | 2 - includes/table/class-followers.php | 84 ++++++++ includes/table/followers-list.php | 36 ---- templates/followers-list.php | 6 +- 7 files changed, 336 insertions(+), 43 deletions(-) create mode 100644 includes/collection/class-followers.php create mode 100644 includes/table/class-followers.php delete mode 100644 includes/table/followers-list.php diff --git a/activitypub.php b/activitypub.php index 35e6de8d..c1cb8239 100644 --- a/activitypub.php +++ b/activitypub.php @@ -29,7 +29,7 @@ function init() { \define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) ); - require_once \dirname( __FILE__ ) . '/includes/table/followers-list.php'; + require_once \dirname( __FILE__ ) . '/includes/table/class-followers.php'; require_once \dirname( __FILE__ ) . '/includes/class-signature.php'; require_once \dirname( __FILE__ ) . '/includes/class-webfinger.php'; require_once \dirname( __FILE__ ) . '/includes/peer/class-followers.php'; @@ -44,6 +44,9 @@ function init() { require_once \dirname( __FILE__ ) . '/includes/class-activitypub.php'; Activitypub::init(); + require_once \dirname( __FILE__ ) . '/includes/collection/class-followers.php'; + Collection\Followers::init(); + // Configure the REST API route require_once \dirname( __FILE__ ) . '/includes/rest/class-outbox.php'; Rest\Outbox::init(); diff --git a/includes/class-admin.php b/includes/class-admin.php index 220ed9b7..fbb953a4 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -31,7 +31,7 @@ class Admin { \add_action( 'load-' . $settings_page, array( self::class, 'add_settings_help_tab' ) ); - $followers_list_page = \add_users_page( \__( 'Followers', 'activitypub' ), \__( 'Followers (Fediverse)', 'activitypub' ), 'read', 'activitypub-followers-list', array( self::class, 'followers_list_page' ) ); + $followers_list_page = \add_users_page( \__( 'Followers', 'activitypub' ), \__( 'Followers', 'activitypub' ), 'read', 'activitypub-followers-list', array( self::class, 'followers_list_page' ) ); \add_action( 'load-' . $followers_list_page, array( self::class, 'add_followers_list_help_tab' ) ); } diff --git a/includes/collection/class-followers.php b/includes/collection/class-followers.php new file mode 100644 index 00000000..7d7f8eb4 --- /dev/null +++ b/includes/collection/class-followers.php @@ -0,0 +1,244 @@ + array( + 'name' => _x( 'Followers', 'taxonomy general name', 'activitypub' ), + 'singular_name' => _x( 'Followers', 'taxonomy singular name', 'activitypub' ), + 'menu_name' => __( 'Followers', 'activitypub' ), + ), + 'hierarchical' => false, + 'show_ui' => false, + 'show_in_menu' => false, + 'show_in_nav_menus' => false, + 'show_admin_column' => false, + 'query_var' => false, + 'rewrite' => false, + 'public' => false, + 'capabilities' => array( + 'edit_terms' => null, + ), + ); + + register_taxonomy( self::TAXONOMY, 'user', $args ); + register_taxonomy_for_object_type( self::TAXONOMY, 'user' ); + + register_term_meta( + self::TAXONOMY, + 'user_id', + array( + 'type' => 'string', + 'single' => true, + //'sanitize_callback' => array( self::class, 'validate_username' ), + ) + ); + + register_term_meta( + self::TAXONOMY, + 'name', + array( + 'type' => 'string', + 'single' => true, + //'sanitize_callback' => array( self::class, 'validate_displayname' ), + ) + ); + + register_term_meta( + self::TAXONOMY, + 'username', + array( + 'type' => 'string', + 'single' => true, + //'sanitize_callback' => array( self::class, 'validate_username' ), + ) + ); + + register_term_meta( + self::TAXONOMY, + 'avatar', + array( + 'type' => 'string', + 'single' => true, + //'sanitize_callback' => array( self::class, 'validate_avatar' ), + ) + ); + + register_term_meta( + self::TAXONOMY, + 'created_at', + array( + 'type' => 'string', + 'single' => true, + //'sanitize_callback' => array( self::class, 'validate_created_at' ), + ) + ); + + register_term_meta( + self::TAXONOMY, + 'inbox', + array( + 'type' => 'string', + 'single' => true, + //'sanitize_callback' => array( self::class, 'validate_created_at' ), + ) + ); + + do_action( 'activitypub_after_register_taxonomy' ); + } + + /** + * Handle the "Follow" Request + * + * @param array $object The JSON "Follow" Activity + * @param int $user_id The ID of the WordPress User + * + * @return void + */ + public static function handle_follow_request( $object, $user_id ) { + // save follower + self::add_follower( $user_id, $object['actor'] ); + } + + /** + * Handles "Unfollow" requests + * + * @param array $object The JSON "Undo" Activity + * @param int $user_id The ID of the WordPress User + */ + public static function handle_undo_request( $object, $user_id ) { + if ( + isset( $object['object'] ) && + isset( $object['object']['type'] ) && + 'Follow' === $object['object']['type'] + ) { + self::remove_follower( $user_id, $object['actor'] ); + } + } + + /** + * Undocumented function + * + * @return void + */ + public static function add_follower( $user_id, $actor ) { + $remote_data = get_remote_metadata_by_actor( $actor ); + + if ( ! $remote_data || is_wp_error( $remote_data ) || ! is_array( $remote_data ) ) { + $remote_data = array(); + } + + $term = term_exists( $actor, self::TAXONOMY ); + + if ( ! $term ) { + $term = wp_insert_term( + $actor, + self::TAXONOMY, + array( + 'slug' => sanitize_title( $actor ), + 'description' => wp_json_encode( $remote_data ), + ) + ); + } + + $term_id = $term['term_id']; + + $map_meta = array( + 'name' => 'name', + 'preferredUsername' => 'username', + 'inbox' => 'inbox', + ); + + foreach ( $map_meta as $remote => $internal ) { + if ( ! empty( $remote_data[ $remote ] ) ) { + update_term_meta( $term_id, $internal, esc_html( $remote_data[ $remote ] ), true ); + } + } + + if ( ! empty( $remote_data['icon']['url'] ) ) { + update_term_meta( $term_id, 'avatar', esc_url_raw( $remote_data['icon']['url'] ), true ); + } + + wp_set_object_terms( $user_id, $actor, self::TAXONOMY, true ); + } + + public static function send_ack() { + // get inbox + $inbox = \Activitypub\get_inbox_by_actor( $object['actor'] ); + + // send "Accept" activity + $activity = new Activity( 'Accept' ); + $activity->set_object( $object ); + $activity->set_actor( \get_author_posts_url( $user_id ) ); + $activity->set_to( $object['actor'] ); + $activity->set_id( \get_author_posts_url( $user_id ) . '#follow-' . \preg_replace( '~^https?://~', '', $object['actor'] ) ); + + $activity = $activity->to_simple_json(); + $response = safe_remote_post( $inbox, $activity, $user_id ); + } + + public static function get_followers( $user_id, $number = null, $offset = null ) { + //self::migrate_followers( $user_id ); + + $terms = new WP_Term_Query( + array( + 'taxonomy' => self::TAXONOMY, + 'hide_empty' => false, + 'object_ids' => $user_id, + 'number' => $number, + 'offset' => $offset, + ) + ); + + return $terms->get_terms(); + } + + public static function count_followers( $user_id ) { + return count( self::get_followers( $user_id ) ); + } + + public static function migrate_followers( $user_id ) { + $followes = get_user_meta( $user_id, 'activitypub_followers', true ); + + if ( $followes ) { + foreach ( $followes as $follower ) { + self::add_follower( $user_id, $follower ); + } + } + } +} diff --git a/includes/rest/class-inbox.php b/includes/rest/class-inbox.php index 1a631082..761bfd69 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -17,8 +17,6 @@ class Inbox { public static function init() { \add_action( 'rest_api_init', array( self::class, 'register_routes' ) ); \add_filter( 'rest_pre_serve_request', array( self::class, 'serve_request' ), 11, 4 ); - \add_action( 'activitypub_inbox_follow', array( self::class, 'handle_follow' ), 10, 2 ); - \add_action( 'activitypub_inbox_undo', array( self::class, 'handle_unfollow' ), 10, 2 ); //\add_action( 'activitypub_inbox_like', array( self::class, 'handle_reaction' ), 10, 2 ); //\add_action( 'activitypub_inbox_announce', array( self::class, 'handle_reaction' ), 10, 2 ); \add_action( 'activitypub_inbox_create', array( self::class, 'handle_create' ), 10, 2 ); diff --git a/includes/table/class-followers.php b/includes/table/class-followers.php new file mode 100644 index 00000000..bba32088 --- /dev/null +++ b/includes/table/class-followers.php @@ -0,0 +1,84 @@ + '', + 'avatar' => \__( 'Avatar', 'activitypub' ), + 'name' => \__( 'Name', 'activitypub' ), + 'username' => \__( 'Username', 'activitypub' ), + 'identifier' => \__( 'Identifier', 'activitypub' ), + ); + } + + public function get_sortable_columns() { + return array(); + } + + public function prepare_items() { + $columns = $this->get_columns(); + $hidden = array(); + + $this->process_action(); + $this->_column_headers = array( $columns, $hidden, $this->get_sortable_columns() ); + + $page_num = $this->get_pagenum(); + $per_page = 20; + + $follower = FollowerCollection::get_followers( \get_current_user_id(), $per_page, ( $page_num - 1 ) * $per_page ); + $counter = FollowerCollection::count_followers( \get_current_user_id() ); + + $this->items = array(); + $this->set_pagination_args( + array( + 'total_items' => $counter, + 'total_pages' => round( $counter / $per_page ), + 'per_page' => $per_page, + ) + ); + + foreach ( $follower as $follower ) { + $item = array( + 'avatar' => esc_attr( get_term_meta( $follower->term_id, 'avatar', true ) ), + 'name' => esc_attr( get_term_meta( $follower->term_id, 'name', true ) ), + 'username' => esc_attr( get_term_meta( $follower->term_id, 'username', true ) ), + 'identifier' => esc_attr( $follower->name ), + ); + + $this->items[] = $item; + } + } + + public function get_bulk_actions() { + return array( + 'revoke' => __( 'Revoke', 'activitypub' ), + 'verify' => __( 'Verify', 'activitypub' ), + ); + } + + public function column_default( $item, $column_name ) { + if ( ! array_key_exists( $column_name, $item ) ) { + return __( 'None', 'activitypub' ); + } + return $item[ $column_name ]; + } + + public function column_avatar( $item ) { + return sprintf( + '', + $item['avatar'] + ); + } + + public function column_cb( $item ) { + return sprintf( '', esc_attr( $item['identifier'] ) ); + } +} diff --git a/includes/table/followers-list.php b/includes/table/followers-list.php deleted file mode 100644 index 81444ee7..00000000 --- a/includes/table/followers-list.php +++ /dev/null @@ -1,36 +0,0 @@ - \__( 'Identifier', 'activitypub' ), - ); - } - - public function get_sortable_columns() { - return array(); - } - - public function prepare_items() { - $columns = $this->get_columns(); - $hidden = array(); - - $this->process_action(); - $this->_column_headers = array( $columns, $hidden, $this->get_sortable_columns() ); - - $this->items = array(); - - foreach ( \Activitypub\Peer\Followers::get_followers( \get_current_user_id() ) as $follower ) { - $this->items[]['identifier'] = \esc_attr( $follower ); - } - } - - public function column_default( $item, $column_name ) { - return $item[ $column_name ]; - } -} diff --git a/templates/followers-list.php b/templates/followers-list.php index 057f498f..261808f1 100644 --- a/templates/followers-list.php +++ b/templates/followers-list.php @@ -1,10 +1,10 @@
-

+

-

+

- +