Skip to:
Content

BuddyPress.org

Changeset 13339


Ignore:
Timestamp:
10/11/2022 07:10:58 PM (18 months ago)
Author:
imath
Message:

BP Messages: make sure Threads unread count is consistent

[13102] introduced a regression considering the way we used to set a thread's unread count. This count is set using the list of recipient objects (which include an unread_count property for each user object) by checking for the requested user ID into this list to find the corresponding unread count.

Slicing this list into smaller parts to paginate recipients results introduced potential cases where the requested user ID wasn't found into this paginated list of recipients.

Before doing the slice operation, we need to set the requested user ID thread's unread count to be sure it's the right one and not a null value.

Props sjregan

Closes https://github.com/buddypress/buddypress/pull/26
See #8750 (trunk)

Location:
trunk
Files:
2 edited

Legend:

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

    r13196 r13339  
    201201        }
    202202
    203         // Fetch the recipients.
     203        // Fetch the recipients and set the displayed/logged in user's unread count.
    204204        $this->recipients = $this->get_recipients( $thread_id, $r );
    205 
    206         // Get the unread count for the user.
    207         if ( isset( $this->recipients[ $r['user_id'] ] ) ) {
    208             $this->unread_count = $this->recipients[ $r['user_id'] ]->unread_count;
    209         }
    210205
    211206        // Grab all message meta.
     
    312307        }
    313308
     309        // Set the unread count for the user.
     310        if ( isset( $r['user_id'] ) && $r['user_id'] && isset( $recipients[ $r['user_id'] ]->unread_count ) ) {
     311            $this->unread_count = (int) $recipients[ $r['user_id'] ]->unread_count;
     312        }
     313
    314314        // Paginate the results.
    315315        if ( ! empty( $recipients ) && $r['recipients_per_page'] && $r['recipients_page'] ) {
    316316            $start      = ( $r['recipients_page'] - 1 ) * ( $r['recipients_per_page'] );
    317             $recipients = array_slice( $recipients, $start, $r['recipients_per_page'] );
     317            $recipients = array_slice( $recipients, $start, $r['recipients_per_page'], true );
    318318        }
    319319
     
    757757                'ASC',
    758758                array(
     759                    'user_id'             => $r['user_id'],
    759760                    'update_meta_cache'   => false,
    760761                    'recipients_page'     => $r['recipients_page'],
  • trunk/tests/phpunit/testcases/messages/template.php

    r13148 r13339  
    425425        $this->assertCount( 1, $messages_template->threads[0]->recipients );
    426426    }
     427
     428    /**
     429     * @group pagination
     430     * @group BP_Messages_Box_Template
     431     * @group BP8750
     432     */
     433    public function test_thread_unread_count_setting_per_page_recipients() {
     434        $u1 = self::factory()->user->create();
     435        $u2 = self::factory()->user->create();
     436
     437        // create initial thread
     438        $message_1 = self::factory()->message->create_and_get(
     439            array(
     440                'sender_id'  => $u1,
     441                'recipients' => array( $u2 ),
     442            )
     443        );
     444
     445        // create some replies to thread
     446        self::factory()->message->create_and_get(
     447            array(
     448                'thread_id'  => $message_1->thread_id,
     449                'sender_id'  => $u2,
     450                'recipients' => array( $u1 ),
     451            )
     452        );
     453
     454        self::factory()->message->create_and_get(
     455            array(
     456                'thread_id'  => $message_1->thread_id,
     457                'sender_id'  => $u2,
     458                'recipients' => array( $u1 ),
     459            )
     460        );
     461
     462        // set $u1 as current user.
     463        $old_current_user = get_current_user_id();
     464        $this->set_current_user( $u1 );
     465
     466        $messages_template = new BP_Messages_Box_Template(
     467            array(
     468                'recipients_page'     => 1,
     469                'recipients_per_page' => 1,
     470            )
     471        );
     472
     473        $this->set_current_user( $old_current_user );
     474
     475        $thread = reset( $messages_template->threads );
     476
     477        $this->assertEquals( 2, $thread->unread_count );
     478    }
     479
     480    /**
     481     * @group pagination
     482     * @group BP_Messages_Box_Template
     483     * @group BP8750
     484     */
     485    public function test_thread_unread_count_setting_per_page_recipients_with_specific_user_id() {
     486        $u1 = self::factory()->user->create();
     487        $u2 = self::factory()->user->create();
     488        $u3 = self::factory()->user->create();
     489
     490        // create initial thread
     491        $message_1 = self::factory()->message->create_and_get(
     492            array(
     493                'sender_id'  => $u3,
     494                'recipients' => array( $u1, $u2 ),
     495            )
     496        );
     497
     498        // create some replies to thread
     499        self::factory()->message->create_and_get(
     500            array(
     501                'thread_id'  => $message_1->thread_id,
     502                'sender_id'  => $u2,
     503                'recipients' => array( $u1, $u3 ),
     504            )
     505        );
     506
     507        self::factory()->message->create_and_get(
     508            array(
     509                'thread_id'  => $message_1->thread_id,
     510                'sender_id'  => $u1,
     511                'recipients' => array( $u2, $u3 ),
     512            )
     513        );
     514
     515        self::factory()->message->create_and_get(
     516            array(
     517                'thread_id'  => $message_1->thread_id,
     518                'sender_id'  => $u3,
     519                'recipients' => array( $u2, $u1 ),
     520            )
     521        );
     522
     523        $messages_template = new BP_Messages_Box_Template(
     524            array(
     525                'user_id'             => $u3,
     526                'recipients_page'     => 1,
     527                'recipients_per_page' => 1,
     528            )
     529        );
     530
     531        $thread = reset( $messages_template->threads );
     532
     533        $this->assertFalse( isset( $thread->recipients[ $u3 ] ) );
     534        $this->assertCount( 1, $thread->recipients );
     535        $this->assertEquals( 2, $thread->unread_count );
     536    }
    427537}
Note: See TracChangeset for help on using the changeset viewer.