Activitypub_follow.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * ActivityPub implementation for GNU social
  18. *
  19. * @package GNUsocial
  20. * @author Diogo Cordeiro <diogo@fc.up.pt>
  21. * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. * @link http://www.gnu.org/software/social/
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * ActivityPub error representation
  28. *
  29. * @category Plugin
  30. * @package GNUsocial
  31. * @author Diogo Cordeiro <diogo@fc.up.pt>
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. */
  34. class Activitypub_follow
  35. {
  36. /**
  37. * Generates an ActivityPub representation of a subscription
  38. *
  39. * @author Diogo Cordeiro <diogo@fc.up.pt>
  40. * @param string $actor
  41. * @param string $object
  42. * @param string|null $id Activity id, to be used when generating for an Accept Activity
  43. * @return array pretty array to be used in a response
  44. */
  45. public static function follow_to_array(string $actor, string $object, ?string $id = null): array
  46. {
  47. if ($id === null) {
  48. $id = common_root_url().'follow_from_'.urlencode($actor).'_to_'.urlencode($object);
  49. }
  50. $res = [
  51. '@context' => 'https://www.w3.org/ns/activitystreams',
  52. 'id' => $id,
  53. 'type' => 'Follow',
  54. 'actor' => $actor,
  55. 'object' => $object
  56. ];
  57. return $res;
  58. }
  59. /**
  60. * Handles a Follow Activity received by our inbox.
  61. *
  62. * @param Profile $actor_profile Remote Actor
  63. * @param string $object Local Actor
  64. * @param string $id Activity id
  65. * @throws AlreadyFulfilledException
  66. * @throws HTTP_Request2_Exception
  67. * @throws NoProfileException
  68. * @throws ServerException
  69. * @author Diogo Cordeiro <diogo@fc.up.pt>
  70. */
  71. public static function follow(Profile $actor_profile, string $object, string $id)
  72. {
  73. // Get Actor's Aprofile
  74. $actor_aprofile = Activitypub_profile::from_profile($actor_profile);
  75. // Get Object profile
  76. $object_profile = new Activitypub_explorer;
  77. $object_profile = $object_profile->lookup($object)[0];
  78. if (!Subscription::exists($actor_profile, $object_profile)) {
  79. Subscription::start($actor_profile, $object_profile);
  80. Activitypub_profile::subscribeCacheUpdate($actor_profile, $object_profile);
  81. common_debug('ActivityPubPlugin: Accepted Follow request from '.ActivityPubPlugin::actor_uri($actor_profile).' to '.$object);
  82. } else {
  83. common_debug('ActivityPubPlugin: Received a repeated Follow request from '.ActivityPubPlugin::actor_uri($actor_profile).' to '.$object);
  84. }
  85. // Notify remote instance that we have accepted their request
  86. common_debug('ActivityPubPlugin: Notifying remote instance that we have accepted their Follow request request from '.ActivityPubPlugin::actor_uri($actor_profile).' to '.$object);
  87. $postman = new Activitypub_postman($object_profile, [$actor_aprofile]);
  88. $postman->accept_follow($id);
  89. }
  90. }