Skip to content

Commit

Permalink
Messages: add support for slicing the thread messages
Browse files Browse the repository at this point in the history
  • Loading branch information
renatonascalves committed Apr 21, 2024
1 parent 7297884 commit 1e37ca3
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 12 deletions.
114 changes: 103 additions & 11 deletions includes/bp-messages/classes/class-bp-rest-messages-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ public function register_routes() {
$this->namespace,
$thread_endpoint,
array(
'args' => array(
'id' => array(
'description' => __( 'ID of the thread.', 'buddypress' ),
'type' => 'integer',
'required' => true,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
Expand Down Expand Up @@ -240,12 +249,34 @@ public function get_items_permissions_check( $request ) {
* @return WP_REST_Response
*/
public function get_item( $request ) {
$args = array(
'recipients_page' => $request->get_param( 'recipients_page' ),
'recipients_per_page' => $request->get_param( 'recipients_per_page' ),
'page' => $request->get_param( 'messages_page' ),
'per_page' => $request->get_param( 'messages_per_page' ),
);

$user_id = bp_loggedin_user_id();
if ( ! empty( $request->get_param( 'user_id' ) ) ) {
$user_id = $request->get_param( 'user_id' );
}

$thread = $this->get_thread_object( $request->get_param( 'id' ), $user_id );
$args['user_id'] = $user_id;

/**
* Filter the query arguments for the request.
*
* @param array $args Key value array of query var to query value.
* @param WP_REST_Request $request The request sent to the API.
*/
$args = apply_filters( 'bp_rest_messages_get_item_query_args', $args, $request );

$thread = new BP_Messages_Thread(
$request->get_param( 'id' ),
$request->get_param( 'order' ),
$args
);

$retval = array(
$this->prepare_response_for_collection(
$this->prepare_item_for_response( $thread, $request )
Expand Down Expand Up @@ -291,8 +322,10 @@ public function get_item_permissions_check( $request ) {
$user_id = $request->get_param( 'user_id' );
}

$id = $request->get_param( 'id' );

if ( is_user_logged_in() ) {
$thread = $this->get_thread_object( $request->get_param( 'id' ), $user_id );
$thread = BP_Messages_Thread::is_valid( $id );

if ( empty( $thread ) ) {
$retval = new WP_Error(
Expand All @@ -302,10 +335,8 @@ public function get_item_permissions_check( $request ) {
'status' => 404,
)
);
} elseif ( bp_current_user_can( 'bp_moderate' ) || messages_check_thread_access( $thread->thread_id, $user_id ) ) {
} elseif ( bp_current_user_can( 'bp_moderate' ) || messages_check_thread_access( $id, $user_id ) ) {
$retval = true;
} else {
$retval = $error;
}
}

Expand Down Expand Up @@ -1045,13 +1076,14 @@ public function get_thread_object( $thread_id, $user_id = 0 ) {
$args = array( 'user_id' => $user_id );
}

$thread_object = new BP_Messages_Thread( (int) $thread_id, 'ASC', $args );
// Validate the thread ID.
$thread_id = BP_Messages_Thread::is_valid( $thread_id );

if ( false === (bool) $thread_object::is_valid( $thread_id ) ) {
if ( false === (bool) $thread_id ) {
return '';
}

return $thread_object;
return new BP_Messages_Thread( (int) $thread_id, 'ASC', $args );
}

/**
Expand Down Expand Up @@ -1081,7 +1113,6 @@ public function get_message_object( $message_id ) {
* @return array Endpoint arguments.
*/
public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CREATABLE ) {
$key = 'get_item';
$args = parent::get_endpoint_args_for_item_schema( $method );
$args['id']['description'] = __( 'ID of the Messages Thread.', 'buddypress' );

Expand Down Expand Up @@ -1122,8 +1153,6 @@ public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CRE
unset( $args['subject']['properties'], $args['message']['properties'] );

} else {
unset( $args['sender_id'], $args['subject'], $args['message'], $args['recipients'] );

if ( WP_REST_Server::EDITABLE === $method ) {
$key = 'update_item';

Expand Down Expand Up @@ -1165,6 +1194,69 @@ public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CRE
'validate_callback' => 'rest_validate_request_arg',
'default' => bp_loggedin_user_id(),
);

unset( $args['sender_id'], $args['subject'], $args['message'], $args['recipients'] );
}

if ( WP_REST_Server::READABLE === $method ) {
unset( $args['sender_id'], $args['subject'], $args['message'], $args['recipients'] );

$key = 'get_item';

$args['user_id'] = array(
'description' => __( 'The user ID to get the thread for.', 'buddypress' ),
'required' => false,
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);

$args['recipients_page'] = array(
'description' => __( 'Current page of the recipients collection.', 'buddypress' ),
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 1,
);

$args['recipients_per_page'] = array(
'description' => __( 'Maximum number of recipients to be returned in result set.', 'buddypress' ),
'type' => 'integer',
'default' => 10,
'minimum' => 1,
'maximum' => 100,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);

$args['messages_page'] = array(
'description' => __( 'Current page of the messages collection.', 'buddypress' ),
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 1,
);

$args['messages_per_page'] = array(
'description' => __( 'Maximum number of messages to be returned in result set.', 'buddypress' ),
'type' => 'integer',
'default' => 10,
'minimum' => 1,
'maximum' => 100,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);

$args['order'] = array(
'description' => __( 'Order sort attribute ascending or descending.', 'buddypress' ),
'default' => 'asc',
'type' => 'string',
'enum' => array( 'asc', 'desc' ),
'sanitize_callback' => 'sanitize_key',
'validate_callback' => 'rest_validate_request_arg',
);
}
}

Expand Down
41 changes: 40 additions & 1 deletion tests/testcases/messages/test-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,42 @@ public function test_get_item() {
$this->check_thread_data( $this->endpoint->get_thread_object( $data['id'], $u2 ), $data );
}

/**
* @group get_item
*/
public function test_get_item_messages() {
$u1 = $this->factory->user->create();
$u2 = $this->factory->user->create();
$m = $this->bp_factory->message->create_and_get( array(
'sender_id' => $u1,
'recipients' => array( $u2 ),
'subject' => 'Foo',
'content' => 'Content',
) );

// create a reply.
$message_id = $this->bp_factory->message->create( array(
'sender_id' => $u2,
'thread_id' => $m->thread_id,
'recipients' => array( $u1 ),
'content' => 'Bar',
) );

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

$request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id );
$request->set_param( 'context', 'view' );
$request->set_param( 'messages_per_page', 1 );
$response = $this->server->dispatch( $request );

$this->assertEquals( 200, $response->get_status() );

$all_data = current( $response->get_data() );

$this->assertCount( 1, $all_data['messages'] );
$this->assertSame( $message_id, $all_data['messages'][0]['id'] );
}

/**
* @group get_item
*/
Expand Down Expand Up @@ -231,6 +267,7 @@ public function test_get_item_user_with_no_access() {
$response = $this->server->dispatch( $request );

$this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() );
$this->assertSame( 403, $response->get_status() );
}

/**
Expand All @@ -239,7 +276,8 @@ public function test_get_item_user_with_no_access() {
public function test_get_item_user_is_not_logged_in() {
$u1 = $this->factory->user->create();
$u2 = $this->factory->user->create();
$u3 = $this->factory->user->create();
$this->factory->user->create();

$m = $this->bp_factory->message->create_and_get( array(
'sender_id' => $u1,
'recipients' => array( $u2 ),
Expand All @@ -252,6 +290,7 @@ public function test_get_item_user_is_not_logged_in() {
$response = $this->server->dispatch( $request );

$this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() );
$this->assertSame( 401, $response->get_status() );
}

/**
Expand Down

0 comments on commit 1e37ca3

Please sign in to comment.