Skip to:
Content

BuddyPress.org

Ticket #6040: 6040.patch

File 6040.patch, 15.6 KB (added by imath, 10 years ago)
  • src/bp-friends/bp-friends-activity.php

    diff --git src/bp-friends/bp-friends-activity.php src/bp-friends/bp-friends-activity.php
    index dea258a..c419054 100644
    function bp_friends_format_activity_action_friendship_accepted( $action, $activi 
    134134        $initiator_link = bp_core_get_userlink( $activity->user_id );
    135135        $friend_link    = bp_core_get_userlink( $activity->secondary_item_id );
    136136
    137         $action = sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $initiator_link, $friend_link );
     137        $action = sprintf( __( '%1$s accepted %2$s to be their friend', 'buddypress' ), $initiator_link, $friend_link );
     138
     139        // Check the friendship status on the member's page only
     140        if ( bp_is_user() && friends_check_friendship( $activity->user_id, $activity->secondary_item_id ) ) {
     141                $action = sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $initiator_link, $friend_link );
     142        }
    138143
    139144        // Backward compatibility for legacy filter
    140145        // The old filter has the $friendship object passed to it. We want to
    function bp_friends_format_activity_action_friendship_created( $action, $activit 
    168173        $initiator_link = bp_core_get_userlink( $activity->user_id );
    169174        $friend_link    = bp_core_get_userlink( $activity->secondary_item_id );
    170175
    171         $action = sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $initiator_link, $friend_link );
     176        $action = sprintf( __( '%1$s requested %2$s to be their friend', 'buddypress' ), $initiator_link, $friend_link );
     177
     178        // Check the friendship status on the member's page only
     179        if ( bp_is_user() && friends_check_friendship( $activity->user_id, $activity->secondary_item_id ) ) {
     180                $action = sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $initiator_link, $friend_link );
     181        }
    172182
    173183        // Backward compatibility for legacy filter
    174184        // The old filter has the $friendship object passed to it. We want to
    function bp_friends_friendship_accepted_activity( $friendship_id, $initiator_use 
    246256        }
    247257
    248258        // Get links to both members profiles
     259        $friend_link    = bp_core_get_userlink( $friend_user_id );
     260
     261        // Record in activity streams for the friend
     262        friends_record_activity( array(
     263                'user_id'           => $friend_user_id,
     264                'type'              => 'friendship_accepted',
     265                'item_id'           => $friendship_id,
     266                'secondary_item_id' => $initiator_user_id,
     267        ) );
     268}
     269add_action( 'friends_friendship_accepted', 'bp_friends_friendship_accepted_activity', 10, 4 );
     270
     271/**
     272 * Add activity stream items when one members request another member to be his friend
     273 * for virtual friendship.
     274 *
     275 * @since BuddyPress (2.2.0)
     276 *
     277 * @param int $friendship_id
     278 * @param int $initiator_user_id
     279 * @param int $friend_user_id
     280 * @param object $friendship Optional
     281 */
     282function bp_friends_friendship_requested_activity( $friendship_id, $initiator_user_id, $friend_user_id, $friendship = false ) {
     283
     284        // Bail if Activity component is not active
     285        if ( ! bp_is_active( 'activity' ) ) {
     286                return;
     287        }
     288
     289        // Get links to both members profiles
    249290        $initiator_link = bp_core_get_userlink( $initiator_user_id );
    250         $friend_link    = bp_core_get_userlink( $friend_user_id    );
    251291
    252292        // Record in activity streams for the initiator
    253293        friends_record_activity( array(
    function bp_friends_friendship_accepted_activity( $friendship_id, $initiator_use 
    256296                'item_id'           => $friendship_id,
    257297                'secondary_item_id' => $friend_user_id
    258298        ) );
     299}
     300add_action( 'friends_friendship_requested', 'bp_friends_friendship_requested_activity', 10, 4 );
    259301
    260         // Record in activity streams for the friend
    261         friends_record_activity( array(
    262                 'user_id'           => $friend_user_id,
    263                 'type'              => 'friendship_created',
    264                 'item_id'           => $friendship_id,
    265                 'secondary_item_id' => $initiator_user_id,
    266                 'hide_sitewide'     => true // We've already got the first entry site wide
    267         ) );
     302/**
     303 * Check the friendship exists before deleting all activities related to this friendship
     304 *
     305 * @since BuddyPress (2.2.0)
     306 *
     307 * @param int $friendship_id
     308 * @param object $friendship Optional
     309 */
     310function bp_friends_friendship_rejected_activity( $friendship_id, $friendship = null ) {
     311        if ( empty( $friendship_id ) )  {
     312                return;
     313        }
     314
     315        // Remove the activity stream items about the friendship id
     316        friends_delete_activity( array( 'item_id' => $friendship_id, 'type' => '', 'user_id' => 0 ) );
    268317}
    269 add_action( 'friends_friendship_accepted', 'bp_friends_friendship_accepted_activity', 10, 4 );
     318add_action( 'friends_friendship_rejected', 'bp_friends_friendship_rejected_activity', 10, 2 );
     319
     320/**
     321 * Remove the activity for the initiator about the friendship he withdrawn
     322 *
     323 * @since BuddyPress (2.2.0)
     324 *
     325 * @param int $friendship_id
     326 * @param object $friendship
     327 */
     328function bp_friends_friendship_withdrawn_activity( $friendship_id, $friendship = null ) {
     329        if ( empty( $friendship_id ) || empty( $friendship->initiator_user_id ) )  {
     330                return;
     331        }
     332
     333        // Remove the activity stream items about the friendship id
     334        friends_delete_activity( array( 'item_id' => $friendship_id, 'type' => 'friendship_created', 'user_id' => $friendship->initiator_user_id ) );
     335}
     336add_action( 'friends_friendship_withdrawn', 'bp_friends_friendship_withdrawn_activity', 10, 2 );
  • src/bp-friends/bp-friends-functions.php

    diff --git src/bp-friends/bp-friends-functions.php src/bp-friends/bp-friends-functions.php
    index 2e731c3..1b4e2be 100644
    function friends_remove_friend( $initiator_userid, $friend_userid ) { 
    111111         */
    112112        do_action( 'friends_before_friendship_delete', $friendship_id, $initiator_userid, $friend_userid );
    113113
    114         // Remove the activity stream item for the user who canceled the friendship
    115         friends_delete_activity( array( 'item_id' => $friendship_id, 'type' => 'friendship_accepted', 'user_id' => bp_displayed_user_id() ) );
     114        // Remove the activity stream items about the friendship id
     115        friends_delete_activity( array( 'item_id' => $friendship_id, 'type' => '', 'user_id' => 0 ) );
    116116
    117117        /**
    118118         * Fires before the friendship connection is removed.
    function bp_friends_prime_mentions_results() { 
    751751                'friends' => $results,
    752752        ) );
    753753}
    754 add_action( 'bp_activity_mentions_prime_results', 'bp_friends_prime_mentions_results' );
    755  No newline at end of file
     754add_action( 'bp_activity_mentions_prime_results', 'bp_friends_prime_mentions_results' );
  • tests/phpunit/testcases/friends/activity.php

    diff --git tests/phpunit/testcases/friends/activity.php tests/phpunit/testcases/friends/activity.php
    index 8350c9b..962b11a 100644
    class BP_Tests_Friends_Activity extends BP_UnitTestCase { 
    2020                        'secondary_item_id' => $u2,
    2121                ) );
    2222
    23                 $expected = sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), bp_core_get_userlink( $u1 ), bp_core_get_userlink( $u2 ) );
     23                $expected = sprintf( __( '%1$s accepted %2$s to be their friend', 'buddypress' ), bp_core_get_userlink( $u1 ), bp_core_get_userlink( $u2 ) );
    2424
    2525                $a_obj = new BP_Activity_Activity( $a );
    2626
    class BP_Tests_Friends_Activity extends BP_UnitTestCase { 
    4242                        'secondary_item_id' => $u2,
    4343                ) );
    4444
     45                $expected = sprintf( __( '%1$s requested %2$s to be their friend', 'buddypress' ), bp_core_get_userlink( $u1 ), bp_core_get_userlink( $u2 ) );
     46
     47                $a_obj = new BP_Activity_Activity( $a );
     48
     49                $this->assertSame( $expected, $a_obj->action );
     50        }
     51
     52        /**
     53         * @group activity_action
     54         * @group bp_friends_format_activity_action_friendship_accepted
     55         * @ticket BP6040
     56         */
     57        public function test_bp_friends_format_activity_action_friendship_accepted_is_user() {
     58                $bp = buddypress();
     59                $displayed_user = $bp->displayed_user;
     60
     61                $u1 = $this->factory->user->create();
     62                $u2 = $this->factory->user->create();
     63
     64                $a = $this->factory->activity->create( array(
     65                        'component' => buddypress()->friends->id,
     66                        'type' => 'friendship_accepted',
     67                        'user_id' => $u1,
     68                        'secondary_item_id' => $u2,
     69                ) );
     70
     71                $bp->displayed_user->id = $u1;
     72
     73                // Use directly the class to avoid another activity to be created
     74                // Maybe a new BP_UnitTest_Factory_For_Friends class would be better
     75                $friendship = new BP_Friends_Friendship;
     76                $friendship->initiator_user_id = $u2;
     77                $friendship->friend_user_id    = $u1;
     78                $friendship->is_confirmed      = 1;
     79                $friendship->date_created      = bp_core_current_time();
     80                $friendship->save();
     81
     82                $expected = sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), bp_core_get_userlink( $u1 ), bp_core_get_userlink( $u2 ) );
     83
     84                $a_obj = new BP_Activity_Activity( $a );
     85
     86                $this->assertSame( $expected, $a_obj->action );
     87
     88                // Reset the friendship
     89                $friendship->delete();
     90
     91                // Reset displayed user
     92                $bp->displayed_user = $displayed_user;
     93        }
     94
     95        /**
     96         * @group activity_action
     97         * @group bp_friends_format_activity_action_friendship_created
     98         * @ticket BP6040
     99         */
     100        public function test_bp_friends_format_activity_action_friendship_created_is_user() {
     101                $bp = buddypress();
     102                $displayed_user = $bp->displayed_user;
     103
     104                $u1 = $this->factory->user->create();
     105                $u2 = $this->factory->user->create();
     106
     107                $a = $this->factory->activity->create( array(
     108                        'component' => buddypress()->friends->id,
     109                        'type' => 'friendship_created',
     110                        'user_id' => $u1,
     111                        'secondary_item_id' => $u2,
     112                ) );
     113
     114                $bp->displayed_user->id = $u1;
     115
     116                // Use directly the class to avoid another activity to be created
     117                // Maybe a new BP_UnitTest_Factory_For_Friends class would be better
     118                $friendship = new BP_Friends_Friendship;
     119                $friendship->initiator_user_id = $u1;
     120                $friendship->friend_user_id    = $u2;
     121                $friendship->is_confirmed      = 1;
     122                $friendship->date_created      = bp_core_current_time();
     123                $friendship->save();
     124
    45125                $expected = sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), bp_core_get_userlink( $u1 ), bp_core_get_userlink( $u2 ) );
    46126
    47127                $a_obj = new BP_Activity_Activity( $a );
    48128
    49129                $this->assertSame( $expected, $a_obj->action );
     130
     131                // Reset the friendship
     132                $friendship->delete();
     133
     134                // Reset displayed user
     135                $bp->displayed_user = $displayed_user;
     136        }
     137
     138        /**
     139         * @group bp_friends_friendship_requested_activity
     140         * @ticket BP6040
     141         */
     142        public function test_bp_friends_friendship_requested_activity() {
     143                $u1 = $this->factory->user->create();
     144                $u2 = $this->factory->user->create();
     145
     146                friends_add_friend( $u2, $u1 );
     147                $friendship_id = friends_get_friendship_id( $u2, $u1 );
     148
     149                $fr_act = bp_activity_get( array(
     150                        'component'   => buddypress()->friends->id,
     151                        'item_id'     => $friendship_id,
     152                        'filter'      => array( 'action' => array( 'friendship_created' ) ),
     153                ) );
     154
     155                $this->assertTrue( count( $fr_act['activities'] ) == 1, 'An activity should be created when a friendship is requested' );
     156
     157                $friendship = new BP_Friends_Friendship( $friendship_id );
     158                // Reset the friendship
     159                $friendship->delete();
     160        }
     161
     162        /**
     163         * @group bp_friends_friendship_accepted_activity
     164         * @ticket BP6040
     165         */
     166        public function test_bp_friends_friendship_accepted_activity() {
     167                $u1 = $this->factory->user->create();
     168                $u2 = $this->factory->user->create();
     169
     170                // Force the friendship to be accepted
     171                friends_add_friend( $u2, $u1, true );
     172                $friendship_id = friends_get_friendship_id( $u2, $u1 );
     173
     174                $fa_act = bp_activity_get( array(
     175                        'component'   => buddypress()->friends->id,
     176                        'item_id'     => $friendship_id,
     177                        'filter'      => array( 'action' => array( 'friendship_accepted' ) ),
     178                ) );
     179
     180                $this->assertTrue( count( $fa_act['activities'] ) == 1, 'An activity should be created when a friendship is accepted' );
     181
     182                $friendship = new BP_Friends_Friendship( $friendship_id );
     183                // Reset the friendship
     184                $friendship->delete();
     185        }
     186
     187        /**
     188         * @group bp_friends_friendship_withdrawn_activity
     189         * @ticket BP6040
     190         */
     191        public function test_bp_friends_friendship_withdrawn_activity() {
     192                $old_user = get_current_user_id();
     193
     194                $u1 = $this->factory->user->create();
     195                $u2 = $this->factory->user->create();
     196
     197                friends_add_friend( $u2, $u1 );
     198                $friendship_id = friends_get_friendship_id( $u2, $u1 );
     199
     200                $fr_act = bp_activity_get( array(
     201                        'component'   => buddypress()->friends->id,
     202                        'item_id'     => $friendship_id,
     203                        'filter'      => array( 'action' => array( 'friendship_created' ) ),
     204                        'user_id'     => $u2,
     205                ) );
     206
     207                $this->assertTrue( count( $fr_act['activities'] ) == 1, 'An activity should be created when a friendship is requested' );
     208
     209                // Set current user to u2 to make sure the friendship can be withdrawn
     210                $this->set_current_user( $u2 );
     211
     212                // Withdraw the friendship
     213                friends_withdraw_friendship( $u2, $u1 );
     214
     215                // Reset the current user
     216                $this->set_current_user( $old_user );
     217
     218                $fw_act = bp_activity_get( array(
     219                        'component'   => buddypress()->friends->id,
     220                        'item_id'     => $friendship_id,
     221                        'filter'      => array( 'action' => array( 'friendship_created' ) ),
     222                        'user_id'     => $u2,
     223                ) );
     224
     225                $this->assertTrue( count( $fw_act['activities'] ) == 0, 'The friendship_created activity should be deleted when a friendship is withdrawn' );
     226        }
     227
     228        /**
     229         * @group bp_friends_friendship_rejected_activity
     230         * @ticket BP6040
     231         */
     232        public function test_bp_friends_friendship_rejected_activity() {
     233                $old_user = get_current_user_id();
     234
     235                $u1 = $this->factory->user->create();
     236                $u2 = $this->factory->user->create();
     237
     238                friends_add_friend( $u2, $u1 );
     239                $friendship_id = friends_get_friendship_id( $u2, $u1 );
     240
     241                // Set current user to u1 to make sure the friendship can be rejected
     242                $this->set_current_user( $u1 );
     243
     244                // Withdraw the friendship
     245                friends_reject_friendship( $friendship_id );
     246
     247                // Reset the current user
     248                $this->set_current_user( $friendship_id );
     249
     250                $fj_act = bp_activity_get( array(
     251                        'component'   => buddypress()->friends->id,
     252                        'item_id'     => $friendship_id,
     253                ) );
     254
     255                $this->assertTrue( count( $fj_act['activities'] ) == 0, 'Activities about a friendship should be deleted when a friendship is rejected' );
     256        }
     257
     258        /**
     259         * @group friends_delete_activity
     260         * @ticket BP6040
     261         */
     262        public function test_friends_delete_activity() {
     263                $old_user = get_current_user_id();
     264
     265                $u1 = $this->factory->user->create();
     266                $u2 = $this->factory->user->create();
     267
     268                friends_add_friend( $u2, $u1 );
     269                $friendship_id = friends_get_friendship_id( $u2, $u1 );
     270
     271                // Set current user to u1 to accepte the friendship
     272                $this->set_current_user( $u1 );
     273                friends_accept_friendship( $friendship_id );
     274
     275                // Reset the current user
     276                $this->set_current_user( $old_user );
     277
     278                // Random activities
     279                $au1 = $this->factory->activity->create( array( 'user_id' => $u1 ) );
     280                $au2 = $this->factory->activity->create( array( 'user_id' => $u2 ) );
     281
     282                $fc_act = bp_activity_get( array(
     283                        'component'   => buddypress()->friends->id,
     284                        'item_id'     => $friendship_id,
     285                        'filter'      => array( 'action' => array( 'friendship_accepted', 'friendship_created' ) ), // friendship_accepted is no more used in BuddyPress since 1.1
     286                        'show_hidden' => true
     287                ) );
     288
     289                $this->assertTrue( count( $fc_act['activities'] ) == 2, '2 activities should be created when a friendship is confirmed' );
     290
     291                // Remove the friendship
     292                friends_remove_friend( $u2, $u1 );
     293
     294                $this->assertFalse( friends_check_friendship( $u2, $u1 ), '2 users should not be friend once the friendship is removed' );
     295
     296                $fd_act = bp_activity_get( array(
     297                        'component'   => buddypress()->friends->id,
     298                        'item_id'     => $friendship_id,
     299                        'filter'      => array( 'action' => array( 'friendship_accepted', 'friendship_created' ) ), // friendship_accepted is no more used in BuddyPress since 1.1
     300                        'show_hidden' => true
     301                ) );
     302
     303                $this->assertTrue( count( $fd_act['activities'] ) == 0, 'friends_delete_activity() should remove activities about a deleted friendship' );
    50304        }
    51305}
    52306