Fix announce, clarified language

This commit is contained in:
Django Doucet 2022-10-05 13:58:12 -06:00
parent 63bf62402d
commit 6a69a40295
7 changed files with 107 additions and 91 deletions

View file

@ -14,7 +14,7 @@ class Activity_Dispatcher {
*/ */
public static function init() { public static function init() {
\add_action( 'activitypub_send_post_activity', array( '\Activitypub\Activity_Dispatcher', 'send_post_activity' ) ); \add_action( 'activitypub_send_post_activity', array( '\Activitypub\Activity_Dispatcher', 'send_post_activity' ) );
\add_action( 'activitypub_send_announce_activity', array( '\Activitypub\Activity_Dispatcher', 'send_announce_activity' ) ); \add_action( 'activitypub_send_announce_activity', array( '\Activitypub\Activity_Dispatcher', 'send_announce_activity' ), 10, 2 );
\add_action( 'activitypub_send_update_activity', array( '\Activitypub\Activity_Dispatcher', 'send_update_activity' ) ); \add_action( 'activitypub_send_update_activity', array( '\Activitypub\Activity_Dispatcher', 'send_update_activity' ) );
\add_action( 'activitypub_send_delete_activity', array( '\Activitypub\Activity_Dispatcher', 'send_delete_activity' ) ); \add_action( 'activitypub_send_delete_activity', array( '\Activitypub\Activity_Dispatcher', 'send_delete_activity' ) );
\add_action( 'activitypub_send_delete_url_activity', array( '\Activitypub\Activity_Dispatcher', 'send_delete_url_activity' ), 10, 2 ); \add_action( 'activitypub_send_delete_url_activity', array( '\Activitypub\Activity_Dispatcher', 'send_delete_url_activity' ), 10, 2 );
@ -48,16 +48,27 @@ class Activity_Dispatcher {
/** /**
* Send "announce" activities. * Send "announce" activities.
* *
* @param \Activitypub\Model\Post $activitypub_post * @param str $activitypub_url (ActivityPub object ID)
* @param absint $user_id
*/ */
public static function send_announce_activity( $activitypub_post ) { public static function send_announce_activity( $activitypub_url, $user_id ) {
// get latest version of post // get latest version of post
$user_id = $activitypub_post->get_post_author(); $time = \current_datetime()->format( DATE_ISO8601 );
$activitypub_announce = new \Activitypub\Model\Activity( 'Announce', \Activitypub\Model\Activity::TYPE_FULL ); $post = \get_post( \url_to_postid( $activitypub_url ) );
$activitypub_announce = new \Activitypub\Model\Activity( 'Announce', \Activitypub\Model\Activity::TYPE_SIMPLE );
$activitypub_announce->set_published( $time );
if ( $post ) {
$activitypub_id = $post->guid;
$activitypub_post = new \Activitypub\Model\Post( $post );
$activitypub_announce->from_post( $activitypub_post->to_array() );
} else {
$activitypub_id = $activitypub_url;
$activitypub_announce->set_object( $activitypub_id );
}
$activitypub_announce->set_id( add_query_arg( 'activity', 'announce', $activitypub_id ) );
$activitypub_announce->set_actor( \get_author_posts_url( $user_id ) ); $activitypub_announce->set_actor( \get_author_posts_url( $user_id ) );
$activitypub_create = new \Activitypub\Model\Activity( 'Create', \Activitypub\Model\Activity::TYPE_NONE );
$activitypub_create->from_post( $activitypub_post->to_array() );
$activitypub_announce->from_post( $activitypub_create->to_array() );
foreach ( \Activitypub\get_follower_inboxes( $user_id ) as $inbox => $to ) { foreach ( \Activitypub\get_follower_inboxes( $user_id ) as $inbox => $to ) {
$activitypub_announce->set_to( $to ); $activitypub_announce->set_to( $to );

View file

@ -38,7 +38,7 @@ class Admin {
\add_action( 'load-' . $followers_list_page, array( '\Activitypub\Admin', 'add_followers_list_help_tab' ) ); \add_action( 'load-' . $followers_list_page, array( '\Activitypub\Admin', 'add_followers_list_help_tab' ) );
\add_management_page( \__( 'ActivityPub Management', 'activitypub' ), \__( 'ActivityPub Management', 'activitypub' ), 'manage_options', 'activitypub_tools', array( '\Activitypub\Admin', 'migrate_tools_page' ) ); \add_management_page( \__( 'ActivityPub Management', 'activitypub' ), \__( 'ActivityPub Tools', 'activitypub' ), 'manage_options', 'activitypub_tools', array( '\Activitypub\Admin', 'migrate_tools_page' ) );
} }
/** /**
@ -59,7 +59,7 @@ class Admin {
* Load ActivityPub Tools page * Load ActivityPub Tools page
*/ */
public static function migrate_tools_page() { public static function migrate_tools_page() {
\load_template( \dirname( __FILE__ ) . '/../templates/migrate-page.php' ); \load_template( \dirname( __FILE__ ) . '/../templates/tools-page.php' );
} }
/** /**
@ -145,17 +145,17 @@ class Admin {
$activitypub_db_version = \get_option( 'activitypub_version' ); $activitypub_db_version = \get_option( 'activitypub_version' );
// Needs update // Needs update
if ( empty( $activitypub_db_version ) || $plugin_data['Version'] > $activitypub_db_version ) { if ( empty( $activitypub_db_version ) || \version_compare( $plugin_data['Version'], $activitypub_db_version, '>' ) ) {
// Check for specific migrations // Check for specific migrations
if ( '0.13.5' > $activitypub_db_version ) { if ( version_compare( '0.13.5', $activitypub_db_version, '>' ) ) {
// This updates post_meta with _activitypub_permalink_compat. // This updates post_meta with _activitypub_permalink_compat.
// Posts that have this meta will be backwards compatible with their permalink based ActivityPub ID (URI) // Posts that have this meta will be backwards compatible with their permalink based ActivityPub ID (URI)
// This may create false positives, where the permalink has changed (slug, permalink structure) since federation, // This may create false positives, where the permalink has changed (slug, permalink structure) since federation,
// for those cases a delete_url will allow for federating a delete based on the federated object ID (the old permalink) // for those cases a delete_url will allow for federating a delete based on the federated object ID (the old permalink)
\Activitypub\Migrate\Posts::backcompat_posts(); \Activitypub\Tools\Posts::mark_posts_to_migrate();
} }
} }
\update_option( 'activitypub_version', $plugin_data['Version'] ); \update_option( 'activitypub_version', $plugin_data['Version'] );

View file

@ -1,5 +1,5 @@
<?php <?php
namespace Activitypub\Migrate; namespace Activitypub\Tools;
/** /**
* ActivityPub Migrate DB-Class * ActivityPub Migrate DB-Class
@ -9,9 +9,9 @@ namespace Activitypub\Migrate;
class Posts { class Posts {
/** /**
* Updates ActivityPub Posts for backwards compatibility * Marks ActivityPub Posts for backwards compatibility
*/ */
public static function backcompat_posts() { public static function mark_posts_to_migrate() {
$post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ); $post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) );
$args = array( $args = array(
'numberposts' => -1, 'numberposts' => -1,
@ -25,7 +25,7 @@ class Posts {
} }
} }
public static function get_posts() { public static function get_posts_to_migrate() {
$post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ); $post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) );
$args = array( $args = array(
'numberposts' => -1, 'numberposts' => -1,
@ -37,8 +37,8 @@ class Posts {
return $posts_to_migrate; return $posts_to_migrate;
} }
public static function count_posts() { public static function count_posts_to_migrate() {
$posts = self::get_posts(); $posts = self::get_posts_to_migrate();
return \count( $posts ); return \count( $posts );
} }
@ -52,7 +52,7 @@ class Posts {
*/ */
public static function migrate_post( $post_url, $post_author ) { public static function migrate_post( $post_url, $post_author ) {
self::delete_url( $post_url, $post_author ); self::delete_url( $post_url, $post_author );
self::announce_url( $post_url ); self::announce_url( $post_url, $post_author );
} }
/** /**
@ -60,11 +60,10 @@ class Posts {
* send Announce (obj) * send Announce (obj)
* *
* @param str post_url (post_id) * @param str post_url (post_id)
* @param int user_id
*/ */
public static function announce_url( $post_url ) { public static function announce_url( $post_url, $user_id ) {
$post_id = \url_to_postid( $post_url ); \wp_schedule_single_event( \time(), 'activitypub_send_announce_activity', array( $post_url, $user_id ) );
$activitypub_post = new \Activitypub\Model\Post( \get_post( $post_id ) );
\wp_schedule_single_event( \time() + 1, 'activitypub_send_announce_activity', array( $activitypub_post ) );
} }
/** /**

View file

@ -32,7 +32,7 @@ class Post {
$this->replies = $this->generate_replies(); $this->replies = $this->generate_replies();
$this->updated = $this->generate_updated(); $this->updated = $this->generate_updated();
$this->delete = $this->get_deleted(); $this->delete = $this->get_deleted();
$this->permalink = $this->get_the_post_link(); $this->permalink = $this->get_the_permalink();
} }
public function __call( $method, $params ) { public function __call( $method, $params ) {
@ -89,17 +89,17 @@ class Post {
public function generate_id() { public function generate_id() {
$post = $this->post; $post = $this->post;
$pretty_permalink = \get_post_meta( $post->ID, '_activitypub_permalink_compat', true );
if ( ! empty( $pretty_permalink ) ) {
$object_id = $pretty_permalink;
} else {
$object_id = \add_query_arg( // $object_id = \add_query_arg( //
array( array(
'p' => $post->ID, 'p' => $post->ID,
), ),
trailingslashit( site_url() ) trailingslashit( site_url() )
); );
$pretty_permalink = \get_post_meta( $post->ID, '_activitypub_permalink_compat', true );
if ( ! empty( $pretty_permalink ) ) {
$object_id = $pretty_permalink;
} }
return $object_id; return $object_id;
} }
@ -406,6 +406,18 @@ class Post {
return \sprintf( '<a href="%1$s">%1$s</a>', $link ); return \sprintf( '<a href="%1$s">%1$s</a>', $link );
} }
/**
* Gets the federated permalink
*
* @return string
*/
public function get_the_permalink() {
$post = $this->post;
$link = \get_permalink( $post->ID );
return $link;
}
/** /**
* Adds all tags as hashtags to the post/summary content * Adds all tags as hashtags to the post/summary content
* *

View file

@ -28,7 +28,7 @@ class Migrate_List extends \WP_List_Table {
$this->process_action(); $this->process_action();
$this->_column_headers = array( $columns, $hidden, $sortable ); $this->_column_headers = array( $columns, $hidden, $sortable );
foreach ( \Activitypub\Migrate\Posts::get_posts() as $post ) { foreach ( \Activitypub\Tools\Posts::get_posts_to_migrate() as $post ) {
$this->items[] = array( $this->items[] = array(
'post_author' => $post->post_author, 'post_author' => $post->post_author,
'title' => \sprintf( 'title' => \sprintf(
@ -62,14 +62,14 @@ class Migrate_List extends \WP_List_Table {
// delete // delete
if ( isset( $_REQUEST['action'] ) && 'activitypub_tools' === $_REQUEST['page'] && 'delete' === $_REQUEST['action'] ) { if ( isset( $_REQUEST['action'] ) && 'activitypub_tools' === $_REQUEST['page'] && 'delete' === $_REQUEST['action'] ) {
if ( wp_verify_nonce( $nonce, 'activitypub_delete_post' ) ) { if ( wp_verify_nonce( $nonce, 'activitypub_delete_post' ) ) {
\Activitypub\Migrate\Posts::delete_url( rawurldecode( $_REQUEST['post_url'] ), absint( $_REQUEST['post_author'] ) ); \Activitypub\Tools\Posts::delete_url( rawurldecode( $_REQUEST['post_url'] ), absint( $_REQUEST['post_author'] ) );
\delete_post_meta( \url_to_postid( $_REQUEST['post_url'] ), '_activitypub_permalink_compat' ); \delete_post_meta( \url_to_postid( $_REQUEST['post_url'] ), '_activitypub_permalink_compat' );
} }
} }
// delete and announce // delete and announce
if ( isset( $_REQUEST['action'] ) && 'activitypub_tools' === $_REQUEST['page'] && 'delete_announce' === $_REQUEST['action'] ) { if ( isset( $_REQUEST['action'] ) && 'activitypub_tools' === $_REQUEST['page'] && 'delete_announce' === $_REQUEST['action'] ) {
if ( wp_verify_nonce( $nonce, 'activitypub_delete_announce_post' ) ) { if ( wp_verify_nonce( $nonce, 'activitypub_delete_announce_post' ) ) {
\Activitypub\Migrate\Posts::migrate_post( rawurldecode( $_REQUEST['post_url'] ), absint( $_REQUEST['post_author'] ) ); \Activitypub\Tools\Posts::migrate_post( rawurldecode( $_REQUEST['post_url'] ), absint( $_REQUEST['post_author'] ) );
\delete_post_meta( \url_to_postid( $_REQUEST['post_url'] ), '_activitypub_permalink_compat' ); \delete_post_meta( \url_to_postid( $_REQUEST['post_url'] ), '_activitypub_permalink_compat' );
} }
} }
@ -102,22 +102,24 @@ class Migrate_List extends \WP_List_Table {
$actions = array( $actions = array(
'delete_announce' => sprintf( 'delete_announce' => sprintf(
'<a href="?page=%s&action=%s&post_author=%s&post_url=%s&_wpnonce=%s">%s</a>', '<a href="?page=%s&action=%s&post_author=%s&post_url=%s&_wpnonce=%s" title="%s">%s</a>',
esc_attr( $_REQUEST['page'] ), esc_attr( $_REQUEST['page'] ),
'delete_announce', 'delete_announce',
$item['post_author'], $item['post_author'],
\rawurlencode( $item['migrate'] ), \rawurlencode( $item['migrate'] ),
$delete_announce_nonce, $delete_announce_nonce,
__( 'Delete & Announce', 'activitypub' ) __( 'Delete the federated post, and re-share the original post', 'activitypub' ),
__( 'Delete & Re-share original', 'activitypub' ),
), ),
'delete' => sprintf( 'delete' => sprintf(
'<a href="?page=%s&action=%s&post_author=%s&post_url=%s&_wpnonce=%s">%s</a>', '<a href="?page=%s&action=%s&post_author=%s&post_url=%s&_wpnonce=%s" title="%s">%s</a>',
esc_attr( $_REQUEST['page'] ), esc_attr( $_REQUEST['page'] ),
'delete', 'delete',
$item['post_author'], $item['post_author'],
\rawurlencode( $item['migrate'] ), \rawurlencode( $item['migrate'] ),
$delete_nonce, $delete_nonce,
__( 'Delete', 'activitypub' ) __( 'Delete the federated post', 'activitypub' ),
__( 'Delete', 'activitypub' ),
), ),
); );
return sprintf( '%1$s %2$s', $item['title'], $this->row_actions( $actions, true ) ); return sprintf( '%1$s %2$s', $item['title'], $this->row_actions( $actions, true ) );

View file

@ -1,51 +0,0 @@
<?php
$delete_nonce = wp_create_nonce( 'activitypub_manual_delete_url' );
$announce_nonce = wp_create_nonce( 'activitypub_manual_announce_url' );
if ( isset( $_REQUEST['post_url'] ) && $_REQUEST['page'] == "activitypub_tools" ) {
$post_url = $_REQUEST['post_url'];
$post_author = get_current_user_id();
if ( isset( $_REQUEST['submit_delete'] ) && wp_verify_nonce( $delete_nonce, 'activitypub_manual_delete_url' ) ) {
\Activitypub\Migrate\Posts::delete_url( rawurldecode( $_REQUEST['post_url'] ), $post_author );
}
if ( isset( $_REQUEST['submit_announce'] ) && wp_verify_nonce( $announce_nonce, 'activitypub_manual_announce_url' ) ) {
\Activitypub\Migrate\Posts::announce_url( rawurldecode( $_REQUEST['post_url'] ) );
}
}
?>
<div class="wrap">
<h1><?php \esc_html_e( 'Migrate posts (Fediverse)', 'activitypub' ); ?></h1>
<p><?php \printf( \__( 'You currently have %s posts to migrate.', 'activitypub' ), \esc_attr( \Activitypub\Migrate\Posts::count_posts() ) ); ?></p>
<?php $token_table = new \Activitypub\Table\Migrate_List(); ?>
<form method="POST" id="table">
<?php
$token_table->prepare_items();
$token_table->display();
?>
</form>
<hr class="separator">
<form method="POST" id="delete">
<div>
<p><?php _e( 'Manually Delete a URL', 'activitypub' ); ?></p>
<input type="hidden" name="page" value="activitypub_tools">
<input type="hidden" name="_wpnonce" value="<?php echo $delete_nonce ?>">
<input type="text" class="post_url" id="url" name="post_url" size="40" value="">
<input class="button button-primary" type="submit" value="<?php _e( 'Delete Federated URL', 'activitypub' ); ?>" name="submit_delete" id="submit_delete"><br></p>
</div>
</form>
<hr class="separator">
<form method="POST" id="announce">
<div>
<p><?php _e( 'Manually Announce a Post', 'activitypub' ); ?></p>
<input type="hidden" name="page" value="activitypub_tools">
<input type="hidden" name="_wpnonce" value="<?php echo $announce_nonce ?>">
<input type="text" class="post_url" id="url" name="post_url" size="40" value="">
<input class="button button-primary" type="submit" value="<?php _e( 'Announce a Post URL', 'activitypub' ); ?>" name="submit_announce" id="submit_announce"><br></p>
</div>
</form>
</div>

43
templates/tools-page.php Normal file
View file

@ -0,0 +1,43 @@
<?php
$action_nonce = wp_create_nonce( 'activitypub_action' );
if ( isset( $_REQUEST['post_url'] ) && $_REQUEST['page'] == "activitypub_tools" ) {
$post_url = $_REQUEST['post_url'];
$user_id = get_current_user_id();
if ( isset( $_REQUEST['submit_delete'] ) && wp_verify_nonce( $action_nonce, 'activitypub_action' ) ) {
\Activitypub\Migrate\Posts::delete_url( rawurldecode( $_REQUEST['post_url'] ), $user_id );
}
}
?>
<div class="wrap">
<h1><?php \esc_html_e( 'Migrate posts (Fediverse)', 'activitypub' ); ?></h1>
<p><?php \printf(
\__( 'You currently have %s posts to migrate.', 'activitypub' ),
\esc_attr( \Activitypub\Tools\Posts::count_posts_to_migrate() )
); ?></p>
<p><?php \printf(
\__( 'Posts with ActivityPub Comments, should be treated with care, existing interactions in the fediverse but not on site will be lost.', 'activitypub' ),
\esc_attr( \Activitypub\Tools\Posts::count_posts_to_migrate() )
); ?></p>
<?php $token_table = new \Activitypub\Table\Migrate_List(); ?>
<form method="POST" id="table">
<?php
$token_table->prepare_items();
$token_table->display();
?>
</form>
<hr class="separator">
<form method="POST" id="delete">
<div>
<p><?php _e( 'Manually Delete a URL', 'activitypub' ); ?></p>
<input type="hidden" name="page" value="activitypub_tools">
<input type="hidden" name="_wpnonce" value="<?php echo $action_nonce ?>">
<input type="text" class="post_url" id="url" name="post_url" size="40" value="">
<input class="button button-primary" type="submit" value="<?php _e( 'Delete Federated URL', 'activitypub' ); ?>" name="submit_delete" id="submit_delete"><br></p>
</div>
</form>
</div>