use a taxonomy to save the list of followers

This commit is contained in:
Matthias Pfefferle 2023-04-21 14:56:22 +02:00
parent eeb3ba2952
commit 7769d76849
7 changed files with 336 additions and 43 deletions

View file

@ -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();

View file

@ -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' ) );
}

View file

@ -0,0 +1,244 @@
<?php
namespace Activitypub\Collection;
use WP_Error;
use WP_Term_Query;
use Activitypub\Webfinger;
use Activitypub\Model\Activity;
use function Activitypub\safe_remote_get;
use function Activitypub\safe_remote_post;
use function Activitypub\get_remote_metadata_by_actor;
/**
* ActivityPub Followers Collection
*
* @author Matthias Pfefferle
*/
class Followers {
const TAXONOMY = 'activitypub-followers';
/**
* Register WordPress hooks/actions and register Taxonomy
*
* @return void
*/
public static function init() {
// register "followers" taxonomy
self::register_taxonomy();
\add_action( 'activitypub_inbox_follow', array( self::class, 'handle_follow_request' ), 10, 2 );
\add_action( 'activitypub_inbox_undo', array( self::class, 'handle_undo_request' ), 10, 2 );
}
/**
* Register the "Followers" Taxonomy
*
* @return void
*/
public static function register_taxonomy() {
$args = array(
'labels' => 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 );
}
}
}
}

View file

@ -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 );

View file

@ -0,0 +1,84 @@
<?php
namespace Activitypub\Table;
use WP_List_Table;
use Activitypub\Collection\Followers as FollowerCollection;
if ( ! \class_exists( '\WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
class Followers extends WP_List_Table {
public function get_columns() {
return array(
'cb' => '<input type="checkbox" />',
'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(
'<img src="%s" width="25px;" />',
$item['avatar']
);
}
public function column_cb( $item ) {
return sprintf( '<input type="checkbox" name="tokens[]" value="%s" />', esc_attr( $item['identifier'] ) );
}
}

View file

@ -1,36 +0,0 @@
<?php
namespace Activitypub\Table;
if ( ! \class_exists( '\WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
class Followers_List extends \WP_List_Table {
public function get_columns() {
return array(
'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() );
$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 ];
}
}

View file

@ -1,10 +1,10 @@
<div class="wrap">
<h1><?php \esc_html_e( 'Followers (Fediverse)', 'activitypub' ); ?></h1>
<h1><?php \esc_html_e( 'Followers', 'activitypub' ); ?></h1>
<?php // translators: ?>
<p><?php \printf( \esc_html__( 'You currently have %s followers.', 'activitypub' ), \esc_attr( \Activitypub\Peer\Followers::count_followers( \get_current_user_id() ) ) ); ?></p>
<p><?php \printf( \esc_html__( 'You currently have %s followers.', 'activitypub' ), \esc_attr( \Activitypub\Collection\Followers::count_followers( \get_current_user_id() ) ) ); ?></p>
<?php $token_table = new \Activitypub\Table\Followers_List(); ?>
<?php $token_table = new \Activitypub\Table\Followers(); ?>
<form method="get">
<input type="hidden" name="page" value="indieauth_user_token" />