Skip to:
Content

BuddyPress.org

Changeset 9326


Ignore:
Timestamp:
01/09/2015 08:56:45 AM (10 years ago)
Author:
imath
Message:

Only create a single activity when a friendship is created and make sure to display it on each involved user personal activities.

When a friendship is created, a single public activity will be created. Thanks to the new BP_Activity_Query class introduced in version 2.2, we are able to fetch this activity on each involved user personal activity stream.
Once BuddyPress will be updated to 2.2, all hide_sitewide activities related to a created friendship will be removed as they are not necessary anymore.

Fixes #6040

props boonebgorges, r-a-y

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-update.php

    r9273 r9326  
    400400 * - Add messages meta table
    401401 * - Update the component field of the 'new members' activity type
     402 * - Clean up hidden friendship activities
    402403 *
    403404 * @since BuddyPress (2.2.0)
     
    410411    if ( bp_is_active( 'activity' ) ) {
    411412        bp_migrate_new_member_activity_component();
     413
     414        if ( bp_is_active( 'friends' ) ) {
     415            bp_cleanup_friendship_activities();
     416        }
    412417    }
    413418}
     
    450455
    451456/**
     457 * Remove all hidden friendship activities
     458 *
     459 * @since BuddyPress (2.2.0)
     460 *
     461 * @uses bp_activity_delete() to delete the corresponding friendship activities
     462 */
     463function bp_cleanup_friendship_activities() {
     464    bp_activity_delete( array(
     465        'component'     => buddypress()->friends->id,
     466        'type'          => 'friendship_created',
     467        'hide_sitewide' => true,
     468    ) );
     469 }
     470
     471/**
    452472 * Redirect user to BP's What's New page on first page load after activation.
    453473 *
  • trunk/src/bp-friends/bp-friends-activity.php

    r9318 r9326  
    272272
    273273/**
     274 * Set up activity arguments for use with the 'just-me' scope.
     275 *
     276 * For details on the syntax, see {@link BP_Activity_Query}.
     277 *
     278 * @since BuddyPress (2.2.0)
     279 *
     280 * @param array $retval Empty array by default
     281 * @param array $filter Current activity arguments
     282 * @return array
     283 */
     284function bp_friends_filter_activity_just_me_scope( $retval, $filter ) {
     285    // Get the requested action
     286    $action = $filter['filter']['action'];
     287
     288    // Make sure actions are listed in an array
     289    if ( ! is_array( $action ) ) {
     290        $action = explode( ',', $filter['filter']['action'] );
     291    }
     292
     293    $action = array_flip( array_filter( $action ) );
     294
     295    /**
     296     * If filtering activities for something other than the friendship_created action
     297     * return without changing anything
     298     */
     299    if ( ! empty( $action ) && ! isset( $action['friendship_created'] ) ) {
     300        return $retval;
     301    }
     302
     303    /**
     304     * Else make sure to get the friendship_created action, the user is involved in
     305     * - user initiated the friendship
     306     * - user has been requested a friendship
     307     */
     308    return array(
     309        'relation' => 'OR',
     310        array(
     311            'column' => 'user_id',
     312            'value'  => $filter['user_id']
     313        ),
     314        array(
     315            'relation' => 'AND',
     316            array(
     317                'column' => 'component',
     318                'value'  => 'friends',
     319            ),
     320            array(
     321                'column' => 'secondary_item_id',
     322                'value'  => $filter['user_id'],
     323            ),
     324        ),
     325        'override' => array(
     326            'display_comments' => 'stream',
     327            'filter'           => array( 'user_id' => 0 ),
     328        ),
     329    );
     330}
     331add_filter( 'bp_activity_set_just-me_scope_args', 'bp_friends_filter_activity_just_me_scope', 20, 2 );
     332
     333/**
    274334 * Add activity stream items when one members accepts another members request
    275335 * for virtual friendship.
     
    294354        'secondary_item_id' => $friend_user_id
    295355    ) );
    296 
    297     // Record in activity streams for the friend
    298     friends_record_activity( array(
    299         'user_id'           => $friend_user_id,
    300         'type'              => 'friendship_created',
    301         'item_id'           => $friendship_id,
    302         'secondary_item_id' => $initiator_user_id,
    303         'hide_sitewide'     => true // We've already got the first entry site wide
    304     ) );
    305356}
    306357add_action( 'friends_friendship_accepted', 'bp_friends_friendship_accepted_activity', 10, 4 );
  • trunk/tests/phpunit/testcases/friends/activity.php

    r9241 r9326  
    9696        $this->assertTrue( count( $fd_act['activities'] ) == 0, 'friends_delete_activity() should remove "friendship_created" activities about a deleted friendship' );
    9797    }
     98
     99    /**
     100     * @group bp_friends_friendship_accepted_activity
     101     */
     102    public function test_bp_friends_friendship_accepted_activity() {
     103        $old_user = get_current_user_id();
     104
     105        $u1 = $this->factory->user->create();
     106        $u2 = $this->factory->user->create();
     107
     108        friends_add_friend( $u2, $u1 );
     109        $friendship_id = friends_get_friendship_id( $u2, $u1 );
     110
     111        // Set current user to u1 to accepte the friendship
     112        $this->set_current_user( $u1 );
     113        friends_accept_friendship( $friendship_id );
     114
     115        // Reset the current user
     116        $this->set_current_user( $old_user );
     117
     118        $u1_act = bp_activity_get( array(
     119            'component'   => buddypress()->friends->id,
     120            'item_id'     => $friendship_id,
     121            'scope'       => 'just-me',
     122            'filter'      => array( 'action' => array( 'friendship_created' ), 'user_id' => $u1 ),
     123        ) );
     124
     125        $this->assertTrue( count( $u1_act['activities'] ) == 1, 'a public activity should be listed in the friend stream' );
     126
     127        $u2_act = bp_activity_get( array(
     128            'component'   => buddypress()->friends->id,
     129            'item_id'     => $friendship_id,
     130            'scope'       => 'just-me',
     131            'filter'      => array( 'action' => array( 'friendship_created' ), 'user_id' => $u2 ),
     132        ) );
     133
     134        $this->assertTrue( count( $u2_act['activities'] ) == 1, 'a public activity should be listed in the initiator stream' );
     135    }
     136
     137    /**
     138     * @group bp_cleanup_friendship_activities
     139     */
     140    public function test_bp_cleanup_friendship_activities() {
     141        $old_user = get_current_user_id();
     142
     143        $u1 = $this->factory->user->create();
     144        $u2 = $this->factory->user->create();
     145        $users = array( $u1, $u2 );
     146
     147        friends_add_friend( $u2, $u1 );
     148        $friendship_id = friends_get_friendship_id( $u2, $u1 );
     149
     150        // Set current user to u1 to accepte the friendship and generate a public activity
     151        $this->set_current_user( $u1 );
     152        friends_accept_friendship( $friendship_id );
     153
     154        // Reset the current user
     155        $this->set_current_user( $old_user );
     156
     157        $users[] = $this->factory->user->create();
     158        $users[] = $this->factory->user->create();
     159
     160        foreach( $users as $u ) {
     161            bp_activity_add( array(
     162                'user_id'       => $u,
     163                'item_id'       => $friendship_id,
     164                'type'          => 'friendship_created',
     165                'component'     => buddypress()->friends->id,
     166                'hide_sitewide' => true,
     167            ) );
     168        }
     169
     170        $hidden = bp_activity_get( array(
     171            'component'   => buddypress()->friends->id,
     172            'filter'      => array( 'action' => array( 'friendship_created' ) ),
     173            'show_hidden' => true,
     174        ) );
     175
     176        bp_cleanup_friendship_activities();
     177
     178        $check = bp_activity_get( array(
     179            'component'   => buddypress()->friends->id,
     180            'item_id'     => $friendship_id,
     181            'filter'      => array( 'action' => array( 'friendship_created' ) ),
     182            'show_hidden' => true,
     183        ) );
     184
     185        $this->assertTrue( count( $check['activities'] ) == 1 );
     186    }
    98187}
    99188
Note: See TracChangeset for help on using the changeset viewer.