some more fixes

This commit is contained in:
Matthias Pfefferle 2023-10-06 16:33:54 +02:00
parent 69195f8b3a
commit b9e49d42c3
4 changed files with 31 additions and 151 deletions

View file

@ -69,7 +69,7 @@ class Comments {
$protocol = \get_comment_meta( $comment->comment_ID, 'protocol', true ); $protocol = \get_comment_meta( $comment->comment_ID, 'protocol', true );
// TODO Test if this is returned by Model/Comment // TODO Test if this is returned by Model/Comment
if ( 'activitypub' === $protocol ) { 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 ) { if ( $updated ) {
$format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); $format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
$formatted_datetime = \date_i18n( $format, \strtotime( $updated ) ); $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) * Fires immediately before comment status transition hooks are fired. (useful only for admin)
*/ */
public static function edit_comment( $comment_id, $commentdata ) { 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'] ); $user = \get_userdata( $commentdata['user_id'] );
if ( \in_array( 'administrator', $user->roles ) ) { if ( \in_array( 'administrator', $user->roles ) ) {
$wp_comment = \get_comment( $comment_id ); $wp_comment = \get_comment( $comment_id );

View file

@ -14,7 +14,6 @@ class Mention {
*/ */
public static function init() { public static function init() {
\add_filter( 'the_content', array( self::class, 'the_content' ), 99, 2 ); \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 ); 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;
}
} }

View file

@ -452,7 +452,7 @@ class Inbox {
$commentdata['comment_content'] = \wp_filter_kses( $object['object']['content'] ); $commentdata['comment_content'] = \wp_filter_kses( $object['object']['content'] );
$commentdata['comment_meta']['avatar_url'] = \esc_url_raw( $meta['icon']['url'] ); $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_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 ); $commentdata['comment_meta']['ap_object'] = \serialize( $object );
// disable flood control // disable flood control

View file

@ -65,13 +65,12 @@ class Comment {
$object->set_id( $this->get_id() ); $object->set_id( $this->get_id() );
$object->set_url( \get_comment_link( $comment->ID ) ); $object->set_url( \get_comment_link( $comment->ID ) );
$object->set_context( \get_permalink( $comment->comment_post_ID ) );
$object->set_type( 'Note' ); $object->set_type( 'Note' );
$published = \strtotime( $comment->comment_date_gmt ); $published = \strtotime( $comment->comment_date_gmt );
$object->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', $published ) ); $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 ) { if ( $updated > $published ) {
$object->set_updated( \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( $updated ) ) ); $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 ), get_rest_url_by_path( $path ),
) )
); );
$object->set_cc( $this->get_cc() );
$object->set_tag( $this->get_tags() );
return $object; return $object;
} }
@ -113,68 +111,6 @@ class Comment {
return Users::get_by_id( $this->wp_comment->user_id )->get_url(); 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. * Returns the content for the ActivityPub Item.
* *
@ -191,44 +127,9 @@ class Comment {
$content = \preg_replace( '/[\n\r\t]/', '', $content ); $content = \preg_replace( '/[\n\r\t]/', '', $content );
$content = \trim( $content ); $content = \trim( $content );
$content = \apply_filters( 'the_content', $content, $comment );
$content = \html_entity_decode( $content, \ENT_QUOTES, 'UTF-8' );
return $content; 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) * get_parent_uri (in reply to)
* takes a comment and returns * takes a comment and returns
@ -284,22 +185,23 @@ class Comment {
* Checks a comment ID for a source_id, or source_url * Checks a comment ID for a source_id, or source_url
*/ */
protected function get_source_id( $comment ) { protected function get_source_id( $comment ) {
if ( ! $comment->user_id ) {
if ( $comment->user_id !== 0 ) { return null;
}
$source_id = \get_comment_meta( $comment->ID, 'source_id', true ); $source_id = \get_comment_meta( $comment->ID, 'source_id', true );
if ( ! $source_id ) { if ( ! $source_id ) {
$source_url = \get_comment_meta( $comment->ID, 'source_url', true ); $source_url = \get_comment_meta( $comment->ID, 'source_url', true );
if ( ! $source_url ) { if ( ! $source_url ) {
return null; return null;
} }
$response = safe_remote_get( $source_url ); $response = \safe_remote_get( $source_url );
$body = \wp_remote_retrieve_body( $response ); $body = \wp_remote_retrieve_body( $response );
$remote_status = \json_decode( $body, true ); $remote_status = \json_decode( $body, true );
if ( is_wp_error( $remote_status ) if ( \is_wp_error( $remote_status )
|| ! isset( $remote_status['@context'] ) || ! isset( $remote_status['@context'] )
|| ! isset( $remote_status['object']['id'] ) ) { || ! isset( $remote_status['object']['id'] ) ) {
// the original post may have been deleted, before we started processing deletes. // the original post may have been deleted, before we started processing deletes.
return null; return null;
} }
@ -307,6 +209,4 @@ class Comment {
} }
return $source_id; return $source_id;
} }
return null;
}
} }