Skip to:
Content

BuddyPress.org

Changeset 13402


Ignore:
Timestamp:
01/08/2023 11:12:01 PM (16 months ago)
Author:
espellcaste
Message:

BP_Messages_Thread: return the *right* latest message.

Introducing a new method get_latest_thread_message() to the BP_Messages_Thread class which returns the latest message of a thread.

Closes https://github.com/buddypress/buddypress/pull/52
Fixes #8696

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-messages/bp-messages-cache.php

    r13401 r13402  
    6666    }
    6767
     68    // Delete thread latest message cached data.
     69    wp_cache_delete( "{$message->thread_id}_bp_messages_thread_latest_message", 'bp_messages_threads' );
     70
    6871    // Delete thread recipient cache.
    6972    wp_cache_delete( 'thread_recipients_' . $message->thread_id, 'bp_messages' );
     
    8588        wp_cache_delete( $thread_id, 'bp_messages_threads' );
    8689        wp_cache_delete( "thread_recipients_{$thread_id}", 'bp_messages' );
     90
     91        // Delete thread latest message cached data.
     92        wp_cache_delete( "{$thread_id}_bp_messages_thread_latest_message", 'bp_messages_threads' );
    8793
    8894        // Delete thread messages count.
  • trunk/src/bp-messages/classes/class-bp-messages-thread.php

    r13401 r13402  
    191191        $this->thread_id      = (int) $thread_id;
    192192
     193        // Get latest message.
     194        $latest_message = self::get_latest_thread_message( $this->thread_id );
     195
     196        // Bail early if no thread message is found.
     197        if ( empty( $latest_message ) ) {
     198            return false;
     199        }
     200
     201        // Set latest message data.
     202        $this->last_message_id      = $latest_message->id;
     203        $this->last_message_date    = $latest_message->date_sent;
     204        $this->last_sender_id       = $latest_message->sender_id;
     205        $this->last_message_subject = $latest_message->subject;
     206        $this->last_message_content = $latest_message->message;
     207
    193208        // Get messages for thread.
    194209        $this->messages = self::get_messages( $this->thread_id, $r );
    195210
    196         if ( empty( $this->messages ) ) {
    197             return false;
    198         }
    199 
    200211        // Messages total count.
    201212        $this->messages_total_count = self::get_total_thread_message_count( $this->thread_id );
    202 
    203         $last_message_index         = count( $this->messages ) - 1;
    204         $this->last_message_id      = $this->messages[ $last_message_index ]->id;
    205         $this->last_message_date    = $this->messages[ $last_message_index ]->date_sent;
    206         $this->last_sender_id       = $this->messages[ $last_message_index ]->sender_id;
    207         $this->last_message_subject = $this->messages[ $last_message_index ]->subject;
    208         $this->last_message_content = $this->messages[ $last_message_index ]->message;
    209213
    210214        foreach ( (array) $this->messages as $key => $message ) {
     
    427431         */
    428432        return apply_filters( 'bp_messages_thread_get_messages', (array) $messages, (int) $thread_id, (array) $r );
     433    }
     434
     435    /**
     436     * Get latest thread message.
     437     *
     438     * @since 12.0.0
     439     *
     440     * @global wpdb $wpdb WordPress database object.
     441     *
     442     * @param integer $thread_id The message thread ID.
     443     * @return object|null
     444     */
     445    public static function get_latest_thread_message( $thread_id ) {
     446        global $wpdb;
     447
     448        $thread_id = (int) $thread_id;
     449        $cache_key = "{$thread_id}_bp_messages_thread_latest_message";
     450        $message   = wp_cache_get( $cache_key, 'bp_messages_threads' );
     451
     452        // Get latest message and cache it.
     453        if ( empty( $message ) ) {
     454            $bp      = buddypress();
     455            $message = $wpdb->get_row(
     456                $wpdb->prepare(
     457                    "SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d ORDER BY date_sent DESC",
     458                    $thread_id
     459                )
     460            );
     461
     462            // Cast integers.
     463            if ( ! empty( $message ) ) {
     464                $message->id        = (int) $message->id;
     465                $message->sender_id = (int) $message->sender_id;
     466                $message->thread_id = (int) $message->thread_id;
     467
     468                // Cache message.
     469                wp_cache_set( $cache_key, $message, 'bp_messages_threads' );
     470            }
     471        }
     472
     473        /**
     474         * Latest thread message.
     475         *
     476         * @since 12.0.0
     477         *
     478         * @param object|null $message   Latest thread message or null.
     479         * @param integer     $thread_id ID of the thread.
     480         */
     481        return apply_filters( 'messages_thread_get_latest_message', $message, $thread_id );
    429482    }
    430483
  • trunk/tests/phpunit/includes/factory.php

    r13108 r13402  
    176176
    177177        $thread_id = messages_new_message( $args );
    178         $thread = new BP_Messages_Thread( $thread_id );
    179         return end( $thread->messages )->id;
     178        $message   = BP_Messages_Thread::get_latest_thread_message( $thread_id );
     179
     180        return $message->id;
    180181    }
    181182
  • trunk/tests/phpunit/testcases/messages/class.bp-messages-thread.php

    r13401 r13402  
    6666        // Default results.
    6767        $messages = BP_Messages_Thread::get_messages( $m1->thread_id );
    68         $this->assertTrue( 100 === count( $messages ) );
     68        $this->assertCount( 100, $messages );
    6969
    7070        // Get first 10 messages.
    7171        $messages = BP_Messages_Thread::get_messages( $m1->thread_id, array( 'page' => 1, 'per_page' => 10 ) );
    72         $this->assertTrue( 10 === count( $messages ) );
     72        $this->assertCount( 10, $messages );
    7373
    7474        // Get first 10 messages differently.
    7575        $thread = new BP_Messages_Thread( $m1->thread_id, 'ASC', array( 'page' => 1, 'per_page' => 10 ) );
    76         $this->assertTrue( 10 === count( $thread->messages ) );
     76        $this->assertCount( 10, $thread->messages );
    7777
    7878        // Get all messages.
    7979        $messages = BP_Messages_Thread::get_messages( $m1->thread_id, array( 'page' => null, 'per_page' => null ) );
    80         $this->assertTrue( 100 === count( $messages ) );
     80        $this->assertCount( 100, $messages );
    8181
    8282        // Get all mesages differently.
    8383        $thread = new BP_Messages_Thread( $m1->thread_id, 'ASC', array( 'page' => null, 'per_page' => null ) );
    84         $this->assertTrue( 100 === count( $thread->messages ) );
     84        $this->assertCount( 100, $thread->messages );
    8585
    8686        // Get last message.
    8787        $messages = BP_Messages_Thread::get_messages( $m1->thread_id, array( 'page' => 100, 'per_page' => 1 ) );
    88         $this->assertTrue( 1 === count( $messages ) );
     88        $this->assertCount( 1, $messages );
    8989        $this->assertEquals( $u1, $messages[0]->sender_id );
    9090        $this->assertEquals( 'Last Message', $messages[0]->subject );
     
    196196        $message_2 = self::factory()->message->create_and_get( array(
    197197            'thread_id'  => $message_1->thread_id,
    198             'sender_id'  => $u1,
    199             'recipients' => array( $u2 ),
     198            'sender_id'  => $u2,
     199            'date_sent'  => '2030-10-27 19:21:40',
     200            'recipients' => array( $u1 ),
    200201            'content'    => 'Bar'
    201202        ) );
     
    203204        // Default sort from constructor.
    204205        $thread = new BP_Messages_Thread( $message_1->thread_id );
     206
    205207        $this->assertEquals(
    206208            array( $message_1->id, $message_2->id ),
     
    596598        $thread_id = $message->thread_id;
    597599
     600        // Populate cache.
     601        $thread = BP_Messages_Thread::get_recipients_for_thread( $thread_id );
     602
    598603        // Cache should be populated.
    599604        $this->assertTrue( (bool) wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
     
    603608
    604609        // Cache should be empty.
    605         $this->assertFalse( wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
     610        $this->assertFalse( (bool) wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
    606611
    607612        $thread = new BP_Messages_Thread( $thread_id );
     
    626631        $thread_id = $message->thread_id;
    627632
     633        // Populate cache.
     634        $thread = BP_Messages_Thread::get_recipients_for_thread( $thread_id );
     635
    628636        // Cache should be populated.
    629637        $this->assertTrue( (bool) wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
     
    633641
    634642        // Cache should be empty.
    635         $this->assertFalse( wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
     643        $this->assertFalse( (bool) wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
    636644
    637645        $thread = new BP_Messages_Thread( $thread_id );
     
    734742
    735743    /**
    736      * @group last_message
    737      */
    738     public function test_last_message_populated() {
    739         $u1 = self::factory()->user->create();
    740         $u2 = self::factory()->user->create();
    741 
    742         $date = bp_core_current_time();
    743 
    744         $message = self::factory()->message->create_and_get( array(
    745             'sender_id' => $u1,
    746             'recipients' => array( $u2 ),
    747             'subject' => 'Foo',
    748             'date_sent' => $date,
    749             'content' => 'Bar and baz.',
    750         ) );
    751 
    752         $t1 = $message->thread_id;
    753 
    754         $thread = new BP_Messages_Thread( $t1 );
    755 
    756         $this->assertNotNull( $thread->last_message_id );
    757         $this->assertEquals( 'Foo', $thread->last_message_subject );
    758         $this->assertEquals( $u1, $thread->last_sender_id );
     744     * @group latest_message
     745     */
     746    public function test_get_latest_message_data() {
     747        $u1      = self::factory()->user->create();
     748        $u2      = self::factory()->user->create();
     749        $subject = 'Last One';
     750        $content = 'Bar and baz';
     751
     752        $m = self::factory()->message->create_and_get(
     753            [
     754                'sender_id'  => $u1,
     755                'recipients' => [ $u2 ],
     756                'subject'    => 'Foo',
     757                'content'    => 'Bar',
     758            ]
     759        );
     760
     761        self::factory()->message->create_many(
     762            8,
     763            [
     764                'thread_id'  => $m->thread_id,
     765                'sender_id'  => $u2,
     766                'recipients' => [ $u1 ],
     767                'subject'    => 'Bar',
     768            ]
     769        );
     770
     771        $date = '2030-10-27 19:21:40';
     772
     773        // Last(est) message.
     774        $m2 = self::factory()->message->create_and_get(
     775            [
     776                'thread_id'  => $m->thread_id,
     777                'sender_id'  => $u1,
     778                'recipients' => [ $u2 ],
     779                'date_sent'  => $date,
     780                'subject'    => $subject,
     781                'content'    => $content,
     782            ]
     783        );
     784
     785        $thread = new BP_Messages_Thread( $m->thread_id, 'ASC', [ 'page' => 1, 'per_page' => 3 ] );
     786
     787        $this->assertCount( 3, $thread->messages );
     788        $this->assertEquals( $m2->id, $thread->last_message_id );
     789        $this->assertEquals( $m2->sender_id, $thread->last_sender_id );
    759790        $this->assertEquals( $date, $thread->last_message_date );
    760         $this->assertEquals( 'Bar and baz.', $thread->last_message_content );
     791        $this->assertEquals( $subject, $thread->last_message_subject );
     792        $this->assertEquals( $content, $thread->last_message_content );
     793    }
     794
     795    /**
     796     * @group latest_message
     797     * @group cache
     798     */
     799    public function test_get_latest_message_from_cache() {
     800        $u1   = self::factory()->user->create();
     801        $u2   = self::factory()->user->create();
     802        $date = '2030-10-27 19:21:40';
     803
     804        $m1 = self::factory()->message->create_and_get(
     805            [
     806                'sender_id'  => $u1,
     807                'recipients' => [ $u2 ],
     808                'subject'    => 'Foo',
     809            ]
     810        );
     811
     812        $m2 = self::factory()->message->create_and_get(
     813            [
     814                'thread_id'  => $m1->thread_id,
     815                'sender_id'  => $u1,
     816                'recipients' => [ $u2 ],
     817                'date_sent'  => $date,
     818                'subject'    => 'Last Message',
     819                'content'    => 'Last Message Content',
     820            ]
     821        );
     822
     823        $thread_id      = $m1->thread_id;
     824        $latest_message = wp_cache_get( "{$thread_id}_bp_messages_thread_latest_message", 'bp_messages_threads' );
     825
     826        $this->assertEquals( $thread_id, $latest_message->thread_id );
     827        $this->assertEquals( $m2->id, $latest_message->id );
     828        $this->assertEquals( $m2->sender_id, $latest_message->sender_id );
     829        $this->assertEquals( $date, $latest_message->date_sent );
     830        $this->assertEquals( 'Last Message', $latest_message->subject );
     831        $this->assertEquals( 'Last Message Content', $latest_message->message );
    761832    }
    762833
Note: See TracChangeset for help on using the changeset viewer.