2020class Mastodon_API {
2121 const ACTIVITYPUB_USERNAME_REGEXP = '(?:([A-Za-z0-9_-]+)@((?:[A-Za-z0-9_-]+\.)+[A-Za-z]+)) ' ;
2222 const VERSION = ENABLE_MASTODON_APPS_VERSION ;
23+
24+ /**
25+ * The default term for bookmarking a status.
26+ */
27+ const DEFAULT_BOOKMARK = 'bookmark ' ;
28+
2329 /**
2430 * The OAuth handler.
2531 *
@@ -38,6 +44,7 @@ class Mastodon_API {
3844 const APP_TAXONOMY = 'mastodon-app ' ;
3945 const REMOTE_USER_TAXONOMY = 'mastodon-api-remote-user ' ;
4046 const CPT = 'enable-mastodon-apps ' ;
47+ const BOOKMARKS_TAXONOMY_PREFIX = 'mastodon-api-bookmarks- ' ;
4148
4249 /**
4350 * Constructor
@@ -150,6 +157,8 @@ public function rewrite_rules() {
150157 'api/v1/statuses/([0-9]+)/unfavourite ' => 'api/v1/statuses/$matches[1]/unfavourite ' ,
151158 'api/v1/statuses/([0-9]+)/reblog ' => 'api/v1/statuses/$matches[1]/reblog ' ,
152159 'api/v1/statuses/([0-9]+)/unreblog ' => 'api/v1/statuses/$matches[1]/unreblog ' ,
160+ 'api/v1/statuses/([0-9]+)/bookmark ' => 'api/v1/statuses/$matches[1]/bookmark ' ,
161+ 'api/v1/statuses/([0-9]+)/unbookmark ' => 'api/v1/statuses/$matches[1]/unbookmark ' ,
153162 'api/v1/notifications/([^/]+)/dismiss ' => 'api/v1/notifications/$matches[1]/dismiss ' ,
154163 'api/v1/notifications/([^/|$]+)/?$ ' => 'api/v1/notifications/$matches[1] ' ,
155164 'api/v1/notifications ' => 'api/v1/notifications ' ,
@@ -263,7 +272,7 @@ public function add_rest_routes() {
263272 'api/v1/bookmarks ' ,
264273 array (
265274 'methods ' => 'GET ' ,
266- 'callback ' => ' __return_empty_array ' ,
275+ 'callback ' => array ( $ this , ' api_bookmarks ' ) ,
267276 'permission_callback ' => array ( $ this , 'logged_in_permission ' ),
268277 )
269278 );
@@ -522,6 +531,26 @@ public function add_rest_routes() {
522531 )
523532 );
524533
534+ register_rest_route (
535+ self ::PREFIX ,
536+ 'api/v1/statuses/(?P<post_id>[0-9]+)/bookmark ' ,
537+ array (
538+ 'methods ' => array ( 'POST ' , 'OPTIONS ' ),
539+ 'callback ' => array ( $ this , 'api_bookmark_post ' ),
540+ 'permission_callback ' => array ( $ this , 'logged_in_permission ' ),
541+ )
542+ );
543+
544+ register_rest_route (
545+ self ::PREFIX ,
546+ 'api/v1/statuses/(?P<post_id>[0-9]+)/unbookmark ' ,
547+ array (
548+ 'methods ' => array ( 'POST ' , 'OPTIONS ' ),
549+ 'callback ' => array ( $ this , 'api_unbookmark_post ' ),
550+ 'permission_callback ' => array ( $ this , 'logged_in_permission ' ),
551+ )
552+ );
553+
525554 register_rest_route (
526555 self ::PREFIX ,
527556 'api/v1/statuses/(?P<post_id>[0-9]+) ' ,
@@ -1098,7 +1127,7 @@ function ( $user_id ) {
10981127 'reblogged ' => $ reblogged ,
10991128 'reblogged_by ' => $ reblogged_by ,
11001129 'muted ' => false ,
1101- 'bookmarked ' => false ,
1130+ 'bookmarked ' => $ this -> check_if_status_bookmarked ( $ user_id , $ post -> ID ) ,
11021131 'content ' => $ this ->normalize_whitespace ( $ post ->post_title . PHP_EOL . $ post ->post_content ),
11031132 'filtered ' => array (),
11041133 'reblog ' => null ,
@@ -1858,7 +1887,7 @@ public function convert_activity_to_status( $activity, $user_id ) {
18581887 'favourited ' => false ,
18591888 'reblogged ' => false ,
18601889 'muted ' => false ,
1861- 'bookmarked ' => false ,
1890+ 'bookmarked ' => $ this -> check_if_status_bookmarked ( $ user_id , $ id_parts ) ,
18621891 'content ' => $ activity ['object ' ]['content ' ],
18631892 'filtered ' => array (),
18641893 'reblog ' => null ,
@@ -2890,4 +2919,113 @@ public function api_instance_v2() {
28902919
28912920 return apply_filters ( 'mastodon_api_instance_v2 ' , $ ret );
28922921 }
2922+
2923+ public function api_bookmarks ( $ request ) {
2924+ $ user_id = get_current_user_id ();
2925+ if ( ! $ user_id ) {
2926+ return array ();
2927+ }
2928+
2929+ $ args = $ this ->get_posts_query_args ( $ request );
2930+ if ( empty ( $ args ) ) {
2931+ return array ();
2932+ }
2933+ $ args = apply_filters ( 'mastodon_api_timelines_args ' , $ args , $ request );
2934+
2935+ $ this ->register_bookmarks_taxonomy ( $ user_id );
2936+
2937+ $ args ['numberposts ' ] = -1 ;
2938+ $ args ['post_type ' ] = apply_filters ( 'friends_frontend_post_types ' , array ( 'post ' ) );
2939+ $ args ['tax_query ' ] = array (
2940+ array (
2941+ 'taxonomy ' => self ::BOOKMARKS_TAXONOMY_PREFIX . $ user_id ,
2942+ 'field ' => 'slug ' ,
2943+ 'terms ' => self ::DEFAULT_BOOKMARK ,
2944+ ),
2945+ );
2946+
2947+ return $ this ->get_posts ( $ args );
2948+ }
2949+
2950+ public function api_bookmark_post ( $ request ) {
2951+ $ user_id = get_current_user_id ();
2952+ $ post_id = $ request ->get_param ( 'post_id ' );
2953+ if ( ! $ post_id || ! $ post_id ) {
2954+ return false ;
2955+ }
2956+
2957+ $ post_id = $ this ->maybe_get_remapped_reblog_id ( $ post_id );
2958+
2959+ do_action ( 'mastodon_api_bookmark ' , $ post_id );
2960+
2961+ $ this ->register_bookmarks_taxonomy ( $ user_id );
2962+
2963+ wp_set_object_terms ( $ post_id , self ::DEFAULT_BOOKMARK , self ::BOOKMARKS_TAXONOMY_PREFIX . $ user_id , true );
2964+
2965+ $ post = get_post ( $ post_id );
2966+
2967+ return $ this ->get_status_array ( $ post );
2968+ }
2969+
2970+ public function api_unbookmark_post ( $ request ) {
2971+ $ user_id = get_current_user_id ();
2972+ $ post_id = $ request ->get_param ( 'post_id ' );
2973+ if ( ! $ post_id ) {
2974+ return false ;
2975+ }
2976+
2977+ $ post_id = $ this ->maybe_get_remapped_reblog_id ( $ post_id );
2978+
2979+ do_action ( 'mastodon_api_unbookmark ' , $ post_id );
2980+
2981+ $ this ->register_bookmarks_taxonomy ( $ user_id );
2982+
2983+ wp_remove_object_terms ( $ post_id , self ::DEFAULT_BOOKMARK , self ::BOOKMARKS_TAXONOMY_PREFIX . $ user_id , true );
2984+
2985+ $ post = get_post ( $ post_id );
2986+
2987+ return $ this ->get_status_array ( $ post );
2988+ }
2989+
2990+ private function check_if_status_bookmarked ( $ user_id , $ post_id ) {
2991+ if ( ! $ post_id || ! $ user_id ) {
2992+ return false ;
2993+ }
2994+
2995+ $ this ->register_bookmarks_taxonomy ( $ user_id );
2996+
2997+ $ term = false ;
2998+ $ terms = wp_get_object_terms ( $ post_id , self ::BOOKMARKS_TAXONOMY_PREFIX . $ user_id );
2999+ if ( is_array ( $ terms ) ) {
3000+ foreach ( $ terms as $ t ) {
3001+ if ( $ t ->slug === self ::DEFAULT_BOOKMARK ) {
3002+ return true ;
3003+ }
3004+ }
3005+ }
3006+ }
3007+
3008+ private function register_bookmarks_taxonomy ( $ user_id ) {
3009+ if ( false === $ user_id ) {
3010+ return ;
3011+ }
3012+ $ args = array (
3013+ 'labels ' => array (
3014+ 'name ' => 'Mastodon Bookmarks ' ,
3015+ 'singular_name ' => 'Mastodon Bookmark ' ,
3016+ 'menu_name ' => 'Mastodon Bookmarks ' ,
3017+ ),
3018+ 'public ' => false ,
3019+ 'show_ui ' => false ,
3020+ 'show_in_menu ' => false ,
3021+ 'show_in_rest ' => false ,
3022+ 'rewrite ' => false ,
3023+ );
3024+
3025+ register_taxonomy ( self ::BOOKMARKS_TAXONOMY_PREFIX . $ user_id , null , $ args );
3026+
3027+ if ( is_wp_error ( get_term ( self ::DEFAULT_BOOKMARK , self ::BOOKMARKS_TAXONOMY_PREFIX . $ user_id ) ) ) {
3028+ $ result = wp_insert_term ( self ::DEFAULT_BOOKMARK , self ::BOOKMARKS_TAXONOMY_PREFIX . $ user_id );
3029+ }
3030+ }
28933031}
0 commit comments