Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/20/2021 02:21:24 PM (4 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

File:
1 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' );
Note: See TracChangeset for help on using the changeset viewer.