-
Notifications
You must be signed in to change notification settings - Fork 73
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
Enable Mastodon Apps: support profile editing, blog user #788
Merged
Merged
Changes from 37 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
71fc2c8
Support for `api/v1/accounts/update_credentials` route
mattwiebe 29db809
Properly integrate with `mastodon_api_mapback_user_id`
mattwiebe 8ef9566
s/set/save/_
mattwiebe 6fbc881
Only set properties allowed by `Account`
mattwiebe 0737a74
better user -> blog ID conversion strategy
mattwiebe a33cbd2
Consolidate info single `save()` function
mattwiebe 9195851
Better update handling
mattwiebe 0813733
account for site_(icon|logo) duality
mattwiebe 9d33f3d
Populate note in source to unlock profile editing
mattwiebe 59fe2eb
Adjust to filter approach
mattwiebe 394063d
no longer using Blog class
mattwiebe daa12f7
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
pfefferle c624802
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
mattwiebe c371b97
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
mattwiebe 074684f
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
pfefferle a86d145
fix blog user
pfefferle 278a1b2
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
mattwiebe 9e615cc
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
pfefferle ba9c3f0
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
mattwiebe 2950898
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
pfefferle a19f59b
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
pfefferle f1eb441
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
mattwiebe 9607002
fix bad merge
mattwiebe 0367637
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
mattwiebe d357d86
Use current header and icon locations
mattwiebe 6803bdb
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
mattwiebe c4b65bc
move all `save(name)` functions to discrete `update_name` functions
mattwiebe 864cb01
Handle submitted `fields_attributes` aks Extra Fields
mattwiebe aa4b568
handle deletes, really use the incoming fields
mattwiebe 03f23d1
publish new posts
mattwiebe a2bed42
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
mattwiebe 5bb67b4
only make blocks for block-supporting sites
mattwiebe 750b039
moar site_supports_blocks
mattwiebe 287daec
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
mattwiebe ce2316d
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
mattwiebe d4084d6
proper EMA loading
mattwiebe 404588b
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
mattwiebe fcdf8e1
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
mattwiebe 365af49
move EMA extra fields functions into EMA class
mattwiebe f5a55b4
remove mastodon 4 field notice
mattwiebe d022392
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
mattwiebe a600761
Merge branch 'master' into add/enable-mastodon-apps-profile-editing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
use WP_Query; | ||
use Activitypub\Collection\Users; | ||
|
||
use function Activitypub\site_supports_blocks; | ||
|
||
class Extra_Fields { | ||
|
||
const USER_POST_TYPE = 'ap_extrafield'; | ||
|
@@ -37,6 +39,23 @@ public static function get_actor_fields( $user_id ) { | |
return apply_filters( 'activitypub_get_actor_extra_fields', $fields, $user_id ); | ||
} | ||
|
||
private static function get_formatted_content( $post ) { | ||
$content = \get_the_content( null, false, $post ); | ||
$content = Link::the_content( $content, true ); | ||
if ( site_supports_blocks() ) { | ||
$content = \do_blocks( $content ); | ||
} | ||
$content = \wptexturize( $content ); | ||
$content = \wp_filter_content_tags( $content ); | ||
// replace script and style elements | ||
$content = \preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content ); | ||
$content = \strip_shortcodes( $content ); | ||
$content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) ); | ||
$content = \apply_filters( 'activitypub_extra_field_content', $content, $post ); | ||
|
||
return $content; | ||
} | ||
|
||
/** | ||
* Transforms the Extra Fields (Cutom Post Types) to ActivityPub Actor-Attachments. | ||
* | ||
|
@@ -56,16 +75,7 @@ function( $rel ) { | |
); | ||
|
||
foreach ( $fields as $post ) { | ||
$content = \get_the_content( null, false, $post ); | ||
$content = \do_blocks( $content ); | ||
$content = \wptexturize( $content ); | ||
$content = \wp_filter_content_tags( $content ); | ||
// replace script and style elements | ||
$content = \preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content ); | ||
$content = \strip_shortcodes( $content ); | ||
$content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) ); | ||
$content = \apply_filters( 'activitypub_extra_field_content', $content, $post ); | ||
|
||
$content = self::get_formatted_content( $post ); | ||
$attachments[] = array( | ||
'type' => 'PropertyValue', | ||
'name' => \get_the_title( $post ), | ||
|
@@ -211,10 +221,7 @@ function( $rel ) { | |
'post_title' => $title, | ||
'post_status' => 'publish', | ||
'post_author' => $user_id, | ||
'post_content' => sprintf( | ||
'<!-- wp:paragraph --><p>%s</p><!-- /wp:paragraph -->', | ||
Link::the_content( $url ) | ||
), | ||
'post_content' => self::make_paragraph_block( Link::the_content( $url ) ), | ||
'comment_status' => 'closed', | ||
'menu_order' => $menu_order, | ||
); | ||
|
@@ -231,6 +238,64 @@ function( $rel ) { | |
return $extra_fields; | ||
} | ||
|
||
public static function get_extra_fields_for_mastodon_api( $user_id ) { | ||
$ret = array(); | ||
$fields = self::get_actor_fields( $user_id ); | ||
|
||
foreach ( $fields as $field ) { | ||
$ret[] = array( | ||
'name' => $field->post_title, | ||
'value' => self::get_formatted_content( $field ), | ||
); | ||
} | ||
|
||
return $ret; | ||
} | ||
|
||
private static function make_paragraph_block( $content ) { | ||
if ( ! site_supports_blocks() ) { | ||
return $content; | ||
} | ||
return '<!-- wp:paragraph --><p>' . $content . '</p><!-- /wp:paragraph -->'; | ||
} | ||
|
||
public static function set_extra_fields_from_mastodon_api( $user_id, $fields ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should have all "enable mastodon apps" functions in the integration file and not mix them up with the "core" files!?! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup sounds good to me |
||
// The Mastodon API submits a simple hash, every field. | ||
// We can reasonably assume a similar order for our operations below. | ||
$ids = wp_list_pluck( self::get_actor_fields( $user_id ), 'ID' ); | ||
$is_blog = self::is_blog( $user_id ); | ||
$post_type = $is_blog ? self::BLOG_POST_TYPE : self::USER_POST_TYPE; | ||
|
||
foreach ( $fields as $i => $field ) { | ||
$post_id = $ids[ $i ] ?? null; | ||
$has_post = $post_id && \get_post( $post_id ); | ||
$args = array( | ||
'post_title' => $field['name'], | ||
'post_content' => self::make_paragraph_block( $field['value'] ), | ||
); | ||
|
||
if ( $has_post ) { | ||
$args['ID'] = $ids[ $i ]; | ||
\wp_update_post( $args ); | ||
} else { | ||
$args['post_type'] = $post_type; | ||
$args['post_status'] = 'publish'; | ||
if ( ! $is_blog ) { | ||
$args['post_author'] = $user_id; | ||
} | ||
\wp_insert_post( $args ); | ||
} | ||
} | ||
|
||
// Delete any remaining fields. | ||
if ( \count( $fields ) < \count( $ids ) ) { | ||
$to_delete = \array_slice( $ids, \count( $fields ) ); | ||
foreach ( $to_delete as $id ) { | ||
\wp_delete_post( $id, true ); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the user is the blog user. | ||
* @param int $user_id The user ID. | ||
|
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
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.
That is not completely correct! You can only add 4 extra fields through the Mastodon frontend, but it displays way more than these 4 when a different platform supports/provides it.
See my blog as example:
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.
Nice find, I foolishly believed something I read in an API doc rather than testing things :D