@@ -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 ) {
@@ -2910,4 +2943,138 @@ public function api_instance_v2() {
29102943
29112944 return apply_filters ( 'mastodon_api_instance_v2 ' , $ ret );
29122945 }
2946+
2947+ public function api_tags ( $ request ) {
2948+ $ token = $ this ->oauth ->get_token ();
2949+ $ user_id = $ token ['user_id ' ];
2950+ $ hashtag = $ request ->get_param ( 'id ' );
2951+ $ followed = $ this ->check_if_hashtag_followed ( $ user_id , $ hashtag );
2952+
2953+ $ term = $ this ->find_hashtag_term ( $ hashtag );
2954+ return $ this ->generate_hashtag_array ( $ term , array (), $ followed );
2955+ }
2956+
2957+ public function api_tags_follow ( $ request ) {
2958+ $ token = $ this ->oauth ->get_token ();
2959+ $ user_id = $ token ['user_id ' ];
2960+
2961+ $ tags_followed = get_user_meta ( $ user_id , 'enable-mastodon-apps-tags-followed ' , true );
2962+
2963+ if ( $ tags_followed === false ) {
2964+ $ tags_followed = array ();
2965+ } else {
2966+ $ tags_followed = unserialize ( $ tags_followed );
2967+ }
2968+
2969+ $ hashtag = $ request ->get_param ( 'id ' );
2970+ $ term = $ this ->find_hashtag_term ( $ hashtag );
2971+
2972+ $ tags_followed [$ hashtag ] = true ;
2973+
2974+ update_user_meta ( $ user_id , 'enable-mastodon-apps-tags-followed ' , serialize ( $ tags_followed ) );
2975+
2976+ if ( $ term === null ) {
2977+ return $ this ->generate_hashtag_array ( '' );
2978+ }
2979+
2980+ return $ this ->generate_hashtag_array ( $ term , array (), true );
2981+ }
2982+
2983+ public function api_tags_unfollow ( $ request ) {
2984+ $ token = $ this ->oauth ->get_token ();
2985+ $ user_id = $ token ['user_id ' ];
2986+
2987+ $ tags_followed = get_user_meta ( $ user_id , 'enable-mastodon-apps-tags-followed ' , true );
2988+
2989+ if ( $ tags_followed === false ) {
2990+ $ tags_followed = array ();
2991+ } else {
2992+ $ tags_followed = unserialize ( $ tags_followed );
2993+ }
2994+
2995+ $ hashtag = $ request ->get_param ( 'id ' );
2996+ $ term = $ this ->find_hashtag_term ( $ hashtag );
2997+
2998+ unset( $ tags_followed [$ hashtag ] );
2999+
3000+ update_user_meta ( $ user_id , 'enable-mastodon-apps-tags-followed ' , serialize ( $ tags_followed ) );
3001+
3002+ if ( $ term === null ) {
3003+ return $ this ->generate_hashtag_array ( '' );
3004+ }
3005+
3006+ return $ this ->generate_hashtag_array ( $ term , array () );
3007+ }
3008+
3009+ public function api_followed_tags ( $ request ) {
3010+ $ token = $ this ->oauth ->get_token ();
3011+ $ user_id = $ token ['user_id ' ];
3012+
3013+ $ tags_followed = get_user_meta ( $ user_id , 'enable-mastodon-apps-tags-followed ' , true );
3014+
3015+ if ( $ tags_followed === false ) {
3016+ $ tags_followed = array ();
3017+ } else {
3018+ $ tags_followed = unserialize ( $ tags_followed );
3019+ }
3020+
3021+ $ ret = array ();
3022+ foreach ( $ tags_followed as $ key => $ value ) {
3023+ $ term = $ this ->find_hashtag_term ( $ key );
3024+ $ ret [] = $ this ->generate_hashtag_array ( $ term , array (), true );
3025+ }
3026+
3027+ return $ ret ;
3028+ }
3029+
3030+ private function generate_hashtag_array ( $ term , $ history = array (), $ following = false ) {
3031+ $ ret = array ( 'name ' => $ term ->name , 'url ' => get_term_link ( $ term ), 'history ' => $ history );
3032+ if ( $ following ) {
3033+ $ ret ['following ' ] = true ;
3034+ }
3035+ return $ ret ;
3036+ }
3037+
3038+ private function get_categories () {
3039+ return get_categories ( array ( 'orderby ' => 'name ' , 'hide_empty ' => false ) );
3040+ }
3041+
3042+ private function get_tags () {
3043+ return get_tags ( array ( 'orderby ' => 'name ' , 'hide_empty ' => false ) );
3044+ }
3045+
3046+ private function find_hashtag_term ( $ hashtag ) {
3047+ $ tags = $ this ->get_tags ();
3048+ $ post_data ['tags_input ' ] = array ();
3049+ foreach ( $ tags as $ tag ) {
3050+ if ( strcmp ( $ hashtag , $ tag ->name ) == 0 ) {
3051+ return $ tag ;
3052+ }
3053+ }
3054+ $ categories = $ this ->get_categories ();
3055+ $ post_data ['post_category ' ] = array ();
3056+ foreach ( $ categories as $ category ) {
3057+ if ( strcmp ( $ hashtag , $ category ->name ) == 0 ) {
3058+ return $ category ;
3059+ }
3060+ }
3061+ return null ;
3062+ }
3063+
3064+ private function check_if_hashtag_followed ( $ user_id , $ hashtag ) {
3065+ $ tags_followed = get_user_meta ( $ user_id , 'enable-mastodon-apps-tags-followed ' , true );
3066+
3067+ if ( $ tags_followed === false ) {
3068+ $ tags_followed = array ();
3069+ } else {
3070+ $ tags_followed = unserialize ( $ tags_followed );
3071+ }
3072+
3073+ if ( array_key_exists ( $ hashtag , $ tags_followed ) ) {
3074+ return true ;
3075+ }
3076+
3077+ return false ;
3078+ }
3079+
29133080}
0 commit comments