From 6a69a402956efd4ddc75e7dfe7d5ea8b2982f010 Mon Sep 17 00:00:00 2001 From: Django Doucet Date: Wed, 5 Oct 2022 13:58:12 -0600 Subject: [PATCH] Fix announce, clarified language --- includes/class-activity-dispatcher.php | 27 +++++++--- includes/class-admin.php | 10 ++-- .../{class-migrate.php => class-tools.php} | 21 ++++---- includes/model/class-post.php | 30 +++++++---- includes/table/migrate-list.php | 16 +++--- templates/migrate-page.php | 51 ------------------- templates/tools-page.php | 43 ++++++++++++++++ 7 files changed, 107 insertions(+), 91 deletions(-) rename includes/{class-migrate.php => class-tools.php} (73%) delete mode 100644 templates/migrate-page.php create mode 100644 templates/tools-page.php diff --git a/includes/class-activity-dispatcher.php b/includes/class-activity-dispatcher.php index a9a212a..9303edb 100644 --- a/includes/class-activity-dispatcher.php +++ b/includes/class-activity-dispatcher.php @@ -14,7 +14,7 @@ class Activity_Dispatcher { */ public static function init() { \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_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 ); @@ -48,16 +48,27 @@ class Activity_Dispatcher { /** * 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 - $user_id = $activitypub_post->get_post_author(); - $activitypub_announce = new \Activitypub\Model\Activity( 'Announce', \Activitypub\Model\Activity::TYPE_FULL ); + $time = \current_datetime()->format( DATE_ISO8601 ); + $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_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 ) { $activitypub_announce->set_to( $to ); diff --git a/includes/class-admin.php b/includes/class-admin.php index 6b9f5bb..c662a01 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -38,7 +38,7 @@ class Admin { \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 */ 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' ); // 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 - if ( '0.13.5' > $activitypub_db_version ) { + if ( version_compare( '0.13.5', $activitypub_db_version, '>' ) ) { // This updates post_meta with _activitypub_permalink_compat. // 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, // 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'] ); diff --git a/includes/class-migrate.php b/includes/class-tools.php similarity index 73% rename from includes/class-migrate.php rename to includes/class-tools.php index e634f0f..7ba6a29 100644 --- a/includes/class-migrate.php +++ b/includes/class-tools.php @@ -1,5 +1,5 @@ -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' ) ); $args = array( 'numberposts' => -1, @@ -37,8 +37,8 @@ class Posts { return $posts_to_migrate; } - public static function count_posts() { - $posts = self::get_posts(); + public static function count_posts_to_migrate() { + $posts = self::get_posts_to_migrate(); return \count( $posts ); } @@ -52,7 +52,7 @@ class Posts { */ public static function migrate_post( $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) * * @param str post_url (post_id) + * @param int user_id */ - public static function announce_url( $post_url ) { - $post_id = \url_to_postid( $post_url ); - $activitypub_post = new \Activitypub\Model\Post( \get_post( $post_id ) ); - \wp_schedule_single_event( \time() + 1, 'activitypub_send_announce_activity', array( $activitypub_post ) ); + public static function announce_url( $post_url, $user_id ) { + \wp_schedule_single_event( \time(), 'activitypub_send_announce_activity', array( $post_url, $user_id ) ); } /** diff --git a/includes/model/class-post.php b/includes/model/class-post.php index 9003e25..ee6b3fd 100644 --- a/includes/model/class-post.php +++ b/includes/model/class-post.php @@ -32,7 +32,7 @@ class Post { $this->replies = $this->generate_replies(); $this->updated = $this->generate_updated(); $this->delete = $this->get_deleted(); - $this->permalink = $this->get_the_post_link(); + $this->permalink = $this->get_the_permalink(); } public function __call( $method, $params ) { @@ -89,17 +89,17 @@ class Post { public function generate_id() { $post = $this->post; + $object_id = \add_query_arg( // + array( + 'p' => $post->ID, + ), + trailingslashit( site_url() ) + ); + $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( // - array( - 'p' => $post->ID, - ), - trailingslashit( site_url() ) - ); } return $object_id; } @@ -406,6 +406,18 @@ class Post { return \sprintf( '%1$s', $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 * diff --git a/includes/table/migrate-list.php b/includes/table/migrate-list.php index 00b0863..98d35ac 100644 --- a/includes/table/migrate-list.php +++ b/includes/table/migrate-list.php @@ -28,7 +28,7 @@ class Migrate_List extends \WP_List_Table { $this->process_action(); $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( 'post_author' => $post->post_author, 'title' => \sprintf( @@ -62,14 +62,14 @@ class Migrate_List extends \WP_List_Table { // delete if ( isset( $_REQUEST['action'] ) && 'activitypub_tools' === $_REQUEST['page'] && 'delete' === $_REQUEST['action'] ) { 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 and announce if ( isset( $_REQUEST['action'] ) && 'activitypub_tools' === $_REQUEST['page'] && 'delete_announce' === $_REQUEST['action'] ) { 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' ); } } @@ -102,22 +102,24 @@ class Migrate_List extends \WP_List_Table { $actions = array( 'delete_announce' => sprintf( - '%s', + '%s', esc_attr( $_REQUEST['page'] ), 'delete_announce', $item['post_author'], \rawurlencode( $item['migrate'] ), $delete_announce_nonce, - __( 'Delete & Announce', 'activitypub' ) + __( 'Delete the federated post, and re-share the original post', 'activitypub' ), + __( 'Delete & Re-share original', 'activitypub' ), ), 'delete' => sprintf( - '%s', + '%s', esc_attr( $_REQUEST['page'] ), 'delete', $item['post_author'], \rawurlencode( $item['migrate'] ), $delete_nonce, - __( 'Delete', 'activitypub' ) + __( 'Delete the federated post', 'activitypub' ), + __( 'Delete', 'activitypub' ), ), ); return sprintf( '%1$s %2$s', $item['title'], $this->row_actions( $actions, true ) ); diff --git a/templates/migrate-page.php b/templates/migrate-page.php deleted file mode 100644 index 6bb32d1..0000000 --- a/templates/migrate-page.php +++ /dev/null @@ -1,51 +0,0 @@ - -
-

- -

- - - -
- prepare_items(); - $token_table->display(); - ?> -
-
-
-
-

- - - -

-
-
-
-
-
-

- - - -

-
-
-
diff --git a/templates/tools-page.php b/templates/tools-page.php new file mode 100644 index 0000000..6658911 --- /dev/null +++ b/templates/tools-page.php @@ -0,0 +1,43 @@ + +
+

+ +

+ +

+ + + +
+ prepare_items(); + $token_table->display(); + ?> +
+
+
+
+

+ + + +

+
+
+