|
20 | 20 | class Mastodon_API { |
21 | 21 | const ACTIVITYPUB_USERNAME_REGEXP = '(?:([A-Za-z0-9_-]+)@((?:[A-Za-z0-9_-]+\.)+[A-Za-z]+))'; |
22 | 22 | 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 | + |
23 | 32 | /** |
24 | 33 | * The OAuth handler. |
25 | 34 | * |
@@ -282,7 +291,7 @@ public function add_rest_routes() { |
282 | 291 | 'api/v1/favourites', |
283 | 292 | array( |
284 | 293 | 'methods' => 'GET', |
285 | | - 'callback' => '__return_empty_array', |
| 294 | + 'callback' => array( $this, 'api_favourites'), |
286 | 295 | 'permission_callback' => array( $this, 'logged_in_permission' ), |
287 | 296 | ) |
288 | 297 | ); |
@@ -1092,9 +1101,9 @@ function ( $user_id ) { |
1092 | 1101 | 'url' => null, |
1093 | 1102 | 'replies_count' => 0, |
1094 | 1103 | 'reblogs_count' => 0, |
1095 | | - 'favourites_count' => 0, |
| 1104 | + 'favourites_count' => $this->get_post_favourite_count( $post->ID ), |
1096 | 1105 | 'edited_at' => null, |
1097 | | - 'favourited' => false, |
| 1106 | + 'favourited' => $this->check_if_status_favourited( $user_id, $post->ID ), |
1098 | 1107 | 'reblogged' => $reblogged, |
1099 | 1108 | 'reblogged_by' => $reblogged_by, |
1100 | 1109 | 'muted' => false, |
@@ -1711,33 +1720,59 @@ public function api_get_post_context( $request ) { |
1711 | 1720 | } |
1712 | 1721 |
|
1713 | 1722 | public function api_favourite_post( $request ) { |
| 1723 | + $user_id = get_current_user_id(); |
1714 | 1724 | $post_id = $request->get_param( 'post_id' ); |
1715 | 1725 | if ( ! $post_id ) { |
1716 | 1726 | return false; |
1717 | 1727 | } |
1718 | 1728 |
|
1719 | 1729 | $post_id = $this->maybe_get_remapped_reblog_id( $post_id ); |
1720 | 1730 |
|
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 ); |
1724 | 1746 |
|
1725 | 1747 | $post = get_post( $post_id ); |
1726 | 1748 |
|
1727 | 1749 | return $this->get_status_array( $post ); |
1728 | 1750 | } |
1729 | 1751 |
|
1730 | 1752 | public function api_unfavourite_post( $request ) { |
| 1753 | + $user_id = get_current_user_id(); |
1731 | 1754 | $post_id = $request->get_param( 'post_id' ); |
1732 | 1755 | if ( ! $post_id ) { |
1733 | 1756 | return false; |
1734 | 1757 | } |
1735 | 1758 |
|
1736 | 1759 | $post_id = $this->maybe_get_remapped_reblog_id( $post_id ); |
1737 | 1760 |
|
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 ); |
1741 | 1776 |
|
1742 | 1777 | $post = get_post( $post_id ); |
1743 | 1778 |
|
@@ -1853,9 +1888,9 @@ public function convert_activity_to_status( $activity, $user_id ) { |
1853 | 1888 | 'muted' => false, |
1854 | 1889 | 'replies_count' => 0, // could be fetched. |
1855 | 1890 | 'reblogs_count' => 0, |
1856 | | - 'favourites_count' => 0, |
| 1891 | + 'favourites_count' => $this->get_post_favourite_count( $post->ID ), |
1857 | 1892 | 'edited_at' => null, |
1858 | | - 'favourited' => false, |
| 1893 | + 'favourited' => $this->check_if_status_favourited( $user_id, $id_parts ), |
1859 | 1894 | 'reblogged' => false, |
1860 | 1895 | 'muted' => false, |
1861 | 1896 | 'bookmarked' => false, |
@@ -2890,4 +2925,54 @@ public function api_instance_v2() { |
2890 | 2925 |
|
2891 | 2926 | return apply_filters( 'mastodon_api_instance_v2', $ret ); |
2892 | 2927 | } |
| 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 | + } |
2893 | 2978 | } |
0 commit comments