Skip to:
Content

BuddyPress.org

Changeset 14037


Ignore:
Timestamp:
10/02/2024 01:34:07 PM (23 months ago)
Author:
espellcaste
Message:

BP REST API: Anonymise message data for deleted users.

Messages from deleted users are anonymised properly.

Props niftythree.

See #9160

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-messages/classes/class-bp-messages-rest-controller.php

    r14035 r14037  
    129129                // Include the meta_query for starred messages.
    130130                if ( 'starred' === $args['box'] ) {
    131                         $args['meta_query'] = array( // phpcs:ignore
     131                        $args['meta_query'] = array(
    132132                                array(
    133133                                        'key'   => 'starred_by_user',
     
    472472                }
    473473
    474                 // By default use the last message.
     474                // By default, use the last message.
    475475                $message_id = $thread->last_message_id;
    476476                if ( $request->get_param( 'message_id' ) ) {
     
    822822         */
    823823        public function prepare_message_for_response( $message, $request ) {
     824                $user         = bp_rest_get_user( $message->sender_id );
     825                $deleted_user = ! $user instanceof WP_User;
     826                $content      = $deleted_user
     827                        ? esc_html__( '[deleted]', 'buddypress' )
     828                        : $message->message;
     829
    824830                $data = array(
    825                         'id'        => (int) $message->id,
    826                         'thread_id' => (int) $message->thread_id,
    827                         'sender_id' => (int) $message->sender_id,
    828                         'subject'   => array(
     831                        'id'            => (int) $message->id,
     832                        'thread_id'     => (int) $message->thread_id,
     833                        'sender_id'     => (int) $message->sender_id,
     834                        'subject'       => array(
    829835                                'raw'      => $message->subject,
    830836                                'rendered' => apply_filters( 'bp_get_message_thread_subject', $message->subject ),
    831837                        ),
    832                         'message'   => array(
    833                                 'raw'      => $message->message,
    834                                 'rendered' => apply_filters( 'bp_get_the_thread_message_content', $message->message ),
     838                        'message'       => array(
     839                                'raw'      => $content,
     840                                'rendered' => apply_filters( 'bp_get_the_thread_message_content', $content ),
    835841                        ),
    836                         'date_sent' => bp_rest_prepare_date_response( $message->date_sent ),
     842                        'date_sent'     => bp_rest_prepare_date_response( $message->date_sent, get_date_from_gmt( $message->date_sent ) ),
     843                        'date_sent_gmt' => bp_rest_prepare_date_response( $message->date_sent ),
    837844                );
    838845
     
    875882                $display_name = '';
    876883                $user_info    = get_userdata( (int) $recipient->user_id );
    877 
    878                 if ( $user_info instanceof WP_User && ! empty( $user_info->display_name ) ) {
    879                         $display_name = (string) $user_info->display_name;
     884                $user_exists  = $user_info instanceof WP_User;
     885
     886                if ( $user_exists && ! empty( $user_info->display_name ) ) {
     887                        $display_name = $user_info->display_name;
     888                }
     889
     890                if ( false === $user_exists ) {
     891                        $display_name = esc_html__( 'Deleted User', 'buddypress' );
    880892                }
    881893
    882894                $data = array(
    883895                        'id'           => (int) $recipient->id,
    884                         'is_deleted'   => (int) $recipient->is_deleted,
     896                        'is_deleted'   => $recipient->is_deleted || ! $user_exists,
    885897                        'name'         => $display_name,
    886                         'sender_only'  => (int) $recipient->sender_only,
     898                        'sender_only'  => (bool) $recipient->sender_only,
    887899                        'thread_id'    => (int) $recipient->thread_id,
    888900                        'unread_count' => (int) $recipient->unread_count,
    889901                        'user_id'      => (int) $recipient->user_id,
    890                         'user_link'    => esc_url( bp_members_get_user_url( $recipient->user_id ) ),
     902                        'user_link'    => $user_exists ? esc_url( bp_members_get_user_url( $recipient->user_id ) ) : '',
    891903                );
    892904
     
    926938         */
    927939        public function prepare_item_for_response( $thread, $request ) {
    928                 $excerpt = '';
     940                $user_exists = function ( $user_id ) {
     941                        $user = bp_rest_get_user( $user_id );
     942
     943                        return $user instanceof WP_User;
     944                };
     945
     946                $deleted_user = false === $user_exists( $thread->last_sender_id );
     947                $raw_excerpt  = '';
     948
    929949                if ( isset( $thread->last_message_content ) ) {
    930                         $excerpt = wp_strip_all_tags( bp_create_excerpt( $thread->last_message_content, 75 ) );
    931                 }
     950                        $raw_excerpt = wp_strip_all_tags( bp_create_excerpt( $thread->last_message_content, 75 ) );
     951                }
     952
     953                $deleted_text = esc_html__( '[deleted]', 'buddypress' );
     954
     955                $content = $deleted_user
     956                        ? $deleted_text
     957                        : $thread->last_message_content;
     958
     959                $excerpt = $deleted_user
     960                        ? $deleted_text
     961                        : $raw_excerpt;
    932962
    933963                $data = array(
     
    944974                        ),
    945975                        'message'        => array(
    946                                 'raw'      => $thread->last_message_content,
    947                                 'rendered' => apply_filters( 'bp_get_message_thread_content', $thread->last_message_content ),
     976                                'raw'      => $content,
     977                                'rendered' => apply_filters( 'bp_get_the_thread_message_content', $content ),
    948978                        ),
    949979                        'date'           => bp_rest_prepare_date_response( $thread->last_message_date, get_date_from_gmt( $thread->last_message_date ) ),
    950980                        'date_gmt'       => bp_rest_prepare_date_response( $thread->last_message_date ),
    951                         'unread_count'   => ! empty( $thread->unread_count ) ? absint( $thread->unread_count ) : 0,
    952                         'sender_ids'     => (array) isset( $thread->sender_ids ) ? $thread->sender_ids : array(),
     981                        'unread_count'   => (int) $thread->unread_count,
     982                        'sender_ids'     => wp_parse_id_list( array_values( $thread->sender_ids ) ),
    953983                        'recipients'     => array(),
    954984                        'messages'       => array(),
     
    966996
    967997                // Pluck starred message ids.
    968                 $data['starred_message_ids'] = array_keys( array_filter( wp_list_pluck( $data['messages'], 'is_starred', 'id' ) ) );
     998                $data['starred_message_ids'] = wp_parse_id_list(
     999                        array_keys( array_filter( wp_list_pluck( $data['messages'], 'is_starred', 'id' ) ) )
     1000                );
    9691001
    9701002                $context  = ! empty( $request->get_param( 'context' ) ) ? $request->get_param( 'context' ) : 'view';
     
    10111043                // Add star links for each message of the thread.
    10121044                if ( is_user_logged_in() && bp_is_active( 'messages', 'star' ) ) {
    1013                         $starred_base = $base . bp_get_messages_starred_slug() . '/';
     1045                        $starred_base              = $base . bp_get_messages_starred_slug() . '/';
     1046                        $links['starred-messages'] = array();
    10141047
    10151048                        foreach ( $thread->messages as $message ) {
    1016                                 $links[ $message->id ] = array(
     1049                                $links['star-messages'][ $message->id ] = array(
    10171050                                        'href' => rest_url( $starred_base . $message->id ),
    10181051                                );
     
    12501283        public function get_item_schema() {
    12511284                if ( is_null( $this->schema ) ) {
    1252                         $this->schema = array(
     1285                        $schema = array(
    12531286                                '$schema'    => 'http://json-schema.org/draft-04/schema#',
    12541287                                'title'      => 'bp_messages',
     
    13841417                                                'type'        => 'array',
    13851418                                                'items'       => array(
    1386                                                         'type' => 'object',
     1419                                                        'type'       => 'object',
     1420                                                        'properties' => array(
     1421                                                                'id'           => array(
     1422                                                                        'description' => __( 'ID of the recipient.', 'buddypress' ),
     1423                                                                        'type'        => 'integer',
     1424                                                                        'context'     => array( 'view', 'edit' ),
     1425                                                                        'readonly'    => true,
     1426                                                                ),
     1427                                                                'thread_id'    => array(
     1428                                                                        'description' => __( 'Thread ID.', 'buddypress' ),
     1429                                                                        'type'        => 'integer',
     1430                                                                        'context'     => array( 'view', 'edit' ),
     1431                                                                        'readonly'    => true,
     1432                                                                ),
     1433                                                                'user_id'      => array(
     1434                                                                        'description' => __( 'The user ID of the recipient.', 'buddypress' ),
     1435                                                                        'type'        => 'integer',
     1436                                                                        'context'     => array( 'view', 'edit' ),
     1437                                                                        'readonly'    => true,
     1438                                                                ),
     1439                                                                'unread_count' => array(
     1440                                                                        'description' => __( 'The unread count for the recipient.', 'buddypress' ),
     1441                                                                        'type'        => 'integer',
     1442                                                                        'context'     => array( 'view', 'edit' ),
     1443                                                                        'readonly'    => true,
     1444                                                                ),
     1445                                                                'is_deleted'   => array(
     1446                                                                        'description' => __( 'Status of the recipient.', 'buddypress' ),
     1447                                                                        'type'        => 'boolean',
     1448                                                                        'context'     => array( 'view', 'edit' ),
     1449                                                                        'readonly'    => true,
     1450                                                                ),
     1451                                                                'sender_only'  => array(
     1452                                                                        'description' => __( 'If recipient is the only sender.', 'buddypress' ),
     1453                                                                        'type'        => 'boolean',
     1454                                                                        'context'     => array( 'view', 'edit' ),
     1455                                                                        'readonly'    => true,
     1456                                                                ),
     1457                                                                'name'         => array(
     1458                                                                        'description' => __( 'Name of the recipient.', 'buddypress' ),
     1459                                                                        'type'        => 'string',
     1460                                                                        'context'     => array( 'view', 'edit' ),
     1461                                                                        'readonly'    => true,
     1462                                                                ),
     1463                                                                'user_link'    => array(
     1464                                                                        'description' => __( 'The link of the recipient.', 'buddypress' ),
     1465                                                                        'type'        => 'string',
     1466                                                                        'context'     => array( 'view', 'edit' ),
     1467                                                                        'readonly'    => true,
     1468                                                                ),
     1469                                                        ),
    13871470                                                ),
    13881471                                        ),
     
    13991482                                ),
    14001483                        );
     1484
     1485                        if ( true === buddypress()->avatar->show_avatars ) {
     1486                                $avatar_properties = array();
     1487
     1488                                $avatar_properties['full'] = array(
     1489                                        /* translators: 1: Full avatar width in pixels. 2: Full avatar height in pixels */
     1490                                        '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() ) ),
     1491                                        'type'        => 'string',
     1492                                        'format'      => 'uri',
     1493                                        'context'     => array( 'view', 'edit' ),
     1494                                );
     1495
     1496                                $avatar_properties['thumb'] = array(
     1497                                        /* translators: 1: Thumb avatar width in pixels. 2: Thumb avatar height in pixels */
     1498                                        '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() ) ),
     1499                                        'type'        => 'string',
     1500                                        'format'      => 'uri',
     1501                                        'context'     => array( 'view', 'edit' ),
     1502                                );
     1503
     1504                                $schema['properties']['recipients']['items']['properties']['avatar_urls'] = array(
     1505                                        'description' => __( 'Avatar URLs for the recipient.', 'buddypress' ),
     1506                                        'type'        => 'object',
     1507                                        'context'     => array( 'view', 'edit' ),
     1508                                        'readonly'    => true,
     1509                                        'properties'  => $avatar_properties,
     1510                                );
     1511                        }
     1512
     1513                        $this->schema = $schema;
    14011514                }
    14021515
  • trunk/tests/phpunit/testcases/messages/test-controller.php

    r14035 r14037  
    337337
    338338        /**
     339         * @ticket BP9160
     340         * @group get_item
     341         */
     342        public function test_get_thread_deleted_messages() {
     343                $u1           = static::factory()->user->create();
     344                $deleted_user = static::factory()->user->create();
     345                $m            = $this->bp::factory()->message->create_and_get(
     346                        array(
     347                                'sender_id'  => $deleted_user,
     348                                'recipients' => array( $u1 ),
     349                                'subject'    => 'Foo',
     350                                'content'    => 'Content',
     351                        )
     352                );
     353
     354                $this->bp::factory()->message->create(
     355                        array(
     356                                'thread_id'  => $m->thread_id,
     357                                'sender_id'  => $u1,
     358                                'recipients' => array( $deleted_user ),
     359                                'content'    => 'Bar',
     360                        )
     361                );
     362
     363                // Delete user.
     364                if ( is_multisite() ) {
     365                        wpmu_delete_user( $deleted_user );
     366                } else {
     367                        wp_delete_user( $deleted_user );
     368                }
     369
     370                $this->bp::set_current_user( $u1 );
     371
     372                $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id );
     373                $request->set_param( 'context', 'view' );
     374                $response = $this->server->dispatch( $request );
     375
     376                $this->assertEquals( 200, $response->get_status() );
     377
     378                $all_data          = $response->get_data();
     379                $deleted_recipient = array_values(
     380                        wp_filter_object_list(
     381                                $all_data['recipients'],
     382                                array( 'user_id' => $deleted_user ),
     383                                'AND',
     384                                'is_deleted'
     385                        )
     386                );
     387
     388                $this->assertSame( $deleted_user, $all_data['last_sender_id'] );
     389                $this->assertContains( $deleted_user, $all_data['sender_ids'] );
     390                $this->assertStringContainsString( '<p>[deleted]</p>', $all_data['message']['rendered'] );
     391                $this->assertStringContainsString( '[deleted]', $all_data['excerpt']['rendered'] );
     392                $this->assertTrue( $deleted_recipient[0] );
     393                $this->assertCount( 2, $all_data['recipients'] );
     394        }
     395
     396        /**
    339397         * @group get_item
    340398         */
     
    13801438                        }
    13811439                }
    1382         }
    1383 
    1384         /**
    1385          * @group prepare_links
    1386          */
    1387         public function test_prepare_add_links_to_response() {
    1388                 $this->markTestSkipped();
    1389 
    1390                 $u1 = static::factory()->user->create();
    1391                 $u2 = static::factory()->user->create();
    1392                 $m1 = $this->bp::factory()->message->create_and_get(
    1393                         array(
    1394                                 'sender_id'  => $u1,
    1395                                 'recipients' => array( $u2 ),
    1396                                 'subject'    => 'Bar',
    1397                                 'content'    => 'Content',
    1398                         )
    1399                 );
    1400 
    1401                 $r1 = $this->bp::factory()->message->create_and_get(
    1402                         array(
    1403                                 'thread_id' => $m1->thread_id,
    1404                                 'sender_id' => $u2,
    1405                                 'content'   => 'Reply',
    1406                         )
    1407                 );
    1408 
    1409                 $this->bp::set_current_user( $u2 );
    1410 
    1411                 $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m1->thread_id );
    1412                 $request->set_param( 'context', 'view' );
    1413                 $response = $this->server->dispatch( $request );
    1414 
    1415                 $this->assertEquals( 200, $response->get_status() );
    1416 
    1417                 $get_links = $response->get_data();
    1418 
    1419                 $this->assertNotEmpty( $get_links );
    1420 
    1421                 $links = $get_links['_links'];
    1422 
    1423                 $this->assertEquals( rest_url( $this->endpoint_url . '/' ), $links['collection'][0]['href'] );
    1424                 $this->assertEquals( rest_url( $this->endpoint_url . '/' . $m1->thread_id ), $links['self'][0]['href'] );
    1425                 $this->assertEquals( rest_url( $this->endpoint_url . '/' . bp_get_messages_starred_slug() . '/' . $m1->id ), $links[ $m1->id ][0]['href'] );
    1426                 $this->assertEquals( rest_url( $this->endpoint_url . '/' . bp_get_messages_starred_slug() . '/' . $r1->id ), $links[ $r1->id ][0]['href'] );
    14271440        }
    14281441
     
    14461459                $this->assertEquals( bp_rest_prepare_date_response( $thread->last_message_date ), $data['date_gmt'] );
    14471460                $this->assertEquals( $thread->unread_count, $data['unread_count'] );
    1448                 $this->assertEquals( $thread->sender_ids, $data['sender_ids'] );
     1461                $this->assertEquals( array_values( $thread->sender_ids ), $data['sender_ids'] );
    14491462        }
    14501463
Note: See TracChangeset for help on using the changeset viewer.