Add uninstall hook; decompact actor and object before delivery

This commit is contained in:
Jeremy Dormitzer 2018-11-03 14:30:16 -04:00
parent 1f70456a7f
commit 49f94816b6
7 changed files with 93 additions and 29 deletions

View File

@ -31,7 +31,7 @@ add_action( 'pterotype_init', function() {
\pterotype\schema\run_migrations();
\pterotype\actors\initialize_actors();
if ( ! empty( ob_get_contents() ) ) {
\pterotype\util\log( 'init.log', ob_get_contents(), false );
error_log( ob_get_contents() );
}
} );
@ -40,26 +40,42 @@ add_action( 'pterotype_load', function() {
\pterotype\async\init_tasks();
} );
add_action( 'pterotype_uninstall', function() {
\pterotype\schema\purge_all_data();
} );
add_action( 'generate_rewrite_rules', '\pterotype\webfinger\generate_rewrite_rules', 111 );
add_action( 'parse_request', '\pterotype\webfinger\parse_request', 111 );
add_filter( 'query_vars', '\pterotype\webfinger\query_vars' );
add_filter( 'query_vars', '\pterotype\api\query_vars' );
add_action( 'well_known_webfinger', '\pterotype\webfinger\handle' );
add_action( 'transition_post_status', '\pterotype\posts\handle_post_status_change', 10, 3 );
add_action(
'transition_comment_status', '\pterotype\comments\handle_transition_comment_status', 10, 3
);
add_action( 'comment_post', '\pterotype\comments\handle_comment_post', 10, 2 );
add_action( 'edit_comment', '\pterotype\comments\handle_edit_comment', 10, 1 );
add_action( 'template_redirect', '\pterotype\api\handle_non_api_requests' );
add_action( 'update_option_blogname', function() {
\pterotype\identity\update_identity( PTEROTYPE_BLOG_ACTOR_SLUG );
});
} );
add_action( 'update_option_blogdescription', function() {
\pterotype\identity\update_identity( PTEROTYPE_BLOG_ACTOR_SLUG );
});
} );
$theme = \get_option( 'stylesheet' );
add_action( "update_option_theme_mods_$theme", function() {
\pterotype\identity\update_identity( PTEROTYPE_BLOG_ACTOR_SLUG );
});
} );
?>

View File

