[buddypress-trac] [BuddyPress Trac] #8750: Unread count not set for message threads

buddypress-trac noreply at wordpress.org
Fri Oct 7 00:14:11 UTC 2022


#8750: Unread count not set for message threads
--------------------------+---------------------
 Reporter:  sjregan       |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  10.5.0
Component:  Messages      |     Version:  10.4.0
 Severity:  normal        |  Resolution:
 Keywords:  needs-patch   |
--------------------------+---------------------

Comment (by sjregan):

 Unfortunately this one is a bit more complicated than I first thought and
 would appreciated some advice on how you would resolve this.

 When a user is viewing their inbox, the BP_Messages_Box_Template class is
 used and BP_Messages_Thread::get_current_threads_for_user() is called:

 https://github.com/buddypress/buddypress/blob/master/src/bp-
 messages/classes/class-bp-messages-box-template.php#L173-L175

 {{{#!php
 $threads = BP_Messages_Thread::get_current_threads_for_user(
     array(
         'user_id'             => $this->user_id,
         'box'                 => $this->box,
         'type'                => $this->type,
         'limit'               => $this->pag_num,
         'page'                => $this->pag_page,
         'search_terms'        => $this->search_terms,
         'meta_query'          => $r['meta_query'],
         'recipients_page'     => $r['recipients_page'],
         'recipients_per_page' => $r['recipients_per_page'],
         'messages_page'       => $r['messages_page'],
         'messages_per_page'   => $r['messages_per_page'],
     )
 );
 }}}

 This passes the 'user_id' however, when fetching the threads themselves
 the 'user_id' is not passed on:

 https://github.com/buddypress/buddypress/blob/e0f5aaa4746fbbd9bee36fa810eaf67e103f1013/src
 /bp-messages/classes/class-bp-messages-thread.php#L755-L765

 {{{#!php
 $threads[] = new BP_Messages_Thread(
     $thread_id,
     'ASC',
     array(
         'update_meta_cache'   => false,
         'recipients_page'     => $r['recipients_page'],
         'recipients_per_page' => $r['recipients_per_page'],
         'page'                => $r['messages_page'],
         'per_page'            => $r['messages_per_page'],
     )
 );
 }}}

 I can add the 'user_id' to the arguments array and the current unit tests
 continue to pass.

 The 'user_id' is required because of this:

 https://github.com/buddypress/buddypress/blob/master/src/bp-
 messages/classes/class-bp-messages-thread.php#L207-L209

 {{{#!php
 if ( isset( $this->recipients[ $r['user_id'] ] ) ) {
     $this->unread_count = $this->recipients[ $r['user_id']
 ]->unread_count;
 }
 }}}

 This is where the little complication comes in, the get_recipients()
 method will slice the recipient array based on the 'recipients_per_page'
 and 'recipients_page' arguments:

 https://github.com/buddypress/buddypress/blob/master/src/bp-
 messages/classes/class-bp-messages-thread.php#L317

 I can fix the array_slice() call to preserve the array keys, but there is
 a chance our user with ID 'user_id' has been sliced out of the array, so
 the unread_count will not be set. Also, the
 'bp_messages_thread_get_recipients' filter results may have lost the
 recipient.

 So when setting the unread count, we cannot rely on the recipient still
 being present within the array after the get_recipients() call.

 The only solution I can think of is when the recipient is not present in
 the array, check the 'thread_recipients' cache for them, and if they are
 not there, query the database again.

 We could cache/store the value within get_recipients() for retrieval after
 calling it, but that seems messy.

 Any other suggestions?

-- 
Ticket URL: <https://buddypress.trac.wordpress.org/ticket/8750#comment:2>
BuddyPress Trac <http://buddypress.org/>
BuddyPress Trac


More information about the buddypress-trac mailing list