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 16, 2024
1 parent d9b2960 commit 364139a
Showing 1 changed file with 171 additions and 1 deletion.
172 changes: 171 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 @@ -2931,4 +2964,141 @@ 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 ( false === $tags_followed ) {
$tags_followed = array();
} else {
$tags_followed = unserialize( $tags_followed );
}

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

$tags_followed[ $hashtag ] = true;

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

if ( null === $term ) {
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 3010 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 ] );

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

if ( null === $term ) {
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 ( false === $tags_followed ) {
$tags_followed = array();
} else {
$tags_followed = unserialize( $tags_followed );
}

$ret = array();
foreach ( $tags_followed as $key => $value ) {
$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 ) );

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

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

When a multi-item array uses associative keys, each value should start on a new line.
}

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

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

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

When a multi-item array uses associative keys, each value should start on a new line.
}

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 ) {
return $tag;
}
}
$categories = $this->get_categories();
$post_data['post_category'] = array();
foreach ( $categories as $category ) {
if ( strcmp( $hashtag, $category->name ) === 0 ) {
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 ( false === $tags_followed ) {
$tags_followed = array();
} else {
$tags_followed = unserialize( $tags_followed );
}

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

return false;
}
}

0 comments on commit 364139a

Please sign in to comment.