Skip to content

Commit 77ccdad

Browse files
committed
Add storage of favourited statuses
Make sure we return an integer.
1 parent 3382371 commit 77ccdad

File tree

1 file changed

+96
-11
lines changed

1 file changed

+96
-11
lines changed

includes/class-mastodon-api.php

Lines changed: 96 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
class Mastodon_API {
2121
const ACTIVITYPUB_USERNAME_REGEXP = '(?:([A-Za-z0-9_-]+)@((?:[A-Za-z0-9_-]+\.)+[A-Za-z]+))';
2222
const VERSION = ENABLE_MASTODON_APPS_VERSION;
23+
24+
/**
25+
* The default reaction for favouriting a status.
26+
*
27+
* 2b50 = star
28+
* 2764 = heart
29+
*/
30+
const DEFAULT_REACT = '2b50';
31+
2332
/**
2433
* The OAuth handler.
2534
*
@@ -282,7 +291,7 @@ public function add_rest_routes() {
282291
'api/v1/favourites',
283292
array(
284293
'methods' => 'GET',
285-
'callback' => '__return_empty_array',
294+
'callback' => array( $this, 'api_favourites'),
286295
'permission_callback' => array( $this, 'logged_in_permission' ),
287296
)
288297
);
@@ -1092,9 +1101,9 @@ function ( $user_id ) {
10921101
'url' => null,
10931102
'replies_count' => 0,
10941103
'reblogs_count' => 0,
1095-
'favourites_count' => 0,
1104+
'favourites_count' => $this->get_post_favourite_count( $post->ID ),
10961105
'edited_at' => null,
1097-
'favourited' => false,
1106+
'favourited' => $this->check_if_status_favourited( $user_id, $post->ID ),
10981107
'reblogged' => $reblogged,
10991108
'reblogged_by' => $reblogged_by,
11001109
'muted' => false,
@@ -1711,33 +1720,59 @@ public function api_get_post_context( $request ) {
17111720
}
17121721

17131722
public function api_favourite_post( $request ) {
1723+
$user_id = get_current_user_id();
17141724
$post_id = $request->get_param( 'post_id' );
17151725
if ( ! $post_id ) {
17161726
return false;
17171727
}
17181728

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

1721-
// 2b50 = star
1722-
// 2764 = heart
1723-
do_action( 'mastodon_api_react', $post_id, '2b50' );
1731+
do_action( 'mastodon_api_react', $post_id, SELF::DEFAULT_REACT );
1732+
1733+
$favourite_posts = get_user_meta( $user_id, 'enable-mastodon-apps-favourite-posts', true );
1734+
1735+
if ( $favourite_posts == false ) {
1736+
$favourite_posts = array();
1737+
} else {
1738+
$favourite_posts = unserialize( $favourite_posts );
1739+
}
1740+
1741+
$favourite_posts[$post_id] = SELF::DEFAULT_REACT;
1742+
1743+
$result = update_user_meta( $user_id, 'enable-mastodon-apps-favourite-posts', serialize( $favourite_posts ) );
1744+
1745+
$this->update_favourite_count_on_post( $post_id, 1 );
17241746

17251747
$post = get_post( $post_id );
17261748

17271749
return $this->get_status_array( $post );
17281750
}
17291751

17301752
public function api_unfavourite_post( $request ) {
1753+
$user_id = get_current_user_id();
17311754
$post_id = $request->get_param( 'post_id' );
17321755
if ( ! $post_id ) {
17331756
return false;
17341757
}
17351758

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

1738-
// 2b50 = star
1739-
// 2764 = heart
1740-
do_action( 'mastodon_api_unreact', $post_id, '2b50' );
1761+
do_action( 'mastodon_api_unreact', $post_id, SELF::DEFAULT_REACT );
1762+
1763+
$favourite_posts = get_user_meta( $user_id, 'enable-mastodon-apps-favourite-posts', true );
1764+
1765+
if ( $favourite_posts == false ) {
1766+
$favourite_posts = array();
1767+
} else {
1768+
$favourite_posts = unserialize( $favourite_posts );
1769+
}
1770+
1771+
unset( $favourite_posts[$post_id] );
1772+
1773+
update_user_meta( $user_id, 'enable-mastodon-apps-favourite-posts', serialize( $favourite_posts ) );
1774+
1775+
$this->update_favourite_count_on_post( $post_id, -1 );
17411776

17421777
$post = get_post( $post_id );
17431778

@@ -1853,9 +1888,9 @@ public function convert_activity_to_status( $activity, $user_id ) {
18531888
'muted' => false,
18541889
'replies_count' => 0, // could be fetched.
18551890
'reblogs_count' => 0,
1856-
'favourites_count' => 0,
1891+
'favourites_count' => $this->get_post_favourite_count( $post->ID ),
18571892
'edited_at' => null,
1858-
'favourited' => false,
1893+
'favourited' => $this->check_if_status_favourited( $user_id, $id_parts ),
18591894
'reblogged' => false,
18601895
'muted' => false,
18611896
'bookmarked' => false,
@@ -2890,4 +2925,54 @@ public function api_instance_v2() {
28902925

28912926
return apply_filters( 'mastodon_api_instance_v2', $ret );
28922927
}
2928+
2929+
private function check_if_status_favourited( $user_id, $post_id ) {
2930+
$favourite_posts = get_user_meta( $user_id, 'enable-mastodon-apps-favourite-posts', true );
2931+
2932+
if ( $favourite_posts == false ) {
2933+
$favourite_posts = array();
2934+
} else {
2935+
$favourite_posts = unserialize( $favourite_posts );
2936+
}
2937+
2938+
return array_key_exists( $post_id, $favourite_posts );
2939+
}
2940+
2941+
private function get_post_favourite_count( $post_id ) {
2942+
return intval( get_post_meta( $post_id, 'enable-mastodon-apps-favourite-count', true ) );
2943+
}
2944+
2945+
public function api_favourites( $request ) {
2946+
$user_id = get_current_user_id();
2947+
2948+
$favourite_posts = get_user_meta( $user_id, 'enable-mastodon-apps-favourite-posts', true );
2949+
2950+
if ( $favourite_posts == false ) {
2951+
$favourite_posts = array();
2952+
} else {
2953+
$favourite_posts = unserialize( $favourite_posts );
2954+
}
2955+
2956+
$ret = array();
2957+
foreach( $favourite_posts as $post_id => $value ) {
2958+
$post = get_post( $post_id, 'OBJECT' );
2959+
if( is_object( $post ) ) {
2960+
$ret[] = $this->get_status_array( $post );
2961+
}
2962+
}
2963+
2964+
return $ret;
2965+
}
2966+
2967+
private function update_favourite_count_on_post( $post_id, $increment ) {
2968+
$favourite_count = get_post_meta( $post_id, 'enable-mastodon-apps-favourite-count', true );
2969+
2970+
$favourite_count += $increment;
2971+
2972+
if ( $favourite_count < 0 ) {
2973+
$favourite_count = 0;
2974+
}
2975+
2976+
update_post_meta( $post_id, 'enable-mastodon-apps-favourite-count', $favourite_count );
2977+
}
28932978
}

0 commit comments

Comments
 (0)