@ -1,7 +1,9 @@
<?php
namespace pterotype\schema;
require plugin_dir_path( __FILE__ ) . 'client/identity.php';
require_once plugin_dir_path( __FILE__ ) . 'client/identity.php';
require_once plugin_dir_path( __FILE__ ) . 'server/activities/delete.php';
require_once plugin_dir_path( __FILE__ ) . 'server/actors.php';
function get_previous_version() {
$previous_version = get_option( 'pterotype_previously_migrated_version' );
@ -289,4 +291,34 @@ function migration_1_2_0() {
function migration_1_2_1() {
\pterotype\identity\update_identity( PTEROTYPE_BLOG_ACTOR_SLUG );
}
function purge_all_data() {
global $wpdb;
$blog_actor = \pterotype\actors\get_actor_by_slug( PTEROTYPE_BLOG_ACTOR_SLUG );
$delete = \pterotype\activities\delete\make_delete(
PTEROTYPE_BLOG_ACTOR_SLUG, $blog_actor
);
$delete['to'] = array(
'https://www.w3.org/ns/activitystreams#Public',
$blog_actor['followers']
);
$server = \rest_get_server();
$request = \WP_REST_Request::from_url( $blog_actor['outbox'] );
$request->set_method( 'POST' );
$request->set_body( wp_json_encode( $delete ) );
$request->add_header( 'Content-Type', 'application/ld+json' );
$server->dispatch( $request );
$pfx = $wpdb->prefix;
$wpdb->query( $wpdb->prepare(
"
DROP TABLE {$pfx}pterotype_comments, {$pfx}pterotype_keys,
{$pfx}pterotype_blocks, {$pfx}pterotype_shares,
{$pfx}pterotype_following, {$pfx}pterotype_followers,
{$pfx}pterotype_actor_likes, {$pfx}pterotype_object_likes,
{$pfx}pterotype_outbox, {$pfx}pterotype_inbox,
{$pfx}pterotype_objects, {$pfx}pterotype_actors
"
) );
\delete_option( 'pterotype_previously_migrated_version' );
}
?>

View File

@ -60,6 +60,9 @@ function get_commenter_actor( $row ) {
'@context' => array(
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
array(
'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
),
),
'type' => 'Person',
'id' => get_rest_url(
@ -111,6 +114,9 @@ function get_blog_actor() {
'@context' => array(
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
array(
'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
),
),
'type' => 'Organization',
'id' => get_rest_url(
@ -161,6 +167,9 @@ function get_user_actor( $user ) {
'@context' => array(
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
array(
'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
),
),
'type' => 'Person',
'id' => get_rest_url( null, sprintf( '/pterotype/v1/actor/%s', $handle ) ),

View File

@ -17,7 +17,7 @@ class Send_Accept_Task extends \WP_Async_Task {
$actor_slug = $_POST['actor_slug'];
$accept = $_POST['accept'];
if ( $actor_slug && $accept ) {
sleep( 2 );
sleep( 5 );
\pterotype\outbox\handle_activity( $actor_slug, $accept );
}
}

View File

@ -125,6 +125,7 @@ function get_recipient_urls( $object, $depth, $acc ) {
}
function post_activity_to_inboxes( $actor_id, $activity, $recipients ) {
$activity = \pterotype\util\decompact_object( $activity, array( 'actor', 'object' ) );
foreach ( $recipients as $inbox ) {
if ( $inbox === 'https://www.w3.org/ns/activitystreams#Public' ) {
continue;
@ -150,6 +151,9 @@ function post_activity_to_inboxes( $actor_id, $activity, $recipients ) {
'data_format' => 'body',
);
$response = wp_remote_post( $inbox, $args );
$req_str = print_r( $args, true );
$res_str = print_r( $response, true );
\error_log( "$req_str \n\n $res_str" );
if ( is_wp_error( $response ) ) {
\error_log(
"[Pterotype] Error delivering to $inbox: {$response->get_error_message()}"

View File

@ -102,33 +102,30 @@ function get_id( $object ) {
}
}
function get_log_dir() {
return plugin_dir_path( __FILE__ ) . '../log';
function decompact_object( $object, $fields ) {
return decompact_object_helper( $object, $fields, 0 );
}
function log( $log_file, $str, $append = true ) {
if ( ! WP_DEBUG ) {
return;
function decompact_object_helper( $object, $fields, $depth ) {
if ( $depth == 3 ) {
return $object;
}
$log_dir = get_log_dir();
$log_file = '/' . $log_file;
if ( ! file_exists( $log_dir ) ) {
mkdir( $log_dir, 0777, true );
if ( ! is_array( $object ) ) {
return $object;
}
if ( $append ) {
file_put_contents( $log_dir . $log_file, $str, FILE_APPEND );
} else {
file_put_contents( $log_dir . $log_file, $str );
$decompacted = $object;
foreach ( $object as $field => $value ) {
if ( ! in_array( $field, $fields ) ) {
continue;
}
if ( is_array( $value ) ) {
$decompacted[$field] = decompact_object_helper( $value, $fields, $depth + 1 );
} else if ( filter_var( $value, FILTER_VALIDATE_URL ) ) {
$decompacted[$field] = decompact_object_helper(
dereference_object( $value ), $fields, $depth + 1
);
}
}
}
function log_var( $log_file, $var, $append = true ) {
if ( ! WP_DEBUG ) {
return;
}
ob_start();
var_dump( $var );
$dump = ob_get_clean();
log( $log_file, $dump, $append );
return $decompacted;
}
?>

View File

@ -26,6 +26,11 @@ function pterotype_deactivate() {
flush_rewrite_rules();
}
function pterotype_uninstall() {
do_action( 'pterotype_uninstall' );
flush_rewrite_rules();
}
function pterotype_load() {
do_action( 'pterotype_load' );
}
@ -33,4 +38,5 @@ function pterotype_load() {
add_action( 'setup_theme', 'pterotype_load' );
register_activation_hook( __FILE__, 'pterotype_init' );
register_deactivation_hook( __FILE__, 'pterotype_deactivate' );
register_uninstall_hook( __FILE__, 'pterotype_uninstall' );
?>