Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/19/2021 03:58:09 PM (5 years ago)
Author:
imath
Message:

Improve SQL performance when updating/deleting a list of notifications

Instead of looping into a list of notifications to run an update/delete query for each notification, update/delete a list of notifications using a single query. Use primarly this change for message thread notifications and bulk notification actions.

Mainly introduces 2 new static methods to BP_Notifications_Notification:

  • BP_Notifications_Notification::update_id_list() let you update a list of notifications using their notification IDs or component item IDs.
  • BP_Notifications_Notification::delete_id_list() let you delete a list of notifications using their notification IDs or component item IDs.

Props oztaser

Fixes #8426

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/messages/notifications.php

    r13035 r13112  
    1212                parent::setUp();
    1313
     14                $this->reset_user_id = get_current_user_id();
     15
    1416                $this->filter_fired = '';
     17        }
     18
     19        public function tearDown() {
     20                parent::tearDown();
     21
     22                $this->set_current_user( $this->reset_user_id );
    1523        }
    1624
    … …  
    4149        /**
    4250         * @group messages_format_notifications
    43          * @group imath
    4451         */
    4552        public function test_friends_format_notifications_bp_messages_single_new_message_notification_nonstring_filter() {
    … …  
    146153        }
    147154
     155        /**
     156         * @ticket BP8426
     157         */
     158        public function test_bp_messages_mark_notification_on_mark_thread() {
     159                $u1 = self::factory()->user->create();
     160                $u2 = self::factory()->user->create();
     161                $m1 = self::factory()->message->create_and_get( array(
     162                        'sender_id'  => $u1,
     163                        'recipients' => array( $u2 ),
     164                        'subject'    => 'Foo',
     165                ) );
     166
     167                self::factory()->message->create_many(
     168                        9,
     169                        array(
     170                                'thread_id' => $m1->thread_id,
     171                                'sender_id' => $u2,
     172                                'recipients' => array( $u1 ),
     173                                'subject' => 'Bar',
     174                        )
     175                );
     176
     177                $unreadn = wp_list_pluck(
     178                        BP_Notifications_Notification::get(
     179                                array(
     180                                        'user_id'           => $u1,
     181                                        'component_name'    => buddypress()->messages->id,
     182                                        'component_action'  => 'new_message',
     183                                        'is_new'            => 1,
     184                                )
     185                        ),
     186                        'user_id',
     187                        'id'
     188                );
     189
     190                $this->set_current_user( $u1 );
     191
     192                // Mark a thread read.
     193                bp_messages_mark_notification_on_mark_thread( $m1->thread_id, $u1, count( $unreadn ) );
     194
     195                $readn = wp_list_pluck(
     196                        BP_Notifications_Notification::get(
     197                                array(
     198                                        'user_id'           => $u1,
     199                                        'component_name'    => buddypress()->messages->id,
     200                                        'component_action'  => 'new_message',
     201                                        'is_new'            => 0,
     202                                )
     203                        ),
     204                        'user_id',
     205                        'id'
     206                );
     207
     208                $this->assertSame( $unreadn, $readn );
     209        }
     210
     211        /**
     212         * @ticket BP8426
     213         * @group message_delete_notifications
     214         */
     215        public function test_bp_messages_message_delete_notifications() {
     216                $u1 = self::factory()->user->create();
     217                $u2 = self::factory()->user->create();
     218                $m1 = self::factory()->message->create_and_get( array(
     219                        'sender_id'  => $u1,
     220                        'recipients' => array( $u2 ),
     221                        'subject'    => 'Foo',
     222                ) );
     223
     224                $message_ids = self::factory()->message->create_many(
     225                        9,
     226                        array(
     227                                'thread_id' => $m1->thread_id,
     228                                'sender_id' => $u2,
     229                                'recipients' => array( $u1 ),
     230                                'subject' => 'Bar',
     231                        )
     232                );
     233
     234                $message_ids = wp_list_pluck(
     235                        BP_Notifications_Notification::get(
     236                                array(
     237                                        'user_id'           => $u1,
     238                                        'component_name'    => buddypress()->messages->id,
     239                                        'component_action'  => 'new_message',
     240                                        'is_new'            => 1,
     241                                )
     242                        ),
     243                        'item_id'
     244                );
     245
     246                $test = bp_messages_message_delete_notifications( $m1->thread_id, $message_ids );
     247
     248                $deleted = BP_Notifications_Notification::get(
     249                        array(
     250                                'user_id'           => $u1,
     251                                'component_name'    => buddypress()->messages->id,
     252                                'component_action'  => 'new_message',
     253                                'is_new'            => 1,
     254                        )
     255                );
     256
     257                $this->assertEmpty( $deleted );
     258        }
     259
    148260        public function notification_filter_callback( $value ) {
    149261                $this->filter_fired = current_filter();
Note: See TracChangeset for help on using the changeset viewer.