Skip to content

Commit

Permalink
Improve inline docs and change the starred endpoint URL
Browse files Browse the repository at this point in the history
Changing the starred endpoint URL should limit the confusion between thread ID and Message ID, as the star feature is using message ids.
  • Loading branch information
imath committed Jul 4, 2019
1 parent 203eecb commit cc2d9f5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 40 deletions.
98 changes: 65 additions & 33 deletions includes/bp-messages/classes/class-bp-rest-messages-endpoint.php
Expand Up @@ -46,15 +46,29 @@ public function register_routes() {
'callback' => array( $this, 'create_item' ),
'permission_callback' => array( $this, 'create_item_permissions_check' ),
'args' => array(
'thread_id' => array(
'description' => __( 'Leave empty to init a new thread. Specify the thread ID to reply to an existing one.', 'buddypress' ),
'required' => false,
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
),
'sender_id' => array(
'description' => __( 'The user ID of the sender. Defaults to the logged in user.', 'buddypress' ),
'required' => false,
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
),
'content' => array(
'description' => __( 'Content of the message.', 'buddypress' ),
'description' => __( 'Content of the message/reply.', 'buddypress' ),
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
),
'recipients' => array(
'description' => __( 'Recipients of the message.', 'buddypress' ),
'description' => __( 'Recipients of the message/reply.', 'buddypress' ),
'required' => true,
'type' => 'array',
'items' => array( 'type' => 'integer' ),
Expand All @@ -67,11 +81,12 @@ public function register_routes() {
)
);

$message_endpoint = '/' . $this->rest_base . '/(?P<id>[\d]+)';
// Attention: (?P<id>[\d]+) is the placeholder for **Thread** ID, not the Message ID one.
$thread_endpoint = '/' . $this->rest_base . '/(?P<id>[\d]+)';

register_rest_route(
$this->namespace,
$message_endpoint,
$thread_endpoint,
array(
array(
'methods' => WP_REST_Server::READABLE,
Expand All @@ -89,9 +104,12 @@ public function register_routes() {

// Register the starred route.
if ( bp_is_active( 'messages', 'star' ) ) {
// Attention: (?P<id>[\d]+) is the placeholder for **Message** ID, not the Thread ID one.
$starred_endpoint = '/' . $this->rest_base . '/' . bp_get_messages_starred_slug() . '/(?P<id>[\d]+)';

register_rest_route(
$this->namespace,
trailingslashit( $message_endpoint ) . bp_get_messages_starred_slug(),
$starred_endpoint,
array(
array(
'methods' => WP_REST_Server::EDITABLE,
Expand All @@ -102,16 +120,6 @@ public function register_routes() {
)
);
}

/**
* Fires when REST API supported endpoints are registered.
*
* @since 0.1.0
*
* @param string $namespace The Messages component namespace.
* @param string $message_endpoint The single item message endpoint.
*/
do_action( 'bp_rest_messages_register_routes', $this->namespace, $message_endpoint );
}

/**
Expand Down Expand Up @@ -566,6 +574,15 @@ public function delete_item_permissions_check( $request ) {
return apply_filters( 'bp_rest_messages_delete_item_permissions_check', $retval, $request );
}

/**
* Prepares message data for the REST response.
*
* @since 0.1.0
*
* @param object The Message object.
* @param WP_REST_Request $request Full details about the request.
* @return array The Message data for the REST response.
*/
public function prepare_message_for_response( $message, $request ) {
$data = array(
'id' => (int) $message->id,
Expand Down Expand Up @@ -599,7 +616,7 @@ public function prepare_message_for_response( $message, $request ) {
}

/**
* Prepares message data for return as an object.
* Prepares thread data for return as an object.
*
* @since 0.1.0
*
Expand All @@ -614,26 +631,26 @@ public function prepare_item_for_response( $thread, $request ) {
}

$data = array(
'id' => $thread->thread_id,
'primary_item_id' => $thread->last_message_id,
'secondary_item_id' => $thread->last_sender_id,
'subject' => array(
'id' => $thread->thread_id,
'message_id' => $thread->last_message_id,
'last_sender_id' => $thread->last_sender_id,
'subject' => array(
'raw' => $thread->last_message_subject,
'rendered' => apply_filters( 'bp_get_message_thread_subject', wp_staticize_emoji( $thread->last_message_subject ) ),
),
'excerpt' => array(
'excerpt' => array(
'raw' => $excerpt,
'rendered' => apply_filters( 'bp_get_message_thread_excerpt', $excerpt ),
),
'message' => array(
'message' => array(
'raw' => $thread->last_message_content,
'rendered' => apply_filters( 'bp_get_message_thread_content', wp_staticize_emoji( $thread->last_message_content ) ),
),
'date' => bp_rest_prepare_date_response( $thread->last_message_date ),
'unread_count' => ! empty( $thread->unread_count ) ? $thread->unread_count : 0,
'sender_ids' => $thread->sender_ids,
'recipients' => $thread->recipients,
'messages' => array(),
'date' => bp_rest_prepare_date_response( $thread->last_message_date ),
'unread_count' => ! empty( $thread->unread_count ) ? $thread->unread_count : 0,
'sender_ids' => $thread->sender_ids,
'recipients' => $thread->recipients,
'messages' => array(),
);

// Loop through messages to prepare them for the response.
Expand Down Expand Up @@ -673,12 +690,17 @@ public function prepare_item_for_response( $thread, $request ) {
protected function prepare_item_for_database( $request ) {
$prepared_thread = new stdClass();
$schema = $this->get_item_schema();
$thread = $this->get_thread_object( $request['id'] );

if ( ! empty( $schema['properties']['id'] ) && ! empty( $thread->thread_id ) ) {
$prepared_thread->thread_id = $thread->thread_id;
} else {
$prepared_thread->thread_id = false;
// By default, let's init a new Thread.
$prepared_thread->thread_id = false;

// If it's a reply, get the parent thread.
if ( $request['thread_id'] ) {
$thread = $this->get_thread_object( $request['thread_id'] );

if ( ! empty( $schema['properties']['id'] ) && ! empty( $thread->thread_id ) ) {
$prepared_thread->thread_id = $thread->thread_id;
}
}

if ( ! empty( $schema['properties']['last_sender_id'] ) && ! empty( $thread->sender_id ) ) {
Expand Down Expand Up @@ -719,19 +741,29 @@ protected function prepare_item_for_database( $request ) {
/**
* Get thread object.
*
* @since 0.1.0
*
* @param int $thread_id Thread ID.
* @return BP_Messages_Thread
*/
public function get_thread_object( $thread_id ) {
return new BP_Messages_Thread( $thread_id );
}

/**
* Get the message object thanks to its ID.
*
* @since 0.1.0
*
* @param int $message_id Message ID.
* @return BP_Messages_Message
*/
public function get_message_object( $message_id ) {
return new BP_Messages_Message( $message_id );
}

/**
* Get the plugin schema, conforming to JSON Schema.
* Get the BP Messages schema, conforming to JSON Schema.
*
* @since 0.1.0
*
Expand Down
14 changes: 7 additions & 7 deletions tests/messages/test-controller.php
Expand Up @@ -462,7 +462,7 @@ public function test_get_starred_items() {
) );

// Create a reply.
$r1 = $this->bp_factory->message->create( array(
$r1 = $this->bp_factory->message->create_and_get( array(
'thread_id' => $m1->thread_id,
'sender_id' => $u2,
'recipients' => array( $u1 ),
Expand All @@ -473,7 +473,7 @@ public function test_get_starred_items() {

$star = bp_messages_star_set_action( array(
'user_id' => $u1,
'message_id' => $r1,
'message_id' => $r1->id,
) );

$request = new WP_REST_Request( 'GET', $this->endpoint_url );
Expand All @@ -490,11 +490,11 @@ public function test_get_starred_items() {

$threads = wp_list_pluck( $data, 'id' );
$this->assertNotContains( $m2_id, $threads );
$this->assertContains( $m1->id, $threads );
$this->assertContains( $m1->thread_id, $threads );

$result = reset( $data );
$this->assertNotEmpty( $result['starred_message_ids'] );
$this->assertContains( $r1, $result['starred_message_ids'] );
$this->assertContains( $r1->id, $result['starred_message_ids'] );
}

/**
Expand All @@ -512,7 +512,7 @@ public function test_update_starred_add_star() {
) );

// Create a reply.
$r1 = $this->bp_factory->message->create( array(
$r1 = $this->bp_factory->message->create_and_get( array(
'thread_id' => $m1->thread_id,
'sender_id' => $u2,
'recipients' => array( $u1 ),
Expand All @@ -521,7 +521,7 @@ public function test_update_starred_add_star() {

$this->bp->set_current_user( $u2 );

$request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . $r1 . '/' . bp_get_messages_starred_slug() );
$request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . bp_get_messages_starred_slug() . '/' . $r1->id );
$request->add_header( 'content-type', 'application/json' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
Expand Down Expand Up @@ -550,7 +550,7 @@ public function test_update_starred_remove_star() {

$this->bp->set_current_user( $u2 );

$request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . $m->id . '/' . bp_get_messages_starred_slug() );
$request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . bp_get_messages_starred_slug() . '/' . $m->id );
$request->add_header( 'content-type', 'application/json' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
Expand Down

0 comments on commit cc2d9f5

Please sign in to comment.