Skip to content

Commit 2429b85

Browse files
committed
Add support for ActivityPub Move activity
1 parent d7a5cc0 commit 2429b85

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

feed-parsers/class-feed-parser-activitypub.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function __construct( Feed $friends_feed ) {
4747
\add_action( 'activitypub_inbox_announce', array( $this, 'handle_received_announce' ), 15, 2 );
4848
\add_action( 'activitypub_inbox_like', array( $this, 'handle_received_like' ), 15, 2 );
4949
\add_action( 'activitypub_inbox_undo', array( $this, 'handle_received_undo' ), 15, 2 );
50+
\add_action( 'activitypub_inbox_move', array( $this, 'handle_received_move' ), 15, 2 );
5051
\add_action( 'activitypub_handled_create', array( $this, 'activitypub_handled_create' ), 10, 4 );
5152

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

793+
public function handle_received_move( $activity, $user_id ) {
794+
return $this->handle_received_activity( $activity, $user_id, 'move' );
795+
}
796+
792797
public function handle_received_delete( $activity, $user_id ) {
793798
return $this->handle_received_activity( $activity, $user_id, 'delete' );
794799
}
@@ -840,6 +845,7 @@ public function handle_received_activity( $activity, $user_id, $type ) {
840845
'unannounce',
841846
'like',
842847
'unlike',
848+
'move',
843849
),
844850
true
845851
) ) {
@@ -924,6 +930,8 @@ protected function process_incoming_activity( $type, $activity, $user_id, $user_
924930
return $this->handle_incoming_like( $activity, $user_id );
925931
case 'unlike':
926932
return $this->handle_incoming_unlike( $activity, $user_id );
933+
case 'move':
934+
return $this->handle_incoming_move( $activity, $user_feed );
927935
}
928936
return null;
929937
}
@@ -1209,6 +1217,62 @@ public function handle_incoming_unlike( $activity, $user_id ) {
12091217
return true;
12101218
}
12111219

1220+
public function handle_incoming_move( $activity, User_Feed $user_feed ) {
1221+
$old_url = $activity['object'];
1222+
if ( $user_feed->get_url() !== $old_url ) {
1223+
$this->log( 'Could not determine the right feed to be moved. Looking for ' . $old_url . ', got ' . $user_feed->get_url() );
1224+
return false;
1225+
}
1226+
1227+
$feed = array(
1228+
'url' => $activity['target'],
1229+
'mime-type' => $user_feed->get_mime_type(),
1230+
'title' => $user_feed->get_title(),
1231+
'parser' => $user_feed->get_parser(),
1232+
'post-format' => $user_feed->get_post_format(),
1233+
'active' => $user_feed->is_active(),
1234+
);
1235+
1236+
// Similar as in process_admin_edit_friend_feeds.
1237+
if ( $user_feed->get_url() !== $feed['url'] ) {
1238+
$friend = $user_feed->get_friend_user();
1239+
do_action( 'friends_user_feed_deactivated', $user_feed );
1240+
1241+
if ( ! isset( $feed['mime-type'] ) ) {
1242+
$feed['mime-type'] = $user_feed->get_mime_type();
1243+
}
1244+
1245+
if ( $feed['active'] ) {
1246+
$new_feed = $friend->subscribe( $feed['url'], $feed );
1247+
if ( ! is_wp_error( $new_feed ) ) {
1248+
do_action( 'friends_user_feed_activated', $new_feed );
1249+
}
1250+
} else {
1251+
$new_feed = $friend->save_feed( $feed['url'], $feed );
1252+
}
1253+
1254+
// Since the URL has changed, the above will create a new feed, therefore we need to delete the old one.
1255+
$user_feed->delete();
1256+
1257+
if ( is_wp_error( $new_feed ) ) {
1258+
do_action( 'friends_process_feed_item_submit_error', $new_feed, $feed );
1259+
return $new_feed;
1260+
}
1261+
1262+
do_action( 'friends_process_feed_item_submit', $new_feed, $feed );
1263+
$new_feed->update_last_log(
1264+
sprintf(
1265+
// translators: %s is the old URL.
1266+
__( 'Moved from old URL: %s', 'friends' ),
1267+
$user_feed->get_url()
1268+
)
1269+
);
1270+
return $new_feed;
1271+
}
1272+
1273+
return true;
1274+
}
1275+
12121276
/**
12131277
* Queue a hook to run async.
12141278
*

includes/class-user.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ public function save() {
388388
/**
389389
* Save multiple feeds for a user.
390390
*
391-
* @param string $feeds The feed URLs to subscribe to.
391+
* @param array $feeds The feed URLs to subscribe to.
392392
*
393393
* @return array(\WP_Term)|\WP_error $user The new associated user or an error object.
394394
*/

0 commit comments

Comments
 (0)