8000 Add support for ActivityPub Move activity by akirk · Pull Request #420 · akirk/friends · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add support for ActivityPub Move activity #420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions feed-parsers/class-feed-parser-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function __construct( Feed $friends_feed ) {
\add_action( 'activitypub_inbox_announce', array( $this, 'handle_received_announce' ), 15, 2 );
\add_action( 'activitypub_inbox_like', array( $this, 'handle_received_like' ), 15, 2 );
\add_action( 'activitypub_inbox_undo', array( $this, 'handle_received_undo' ), 15, 2 );
\add_action( 'activitypub_inbox_move', array( $this, 'handle_received_move' ), 15, 2 );
\add_action( 'activitypub_handled_create', array( $this, 'activitypub_handled_create' ), 10, 4 );

\add_action( 'friends_user_feed_activated', array( $this, 'queue_follow_user' ), 10 );
Expand Down Expand Up @@ -789,6 +790,10 @@ public function handle_received_undo( $activity, $user_id ) {
return $this->handle_received_activity( $activity, $user_id, 'undo' );
}

public function handle_received_move( $activity, $user_id ) {
return $this->handle_received_activity( $activity, $user_id, 'move' );
}

public function handle_received_delete( $activity, $user_id ) {
return $this->handle_received_activity( $activity, $user_id, 'delete' );
}
Expand Down Expand Up @@ -840,6 +845,7 @@ public function handle_received_activity( $activity, $user_id, $type ) {
'unannounce',
'like',
'unlike',
'move',
),
true
) ) {
Expand Down Expand Up @@ -924,6 +930,8 @@ protected function process_incoming_activity( $type, $activity, $user_id, $user_
return $this->handle_incoming_like( $activity, $user_id );
case 'unlike':
return $this->handle_incoming_unlike( $activity, $user_id );
case 'move':
return $this->handle_incoming_move( $activity, $user_feed );
}
return null;
}
Expand Down Expand Up @@ -1209,6 +1217,62 @@ public function handle_incoming_unlike( $activity, $user_id ) {
return true;
}

public function handle_incoming_move( $activity, User_Feed $user_feed ) {
$old_url = $activity['object'];
if ( $user_feed->get_url() !== $old_url ) {
$this->log( 'Could not determine the right feed to be moved. Looking for ' . $old_url . ', got ' . $user_feed->get_url() );
return false;
}

$feed = array(
'url' => $activity['target'],
'mime-type' => $user_feed->get_mime_type(),
'title' => $user_feed->get_title(),
'parser' => $user_feed->get_parser(),
'post-format' => $user_feed->get_post_format(),
'active' => $user_feed->is_active(),
);

// Similar as in process_admin_edit_friend_feeds.
if ( $user_feed->get_url() !== $feed['url'] ) {
$friend = $user_feed->get_friend_user();
do_action( 'friends_user_feed_deactivated', $user_feed );

if ( ! isset( $feed['mime-type'] ) ) {
$feed['mime-type'] = $user_feed->get_mime_type();
}

if ( $feed['active'] ) {
$new_feed = $friend->subscribe( $feed['url'], $feed );
if ( ! is_wp_error( $new_feed ) ) {
do_action( 'friends_user_feed_activated', $new_feed );
}
} else {
$new_feed = $friend->save_feed( $feed['url'], $feed );
}

// Since the URL has changed, the above will create a new feed, therefore we need to delete the old one.
$user_feed->delete();

if ( is_wp_error( $new_feed ) ) {
do_action( 'friends_process_feed_item_submit_error', $new_feed, $feed );
return $new_feed;
}

do_action( 'friends_process_feed_item_submit', $new_feed, $feed );
$new_feed->update_last_log(
sprintf(
// translators: %s is the old URL.
__( 'Moved from old URL: %s', 'friends' ),
$user_feed->get_url()
)
);
return $new_feed;
}

return true;
}

/**
* Queue a hook to run async.
*
Expand Down
2 changes: 1 addition & 1 deletion includes/class-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ public function save() {
/**
* Save multiple feeds for a user.
*
* @param string $feeds The feed URLs to subscribe to.
* @param array $feeds The feed URLs to subscribe to.
*
* @return array(\WP_Term)|\WP_error $user The new associated user or an error object.
*/
Expand Down
27 changes: 27 additions & 0 deletions tests/test-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,31 @@ public function example_actors() {

return $actors;
}

public function test_incoming_move() {
$new_url = 'https://example.com/new_url';

$request = new \WP_REST_Request( 'POST', '/activitypub/1.0/users/' . get_current_user_id() . '/inbox' );
$request->set_body(
wp_json_encode(
array(
'type' => 'Move',
'id' => $this->actor . '/move',
'actor' => $this->actor,
'object' => $this->actor,
'target' => $new_url,
)
)
);
$request->set_header( 'Content-type', 'application/json' );

$response = $this->server->dispatch( $request );
$this->assertEquals( 202, $response->get_status() );

$old_user_feed = User_Feed::get_by_url( $this->actor );
$this->assertTrue( is_wp_error( $old_user_feed ) );

$new_user_feed = User_Feed::get_by_url( $new_url );
$this->assertInstanceof( 'Friends\User_Feed', $new_user_feed );
}
}
Loading
0