Skip to:
Content

BuddyPress.org

Changeset 13154


Ignore:
Timestamp:
11/20/2021 02:21:24 PM (2 years ago)
Author:
imath
Message:

Avoid generating many new_avatar activities in a short time frame

For an hour time frame, if a user publishes many new_avatar activities, only the latest one will be kept.

Props oztaser

See #8581

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-members/bp-members-activity.php

    r13105 r13154  
    193193    $user_id = apply_filters( 'bp_members_new_avatar_user_id', $user_id );
    194194
     195    // Check to make sure that a user has just one `new_avatar` activity per throttle time.
     196    $last_new_avatar_activity = bp_activity_get(
     197        array(
     198            'user_id'   => $user_id,
     199            'component' => buddypress()->members->id,
     200            'type'      => 'new_avatar',
     201            'per_page'  => 1,
     202        )
     203    );
     204
     205    if ( ! empty( $last_new_avatar_activity['activities'] ) ) {
     206        /**
     207         * Filters the throttle time, in seconds, used to prevent generating multiple `new_avatar` activity
     208         * in a short amount of time.
     209         *
     210         * @since 10.0.0
     211         *
     212         * @param int $value Throttle time, in seconds.
     213         */
     214        $throttle_period = apply_filters( 'bp_members_new_avatar_throttle_time', HOUR_IN_SECONDS );
     215        $then            = strtotime( $last_new_avatar_activity['activities'][0]->date_recorded );
     216        $now             = bp_core_current_time( true, 'timestamp' );
     217
     218        // Delete the old activity.
     219        if ( ( $now - $then ) < $throttle_period ) {
     220            bp_activity_delete(
     221                array(
     222                    'id' => $last_new_avatar_activity['activities'][0]->id,
     223                )
     224            );
     225        }
     226    }
     227
    195228    // Add the activity.
    196     bp_activity_add( array(
    197         'user_id'   => $user_id,
    198         'component' => buddypress()->members->id,
    199         'type'      => 'new_avatar'
    200     ) );
     229    bp_activity_add(
     230        array(
     231            'user_id'   => $user_id,
     232            'component' => buddypress()->members->id,
     233            'type'      => 'new_avatar',
     234        )
     235    );
    201236}
    202237add_action( 'bp_members_avatar_uploaded', 'bp_members_new_avatar_activity' );
  • trunk/tests/phpunit/testcases/members/activity.php

    r12851 r13154  
    9494        $this->assertSame( $expected, $found );
    9595    }
     96
     97    /**
     98     * @group bp_members_new_avatar_activity
     99     */
     100    public function test_bp_members_new_avatar_activity_throttled() {
     101        $u = self::factory()->user->create();
     102        $a = self::factory()->activity->create( array(
     103            'component' => 'members',
     104            'type'      => 'new_avatar',
     105            'user_id'   => $u,
     106        ) );
     107
     108        bp_members_new_avatar_activity( $u );
     109
     110        $new_avatar_activities = bp_activity_get( array(
     111            'user_id'     => $u,
     112            'component'   => buddypress()->members->id,
     113            'type'        => 'new_avatar',
     114            'count_total' => 'count_query',
     115        ) );
     116
     117        $this->assertEquals( 1, $new_avatar_activities['total'] );
     118        $this->assertNotSame( $a, $new_avatar_activities['activities'][0]->id );
     119    }
     120
     121    /**
     122     * @group bp_members_new_avatar_activity
     123     */
     124    public function test_bp_members_new_avatar_activity_outside_of_throttle_time() {
     125        $u = self::factory()->user->create();
     126
     127        $time      = strtotime( bp_core_current_time() );
     128        $prev_time = date( 'Y-m-d H:i:s', $time - ( 121 * HOUR_IN_SECONDS ) );
     129
     130        $a = self::factory()->activity->create( array(
     131            'component'     => 'members',
     132            'type'          => 'new_avatar',
     133            'user_id'       => $u,
     134            'recorded_time' => $prev_time,
     135        ) );
     136
     137        bp_members_new_avatar_activity( $u );
     138
     139        $new_avatar_activities = bp_activity_get( array(
     140            'user_id'     => $u,
     141            'component'   => buddypress()->members->id,
     142            'type'        => 'new_avatar',
     143            'count_total' => 'count_query',
     144        ) );
     145
     146        $this->assertEquals( 2, $new_avatar_activities['total'] );
     147    }
    96148}
Note: See TracChangeset for help on using the changeset viewer.