|
6 | 6 | */ |
7 | 7 | class Activitypub_Outbox { |
8 | 8 | /** |
9 | | - * Register the Route. |
| 9 | + * Register routes |
10 | 10 | */ |
11 | 11 | public static function register_routes() { |
12 | 12 | register_rest_route( |
13 | | - 'activitypub/1.0', '/outbox', array( |
| 13 | + 'activitypub/1.0', '/users/(?P<id>\d+)/outbox', array( |
14 | 14 | array( |
15 | 15 | 'methods' => WP_REST_Server::READABLE, |
16 | 16 | 'callback' => array( 'Activitypub_Outbox', 'get' ), |
| 17 | + 'args' => self::request_parameters(), |
17 | 18 | ), |
18 | 19 | ) |
19 | 20 | ); |
20 | 21 | } |
21 | 22 |
|
22 | 23 | public static function get( $request ) { |
23 | | - $outbox = new stdClass(); |
| 24 | + $author_id = $request->get_param( 'id' ); |
| 25 | + $author = get_user_by( 'ID', $author_id ); |
24 | 26 |
|
25 | | - $outbox->{'@context'} = array( |
| 27 | + if ( ! $author ) { |
| 28 | + return new WP_Error( 'rest_invalid_param', __( 'User not found', 'activitypub' ), array( |
| 29 | + 'status' => 404, 'params' => array( |
| 30 | + 'user_id' => __( 'User not found', 'activitypub' ) |
| 31 | + ) |
| 32 | + ) ); |
| 33 | + } |
| 34 | + |
| 35 | + $page = $request->get_param( 'page', 0 ); |
| 36 | + |
| 37 | + /* |
| 38 | + * Action triggerd prior to the ActivityPub profile being created and sent to the client |
| 39 | + */ |
| 40 | + do_action( 'activitypub_outbox_pre' ); |
| 41 | + |
| 42 | + $json = new stdClass(); |
| 43 | + |
| 44 | + $json->{'@context'} = array( |
26 | 45 | 'https://www.w3.org/ns/activitystreams', |
27 | 46 | 'https://w3id.org/security/v1', |
28 | 47 | ); |
29 | 48 |
|
30 | | - //var_dump($request->get_param('page')); |
| 49 | + $json->generator = 'http://wordpress.org/?v=' . get_bloginfo_rss( 'version' ); |
| 50 | + $json->actor = get_author_posts_url( $author_id ); |
| 51 | + $json->type = 'OrderedCollectionPage'; |
| 52 | + $json->partOf = get_rest_url( null, "/activitypub/1.0/users/$author_id/outbox" ); // phpcs:ignore |
| 53 | + |
| 54 | + $count_posts = wp_count_posts(); |
| 55 | + $json->totalItems = $count_posts->publish; |
| 56 | + |
| 57 | + $posts = get_posts( array( |
| 58 | + 'author' => $author_id, |
| 59 | + 'offset' => $page, |
| 60 | + ) ); |
| 61 | + |
| 62 | + $json->first = add_query_arg( 'page', 0, $json->partOf ); |
| 63 | + $json->last = add_query_arg( 'page', ( $json->totalItems%10 )-1, $json->partOf ); |
| 64 | + if ( $json->last < $page ) { |
| 65 | + $json->next = add_query_arg( 'page', ++$page, $json->partOf ); |
| 66 | + } |
| 67 | + |
| 68 | + foreach ( $posts as $post ) { |
| 69 | + $json->orderedItems[] = self::post_to_json( $post ); // phpcs:ignore |
| 70 | + } |
| 71 | + |
| 72 | + // filter output |
| 73 | + $json = apply_filters( 'activitypub_outbox_array', $json ); |
| 74 | + |
| 75 | + /* |
| 76 | + * Action triggerd after the ActivityPub profile has been created and sent to the client |
| 77 | + */ |
| 78 | + do_action( 'activitypub_outbox_post' ); |
| 79 | + |
| 80 | + $response = new WP_REST_Response( $json, 200 ); |
| 81 | + |
| 82 | + $response->header( 'Content-Type', 'application/activity-json' ); |
| 83 | + |
| 84 | + return $response; |
| 85 | + } |
| 86 | + |
| 87 | + public static function post_to_json( $post ) { |
| 88 | + $json = new stdClass(); |
| 89 | + |
| 90 | + $json->published = $post->post_date; |
| 91 | + $json->id = $post->guid; |
| 92 | + $json->type = 'Create'; |
| 93 | + $json->actor = 'https://mastodon.social/users/pfefferle'; |
| 94 | + $json->to = array( 'https://www.w3.org/ns/activitystreams#Public' ); |
| 95 | + |
| 96 | + $json->object = array( |
| 97 | + 'id' => $post->guid, |
| 98 | + 'type' => 'Note', |
| 99 | + 'published' => $post->post_date, |
| 100 | + 'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ), |
| 101 | + 'content' => $post->post_content, |
| 102 | + 'contentMap' => array( |
| 103 | + strstr( get_locale(), '_', true ) => $post->post_content, |
| 104 | + ), |
| 105 | + ); |
| 106 | + |
| 107 | + return $json; |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + public static function request_parameters() { |
| 112 | + $params = array(); |
| 113 | + |
| 114 | + $params['page'] = array( |
| 115 | + 'type' => 'integer', |
| 116 | + ); |
| 117 | + |
| 118 | + $params['id'] = array( |
| 119 | + 'required' => true, |
| 120 | + 'type' => 'integer', |
| 121 | + ); |
31 | 122 |
|
32 | | - return new WP_REST_Response( $outbox, 200 ); |
| 123 | + return $params; |
33 | 124 | } |
34 | 125 | } |
0 commit comments