From b9e49d42c36ef3d4ce280ecc29f92972068c3402 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 6 Oct 2023 16:33:54 +0200 Subject: [PATCH] some more fixes --- includes/class-comments.php | 4 +- includes/class-mention.php | 20 ---- includes/rest/class-inbox.php | 2 +- includes/transformer/class-comment.php | 156 +++++-------------------- 4 files changed, 31 insertions(+), 151 deletions(-) diff --git a/includes/class-comments.php b/includes/class-comments.php index 5af6f77..1025158 100644 --- a/includes/class-comments.php +++ b/includes/class-comments.php @@ -69,7 +69,7 @@ class Comments { $protocol = \get_comment_meta( $comment->comment_ID, 'protocol', true ); // TODO Test if this is returned by Model/Comment if ( 'activitypub' === $protocol ) { - $updated = \get_comment_meta( $comment->comment_ID, 'ap_last_modified', true ); + $updated = \get_comment_meta( $comment->comment_ID, 'activitypub_last_modified', true ); if ( $updated ) { $format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); $formatted_datetime = \date_i18n( $format, \strtotime( $updated ) ); @@ -125,7 +125,7 @@ class Comments { * Fires immediately before comment status transition hooks are fired. (useful only for admin) */ public static function edit_comment( $comment_id, $commentdata ) { - update_comment_meta( $comment_id, 'ap_last_modified', \wp_date( 'Y-m-d H:i:s' ) ); + update_comment_meta( $comment_id, 'activitypub_last_modified', \wp_date( 'Y-m-d H:i:s' ) ); $user = \get_userdata( $commentdata['user_id'] ); if ( \in_array( 'administrator', $user->roles ) ) { $wp_comment = \get_comment( $comment_id ); diff --git a/includes/class-mention.php b/includes/class-mention.php index 4aedfb0..cf9b95a 100644 --- a/includes/class-mention.php +++ b/includes/class-mention.php @@ -14,7 +14,6 @@ class Mention { */ public static function init() { \add_filter( 'the_content', array( self::class, 'the_content' ), 99, 2 ); - \add_filter( 'activitypub_extract_mentions', array( self::class, 'extract_mentions' ), 99, 2 ); } /** @@ -146,23 +145,4 @@ class Mention { return new WP_Error( 'activitypub_no_inbox', \__( 'No "Inbox" found', 'activitypub' ), $metadata ); } - - /** - * Extract the mentions from the post_content. - * - * @param array $mentions The already found mentions. - * @param string $post_content The post content. - * - * @return mixed The discovered mentions. - */ - public static function extract_mentions( $mentions, $post_content ) { - \preg_match_all( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/i', $post_content, $matches ); - foreach ( $matches[0] as $match ) { - $link = Webfinger::resolve( $match ); - if ( ! is_wp_error( $link ) ) { - $mentions[ $match ] = $link; - } - } - return $mentions; - } } diff --git a/includes/rest/class-inbox.php b/includes/rest/class-inbox.php index e899093..9a9bdba 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -452,7 +452,7 @@ class Inbox { $commentdata['comment_content'] = \wp_filter_kses( $object['object']['content'] ); $commentdata['comment_meta']['avatar_url'] = \esc_url_raw( $meta['icon']['url'] ); $commentdata['comment_meta']['ap_published'] = \wp_date( 'Y-m-d H:i:s', strtotime( $object['object']['published'] ) ); - $commentdata['comment_meta']['ap_last_modified'] = $object['object']['updated']; + $commentdata['comment_meta']['activitypub_last_modified'] = $object['object']['updated']; $commentdata['comment_meta']['ap_object'] = \serialize( $object ); // disable flood control diff --git a/includes/transformer/class-comment.php b/includes/transformer/class-comment.php index e0839ce..0c3eac7 100644 --- a/includes/transformer/class-comment.php +++ b/includes/transformer/class-comment.php @@ -65,13 +65,12 @@ class Comment { $object->set_id( $this->get_id() ); $object->set_url( \get_comment_link( $comment->ID ) ); - $object->set_context( \get_permalink( $comment->comment_post_ID ) ); $object->set_type( 'Note' ); $published = \strtotime( $comment->comment_date_gmt ); $object->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', $published ) ); - $updated = \get_comment_meta( $comment->comment_ID, 'ap_last_modified', true ); + $updated = \get_comment_meta( $comment->comment_ID, 'activitypub_last_modified', true ); if ( $updated > $published ) { $object->set_updated( \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( $updated ) ) ); } @@ -92,8 +91,7 @@ class Comment { get_rest_url_by_path( $path ), ) ); - $object->set_cc( $this->get_cc() ); - $object->set_tag( $this->get_tags() ); + return $object; } @@ -113,68 +111,6 @@ class Comment { return Users::get_by_id( $this->wp_comment->user_id )->get_url(); } - /** - * Returns a list of Mentions, used in the Comment. - * - * @see https://docs.joinmastodon.org/spec/activitypub/#Mention - * - * @return array The list of Mentions. - */ - protected function get_cc() { - $cc = array(); - - $mentions = $this->get_mentions(); - if ( $mentions ) { - foreach ( $mentions as $mention => $url ) { - $cc[] = $url; - } - } - - return $cc; - } - - /** - * Returns a list of Tags, used in the Comment. - * - * This includes Hash-Tags and Mentions. - * - * @return array The list of Tags. - */ - protected function get_tags() { - // TODO Delete Or Modify - $tags = array(); - - $comment_tags = $this->get_hashtags(); - if ( $comment_tags ) { - foreach ( $comment_tags as $comment_tag ) { - $tag_link = \get_tag_link( $comment_tag ); - if ( ! $tag_link ) { - continue; - } - $tag = array( - 'type' => 'Hashtag', - 'href' => \esc_url( $tag_link ), - 'name' => esc_hashtag( $comment_tag ), - ); - $tags[] = $tag; - } - } - - $mentions = $this->get_mentions(); - if ( $mentions ) { - foreach ( $mentions as $mention => $url ) { - $tag = array( - 'type' => 'Mention', - 'href' => \esc_url( $url ), - 'name' => \esc_html( $mention ), - ); - $tags[] = $tag; - } - } - - return $tags; - } - /** * Returns the content for the ActivityPub Item. * @@ -185,50 +121,15 @@ class Comment { protected function get_content() { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited $comment = $this->wp_comment; - $content = $comment->comment_content; + $content = $comment->comment_content; - $content = \wpautop( $content ); - $content = \preg_replace( '/[\n\r\t]/', '', $content ); - $content = \trim( $content ); + $content = \wpautop( $content ); + $content = \preg_replace( '/[\n\r\t]/', '', $content ); + $content = \trim( $content ); - $content = \apply_filters( 'the_content', $content, $comment ); - $content = \html_entity_decode( $content, \ENT_QUOTES, 'UTF-8' ); return $content; } - /** - * Helper function to get the @-Mentions from the comment content. - * - * @return array The list of @-Mentions. - */ - protected function get_mentions() { - return apply_filters( 'activitypub_extract_mentions', array(), $this->wp_comment->comment_content, $this->wp_comment ); - } - - /** - * Helper function to get the #HashTags from the comment content. - * - * @return array The list of @-Mentions. - */ - protected function get_hashtags() { - $content = $this->get_content(); - - $tags = []; - //TODO fix hashtag - if ( \preg_match_all( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', $content, $match ) ) { - $tags = \implode( ', ', $match[1] ); - } - - $hashtags = []; - preg_match_all( "/(#\w+)/u", $content, $matches ); - if ( $matches ) { - $hashtags = array_count_values( $matches[0] ); - $hashtags = array_keys( $hashtags ); - } - - return $hashtags; - } - /** * get_parent_uri (in reply to) * takes a comment and returns @@ -284,29 +185,28 @@ class Comment { * Checks a comment ID for a source_id, or source_url */ protected function get_source_id( $comment ) { - - if ( $comment->user_id !== 0 ) { - - $source_id = \get_comment_meta( $comment->ID, 'source_id', true ); - if ( ! $source_id ) { - $source_url = \get_comment_meta( $comment->ID, 'source_url', true ); - if ( ! $source_url ) { - return null; - } - $response = safe_remote_get( $source_url ); - $body = \wp_remote_retrieve_body( $response ); - $remote_status = \json_decode( $body, true ); - if ( is_wp_error( $remote_status ) - || ! isset( $remote_status['@context'] ) - || ! isset( $remote_status['object']['id'] ) ) { - - // the original post may have been deleted, before we started processing deletes. - return null; - } - $source_id = $remote_status['object']['id']; - } - return $source_id; + if ( ! $comment->user_id ) { + return null; } - return null; + + $source_id = \get_comment_meta( $comment->ID, 'source_id', true ); + + if ( ! $source_id ) { + $source_url = \get_comment_meta( $comment->ID, 'source_url', true ); + if ( ! $source_url ) { + return null; + } + $response = \safe_remote_get( $source_url ); + $body = \wp_remote_retrieve_body( $response ); + $remote_status = \json_decode( $body, true ); + if ( \is_wp_error( $remote_status ) + || ! isset( $remote_status['@context'] ) + || ! isset( $remote_status['object']['id'] ) ) { + // the original post may have been deleted, before we started processing deletes. + return null; + } + $source_id = $remote_status['object']['id']; + } + return $source_id; } }