Skip to content

Commit

Permalink
Add hashtag follow support.
Browse files Browse the repository at this point in the history
  • Loading branch information
toolstack committed Jan 13, 2024
1 parent 8c81562 commit 64d4d56
Showing 1 changed file with 168 additions and 1 deletion.
169 changes: 168 additions & 1 deletion includes/class-mastodon-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ public function rewrite_rules() {
'api/v1/timelines/(home|public)' => 'api/v1/timelines/$matches[1]',
'api/v1/timelines/tag/([^/|$]+)' => 'api/v1/timelines/tag/$matches[1]',
'api/v2/search' => 'api/v1/search',
'api/v1/tags/(.+)' => 'api/v1/tags/$matches[1]',
'api/v1/tags/([^/]+)/follow' => 'api/v1/tags/$matches[1]/follow',
'api/v1/tags/([^/]+)/unfollow' => 'api/v1/tags/$matches[1]/unfollow',
);

foreach ( $generic as $rule ) {
Expand Down Expand Up @@ -254,7 +257,7 @@ public function add_rest_routes() {
'api/v1/followed_tags',
array(
'methods' => 'GET',
'callback' => '__return_empty_array',
'callback' => array( $this, 'api_followed_tags' ),
'permission_callback' => array( $this, 'logged_in_permission' ),
)
);
Expand Down Expand Up @@ -660,6 +663,36 @@ public function add_rest_routes() {
'permission_callback' => array( $this, 'logged_in_permission' ),
)
);

register_rest_route(
self::PREFIX,
'api/v1/tags/(?P<id>[^/]+)',
array(
'methods' => array( 'GET', 'OPTIONS' ),
'callback' => array( $this, 'api_tags' ),
'permission_callback' => array( $this, 'logged_in_permission' ),
)
);

register_rest_route(
self::PREFIX,
'api/v1/tags/(?P<id>[^/]+)/follow',
array(
'methods' => array( 'POST', 'OPTIONS' ),
'callback' => array( $this, 'api_tags_follow' ),
'permission_callback' => array( $this, 'logged_in_permission' ),
)
);

register_rest_route(
self::PREFIX,
'api/v1/tags/(?P<id>[^/]+)/unfollow',
array(
'methods' => array( 'POST', 'OPTIONS' ),
'callback' => array( $this, 'api_tags_unfollow' ),
'permission_callback' => array( $this, 'logged_in_permission' ),
)
);
}

