-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Outbox Collection #593
Draft
pfefferle
wants to merge
35
commits into
trunk
Choose a base branch
from
add/outbox-collection
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Outbox Collection #593
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
5565c26
init
pfefferle 7448cd6
outbox should not be public!
pfefferle 6ac167f
add basic `to_activity` function
pfefferle 704030c
Merge branch 'master' into add/outbox-collection
pfefferle 89ece40
do not allow to instance post transformer
pfefferle 0ba6d7b
Merge branch 'master' into add/outbox-collection
pfefferle efda4a5
fix tests
pfefferle 1558551
Merge branch 'master' into add/outbox-collection
pfefferle 6edc4b5
Merge branch 'master' into add/outbox-collection
pfefferle a38c565
Merge branch 'master' into add/outbox-collection
pfefferle 2a3a6c3
Merge branch 'trunk' into add/outbox-collection
pfefferle 5d66494
fix phpcs
pfefferle b1d260d
revert change
pfefferle 81b0b88
remove unused `use` declarations
pfefferle 8c0c3fa
the handler should not handle outgoing stuff
pfefferle 3eb8ca6
Merge branch 'trunk' into add/outbox-collection
pfefferle 43a3343
Update class-activitypub.php
pfefferle 8746447
fix PHPCS
pfefferle 242dea4
more PHPCS fixes
pfefferle f64aade
fix namespace issue
pfefferle 5ca19a6
remove unneeded function
pfefferle 9f525b0
fix sticky post endpoint
pfefferle e0aeefd
no need to check for User-ID
pfefferle 2d3ba4b
Merge branch 'trunk' into add/outbox-collection
pfefferle 1433e4f
Merge branch 'trunk' into add/outbox-collection
pfefferle 943fc02
Merge branch 'trunk' into add/outbox-collection
pfefferle 7f0a227
Merge branch 'trunk' into add/outbox-collection
pfefferle fb98bd2
Merge branch 'trunk' into add/outbox-collection
pfefferle 945b335
Merge branch 'trunk' into add/outbox-collection
pfefferle 9345991
support JSON and Arrays beside WP_Comments and WP_Posts
pfefferle d1e9839
fix auto-complete issue
pfefferle 59ab8b9
simplify code.
pfefferle 8133a1c
convert JSON to Activity_Object to not have everything duplicated
pfefferle 0bdb30d
add support for maps and mentions
pfefferle 593740f
Merge branch 'trunk' into add/outbox-collection
pfefferle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Outbox collection file. | ||
* | ||
* @package Activitypub | ||
*/ | ||
|
||
namespace Activitypub\Collection; | ||
|
||
use Activitypub\Transformer\Factory; | ||
|
||
/** | ||
* ActivityPub Outbox Collection | ||
*/ | ||
class Outbox { | ||
const POST_TYPE = 'ap_outbox'; | ||
|
||
/** | ||
* Add an Item to the outbox. | ||
* | ||
* @param string|array|Base_Object|WP_Post|WP_Comment $item The item to add. | ||
* @param int $user_id The user ID. | ||
* @param string $activity_type The activity type. | ||
* | ||
* @return mixed The added item or an error. | ||
*/ | ||
public static function add_item( $item, $user_id, $activity_type = 'Create' ) { | ||
$transformer = Factory::get_transformer( $item ); | ||
$object = $transformer->transform(); | ||
|
||
if ( ! $object || is_wp_error( $object ) ) { | ||
return $object; | ||
} | ||
|
||
$outbox_item = array( | ||
'post_type' => self::POST_TYPE, | ||
'post_title' => $object->get_id(), | ||
'post_content' => $object->to_json(), | ||
'post_author' => $user_id, | ||
'post_status' => 'draft', | ||
); | ||
|
||
$has_kses = false !== \has_filter( 'content_save_pre', 'wp_filter_post_kses' ); | ||
if ( $has_kses ) { | ||
// Prevent KSES from corrupting JSON in post_content. | ||
\kses_remove_filters(); | ||
} | ||
|
||
$result = \wp_insert_post( $outbox_item, true ); | ||
|
||
if ( $has_kses ) { | ||
\kses_init_filters(); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?php | ||
/** | ||
* Activity Object Transformer Class. | ||
* | ||
* @package Activitypub | ||
*/ | ||
|
||
namespace Activitypub\Transformer; | ||
|
||
/** | ||
* Activity Object Transformer Class. | ||
*/ | ||
class Activity_Object extends Base { | ||
/** | ||
* Transform the WordPress Object into an ActivityPub Object. | ||
* | ||
* @return Base_Object The ActivityPub Object. | ||
*/ | ||
public function to_object() { | ||
return $this->transform_object_properties( $this->item ); | ||
} | ||
|
||
/** | ||
* Get the ID of the WordPress Object. | ||
* | ||
* @return string The ID of the WordPress Object. | ||
*/ | ||
protected function get_id() { | ||
return ''; | ||
} | ||
|
||
/** | ||
* Helper function to get the @-Mentions from the post content. | ||
* | ||
* @return array The list of @-Mentions. | ||
*/ | ||
protected function get_mentions() { | ||
/** | ||
* Filter the mentions in the post content. | ||
* | ||
* @param array $mentions The mentions. | ||
* @param string $content The post content. | ||
* @param WP_Post $post The post object. | ||
* | ||
* @return array The filtered mentions. | ||
*/ | ||
return apply_filters( | ||
'activitypub_extract_mentions', | ||
array(), | ||
$this->item->get_content() . ' ' . $this->item->get_summary(), | ||
$this->item | ||
); | ||
} | ||
|
||
/** | ||
* Returns a list of Mentions, used in the Post. | ||
* | ||
* @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 $url ) { | ||
$cc[] = $url; | ||
} | ||
} | ||
|
||
return $cc; | ||
} | ||
|
||
/** | ||
* Returns the content map for the post. | ||
* | ||
* @return array The content map for the post. | ||
*/ | ||
protected function get_content_map() { | ||
$content = $this->item->get_content(); | ||
|
||
if ( ! $content ) { | ||
return null; | ||
} | ||
|
||
return array( | ||
$this->get_locale() => $content, | ||
); | ||
} | ||
|
||
/** | ||
* Returns the name map for the post. | ||
* | ||
* @return array The name map for the post. | ||
*/ | ||
protected function get_name_map() { | ||
$name = $this->item->get_name(); | ||
|
||
if ( ! $name ) { | ||
return null; | ||
} | ||
|
||
return array( | ||
$this->get_locale() => $name, | ||
); | ||
} | ||
|
||
/** | ||
* Returns the summary map for the post. | ||
* | ||
* @return array The summary map for the post. | ||
*/ | ||
protected function get_summary_map() { | ||
$summary = $this->item->get_summary(); | ||
|
||
if ( ! $summary ) { | ||
return null; | ||
} | ||
|
||
return array( | ||
$this->get_locale() => $summary, | ||
); | ||
} | ||
|
||
/** | ||
* Returns a list of Tags, used in the Comment. | ||
* | ||
* This includes Hash-Tags and Mentions. | ||
* | ||
* @return array The list of Tags. | ||
*/ | ||
protected function get_tag() { | ||
$tags = $this->item->get_tags(); | ||
|
||
if ( ! $tags ) { | ||
$tags = array(); | ||
} | ||
|
||
$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 \array_unique( $tags, SORT_REGULAR ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be simplified to ^?
If
get_mentions()
always returned an array, the fallback would not be needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove the
if
then and directly iterate but we can't simply returnget_mention()
because it has some more data, not only the list of URLs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And we could use an
array_filter
instead!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, maybe
return array_values( $this->get_mentions() );
?Currently the function doesn't remove anything from
$mentions
, it just puts everything in$cc
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, array_values should do the trick! It thought it has some more informations, but it's simply the key is the WebFinger handle and the URL is the value!