<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][14037] trunk: BP REST API: Anonymise message data for deleted users.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/14037">14037</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/14037","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>espellcaste</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-10-02 13:34:07 +0000 (Wed, 02 Oct 2024)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>BP REST API: Anonymise message data for deleted users.

Messages from deleted users are anonymised properly.

Props niftythree.

See <a href="http://buddypress.trac.wordpress.org/ticket/9160">#9160</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpmessagesclassesclassbpmessagesrestcontrollerphp">trunk/src/bp-messages/classes/class-bp-messages-rest-controller.php</a></li>
<li><a href="#trunktestsphpunittestcasesmessagestestcontrollerphp">trunk/tests/phpunit/testcases/messages/test-controller.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpmessagesclassesclassbpmessagesrestcontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-messages/classes/class-bp-messages-rest-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-messages/classes/class-bp-messages-rest-controller.php       2024-10-02 04:18:20 UTC (rev 14036)
+++ trunk/src/bp-messages/classes/class-bp-messages-rest-controller.php 2024-10-02 13:34:07 UTC (rev 14037)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -128,7 +128,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Include the meta_query for starred messages.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( 'starred' === $args['box'] ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $args['meta_query'] = array( // phpcs:ignore
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $args['meta_query'] = array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                 array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'key'   => 'starred_by_user',
</span><span class="cx" style="display: block; padding: 0 10px">                                        'value' => $args['user_id'],
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -471,7 +471,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        messages_mark_thread_unread( $thread->thread_id, $updated_user_id );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // By default use the last message.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // By default, use the last message.
</ins><span class="cx" style="display: block; padding: 0 10px">                 $message_id = $thread->last_message_id;
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $request->get_param( 'message_id' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $message_id = $request->get_param( 'message_id' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -821,19 +821,26 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @return array The Message data for the REST response.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function prepare_message_for_response( $message, $request ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $user         = bp_rest_get_user( $message->sender_id );
+               $deleted_user = ! $user instanceof WP_User;
+               $content      = $deleted_user
+                       ? esc_html__( '[deleted]', 'buddypress' )
+                       : $message->message;
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $data = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'id'        => (int) $message->id,
-                       'thread_id' => (int) $message->thread_id,
-                       'sender_id' => (int) $message->sender_id,
-                       'subject'   => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'id'            => (int) $message->id,
+                       'thread_id'     => (int) $message->thread_id,
+                       'sender_id'     => (int) $message->sender_id,
+                       'subject'       => array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'raw'      => $message->subject,
</span><span class="cx" style="display: block; padding: 0 10px">                                'rendered' => apply_filters( 'bp_get_message_thread_subject', $message->subject ),
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'message'   => array(
-                               'raw'      => $message->message,
-                               'rendered' => apply_filters( 'bp_get_the_thread_message_content', $message->message ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'message'       => array(
+                               'raw'      => $content,
+                               'rendered' => apply_filters( 'bp_get_the_thread_message_content', $content ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'date_sent' => bp_rest_prepare_date_response( $message->date_sent ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'date_sent'     => bp_rest_prepare_date_response( $message->date_sent, get_date_from_gmt( $message->date_sent ) ),
+                       'date_sent_gmt' => bp_rest_prepare_date_response( $message->date_sent ),
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( bp_is_active( 'messages', 'star' ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -874,20 +881,25 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public function prepare_recipient_for_response( $recipient, $request ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $display_name = '';
</span><span class="cx" style="display: block; padding: 0 10px">                $user_info    = get_userdata( (int) $recipient->user_id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $user_exists  = $user_info instanceof WP_User;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( $user_info instanceof WP_User && ! empty( $user_info->display_name ) ) {
-                       $display_name = (string) $user_info->display_name;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( $user_exists && ! empty( $user_info->display_name ) ) {
+                       $display_name = $user_info->display_name;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( false === $user_exists ) {
+                       $display_name = esc_html__( 'Deleted User', 'buddypress' );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $data = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'id'           => (int) $recipient->id,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'is_deleted'   => (int) $recipient->is_deleted,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'is_deleted'   => $recipient->is_deleted || ! $user_exists,
</ins><span class="cx" style="display: block; padding: 0 10px">                         'name'         => $display_name,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'sender_only'  => (int) $recipient->sender_only,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'sender_only'  => (bool) $recipient->sender_only,
</ins><span class="cx" style="display: block; padding: 0 10px">                         'thread_id'    => (int) $recipient->thread_id,
</span><span class="cx" style="display: block; padding: 0 10px">                        'unread_count' => (int) $recipient->unread_count,
</span><span class="cx" style="display: block; padding: 0 10px">                        'user_id'      => (int) $recipient->user_id,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'user_link'    => esc_url( bp_members_get_user_url( $recipient->user_id ) ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'user_link'    => $user_exists ? esc_url( bp_members_get_user_url( $recipient->user_id ) ) : '',
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Fetch the user avatar urls (Full & thumb).
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -925,11 +937,29 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @return WP_REST_Response
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function prepare_item_for_response( $thread, $request ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $excerpt = '';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $user_exists = function ( $user_id ) {
+                       $user = bp_rest_get_user( $user_id );
+
+                       return $user instanceof WP_User;
+               };
+
+               $deleted_user = false === $user_exists( $thread->last_sender_id );
+               $raw_excerpt  = '';
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( isset( $thread->last_message_content ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $excerpt = wp_strip_all_tags( bp_create_excerpt( $thread->last_message_content, 75 ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $raw_excerpt = wp_strip_all_tags( bp_create_excerpt( $thread->last_message_content, 75 ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $deleted_text = esc_html__( '[deleted]', 'buddypress' );
+
+               $content = $deleted_user
+                       ? $deleted_text
+                       : $thread->last_message_content;
+
+               $excerpt = $deleted_user
+                       ? $deleted_text
+                       : $raw_excerpt;
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $data = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'id'             => (int) isset( $thread->thread_id ) ? $thread->thread_id : 0,
</span><span class="cx" style="display: block; padding: 0 10px">                        'message_id'     => (int) isset( $thread->last_message_id ) ? $thread->last_message_id : 0,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -943,13 +973,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                'rendered' => apply_filters( 'bp_get_message_thread_excerpt', $excerpt ),
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'message'        => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'raw'      => $thread->last_message_content,
-                               'rendered' => apply_filters( 'bp_get_message_thread_content', $thread->last_message_content ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'raw'      => $content,
+                               'rendered' => apply_filters( 'bp_get_the_thread_message_content', $content ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'date'           => bp_rest_prepare_date_response( $thread->last_message_date, get_date_from_gmt( $thread->last_message_date ) ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'date_gmt'       => bp_rest_prepare_date_response( $thread->last_message_date ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'unread_count'   => ! empty( $thread->unread_count ) ? absint( $thread->unread_count ) : 0,
-                       'sender_ids'     => (array) isset( $thread->sender_ids ) ? $thread->sender_ids : array(),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'unread_count'   => (int) $thread->unread_count,
+                       'sender_ids'     => wp_parse_id_list( array_values( $thread->sender_ids ) ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         'recipients'     => array(),
</span><span class="cx" style="display: block; padding: 0 10px">                        'messages'       => array(),
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -965,7 +995,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Pluck starred message ids.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $data['starred_message_ids'] = array_keys( array_filter( wp_list_pluck( $data['messages'], 'is_starred', 'id' ) ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $data['starred_message_ids'] = wp_parse_id_list(
+                       array_keys( array_filter( wp_list_pluck( $data['messages'], 'is_starred', 'id' ) ) )
+               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $context  = ! empty( $request->get_param( 'context' ) ) ? $request->get_param( 'context' ) : 'view';
</span><span class="cx" style="display: block; padding: 0 10px">                $data     = $this->add_additional_fields_to_object( $data, $request );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1010,10 +1042,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Add star links for each message of the thread.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( is_user_logged_in() && bp_is_active( 'messages', 'star' ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $starred_base = $base . bp_get_messages_starred_slug() . '/';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $starred_base              = $base . bp_get_messages_starred_slug() . '/';
+                       $links['starred-messages'] = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        foreach ( $thread->messages as $message ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $links[ $message->id ] = array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $links['star-messages'][ $message->id ] = array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                         'href' => rest_url( $starred_base . $message->id ),
</span><span class="cx" style="display: block; padding: 0 10px">                                );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1249,7 +1282,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function get_item_schema() {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( is_null( $this->schema ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->schema = array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $schema = array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                 '$schema'    => 'http://json-schema.org/draft-04/schema#',
</span><span class="cx" style="display: block; padding: 0 10px">                                'title'      => 'bp_messages',
</span><span class="cx" style="display: block; padding: 0 10px">                                'type'       => 'object',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1383,7 +1416,57 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                                'readonly'    => true,
</span><span class="cx" style="display: block; padding: 0 10px">                                                'type'        => 'array',
</span><span class="cx" style="display: block; padding: 0 10px">                                                'items'       => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                        'type' => 'object',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                                 'type'       => 'object',
+                                                       'properties' => array(
+                                                               'id'           => array(
+                                                                       'description' => __( 'ID of the recipient.', 'buddypress' ),
+                                                                       'type'        => 'integer',
+                                                                       'context'     => array( 'view', 'edit' ),
+                                                                       'readonly'    => true,
+                                                               ),
+                                                               'thread_id'    => array(
+                                                                       'description' => __( 'Thread ID.', 'buddypress' ),
+                                                                       'type'        => 'integer',
+                                                                       'context'     => array( 'view', 'edit' ),
+                                                                       'readonly'    => true,
+                                                               ),
+                                                               'user_id'      => array(
+                                                                       'description' => __( 'The user ID of the recipient.', 'buddypress' ),
+                                                                       'type'        => 'integer',
+                                                                       'context'     => array( 'view', 'edit' ),
+                                                                       'readonly'    => true,
+                                                               ),
+                                                               'unread_count' => array(
+                                                                       'description' => __( 'The unread count for the recipient.', 'buddypress' ),
+                                                                       'type'        => 'integer',
+                                                                       'context'     => array( 'view', 'edit' ),
+                                                                       'readonly'    => true,
+                                                               ),
+                                                               'is_deleted'   => array(
+                                                                       'description' => __( 'Status of the recipient.', 'buddypress' ),
+                                                                       'type'        => 'boolean',
+                                                                       'context'     => array( 'view', 'edit' ),
+                                                                       'readonly'    => true,
+                                                               ),
+                                                               'sender_only'  => array(
+                                                                       'description' => __( 'If recipient is the only sender.', 'buddypress' ),
+                                                                       'type'        => 'boolean',
+                                                                       'context'     => array( 'view', 'edit' ),
+                                                                       'readonly'    => true,
+                                                               ),
+                                                               'name'         => array(
+                                                                       'description' => __( 'Name of the recipient.', 'buddypress' ),
+                                                                       'type'        => 'string',
+                                                                       'context'     => array( 'view', 'edit' ),
+                                                                       'readonly'    => true,
+                                                               ),
+                                                               'user_link'    => array(
+                                                                       'description' => __( 'The link of the recipient.', 'buddypress' ),
+                                                                       'type'        => 'string',
+                                                                       'context'     => array( 'view', 'edit' ),
+                                                                       'readonly'    => true,
+                                                               ),
+                                                       ),
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'starred_message_ids' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1398,6 +1481,36 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><span class="cx" style="display: block; padding: 0 10px">                        );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       if ( true === buddypress()->avatar->show_avatars ) {
+                               $avatar_properties = array();
+
+                               $avatar_properties['full'] = array(
+                                       /* translators: 1: Full avatar width in pixels. 2: Full avatar height in pixels */
+                                       'description' => sprintf( __( 'Avatar URL with full image size (%1$d x %2$d pixels).', 'buddypress' ), number_format_i18n( bp_core_avatar_full_width() ), number_format_i18n( bp_core_avatar_full_height() ) ),
+                                       'type'        => 'string',
+                                       'format'      => 'uri',
+                                       'context'     => array( 'view', 'edit' ),
+                               );
+
+                               $avatar_properties['thumb'] = array(
+                                       /* translators: 1: Thumb avatar width in pixels. 2: Thumb avatar height in pixels */
+                                       'description' => sprintf( __( 'Avatar URL with thumb image size (%1$d x %2$d pixels).', 'buddypress' ), number_format_i18n( bp_core_avatar_thumb_width() ), number_format_i18n( bp_core_avatar_thumb_height() ) ),
+                                       'type'        => 'string',
+                                       'format'      => 'uri',
+                                       'context'     => array( 'view', 'edit' ),
+                               );
+
+                               $schema['properties']['recipients']['items']['properties']['avatar_urls'] = array(
+                                       'description' => __( 'Avatar URLs for the recipient.', 'buddypress' ),
+                                       'type'        => 'object',
+                                       'context'     => array( 'view', 'edit' ),
+                                       'readonly'    => true,
+                                       'properties'  => $avatar_properties,
+                               );
+                       }
+
+                       $this->schema = $schema;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span></span></pre></div>
<a id="trunktestsphpunittestcasesmessagestestcontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/testcases/messages/test-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/messages/test-controller.php        2024-10-02 04:18:20 UTC (rev 14036)
+++ trunk/tests/phpunit/testcases/messages/test-controller.php  2024-10-02 13:34:07 UTC (rev 14037)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -336,8 +336,66 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket BP9160
</ins><span class="cx" style="display: block; padding: 0 10px">          * @group get_item
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        public function test_get_thread_deleted_messages() {
+               $u1           = static::factory()->user->create();
+               $deleted_user = static::factory()->user->create();
+               $m            = $this->bp::factory()->message->create_and_get(
+                       array(
+                               'sender_id'  => $deleted_user,
+                               'recipients' => array( $u1 ),
+                               'subject'    => 'Foo',
+                               'content'    => 'Content',
+                       )
+               );
+
+               $this->bp::factory()->message->create(
+                       array(
+                               'thread_id'  => $m->thread_id,
+                               'sender_id'  => $u1,
+                               'recipients' => array( $deleted_user ),
+                               'content'    => 'Bar',
+                       )
+               );
+
+               // Delete user.
+               if ( is_multisite() ) {
+                       wpmu_delete_user( $deleted_user );
+               } else {
+                       wp_delete_user( $deleted_user );
+               }
+
+               $this->bp::set_current_user( $u1 );
+
+               $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id );
+               $request->set_param( 'context', 'view' );
+               $response = $this->server->dispatch( $request );
+
+               $this->assertEquals( 200, $response->get_status() );
+
+               $all_data          = $response->get_data();
+               $deleted_recipient = array_values(
+                       wp_filter_object_list(
+                               $all_data['recipients'],
+                               array( 'user_id' => $deleted_user ),
+                               'AND',
+                               'is_deleted'
+                       )
+               );
+
+               $this->assertSame( $deleted_user, $all_data['last_sender_id'] );
+               $this->assertContains( $deleted_user, $all_data['sender_ids'] );
+               $this->assertStringContainsString( '<p>[deleted]</p>', $all_data['message']['rendered'] );
+               $this->assertStringContainsString( '[deleted]', $all_data['excerpt']['rendered'] );
+               $this->assertTrue( $deleted_recipient[0] );
+               $this->assertCount( 2, $all_data['recipients'] );
+       }
+
+       /**
+        * @group get_item
+        */
</ins><span class="cx" style="display: block; padding: 0 10px">         public function test_get_item_user_is_not_logged_in() {
</span><span class="cx" style="display: block; padding: 0 10px">                $u1 = static::factory()->user->create();
</span><span class="cx" style="display: block; padding: 0 10px">                $u2 = static::factory()->user->create();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1382,51 +1440,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @group prepare_links
-        */
-       public function test_prepare_add_links_to_response() {
-               $this->markTestSkipped();
-
-               $u1 = static::factory()->user->create();
-               $u2 = static::factory()->user->create();
-               $m1 = $this->bp::factory()->message->create_and_get(
-                       array(
-                               'sender_id'  => $u1,
-                               'recipients' => array( $u2 ),
-                               'subject'    => 'Bar',
-                               'content'    => 'Content',
-                       )
-               );
-
-               $r1 = $this->bp::factory()->message->create_and_get(
-                       array(
-                               'thread_id' => $m1->thread_id,
-                               'sender_id' => $u2,
-                               'content'   => 'Reply',
-                       )
-               );
-
-               $this->bp::set_current_user( $u2 );
-
-               $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m1->thread_id );
-               $request->set_param( 'context', 'view' );
-               $response = $this->server->dispatch( $request );
-
-               $this->assertEquals( 200, $response->get_status() );
-
-               $get_links = $response->get_data();
-
-               $this->assertNotEmpty( $get_links );
-
-               $links = $get_links['_links'];
-
-               $this->assertEquals( rest_url( $this->endpoint_url . '/' ), $links['collection'][0]['href'] );
-               $this->assertEquals( rest_url( $this->endpoint_url . '/' . $m1->thread_id ), $links['self'][0]['href'] );
-               $this->assertEquals( rest_url( $this->endpoint_url . '/' . bp_get_messages_starred_slug() . '/' . $m1->id ), $links[ $m1->id ][0]['href'] );
-               $this->assertEquals( rest_url( $this->endpoint_url . '/' . bp_get_messages_starred_slug() . '/' . $r1->id ), $links[ $r1->id ][0]['href'] );
-       }
-
-       /**
</del><span class="cx" style="display: block; padding: 0 10px">          * @group get_item
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_prepare_item() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1445,7 +1458,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( bp_rest_prepare_date_response( $thread->last_message_date ), $data['date_gmt'] );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( $thread->unread_count, $data['unread_count'] );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertEquals( $thread->sender_ids, $data['sender_ids'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertEquals( array_values( $thread->sender_ids ), $data['sender_ids'] );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_get_item_schema() {
</span></span></pre>
</div>
</div>

</body>
</html>