|
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 | ); |
@@ -1083,9 +1092,9 @@ function ( $user_id ) { |
1083 | 1092 | 'url' => null, |
1084 | 1093 | 'replies_count' => 0, |
1085 | 1094 | 'reblogs_count' => 0, |
1086 | | - 'favourites_count' => 0, |
| 1095 | + 'favourites_count' => $this->get_post_favourite_count( $post->ID ), |
1087 | 1096 | 'edited_at' => null, |
1088 | | - 'favourited' => false, |
| 1097 | + 'favourited' => $this->check_if_status_favourited( $user_id, $post->ID ), |
1089 | 1098 | 'reblogged' => $reblogged, |
1090 | 1099 | 'reblogged_by' => $reblogged_by, |
1091 | 1100 | 'muted' => false, |
@@ -1676,33 +1685,68 @@ public function api_get_post_context( $request ) { |
1676 | 1685 | } |
1677 | 1686 |
|
1678 | 1687 | public function api_favourite_post( $request ) { |
| 1688 | + $user_id = get_current_user_id(); |
1679 | 1689 | $post_id = $request->get_param( 'post_id' ); |
1680 | 1690 | if ( ! $post_id ) { |
1681 | 1691 | return false; |
1682 | 1692 | } |
1683 | 1693 |
|
1684 | 1694 | $post_id = $this->maybe_get_remapped_reblog_id( $post_id ); |
1685 | 1695 |
|
1686 | | - // 2b50 = star |
1687 | | - // 2764 = heart |
1688 | | - do_action( 'mastodon_api_react', $post_id, '2b50' ); |
| 1696 | + do_action( 'mastodon_api_react', $post_id, SELF::DEFAULT_REACT ); |
| 1697 | + |
| 1698 | + $favourite_posts = get_user_meta( $user_id, 'enable-mastodon-apps-favourite-posts', true ); |
| 1699 | + |
| 1700 | + if ( $favourite_posts == false ) { |
| 1701 | + $favourite_posts = array(); |
| 1702 | + } else { |
| 1703 | + $favourite_posts = unserialize( $favourite_posts ); |
| 1704 | + } |
| 1705 | + |
| 1706 | + $favourite_posts[$post_id] = SELF::DEFAULT_REACT; |
| 1707 | + |
| 1708 | + $result = update_user_meta( $user_id, 'enable-mastodon-apps-favourite-posts', serialize( $favourite_posts ) ); |
| 1709 | + |
| 1710 | + $favourite_count = get_post_meta( $post_id, 'enable-mastodon-apps-favourite-count', true ); |
| 1711 | + |
| 1712 | + $favourite_count++; |
| 1713 | + |
| 1714 | + update_post_meta( $post_id, 'enable-mastodon-apps-favourite-count', $favourite_count ); |
1689 | 1715 |
|
1690 | 1716 | $post = get_post( $post_id ); |
1691 | 1717 |
|
1692 | 1718 | return $this->get_status_array( $post ); |
1693 | 1719 | } |
1694 | 1720 |
|
1695 | 1721 | public function api_unfavourite_post( $request ) { |
| 1722 | + $user_id = get_current_user_id(); |
1696 | 1723 | $post_id = $request->get_param( 'post_id' ); |
1697 | 1724 | if ( ! $post_id ) { |
1698 | 1725 | return false; |
1699 | 1726 | } |
1700 | 1727 |
|
1701 | 1728 | $post_id = $this->maybe_get_remapped_reblog_id( $post_id ); |
1702 | 1729 |
|
1703 | | - // 2b50 = star |
1704 | | - // 2764 = heart |
1705 | | - do_action( 'mastodon_api_unreact', $post_id, '2b50' ); |
| 1730 | + do_action( 'mastodon_api_unreact', $post_id, SELF::DEFAULT_REACT ); |
| 1731 | + |
| 1732 | + $favourite_posts = get_user_meta( $user_id, 'enable-mastodon-apps-favourite-posts', true ); |
| 1733 | + |
| 1734 | + if ( $favourite_posts == false ) { |
| 1735 | + $favourite_posts = array(); |
| 1736 | + } else { |
| 1737 | + $favourite_posts = unserialize( $favourite_posts ); |
| 1738 | + } |
| 1739 | + |
| 1740 | + unset( $favourite_posts[$post_id] ); |
| 1741 | + |
| 1742 | + update_user_meta( $user_id, 'enable-mastodon-apps-favourite-posts', serialize( $favourite_posts ) ); |
| 1743 | + |
| 1744 | + $favourite_count = get_post_meta( $post_id, 'enable-mastodon-apps-favourite-count', true ); |
| 1745 | + |
| 1746 | + $favourite_count--; |
| 1747 | + if ( $favourite_count < 0 ) { $favourite_count = 0; } |
| 1748 | + |
| 1749 | + update_post_meta( $post_id, 'enable-mastodon-apps-favourite-count', $favourite_count ); |
1706 | 1750 |
|
1707 | 1751 | $post = get_post( $post_id ); |
1708 | 1752 |
|
@@ -1811,9 +1855,9 @@ public function convert_activity_to_status( $activity, $user_id ) { |
1811 | 1855 | 'muted' => false, |
1812 | 1856 | 'replies_count' => 0, // could be fetched. |
1813 | 1857 | 'reblogs_count' => 0, |
1814 | | - 'favourites_count' => 0, |
| 1858 | + 'favourites_count' => $this->get_post_favourite_count( $post->ID ), |
1815 | 1859 | 'edited_at' => null, |
1816 | | - 'favourited' => false, |
| 1860 | + 'favourited' => $this->check_if_status_favourited( $user_id, $id_parts ), |
1817 | 1861 | 'reblogged' => false, |
1818 | 1862 | 'muted' => false, |
1819 | 1863 | 'bookmarked' => false, |
@@ -2845,4 +2889,47 @@ public function api_instance_v2() { |
2845 | 2889 |
|
2846 | 2890 | return apply_filters( 'mastodon_api_instance_v2', $ret ); |
2847 | 2891 | } |
| 2892 | + private function check_if_status_favourited( $user_id, $post_id ) { |
| 2893 | + $favourite_posts = get_user_meta( $user_id, 'enable-mastodon-apps-favourite-posts', true ); |
| 2894 | + |
| 2895 | + if ( $favourite_posts == false ) { |
| 2896 | + $favourite_posts = array(); |
| 2897 | + } else { |
| 2898 | + $favourite_posts = unserialize( $favourite_posts ); |
| 2899 | + } |
| 2900 | + |
| 2901 | + return array_key_exists( $post_id, $favourite_posts ); |
| 2902 | + } |
| 2903 | + |
| 2904 | + private function get_post_favourite_count( $post_id ) { |
| 2905 | + return get_post_meta( $post_id, 'enable-mastodon-apps-favourite-count', true ); |
| 2906 | + } |
| 2907 | + |
| 2908 | + public function api_favourites( $request ) { |
| 2909 | + $user_id = get_current_user_id(); |
| 2910 | + |
| 2911 | + $favourite_posts = get_user_meta( $user_id, 'enable-mastodon-apps-favourite-posts', true ); |
| 2912 | + |
| 2913 | + if ( $favourite_posts == false ) { |
| 2914 | + $favourite_posts = array(); |
| 2915 | + } else { |
| 2916 | + $favourite_posts = unserialize( $favourite_posts ); |
| 2917 | + } |
| 2918 | + |
| 2919 | + $args = $this->get_posts_query_args( $request ); |
| 2920 | + if ( empty( $args ) ) { |
| 2921 | + return array(); |
| 2922 | + } |
| 2923 | + $args = apply_filters( 'mastodon_api_timelines_args', $args, $request ); |
| 2924 | + |
| 2925 | + $ret = array(); |
| 2926 | + foreach( $favourite_posts as $post_id => $value ) { |
| 2927 | + $post = get_post( $post_id, 'OBJECT' ); |
| 2928 | + if( is_object( $post ) ) { |
| 2929 | + $ret[] = $this->get_status_array( $post ); |
| 2930 | + } |
| 2931 | + } |
| 2932 | + |
| 2933 | + return $ret; |
| 2934 | + } |
2848 | 2935 | } |
0 commit comments