Add attachment support

This commit is contained in:
Alex Kirk 2022-12-02 14:27:00 +01:00
parent 2542127d72
commit 57a95fad01
2 changed files with 40 additions and 3 deletions

View file

@ -237,6 +237,29 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser {
} }
} }
if ( ! empty( $object['attachment'] ) ) {
foreach ( $object['attachment'] as $attachment ) {
if ( ! isset( $attachment['type'] ) || ! isset( $attachment['mediaType'] ) ) {
continue;
}
if ( 'Document' !== $attachment['type'] || strpos( $attachment['mediaType'], 'image/' ) !== 0 ) {
continue;
}
$data['content'] .= PHP_EOL;
$data['content'] .= '<!-- wp:image -->';
$data['content'] .= '<p><img src="' . esc_url( $attachment['url'] ) . '" width="' . esc_attr( $attachment['width'] ) . '" height="' . esc_attr( $attachment['height'] ) . '" /></p>';
$data['content'] .= '<!-- /wp:image -->';
}
$meta = \Activitypub\get_remote_metadata_by_actor( $object['attributedTo'] );
$this->log( 'Attributed to ' . $object['attributedTo'], compact( 'meta' ) );
if ( isset( $meta['name'] ) ) {
$data['author'] = $meta['name'];
} elseif ( isset( $meta['preferredUsername'] ) ) {
$data['author'] = $meta['preferredUsername'];
}
}
$this->log( $this->log(
'Received feed item', 'Received feed item',
array( array(

View file

@ -29,6 +29,9 @@ class Test_Friends_Feed_Parser_ActivityPub extends \WP_UnitTestCase {
$request->set_param( 'id', $id ); $request->set_param( 'id', $id );
$request->set_param( 'actor', $this->actor ); $request->set_param( 'actor', $this->actor );
$attachment_url = 'https://mastodon.local/files/original/1234.png';
$attachment_width = 400;
$attachment_height = 600;
$request->set_param( $request->set_param(
'object', 'object',
array( array(
@ -38,6 +41,18 @@ class Test_Friends_Feed_Parser_ActivityPub extends \WP_UnitTestCase {
'content' => $content, 'content' => $content,
'url' => 'https://mastodon.local/users/akirk/statuses/' . ( $status_id++ ), 'url' => 'https://mastodon.local/users/akirk/statuses/' . ( $status_id++ ),
'published' => $date, 'published' => $date,
'attachment' => array(
array(
'type' => 'Document',
'mediaType' => 'image/png',
'url' => $attachment_url,
'name' => '',
'blurhash' => '',
'width' => $attachment_width,
'height' => $attachment_height,
),
),
) )
); );
@ -52,7 +67,8 @@ class Test_Friends_Feed_Parser_ActivityPub extends \WP_UnitTestCase {
); );
$this->assertEquals( $post_count + 1, count( $posts ) ); $this->assertEquals( $post_count + 1, count( $posts ) );
$this->assertEquals( $content, $posts[0]->post_content ); $this->assertStringStartsWith( $content, $posts[0]->post_content );
$this->assertStringContainsString( '<img src="' . esc_url( $attachment_url ) . '" width="' . esc_attr( $attachment_width ) . '" height="' . esc_attr( $attachment_height ) . '" />', $posts[0]->post_content );
$this->assertEquals( $this->friend_id, $posts[0]->post_author ); $this->assertEquals( $this->friend_id, $posts[0]->post_author );
// Do another test post, this time with a URL that has an @-id. // Do another test post, this time with a URL that has an @-id.
@ -144,8 +160,6 @@ class Test_Friends_Feed_Parser_ActivityPub extends \WP_UnitTestCase {
$this->assertEquals( 'Matthias Pfefferle', get_post_meta( $posts[0]->ID, 'author', true ) ); $this->assertEquals( 'Matthias Pfefferle', get_post_meta( $posts[0]->ID, 'author', true ) );
} }
public function set_up() { public function set_up() {
if ( ! class_exists( '\Friends\Friends' ) ) { if ( ! class_exists( '\Friends\Friends' ) ) {
return $this->markTestSkipped( 'The Friends plugin is not loaded.' ); return $this->markTestSkipped( 'The Friends plugin is not loaded.' );