From 57a95fad0108bfb0abbb4ea03115ca1252d36ae1 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Fri, 2 Dec 2022 14:27:00 +0100 Subject: [PATCH] Add attachment support --- .../class-friends-feed-parser-activitypub.php | 23 +++++++++++++++++++ ...-class-friends-feed-parser-activitypub.php | 20 +++++++++++++--- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/integration/class-friends-feed-parser-activitypub.php b/integration/class-friends-feed-parser-activitypub.php index 69b1c76..34f3d77 100644 --- a/integration/class-friends-feed-parser-activitypub.php +++ b/integration/class-friends-feed-parser-activitypub.php @@ -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'] .= ''; + $data['content'] .= '

'; + $data['content'] .= ''; + } + $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( 'Received feed item', array( diff --git a/tests/test-class-friends-feed-parser-activitypub.php b/tests/test-class-friends-feed-parser-activitypub.php index 397eaab..f48efb1 100644 --- a/tests/test-class-friends-feed-parser-activitypub.php +++ b/tests/test-class-friends-feed-parser-activitypub.php @@ -29,6 +29,9 @@ class Test_Friends_Feed_Parser_ActivityPub extends \WP_UnitTestCase { $request->set_param( 'id', $id ); $request->set_param( 'actor', $this->actor ); + $attachment_url = 'https://mastodon.local/files/original/1234.png'; + $attachment_width = 400; + $attachment_height = 600; $request->set_param( 'object', array( @@ -38,6 +41,18 @@ class Test_Friends_Feed_Parser_ActivityPub extends \WP_UnitTestCase { 'content' => $content, 'url' => 'https://mastodon.local/users/akirk/statuses/' . ( $status_id++ ), '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( $content, $posts[0]->post_content ); + $this->assertStringStartsWith( $content, $posts[0]->post_content ); + $this->assertStringContainsString( '', $posts[0]->post_content ); $this->assertEquals( $this->friend_id, $posts[0]->post_author ); // 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 ) ); } - - public function set_up() { if ( ! class_exists( '\Friends\Friends' ) ) { return $this->markTestSkipped( 'The Friends plugin is not loaded.' );