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 reaction for favouriting a status.
26+ *
27+ * 2b50 = star
28+ * 2764 = heart
29+ */
30+ const DEFAULT_REACT = '2b50 ' ;
31+
2332 /**
2433 * The OAuth handler.
2534 *
@@ -38,6 +47,7 @@ class Mastodon_API {
3847 const APP_TAXONOMY = 'mastodon-app ' ;
3948 const REMOTE_USER_TAXONOMY = 'mastodon-api-remote-user ' ;
4049 const CPT = 'enable-mastodon-apps ' ;
50+ const FAVOURITES_TAXONOMY_PREFIX = 'mastodon-api-favourites- ' ;
4151
4252 /**
4353 * Constructor
@@ -282,7 +292,7 @@ public function add_rest_routes() {
282292 'api/v1/favourites ' ,
283293 array (
284294 'methods ' => 'GET ' ,
285- 'callback ' => ' __return_empty_array ' ,
295+ 'callback ' => array ( $ this , ' api_favourites ' ) ,
286296 'permission_callback ' => array ( $ this , 'logged_in_permission ' ),
287297 )
288298 );
@@ -1092,9 +1102,9 @@ function ( $user_id ) {
10921102 'url ' => null ,
10931103 'replies_count ' => 0 ,
10941104 'reblogs_count ' => 0 ,
1095- 'favourites_count ' => 0 ,
1105+ 'favourites_count ' => $ this -> get_post_favourite_count ( $ post -> ID ) ,
10961106 'edited_at ' => null ,
1097- 'favourited ' => false ,
1107+ 'favourited ' => $ this -> check_if_status_favourited ( $ user_id , $ post -> ID ) ,
10981108 'reblogged ' => $ reblogged ,
10991109 'reblogged_by ' => $ reblogged_by ,
11001110 'muted ' => false ,
@@ -1711,33 +1721,43 @@ public function api_get_post_context( $request ) {
17111721 }
17121722
17131723 public function api_favourite_post ( $ request ) {
1724+ $ user_id = get_current_user_id ();
17141725 $ post_id = $ request ->get_param ( 'post_id ' );
1715- if ( ! $ post_id ) {
1726+ if ( ! $ post_id || ! $ user_id ) {
17161727 return false ;
17171728 }
17181729
17191730 $ post_id = $ this ->maybe_get_remapped_reblog_id ( $ post_id );
17201731
1721- // 2b50 = star
1722- // 2764 = heart
1723- do_action ( 'mastodon_api_react ' , $ post_id , '2b50 ' );
1732+ do_action ( 'mastodon_api_react ' , $ post_id , self ::DEFAULT_REACT );
1733+
1734+ $ this ->register_favourites_taxonomy ( $ user_id );
1735+
1736+ $ term = wp_set_object_terms ( $ post_id , self ::DEFAULT_REACT , self ::FAVOURITES_TAXONOMY_PREFIX . $ user_id , true );
1737+
1738+ $ this ->update_favourite_count_on_post ( $ post_id , 1 );
17241739
17251740 $ post = get_post ( $ post_id );
17261741
17271742 return $ this ->get_status_array ( $ post );
17281743 }
17291744
17301745 public function api_unfavourite_post ( $ request ) {
1746+ $ user_id = get_current_user_id ();
17311747 $ post_id = $ request ->get_param ( 'post_id ' );
1732- if ( ! $ post_id ) {
1748+ if ( ! $ post_id || ! $ user_id ) {
17331749 return false ;
17341750 }
17351751
17361752 $ post_id = $ this ->maybe_get_remapped_reblog_id ( $ post_id );
17371753
1738- // 2b50 = star
1739- // 2764 = heart
1740- do_action ( 'mastodon_api_unreact ' , $ post_id , '2b50 ' );
1754+ do_action ( 'mastodon_api_unreact ' , $ post_id , self ::DEFAULT_REACT );
1755+
1756+ $ this ->register_favourites_taxonomy ( $ user_id );
1757+
1758+ $ term = wp_remove_object_terms ( $ post_id , self ::DEFAULT_REACT , self ::FAVOURITES_TAXONOMY_PREFIX . $ user_id , true );
1759+
1760+ $ this ->update_favourite_count_on_post ( $ post_id , -1 );
17411761
17421762 $ post = get_post ( $ post_id );
17431763
@@ -1853,9 +1873,9 @@ public function convert_activity_to_status( $activity, $user_id ) {
18531873 'muted ' => false ,
18541874 'replies_count ' => 0 , // could be fetched.
18551875 'reblogs_count ' => 0 ,
1856- 'favourites_count ' => 0 ,
1876+ 'favourites_count ' => $ this -> get_post_favourite_count ( $ post -> ID ) ,
18571877 'edited_at ' => null ,
1858- 'favourited ' => false ,
1878+ 'favourited ' => $ this -> check_if_status_favourited ( $ user_id , $ id_parts ) ,
18591879 'reblogged ' => false ,
18601880 'muted ' => false ,
18611881 'bookmarked ' => false ,
@@ -2890,4 +2910,89 @@ public function api_instance_v2() {
28902910
28912911 return apply_filters ( 'mastodon_api_instance_v2 ' , $ ret );
28922912 }
2913+
2914+ private function check_if_status_favourited ( $ user_id , $ post_id ) {
2915+ if ( ! $ post_id || ! $ user_id ) {
2916+ return false ;
2917+ }
2918+
2919+ $ this ->register_favourites_taxonomy ( $ user_id );
2920+
2921+ $ term = false ;
2922+ $ terms = wp_get_object_terms ( $ post_id , self ::FAVOURITES_TAXONOMY_PREFIX . $ user_id );
2923+ if ( is_array ( $ terms ) ) {
2924+ foreach ( $ terms as $ t ) {
2925+ if ( $ t ->slug === $ reaction ) {
2926+ return true ;
2927+ }
2928+ }
2929+ }
2930+ }
2931+
2932+ private function get_post_favourite_count ( $ post_id ) {
2933+ return intval ( get_post_meta ( $ post_id , 'enable-mastodon-apps-favourite-count ' , true ) );
2934+ }
2935+
2936+ public function api_favourites ( $ request ) {
2937+ $ user_id = get_current_user_id ();
2938+ if ( ! $ user_id ) {
2939+ return array ();
2940+ }
2941+
2942+ $ args = $ this ->get_posts_query_args ( $ request );
2943+ if ( empty ( $ args ) ) {
2944+ return array ();
2945+ }
2946+ $ args = apply_filters ( 'mastodon_api_timelines_args ' , $ args , $ request );
2947+
2948+ $ this ->register_favourites_taxonomy ( $ user_id );
2949+
2950+ $ args ['numberposts ' ] = -1 ;
2951+ $ args ['post_type ' ] = apply_filters ( 'friends_frontend_post_types ' , array ( 'post ' ) );
2952+ $ args ['tax_query ' ] = array (
2953+ array (
2954+ 'taxonomy ' => self ::FAVOURITES_TAXONOMY_PREFIX . $ user_id ,
2955+ 'field ' => 'slug ' ,
2956+ 'terms ' => self ::DEFAULT_REACT ,
2957+ ),
2958+ );
2959+
2960+ return $ this ->get_posts ( $ args );
2961+ }
2962+
2963+ private function update_favourite_count_on_post ( $ post_id , $ increment ) {
2964+ $ favourite_count = get_post_meta ( $ post_id , 'enable-mastodon-apps-favourite-count ' , true );
2965+
2966+ $ favourite_count += $ increment ;
2967+
2968+ if ( $ favourite_count < 0 ) {
2969+ $ favourite_count = 0 ;
2970+ }
2971+
2972+ update_post_meta ( $ post_id , 'enable-mastodon-apps-favourite-count ' , $ favourite_count );
2973+ }
2974+
2975+ public function register_favourites_taxonomy ( $ user_id ) {
2976+ if ( false === $ user_id ) {
2977+ return ;
2978+ }
2979+ $ args = array (
2980+ 'labels ' => array (
2981+ 'name ' => 'Mastodon Favourites ' ,
2982+ 'singular_name ' => 'Mastodon Favourite ' ,
2983+ 'menu_name ' => 'Mastodon Favourites ' ,
2984+ ),
2985+ 'public ' => false ,
2986+ 'show_ui ' => false ,
2987+ 'show_in_menu ' => false ,
2988+ 'show_in_rest ' => false ,
2989+ 'rewrite ' => false ,
2990+ );
2991+
2992+ register_taxonomy ( self ::FAVOURITES_TAXONOMY_PREFIX . $ user_id , null , $ args );
2993+
2994+ if ( is_wp_error ( get_term ( self ::DEFAULT_REACT , self ::FAVOURITES_TAXONOMY_PREFIX . $ user_id ) ) ) {
2995+ $ result = wp_insert_term ( self ::DEFAULT_REACT , self ::FAVOURITES_TAXONOMY_PREFIX . $ user_id );
2996+ }
2997+ }
28932998}
0 commit comments