public function query_vars( $query_vars ) {
Expand Down Expand Up @@ -2910,4 +2943,138 @@ public function api_instance_v2() {

return apply_filters( 'mastodon_api_instance_v2', $ret );
}

public function api_tags( $request ) {
$token = $this->oauth->get_token();
$user_id = $token['user_id'];
$hashtag = $request->get_param( 'id' );
$followed = $this->check_if_hashtag_followed( $user_id, $hashtag );

$term = $this->find_hashtag_term( $hashtag );
return $this->generate_hashtag_array( $term, array(), $followed );
}

public function api_tags_follow( $request ) {
$token = $this->oauth->get_token();
$user_id = $token['user_id'];

$tags_followed = get_user_meta( $user_id, 'enable-mastodon-apps-tags-followed', true );

if ( $tags_followed === false ) {

Check failure on line 2963 in includes/class-mastodon-api.php

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

Use Yoda Condition checks, you must.
$tags_followed = array();
} else {
$tags_followed = unserialize( $tags_followed );
}

$hashtag = $request->get_param( 'id' );
$term = $this->find_hashtag_term( $hashtag );

$tags_followed[$hashtag] = true;

Check failure on line 2972 in includes/class-mastodon-api.php

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

Array keys must be surrounded by spaces unless they contain a string or an integer.

update_user_meta( $user_id, 'enable-mastodon-apps-tags-followed', serialize( $tags_followed ) );

if ( $term === null ) {

Check failure on line 2976 in includes/class-mastodon-api.php

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

Use Yoda Condition checks, you must.
return $this->generate_hashtag_array( '' );
}

return $this->generate_hashtag_array( $term, array(), true );
}

public function api_tags_unfollow( $request ) {
$token = $this->oauth->get_token();
$user_id = $token['user_id'];

$tags_followed = get_user_meta( $user_id, 'enable-mastodon-apps-tags-followed', true );

if ( $tags_followed === false ) {

Check failure on line 2989 in includes/class-mastodon-api.php

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

Use Yoda Condition checks, you must.
$tags_followed = array();
} else {
$tags_followed = unserialize( $tags_followed );
}

$hashtag = $request->get_param( 'id' );
$term = $this->find_hashtag_term( $hashtag );

unset( $tags_followed[$hashtag] );

Check failure on line 2998 in includes/class-mastodon-api.php

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

Array keys must be surrounded by spaces unless they contain a string or an integer.

update_user_meta( $user_id, 'enable-mastodon-apps-tags-followed', serialize( $tags_followed ) );

if ( $term === null ) {

Check failure on line 3002 in includes/class-mastodon-api.php

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

Use Yoda Condition checks, you must.
return $this->generate_hashtag_array( '' );
}

return $this->generate_hashtag_array( $term, array() );
}

public function api_followed_tags( $request ) {
$token = $this->oauth->get_token();
$user_id = $token['user_id'];

$tags_followed = get_user_meta( $user_id, 'enable-mastodon-apps-tags-followed', true );

if ( $tags_followed === false ) {

Check failure on line 3015 in includes/class-mastodon-api.php

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

Use Yoda Condition checks, you must.
$tags_followed = array();
} else {
$tags_followed = unserialize( $tags_followed );
}

$ret = array();
foreach( $tags_followed as $key => $value ) {

Check failure on line 3022 in includes/class-mastodon-api.php

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

Space after opening control structure is required

Check failure on line 3022 in includes/class-mastodon-api.php

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

No space before opening parenthesis is prohibited

Check failure on line 3022 in includes/class-mastodon-api.php

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

Expected 1 space after FOREACH keyword; 0 found
$term = $this->find_hashtag_term( $key );
$ret[] = $this->generate_hashtag_array( $term, array(), true );
}

return $ret;
}

private function generate_hashtag_array( $term, $history = array(), $following = false ) {
$ret = array( 'name' => $term->name, 'url' => get_term_link( $term ), 'history' => $history );
if( $following ) {
$ret['following'] = true;
}
return $ret;
}

private function get_categories() {
return get_categories( array( 'orderby' => 'name', 'hide_empty' => false ) );
}

private function get_tags() {
return get_tags( array( 'orderby' => 'name', 'hide_empty' => false ) );
}

private function find_hashtag_term( $hashtag ) {
$tags = $this->get_tags();
$post_data['tags_input'] = array();
foreach( $tags as $tag ) {
if( strcmp( $hashtag, $tag->name ) == 0 ) {

Check warning on line 3050 in includes/class-mastodon-api.php

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

Loose comparisons are not allowed. Expected: "==="; Found: "=="
return $tag;
}
}
$categories = $this->get_categories();
$post_data['post_category'] = array();
foreach( $categories as $category ) {
if( strcmp( $hashtag, $category->name ) == 0 ) {

Check warning on line 3057 in includes/class-mastodon-api.php

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

Loose comparisons are not allowed. Expected: "==="; Found: "=="
return $category;
}
}
return null;
}

private function check_if_hashtag_followed( $user_id, $hashtag ) {
$tags_followed = get_user_meta( $user_id, 'enable-mastodon-apps-tags-followed', true );

if ( $tags_followed === false ) {
$tags_followed = array();
} else {
$tags_followed = unserialize( $tags_followed );
}

if( array_key_exists( $hashtag, $tags_followed ) ) {
return true;
}

return false;
}

}

0 comments on commit 64d4d56

Please sign in to comment.