Skip to content

Commit

Permalink
Add bookmark support.
Browse files Browse the repository at this point in the history
  • Loading branch information
toolstack committed Jan 9, 2024
1 parent 24c0179 commit 4b24480
Showing 1 changed file with 115 additions and 3 deletions.
118 changes: 115 additions & 3 deletions includes/class-mastodon-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ public function rewrite_rules() {
'api/v1/statuses/([0-9]+)/unfavourite' => 'api/v1/statuses/$matches[1]/unfavourite',
'api/v1/statuses/([0-9]+)/reblog' => 'api/v1/statuses/$matches[1]/reblog',
'api/v1/statuses/([0-9]+)/unreblog' => 'api/v1/statuses/$matches[1]/unreblog',
'api/v1/statuses/([0-9]+)/bookmark' => 'api/v1/statuses/$matches[1]/bookmark',
'api/v1/statuses/([0-9]+)/unbookmark' => 'api/v1/statuses/$matches[1]/unbookmark',
'api/v1/notifications/([^/]+)/dismiss' => 'api/v1/notifications/$matches[1]/dismiss',
'api/v1/notifications/([^/|$]+)/?$' => 'api/v1/notifications/$matches[1]',
'api/v1/notifications' => 'api/v1/notifications',
Expand Down Expand Up @@ -263,7 +265,7 @@ public function add_rest_routes() {
'api/v1/bookmarks',
array(
'methods' => 'GET',
'callback' => '__return_empty_array',
'callback' => array( $this, 'api_bookmarks' ),
'permission_callback' => array( $this, 'logged_in_permission' ),
)
);
Expand Down Expand Up @@ -522,6 +524,26 @@ public function add_rest_routes() {
)
);

register_rest_route(
self::PREFIX,
'api/v1/statuses/(?P<post_id>[0-9]+)/bookmark',
array(
'methods' => array( 'POST', 'OPTIONS' ),
'callback' => array( $this, 'api_bookmark_post' ),
'permission_callback' => array( $this, 'logged_in_permission' ),
)
);

register_rest_route(
self::PREFIX,
'api/v1/statuses/(?P<post_id>[0-9]+)/unbookmark',
array(
'methods' => array( 'POST', 'OPTIONS' ),
'callback' => array( $this, 'api_unbookmark_post' ),
'permission_callback' => array( $this, 'logged_in_permission' ),
)
);

register_rest_route(
self::PREFIX,
'api/v1/statuses/(?P<post_id>[0-9]+)',
Expand Down Expand Up @@ -1089,7 +1111,7 @@ function ( $user_id ) {
'reblogged' => $reblogged,
'reblogged_by' => $reblogged_by,
'muted' => false,
'bookmarked' => false,
'bookmarked' => $this->check_if_status_bookmarked( $user_id, $post->ID ),
'content' => $this->normalize_whitespace( $post->post_title . PHP_EOL . $post->post_content ),
'filtered' => array(),
'reblog' => null,
Expand Down Expand Up @@ -1816,7 +1838,7 @@ public function convert_activity_to_status( $activity, $user_id ) {
'favourited' => false,
'reblogged' => false,
'muted' => false,
'bookmarked' => false,
'bookmarked' => $this->check_if_status_bookmarked( $user_id, $id_parts ),
'content' => $activity['object']['content'],
'filtered' => array(),
'reblog' => null,
Expand Down Expand Up @@ -2845,4 +2867,94 @@ public function api_instance_v2() {

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

public function api_bookmarks( $request ) {
$user_id = get_current_user_id();

$bookmarks = get_user_meta( $user_id, 'enable-mastodon-apps-bookmarks', true );

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

$ret = array();
foreach( $bookmarks as $post_id => $value ) {
$post = get_post( $post_id, 'OBJECT' );
if( is_object( $post ) ) {
$ret[] = $this->get_status_array( $post );
}
}

return $ret;
}

public function api_bookmark_post( $request ) {
$user_id = get_current_user_id();
$post_id = $request->get_param( 'post_id' );
if ( ! $post_id ) {
return false;
}

$post_id = $this->maybe_get_remapped_reblog_id( $post_id );

do_action( 'mastodon_api_bookmark', $post_id );

$bookmarks = get_user_meta( $user_id, 'enable-mastodon-apps-bookmarks', true );

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

$bookmarks[$post_id] = true;

$result = update_user_meta( $user_id, 'enable-mastodon-apps-bookmarks', serialize( $bookmarks ) );

$post = get_post( $post_id );

return $this->get_status_array( $post );
}

public function api_unbookmark_post( $request ) {
$user_id = get_current_user_id();
$post_id = $request->get_param( 'post_id' );
if ( ! $post_id ) {
return false;
}

$post_id = $this->maybe_get_remapped_reblog_id( $post_id );

do_action( 'mastodon_api_unbookmark', $post_id );

$bookmarks = get_user_meta( $user_id, 'enable-mastodon-apps-favourite-posts', true );

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

unset( $bookmarks[$post_id] );

update_user_meta( $user_id, 'enable-mastodon-apps-bookmarks', serialize( $bookmarks ) );

$post = get_post( $post_id );

return $this->get_status_array( $post );
}

private function check_if_status_bookmarked( $user_id, $post_id ) {
$bookmarks = get_user_meta( $user_id, 'enable-mastodon-apps-bookmarks', true );

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

return array_key_exists( $post_id, $bookmarks );
}
}

0 comments on commit 4b24480

Please sign in to comment.