@@ -161,6 +161,9 @@ public function rewrite_rules() {
161161 'api/v1/timelines/(home|public) ' => 'api/v1/timelines/$matches[1] ' ,
162162 'api/v1/timelines/tag/([^/|$]+) ' => 'api/v1/timelines/tag/$matches[1] ' ,
163163 'api/v2/search ' => 'api/v1/search ' ,
164+ 'api/v1/tags/(.+) ' => 'api/v1/tags/$matches[1] ' ,
165+ 'api/v1/tags/([^/]+)/follow ' => 'api/v1/tags/$matches[1]/follow ' ,
166+ 'api/v1/tags/([^/]+)/unfollow ' => 'api/v1/tags/$matches[1]/unfollow ' ,
164167 );
165168
166169 foreach ( $ generic as $ rule ) {
@@ -254,7 +257,7 @@ public function add_rest_routes() {
254257 'api/v1/followed_tags ' ,
255258 array (
256259 'methods ' => 'GET ' ,
257- 'callback ' => ' __return_empty_array ' ,
260+ 'callback ' => array ( $ this , ' api_followed_tags ' ) ,
258261 'permission_callback ' => array ( $ this , 'logged_in_permission ' ),
259262 )
260263 );
@@ -660,6 +663,36 @@ public function add_rest_routes() {
660663 'permission_callback ' => array ( $ this , 'logged_in_permission ' ),
661664 )
662665 );
666+
667+ register_rest_route (
668+ self ::PREFIX ,
669+ 'api/v1/tags/(?P<id>[^/]+) ' ,
670+ array (
671+ 'methods ' => array ( 'GET ' , 'OPTIONS ' ),
672+ 'callback ' => array ( $ this , 'api_tags ' ),
673+ 'permission_callback ' => array ( $ this , 'logged_in_permission ' ),
674+ )
675+ );
676+
677+ register_rest_route (
678+ self ::PREFIX ,
679+ 'api/v1/tags/(?P<id>[^/]+)/follow ' ,
680+ array (
681+ 'methods ' => array ( 'POST ' , 'OPTIONS ' ),
682+ 'callback ' => array ( $ this , 'api_tags_follow ' ),
683+ 'permission_callback ' => array ( $ this , 'logged_in_permission ' ),
684+ )
685+ );
686+
687+ register_rest_route (
688+ self ::PREFIX ,
689+ 'api/v1/tags/(?P<id>[^/]+)/unfollow ' ,
690+ array (
691+ 'methods ' => array ( 'POST ' , 'OPTIONS ' ),
692+ 'callback ' => array ( $ this , 'api_tags_unfollow ' ),
693+ 'permission_callback ' => array ( $ this , 'logged_in_permission ' ),
694+ )
695+ );
663696 }
664697
665698 public function query_vars ( $ query_vars ) {
@@ -2931,4 +2964,138 @@ public function api_instance_v2() {
29312964
29322965 return apply_filters ( 'mastodon_api_instance_v2 ' , $ ret );
29332966 }
2967+
2968+ public function api_tags ( $ request ) {
2969+ $ token = $ this ->oauth ->get_token ();
2970+ $ user_id = $ token ['user_id ' ];
2971+ $ hashtag = $ request ->get_param ( 'id ' );
2972+ $ followed = $ this ->check_if_hashtag_followed ( $ user_id , $ hashtag );
2973+
2974+ $ term = $ this ->find_hashtag_term ( $ hashtag );
2975+ return $ this ->generate_hashtag_array ( $ term , array (), $ followed );
2976+ }
2977+
2978+ public function api_tags_follow ( $ request ) {
2979+ $ token = $ this ->oauth ->get_token ();
2980+ $ user_id = $ token ['user_id ' ];
2981+
2982+ $ tags_followed = get_user_meta ( $ user_id , 'enable-mastodon-apps-tags-followed ' , true );
2983+
2984+ if ( $ tags_followed === false ) {
2985+ $ tags_followed = array ();
2986+ } else {
2987+ $ tags_followed = unserialize ( $ tags_followed );
2988+ }
2989+
2990+ $ hashtag = $ request ->get_param ( 'id ' );
2991+ $ term = $ this ->find_hashtag_term ( $ hashtag );
2992+
2993+ $ tags_followed [$ hashtag ] = true ;
2994+
2995+ update_user_meta ( $ user_id , 'enable-mastodon-apps-tags-followed ' , serialize ( $ tags_followed ) );
2996+
2997+ if ( $ term === null ) {
2998+ return $ this ->generate_hashtag_array ( '' );
2999+ }
3000+
3001+ return $ this ->generate_hashtag_array ( $ term , array (), true );
3002+ }
3003+
3004+ public function api_tags_unfollow ( $ request ) {
3005+ $ token = $ this ->oauth ->get_token ();
3006+ $ user_id = $ token ['user_id ' ];
3007+
3008+ $ tags_followed = get_user_meta ( $ user_id , 'enable-mastodon-apps-tags-followed ' , true );
3009+
3010+ if ( $ tags_followed === false ) {
3011+ $ tags_followed = array ();
3012+ } else {
3013+ $ tags_followed = unserialize ( $ tags_followed );
3014+ }
3015+
3016+ $ hashtag = $ request ->get_param ( 'id ' );
3017+ $ term = $ this ->find_hashtag_term ( $ hashtag );
3018+
3019+ unset( $ tags_followed [$ hashtag ] );
3020+
3021+ update_user_meta ( $ user_id , 'enable-mastodon-apps-tags-followed ' , serialize ( $ tags_followed ) );
3022+
3023+ if ( $ term === null ) {
3024+ return $ this ->generate_hashtag_array ( '' );
3025+ }
3026+
3027+ return $ this ->generate_hashtag_array ( $ term , array () );
3028+ }
3029+
3030+ public function api_followed_tags ( $ request ) {
3031+ $ token = $ this ->oauth ->get_token ();
3032+ $ user_id = $ token ['user_id ' ];
3033+
3034+ $ tags_followed = get_user_meta ( $ user_id , 'enable-mastodon-apps-tags-followed ' , true );
3035+
3036+ if ( $ tags_followed === false ) {
3037+ $ tags_followed = array ();
3038+ } else {
3039+ $ tags_followed = unserialize ( $ tags_followed );
3040+ }
3041+
3042+ $ ret = array ();
3043+ foreach ( $ tags_followed as $ key => $ value ) {
3044+ $ term = $ this ->find_hashtag_term ( $ key );
3045+ $ ret [] = $ this ->generate_hashtag_array ( $ term , array (), true );
3046+ }
3047+
3048+ return $ ret ;
3049+ }
3050+
3051+ private function generate_hashtag_array ( $ term , $ history = array (), $ following = false ) {
3052+ $ ret = array ( 'name ' => $ term ->name , 'url ' => get_term_link ( $ term ), 'history ' => $ history );
3053+ if ( $ following ) {
3054+ $ ret ['following ' ] = true ;
3055+ }
3056+ return $ ret ;
3057+ }
3058+
3059+ private function get_categories () {
3060+ return get_categories ( array ( 'orderby ' => 'name ' , 'hide_empty ' => false ) );
3061+ }
3062+
3063+ private function get_tags () {
3064+ return get_tags ( array ( 'orderby ' => 'name ' , 'hide_empty ' => false ) );
3065+ }
3066+
3067+ private function find_hashtag_term ( $ hashtag ) {
3068+ $ tags = $ this ->get_tags ();
3069+ $ post_data ['tags_input ' ] = array ();
3070+ foreach ( $ tags as $ tag ) {
3071+ if ( strcmp ( $ hashtag , $ tag ->name ) == 0 ) {
3072+ return $ tag ;
3073+ }
3074+ }
3075+ $ categories = $ this ->get_categories ();
3076+ $ post_data ['post_category ' ] = array ();
3077+ foreach ( $ categories as $ category ) {
3078+ if ( strcmp ( $ hashtag , $ category ->name ) == 0 ) {
3079+ return $ category ;
3080+ }
3081+ }
3082+ return null ;
3083+ }
3084+
3085+ private function check_if_hashtag_followed ( $ user_id , $ hashtag ) {
3086+ $ tags_followed = get_user_meta ( $ user_id , 'enable-mastodon-apps-tags-followed ' , true );
3087+
3088+ if ( $ tags_followed === false ) {
3089+ $ tags_followed = array ();
3090+ } else {
3091+ $ tags_followed = unserialize ( $ tags_followed );
3092+ }
3093+
3094+ if ( array_key_exists ( $ hashtag , $ tags_followed ) ) {
3095+ return true ;
3096+ }
3097+
3098+ return false ;
3099+ }
3100+
29343101}
0 commit comments