fix_subscriptions.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env php
  2. <?php
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. /**
  18. * ActivityPub implementation for GNU social
  19. *
  20. * @package GNUsocial
  21. * @author Diogo Cordeiro <diogo@fc.up.pt>
  22. * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. define('INSTALLDIR', dirname(__DIR__, 3));
  26. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  27. $shortoptions = 'i:af';
  28. $longoptions = ['id=', 'all', 'force'];
  29. $helptext = <<<END_OF_HELP
  30. fix_subsriptions.php [options]
  31. For every ActivityPub subscription, re-send Follow activity.
  32. -i --id Local user id whose follows shall be re-sent
  33. -a --all update all
  34. END_OF_HELP;
  35. require_once INSTALLDIR.'/scripts/commandline.inc';
  36. if (have_option('i', 'id')) {
  37. $id = get_option_value('i', 'id');
  38. $user = User::getByID($id);
  39. fix_subscriptions($user->getProfile());
  40. exit(0);
  41. } elseif (!have_option('a', 'all')) {
  42. show_help();
  43. exit(1);
  44. }
  45. $user = new User();
  46. $cnt = $user->find();
  47. while ($user->fetch()) {
  48. fix_subscriptions($user->getProfile());
  49. }
  50. $user->free();
  51. unset($user);
  52. printfnq("Done.\n");
  53. function fix_subscriptions(Profile $profile)
  54. {
  55. // Collect every remote AP subscription
  56. $aprofiles = [];
  57. $subs = Subscription::getSubscribedIDs($profile->getID(), 0, null);
  58. $subs_aprofiles = Activitypub_profile::multiGet('profile_id', $subs);
  59. foreach ($subs_aprofiles->fetchAll() as $ap) {
  60. $aprofiles[$ap->getID()] = $ap;
  61. }
  62. unset($subs_aprofiles);
  63. // For each remote AP subscription, send a Follow activity
  64. foreach ($aprofiles as $sub) {
  65. try {
  66. $postman = new Activitypub_postman($profile, [$sub]);
  67. $postman->follow();
  68. printfnq(
  69. 'Ensured subscription between ' . $profile->getBestName()
  70. . ' and ' . $sub->getUri() . "\n"
  71. );
  72. } catch (Exception $e) {
  73. // Let it go
  74. printfnq('Failed to ensure subscription between ' . $profile->getBestName()
  75. . ' and ' . $sub->getUri() . "\n"
  76. );
  77. printfnq('The reason was: ' . $e->getMessage() . "\n");
  78. }
  79. }
  80. }