Skip to content
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

Add "Move Account" functionality #685

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,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' ) );
Expand Down
8 changes: 8 additions & 0 deletions includes/activity/class-actor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
96 changes: 96 additions & 0 deletions includes/class-move.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
namespace Activitypub;

/**
* ActivityPub (Account) Move Class
*
* @author Matthias Pfefferle
*/
class Move {
/**
* Initialize the class, registering WordPress hooks
*/
public static function init() {
\add_filter( 'activitypub_activity_user_object_array', array( self::class, 'extend_actor_profiles' ), 10, 3 );
}

/**
* Extend the actor profiles and add the "movedTo" and "alsoKnownAs" properties
*
* @param array $actor the actor profile
*
* @return array the extended actor profile
*/
public static function extend_actor_profiles( $actor, $id, $user ) {
// Check if the user is a valid user object
if ( ! $user instanceof \Activitypub\Model\User ) {
return $actor;
}

$move_to_host = apply_filters( 'activitypub_move_actor_to_host', false, $id, $user );

if ( $move_to_host && is_string( $move_to_host ) ) {
$actor['movedTo'] = self::normalize_host( $move_to_host, $user->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;
}
}
Loading