From 30024b4d473cbe5f56e71b7fc48e5bfeaa684a87 Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Tue, 4 Aug 2020 13:48:09 +0100 Subject: [PATCH] [ActivityPub][SCRIPTS] Add fix_subscriptions.php --- .../TheFreeNetwork/scripts/fix_duplicates.php | 0 .../classes/Activitypub_profile.php | 21 ++--- .../ActivityPub/scripts/fix_subscriptions.php | 91 +++++++++++++++++++ .../scripts/update_activitypub_profiles.php | 30 ++---- scripts/simple_console.php | 0 5 files changed, 109 insertions(+), 33 deletions(-) mode change 100644 => 100755 modules/TheFreeNetwork/scripts/fix_duplicates.php create mode 100755 plugins/ActivityPub/scripts/fix_subscriptions.php mode change 100644 => 100755 scripts/simple_console.php diff --git a/modules/TheFreeNetwork/scripts/fix_duplicates.php b/modules/TheFreeNetwork/scripts/fix_duplicates.php old mode 100644 new mode 100755 diff --git a/plugins/ActivityPub/classes/Activitypub_profile.php b/plugins/ActivityPub/classes/Activitypub_profile.php index 867f7ed7be..3167e25be3 100644 --- a/plugins/ActivityPub/classes/Activitypub_profile.php +++ b/plugins/ActivityPub/classes/Activitypub_profile.php @@ -721,20 +721,17 @@ class Activitypub_profile extends Managed_DataObject } $subs = Subscription::getSubscribedIDs($profile->id, $offset, $limit); - try { - $profiles = []; - $users = User::multiGet('id', $subs); - foreach ($users->fetchAll() as $user) { - $profiles[$user->id] = $user->getProfile(); - } + $profiles = []; - $ap_profiles = Activitypub_profile::multiGet('profile_id', $subs); - foreach ($ap_profiles->fetchAll() as $ap) { - $profiles[$ap->getID()] = $ap->local_profile(); - } - } catch (NoResultException $e) { - return $e->obj; + $users = User::multiGet('id', $subs); + foreach ($users->fetchAll() as $user) { + $profiles[$user->id] = $user->getProfile(); + } + + $ap_profiles = Activitypub_profile::multiGet('profile_id', $subs); + foreach ($ap_profiles->fetchAll() as $ap) { + $profiles[$ap->getID()] = $ap->local_profile(); } if ($cache) { diff --git a/plugins/ActivityPub/scripts/fix_subscriptions.php b/plugins/ActivityPub/scripts/fix_subscriptions.php new file mode 100755 index 0000000000..8c47a3d00a --- /dev/null +++ b/plugins/ActivityPub/scripts/fix_subscriptions.php @@ -0,0 +1,91 @@ +#!/usr/bin/env php +. + +/** + * ActivityPub implementation for GNU social + * + * @package GNUsocial + * @author Diogo Cordeiro + * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later + */ + +define('INSTALLDIR', dirname(__DIR__, 3)); +define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public'); + +$shortoptions = 'i:af'; +$longoptions = ['id=', 'all', 'force']; + +$helptext = <<getProfile()); + exit(0); +} elseif (!have_option('a', 'all')) { + show_help(); + exit(1); +} + +$user = new User(); +$cnt = $user->find(); +while ($user->fetch()) { + fix_subscriptions($user->getProfile()); +} +$user->free(); +unset($user); + +printfnq("Done.\n"); + +function fix_subscriptions(Profile $profile) +{ + // Collect every remote AP subscription + $aprofiles = []; + $subs = Subscription::getSubscribedIDs($profile->getID(), 0, null); + $subs_aprofiles = Activitypub_profile::multiGet('profile_id', $subs); + foreach ($subs_aprofiles->fetchAll() as $ap) { + $aprofiles[$ap->getID()] = $ap; + } + unset($subs_aprofiles); + // For each remote AP subscription, send a Follow activity + foreach ($aprofiles as $sub) { + try { + $postman = new Activitypub_postman($profile, [$sub]); + $postman->follow(); + printfnq( + 'Ensured subscription between ' . $profile->getBestName() + . ' and ' . $sub->getUri() . "\n" + ); + } catch (Exception $e) { + // Let it go + printfnq('Failed to ensure subscription between ' . $profile->getBestName() + . ' and ' . $sub->getUri() . "\n" + ); + printfnq('The reason was: ' . $e->getMessage() . "\n"); + } + } +} diff --git a/plugins/ActivityPub/scripts/update_activitypub_profiles.php b/plugins/ActivityPub/scripts/update_activitypub_profiles.php index ae909e0cb0..03f8b0f7b4 100755 --- a/plugins/ActivityPub/scripts/update_activitypub_profiles.php +++ b/plugins/ActivityPub/scripts/update_activitypub_profiles.php @@ -44,9 +44,7 @@ END_OF_HELP; require_once INSTALLDIR.'/scripts/commandline.inc'; -$quiet = have_option('q', 'quiet'); - -if (!$quiet) { +if (!have_option('q', 'quiet')) { echo "ActivityPub Profiles updater will now start!\n"; echo "Summoning Diogo Cordeiro, Richard Stallman and Chuck Norris to help us with this task!\n"; } @@ -60,9 +58,7 @@ if (have_option('u', 'uri')) { echo $e->getMessage()."\n"; exit(1); } - if (!$quiet) { - echo 'Updated '.Activitypub_profile::update_profile($user, $res)->getBestName()."\n"; - } + printfnq('Updated '.Activitypub_profile::update_profile($user, $res)->getBestName()."\n"); exit(0); } elseif (!have_option('a', 'all')) { show_help(); @@ -72,18 +68,12 @@ if (have_option('u', 'uri')) { $user = new Activitypub_profile(); $cnt = $user->find(); if (!empty($cnt)) { - if (!$quiet) { - echo "Found {$cnt} ActivityPub profiles:\n"; - } + printfnq("Found {$cnt} ActivityPub profiles:\n"); } else { if (have_option('u', 'uri')) { - if (!$quiet) { - echo "Couldn't find an existing ActivityPub profile with that URI.\n"; - } + printfnq("Couldn't find an existing ActivityPub profile with that URI.\n"); } else { - if (!$quiet) { - echo "Couldn't find any existing ActivityPub profiles.\n"; - } + printfnq("Couldn't find any existing ActivityPub profiles.\n"); } exit(0); } @@ -91,14 +81,12 @@ while ($user->fetch()) { try { $res = Activitypub_explorer::get_remote_user_activity($user->uri); $updated_profile = Activitypub_profile::update_profile($user, $res); - if (!$quiet) { - echo 'Updated '.$updated_profile->getBestName()."\n"; - } + printfnq('Updated '.$updated_profile->getBestName()."\n"); } catch (NoProfileException $e) { - if (!$quiet) { - echo 'Deleted '.$user->uri."\n"; - } + printfnq('Deleted '.$user->uri."\n"); } catch (Exception $e) { // let it go } } +$user->free(); +unset($user); diff --git a/scripts/simple_console.php b/scripts/simple_console.php old mode 100644 new mode 100755