Skip to content

Commit 929b5dc

Browse files
pfefferleobenland
andauthored
Add: Preview support for Articles (#1331)
* Add: Preview support for Article This adds a fallback to an Article for Microblogging services like Mastodon, because the `summary` is used for content warnings and therefore not reliable enough. * Added changelog * Add tests * update readme * Update CHANGELOG.md Co-authored-by: Konstantin Obenland <[email protected]> * Update changelog * remove empty line --------- Co-authored-by: Konstantin Obenland <[email protected]>
1 parent 1810c10 commit 929b5dc

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
* A fallback `Note` for `Article` objects to improve previews on services that don't support Articles yet.
1213
* A reply `context` for Posts and Comments to allow relying parties to discover the whole conversation of a thread.
1314
* Setting to adjust the number of days Outbox items are kept before being purged.
1415
* Undo API for Outbox items.

includes/transformer/class-post.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,4 +1148,20 @@ public function get_shares() {
11481148
'totalItems' => Interactions::count_by_type( $this->item->ID, 'repost' ),
11491149
);
11501150
}
1151+
1152+
/**
1153+
* Get the preview of the post.
1154+
*
1155+
* @return array|null The preview of the post or null if the post is not an Article.
1156+
*/
1157+
public function get_preview() {
1158+
if ( 'Article' !== $this->get_type() ) {
1159+
return null;
1160+
}
1161+
1162+
return array(
1163+
'type' => 'Note',
1164+
'content' => $this->get_summary(),
1165+
);
1166+
}
11511167
}

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ For reasons of data protection, it is not possible to see the followers of other
131131

132132
= Unreleased =
133133

134+
* Added: A fallback `Note` for `Article` objects to improve previews on services that don't support Articles yet.
134135
* Added: A reply `context` for Posts and Comments to allow relying parties to discover the whole conversation of a thread.
135136
* Added: Allow Activities on URLs instead of requiring Activity-Objects. This is useful especially for sending Announces and Likes.
136137
* Added: Undo API for Outbox items.

tests/includes/transformer/class-test-post.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,4 +624,44 @@ public function create_upload_object( $file, $parent_id = 0 ) {
624624

625625
return $id;
626626
}
627+
628+
/**
629+
* Test preview property generation.
630+
*
631+
* @covers ::get_preview
632+
*/
633+
public function test_preview_property() {
634+
// Create a test post of type "Article".
635+
$post = $this->factory->post->create_and_get(
636+
array(
637+
'post_title' => 'Test Article',
638+
'post_content' => str_repeat( 'Long content. ', 100 ),
639+
'post_status' => 'publish',
640+
)
641+
);
642+
643+
$transformer = new Post( $post );
644+
$preview = $transformer->get_preview();
645+
646+
// Check if the preview for an Article is correctly generated.
647+
$this->assertIsArray( $preview );
648+
$this->assertEquals( 'Note', $preview['type'] );
649+
$this->assertArrayHasKey( 'content', $preview );
650+
$this->assertNotEmpty( $preview['content'] );
651+
652+
// Create a test post of type "Note" (short content).
653+
$note_post = $this->factory->post->create_and_get(
654+
array(
655+
'post_title' => '',
656+
'post_content' => 'Short note content',
657+
'post_status' => 'publish',
658+
)
659+
);
660+
661+
$note_transformer = new Post( $note_post );
662+
$note_preview = $note_transformer->get_preview();
663+
664+
// Check if the preview for a Note is null.
665+
$this->assertNull( $note_preview );
666+
}
627667
}

0 commit comments

Comments
 (0)