From 6737d342f34475505e5a781b0949b438e734818b Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 5 Feb 2024 18:44:08 +0100 Subject: [PATCH 1/4] init --- activitypub.php | 1 + includes/activity/class-actor.php | 8 +++ includes/class-move.php | 96 +++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 includes/class-move.php diff --git a/activitypub.php b/activitypub.php index aa9ea1272..bf4b9ccf7 100644 --- a/activitypub.php +++ b/activitypub.php @@ -67,6 +67,7 @@ function rest_init() { */ function plugin_init() { \add_action( 'init', array( __NAMESPACE__ . '\Migration', 'init' ) ); + \add_action( 'init', array( __NAMESPACE__ . '\Move', 'init' ) ); \add_action( 'init', array( __NAMESPACE__ . '\Activitypub', 'init' ) ); \add_action( 'init', array( __NAMESPACE__ . '\Activity_Dispatcher', 'init' ) ); \add_action( 'init', array( __NAMESPACE__ . '\Handler', 'init' ) ); diff --git a/includes/activity/class-actor.php b/includes/activity/class-actor.php index 1751aa29b..70b5de651 100644 --- a/includes/activity/class-actor.php +++ b/includes/activity/class-actor.php @@ -42,6 +42,14 @@ class Actor extends Base_Object { '@id' => 'lemmy:moderators', '@type' => '@id', ), + 'alsoKnownAs' => array( + '@id' => 'as:alsoKnownAs', + '@type' => '@id', + ), + 'movedTo' => array( + '@id' => 'as:movedTo', + '@type' => '@id', + ), 'postingRestrictedToMods' => 'lemmy:postingRestrictedToMods', 'discoverable' => 'toot:discoverable', 'indexable' => 'toot:indexable', diff --git a/includes/class-move.php b/includes/class-move.php new file mode 100644 index 000000000..b0bb1194b --- /dev/null +++ b/includes/class-move.php @@ -0,0 +1,96 @@ +get_id() ); + } + + $move_from_host = apply_filters( 'activitypub_move_actor_from_host', false, $id, $user ); + + if ( $move_from_host && is_array( $move_from_host ) ) { + $actor['alsoKnownAs'] = self::normalize_hosts( $move_from_host, $user->get_id() ); + } + + return $actor; + } + + /** + * Add settings to the admin interface + * + * @return void + */ + public static function add_settings() { + + } + + /** + * Normalize the host + * + * Returns the host if it is a valid URL, otherwise it tries to replace + * the host of the Actor-ID with the new host + * + * @param string $host_or_url the host or the url + * @param string $id the Actor-ID (URL) + * + * @return string the normalized host + */ + public static function normalize_host( $host_or_url, $id ) { + // if it is a valid URL use it + if ( filter_var( $host_or_url, FILTER_VALIDATE_URL ) ) { + return $host_or_url; + } + + // otherwise try to replace the host of the Actor-ID with the new host + $id = str_replace( wp_parse_url( get_home_url(), PHP_URL_HOST ), $host_or_url, $id ); + + return $id; + } + + /** + * Normalize the hosts + * + * Returns an array of normalized hosts + * + * @param string $hosts_or_urls the host or the url + * @param string $id the Actor-ID (URL) + * + * @return array the normalized hosts + */ + public static function normalize_hosts( $hosts_or_urls, $id ) { + $normalized_hosts = array(); + + foreach ( $hosts_or_urls as $host_or_url ) { + $normalized_hosts[] = self::normalize_host( $host_or_url, $id ); + } + + return $normalized_hosts; + } +} From 367686ea0ee4b40a15aec60287af88411a75ac05 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 16 Sep 2024 18:28:07 +0200 Subject: [PATCH 2/4] simplify code to provide a barebone functionality and improve from there --- includes/class-move.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/includes/class-move.php b/includes/class-move.php index b0bb1194b..78aafcb60 100644 --- a/includes/class-move.php +++ b/includes/class-move.php @@ -27,16 +27,16 @@ public static function extend_actor_profiles( $actor, $id, $user ) { return $actor; } - $move_to_host = apply_filters( 'activitypub_move_actor_to_host', false, $id, $user ); + $move_to = \get_user_option( 'activitypub_move_to', $user->get__id() ); - if ( $move_to_host && is_string( $move_to_host ) ) { - $actor['movedTo'] = self::normalize_host( $move_to_host, $user->get_id() ); + if ( $move_to ) { + $actor['movedTo'] = $move_to; } - $move_from_host = apply_filters( 'activitypub_move_actor_from_host', false, $id, $user ); + $also_known_as = \get_user_option( 'activitypub_also_known_as', $user->get__id() ); - if ( $move_from_host && is_array( $move_from_host ) ) { - $actor['alsoKnownAs'] = self::normalize_hosts( $move_from_host, $user->get_id() ); + if ( $also_known_as ) { + $actor['alsoKnownAs'] = (array) $also_known_as; } return $actor; @@ -48,7 +48,6 @@ public static function extend_actor_profiles( $actor, $id, $user ) { * @return void */ public static function add_settings() { - } /** From 66729e4cf5dd645e3b83f560b98c6ab88af7a79e Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Tue, 29 Oct 2024 15:32:13 +0100 Subject: [PATCH 3/4] re-add namespaces --- includes/activity/class-actor.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/includes/activity/class-actor.php b/includes/activity/class-actor.php index 654ff7f4e..72d4bb617 100644 --- a/includes/activity/class-actor.php +++ b/includes/activity/class-actor.php @@ -43,6 +43,14 @@ class Actor extends Base_Object { '@id' => 'lemmy:moderators', '@type' => '@id', ), + 'alsoKnownAs' => array( + '@id' => 'as:alsoKnownAs', + '@type' => '@id', + ), + 'movedTo' => array( + '@id' => 'as:movedTo', + '@type' => '@id', + ), 'attributionDomains' => array( '@id' => 'toot:attributionDomains', '@type' => '@id', From 7b125ef28e2b65aac92d39ba2ee65b655c4a5c01 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Tue, 29 Oct 2024 15:36:31 +0100 Subject: [PATCH 4/4] fix PHPCS --- includes/class-move.php | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/includes/class-move.php b/includes/class-move.php index 78aafcb60..bf251076f 100644 --- a/includes/class-move.php +++ b/includes/class-move.php @@ -1,4 +1,10 @@