[ActivityPub][SCRIPTS] Add fix_subscriptions.php

This commit is contained in:
Diogo Cordeiro 2020-08-04 13:48:09 +01:00
parent 09c3236afc
commit 379fbb6e5d
5 changed files with 109 additions and 33 deletions

0
modules/TheFreeNetwork/scripts/fix_duplicates.php Normal file → Executable file
View File

View File

@ -721,20 +721,17 @@ class Activitypub_profile extends Managed_DataObject
} }
$subs = Subscription::getSubscribedIDs($profile->id, $offset, $limit); $subs = Subscription::getSubscribedIDs($profile->id, $offset, $limit);
try {
$profiles = [];
$users = User::multiGet('id', $subs); $profiles = [];
foreach ($users->fetchAll() as $user) {
$profiles[$user->id] = $user->getProfile();
}
$ap_profiles = Activitypub_profile::multiGet('profile_id', $subs); $users = User::multiGet('id', $subs);
foreach ($ap_profiles->fetchAll() as $ap) { foreach ($users->fetchAll() as $user) {
$profiles[$ap->getID()] = $ap->local_profile(); $profiles[$user->id] = $user->getProfile();
} }
} catch (NoResultException $e) {
return $e->obj; $ap_profiles = Activitypub_profile::multiGet('profile_id', $subs);
foreach ($ap_profiles->fetchAll() as $ap) {
$profiles[$ap->getID()] = $ap->local_profile();
} }
if ($cache) { if ($cache) {

View File

@ -0,0 +1,91 @@
#!/usr/bin/env php
<?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/**
* ActivityPub implementation for GNU social
*
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @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 = <<<END_OF_HELP
fix_subsriptions.php [options]
For every ActivityPub subscription, re-send Follow activity.
-i --id Local user id whose follows shall be re-sent
-a --all update all
END_OF_HELP;
require_once INSTALLDIR.'/scripts/commandline.inc';
if (have_option('i', 'id')) {
$id = get_option_value('i', 'id');
$user = User::getByID($id);
fix_subscriptions($user->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");
}
}
}

View File

@ -44,9 +44,7 @@ END_OF_HELP;
require_once INSTALLDIR.'/scripts/commandline.inc'; require_once INSTALLDIR.'/scripts/commandline.inc';
$quiet = have_option('q', 'quiet'); if (!have_option('q', 'quiet')) {
if (!$quiet) {
echo "ActivityPub Profiles updater will now start!\n"; echo "ActivityPub Profiles updater will now start!\n";
echo "Summoning Diogo Cordeiro, Richard Stallman and Chuck Norris to help us with this task!\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"; echo $e->getMessage()."\n";
exit(1); exit(1);
} }
if (!$quiet) { printfnq('Updated '.Activitypub_profile::update_profile($user, $res)->getBestName()."\n");
echo 'Updated '.Activitypub_profile::update_profile($user, $res)->getBestName()."\n";
}
exit(0); exit(0);
} elseif (!have_option('a', 'all')) { } elseif (!have_option('a', 'all')) {
show_help(); show_help();
@ -72,18 +68,12 @@ if (have_option('u', 'uri')) {
$user = new Activitypub_profile(); $user = new Activitypub_profile();
$cnt = $user->find(); $cnt = $user->find();
if (!empty($cnt)) { if (!empty($cnt)) {
if (!$quiet) { printfnq("Found {$cnt} ActivityPub profiles:\n");
echo "Found {$cnt} ActivityPub profiles:\n";
}
} else { } else {
if (have_option('u', 'uri')) { if (have_option('u', 'uri')) {
if (!$quiet) { printfnq("Couldn't find an existing ActivityPub profile with that URI.\n");
echo "Couldn't find an existing ActivityPub profile with that URI.\n";
}
} else { } else {
if (!$quiet) { printfnq("Couldn't find any existing ActivityPub profiles.\n");
echo "Couldn't find any existing ActivityPub profiles.\n";
}
} }
exit(0); exit(0);
} }
@ -91,14 +81,12 @@ while ($user->fetch()) {
try { try {
$res = Activitypub_explorer::get_remote_user_activity($user->uri); $res = Activitypub_explorer::get_remote_user_activity($user->uri);
$updated_profile = Activitypub_profile::update_profile($user, $res); $updated_profile = Activitypub_profile::update_profile($user, $res);
if (!$quiet) { printfnq('Updated '.$updated_profile->getBestName()."\n");
echo 'Updated '.$updated_profile->getBestName()."\n";
}
} catch (NoProfileException $e) { } catch (NoProfileException $e) {
if (!$quiet) { printfnq('Deleted '.$user->uri."\n");
echo 'Deleted '.$user->uri."\n";
}
} catch (Exception $e) { } catch (Exception $e) {
// let it go // let it go
} }
} }
$user->free();
unset($user);

0
scripts/simple_console.php Normal file → Executable file
View File