Skip to content

Commit 2ab439f

Browse files
authored
Following: Add missing Outbox Activity (#1969)
1 parent d8974e6 commit 2ab439f

File tree

4 files changed

+63
-27
lines changed

4 files changed

+63
-27
lines changed

includes/class-cli.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,11 @@ public function move( $args ) {
246246
* @param array $args The arguments.
247247
*/
248248
public function follow( $args ) {
249-
$user_id = \get_current_user_id();
250-
$follow_id = follow( $args[0], $user_id );
249+
$user_id = \get_current_user_id();
250+
$follow = follow( $args[0], $user_id );
251251

252-
if ( is_wp_error( $follow_id ) ) {
253-
\WP_CLI::error( $follow_id->get_error_message() );
252+
if ( is_wp_error( $follow ) ) {
253+
\WP_CLI::error( $follow->get_error_message() );
254254
} else {
255255
\WP_CLI::success( 'Follow Scheduled.' );
256256
}

includes/collection/class-following.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77

88
namespace Activitypub\Collection;
99

10+
use Activitypub\Activity\Activity;
1011
use Activitypub\Http;
1112

13+
use function Activitypub\add_to_outbox;
14+
1215
/**
1316
* ActivityPub Following Collection.
1417
*/
@@ -51,6 +54,10 @@ class Following {
5154
/**
5255
* Follow a user.
5356
*
57+
* Please do not use this method directly, use `Activitypub\follow` instead.
58+
*
59+
* @see Activitypub\follow
60+
*
5461
* @param \WP_Post|int $post The ID of the remote Actor.
5562
* @param int $user_id The ID of the WordPress User.
5663
*
@@ -68,7 +75,21 @@ public static function follow( $post, $user_id ) {
6875
$pending = $all_meta[ self::PENDING_META_KEY ] ?? array();
6976

7077
if ( ! \in_array( (string) $user_id, $following, true ) && ! \in_array( (string) $user_id, $pending, true ) ) {
78+
$actor = Actors::get_by_id( $user_id );
79+
80+
if ( \is_wp_error( $actor ) ) {
81+
return $actor;
82+
}
83+
7184
\add_post_meta( $post->ID, self::PENDING_META_KEY, (string) $user_id );
85+
86+
$follow = new Activity();
87+
$follow->set_type( 'Follow' );
88+
$follow->set_actor( $actor->get_id() );
89+
$follow->set_object( $post->guid );
90+
$follow->set_to( array( $post->guid ) );
91+
92+
return add_to_outbox( $follow, null, $user_id );
7293
}
7394

7495
return $post;
@@ -125,6 +146,10 @@ public static function reject( $post, $user_id ) {
125146
/**
126147
* Remove a follow request.
127148
*
149+
* Please do not use this method directly, use `Activitypub\unfollow` instead.
150+
*
151+
* @see Activitypub\unfollow
152+
*
128153
* @param \WP_Post|int $post The ID of the remote Actor.
129154
* @param int $user_id The ID of the WordPress User.
130155
*

includes/functions.php

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,12 +1529,16 @@ function add_to_outbox( $data, $activity_type = null, $user_id = 0, $content_vis
15291529
/**
15301530
* Follow a user.
15311531
*
1532-
* @param string $remote_actor The Actor URL or WebFinger Resource.
1533-
* @param int $user_id The ID of the WordPress User.
1532+
* @param string|int $remote_actor The Actor URL, WebFinger Resource or Post-ID of the remote Actor.
1533+
* @param int $user_id The ID of the WordPress User.
15341534
*
1535-
* @return int|\WP_Error The ID of the Outbox item or a WP_Error.
1535+
* @return \WP_Post|\WP_Error The ID of the Outbox item or a WP_Error.
15361536
*/
15371537
function follow( $remote_actor, $user_id ) {
1538+
if ( \is_numeric( $remote_actor ) ) {
1539+
return Following::follow( $remote_actor, $user_id );
1540+
}
1541+
15381542
if ( ! \filter_var( $remote_actor, FILTER_VALIDATE_URL ) ) {
15391543
$remote_actor = Webfinger::resolve( $remote_actor );
15401544
}
@@ -1549,24 +1553,37 @@ function follow( $remote_actor, $user_id ) {
15491553
return $remote_actor_post;
15501554
}
15511555

1552-
$actor = Actors::get_by_id( $user_id );
1556+
return Following::follow( $remote_actor_post, $user_id );
1557+
}
1558+
1559+
/**
1560+
* Unfollow a user.
1561+
*
1562+
* @param string|int $remote_actor The Actor URL, WebFinger Resource or Post-ID of the remote Actor.
1563+
* @param int $user_id The ID of the WordPress User.
1564+
*
1565+
* @return \WP_Post|\WP_Error The ID of the Outbox item or a WP_Error.
1566+
*/
1567+
function unfollow( $remote_actor, $user_id ) {
1568+
if ( \is_numeric( $remote_actor ) ) {
1569+
return Following::unfollow( $remote_actor, $user_id );
1570+
}
15531571

1554-
if ( \is_wp_error( $actor ) ) {
1555-
return $actor;
1572+
if ( ! \filter_var( $remote_actor, FILTER_VALIDATE_URL ) ) {
1573+
$remote_actor = Webfinger::resolve( $remote_actor );
15561574
}
15571575

1558-
$result = Following::follow( $remote_actor_post, $user_id );
1559-
if ( \is_wp_error( $result ) ) {
1560-
return $result;
1576+
if ( \is_wp_error( $remote_actor ) ) {
1577+
return $remote_actor;
15611578
}
15621579

1563-
$follow = new Activity();
1564-
$follow->set_type( 'Follow' );
1565-
$follow->set_actor( $actor->get_id() );
1566-
$follow->set_object( $remote_actor );
1567-
$follow->set_to( array( $remote_actor ) );
1580+
$remote_actor_post = Actors::fetch_remote_by_uri( $remote_actor );
1581+
1582+
if ( \is_wp_error( $remote_actor_post ) ) {
1583+
return $remote_actor_post;
1584+
}
15681585

1569-
return add_to_outbox( $follow, null, $user_id );
1586+
return Following::unfollow( $remote_actor_post, $user_id );
15701587
}
15711588

15721589
/**

includes/table/class-following.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Activitypub\Sanitize;
1313
use Activitypub\Webfinger;
1414

15+
use function Activitypub\follow;
1516
use function Activitypub\object_to_uri;
1617

1718
if ( ! \class_exists( '\WP_List_Table' ) ) {
@@ -122,14 +123,7 @@ public function process_action() {
122123
}
123124

124125
$profile = \sanitize_text_field( \wp_unslash( $_REQUEST['activitypub-profile'] ) );
125-
$post = Actors::fetch_remote_by_uri( $profile );
126-
127-
if ( \is_wp_error( $post ) ) {
128-
\add_settings_error( 'activitypub', 'followed', $post->get_error_message() );
129-
break;
130-
}
131-
132-
$result = Following_Collection::follow( $post, $this->user_id );
126+
$result = follow( $profile, $this->user_id );
133127
if ( \is_wp_error( $result ) ) {
134128
\add_settings_error( 'activitypub', 'followed', $result->get_error_message() );
135129
} else {

0 commit comments

Comments
 (0)