ActivityFollow.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. /**
  20. * ActivityPub implementation for GNU social
  21. *
  22. * @package GNUsocial
  23. * @category ActivityPub
  24. *
  25. * @author Diogo Peralta Cordeiro <@diogo.site>
  26. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  27. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  28. */
  29. namespace Plugin\ActivityPub\Util\Model;
  30. use ActivityPhp\Type\AbstractObject;
  31. use App\Core\DB\DB;
  32. use App\Entity\Activity as GSActivity;
  33. use App\Util\Exception\ClientException;
  34. use Component\Subscription\Subscription;
  35. use DateTime;
  36. use InvalidArgumentException;
  37. use Plugin\ActivityPub\Entity\ActivitypubActivity;
  38. /**
  39. * This class handles translation between JSON and ActivityPub Activities
  40. *
  41. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  42. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  43. */
  44. class ActivityFollow extends Activity
  45. {
  46. protected static function handle_core_activity(\App\Entity\Actor $actor, AbstractObject $type_activity, mixed $type_object, ?ActivitypubActivity &$ap_act): ActivitypubActivity
  47. {
  48. if ($type_object instanceof AbstractObject) {
  49. $subscribed = Actor::fromJson($type_object)->getActorId();
  50. } elseif ($type_object instanceof \App\Entity\Actor) {
  51. $subscribed = $type_object;
  52. } else {
  53. throw new InvalidArgumentException('Follow{:Object} should be either an AbstractObject or an Actor.');
  54. }
  55. // Execute Subscribe
  56. $act = Subscription::subscribe($actor, $subscribed, 'ActivityPub');
  57. if (\is_null($act)) {
  58. throw new ClientException('You are already subscribed to this actor.');
  59. }
  60. // Store ActivityPub Activity
  61. $ap_act = ActivitypubActivity::create([
  62. 'activity_id' => $act->getId(),
  63. 'activity_uri' => $type_activity->get('id'),
  64. 'created' => new DateTime($type_activity->get('published') ?? 'now'),
  65. 'modified' => new DateTime(),
  66. ]);
  67. DB::persist($ap_act);
  68. return $ap_act;
  69. }
  70. public static function handle_undo(\App\Entity\Actor $actor, AbstractObject $type_activity, GSActivity $type_object, ?ActivitypubActivity &$ap_act): ActivitypubActivity
  71. {
  72. // Execute Unsubscribe
  73. $act = Subscription::unsubscribe($actor, $type_object->getObjectId(), 'ActivityPub');
  74. if (\is_null($act)) {
  75. throw new ClientException('You are already unsubscribed of this actor.');
  76. }
  77. // Store ActivityPub Activity
  78. $ap_act = ActivitypubActivity::create([
  79. 'activity_id' => $act->getId(),
  80. 'activity_uri' => $type_activity->get('id'),
  81. 'created' => new DateTime($type_activity->get('published') ?? 'now'),
  82. 'modified' => new DateTime(),
  83. ]);
  84. DB::persist($ap_act);
  85. return $ap_act;
  86. }
  87. }