UnboundGroup.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 Actor
  24. *
  25. * @author Diogo Peralta Cordeiro <@diogo.site>
  26. * @copyright 2022 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\UnboundGroup;
  30. use App\Core\DB;
  31. use App\Core\Event;
  32. use App\Core\Modules\Plugin;
  33. use App\Entity\Actor;
  34. use EventResult;
  35. use Plugin\ActivityPub\Entity\ActivitypubActor;
  36. use Plugin\UnboundGroup\Controller\GroupSettings;
  37. use Plugin\UnboundGroup\Entity\activitypubGroupUnbound;
  38. use Symfony\Component\HttpFoundation\Request;
  39. /**
  40. * When enabled, Adds ActivityPub "FEP-2100 Unbound Group and Organization" support to GNU social
  41. *
  42. * @copyright 2022 Free Software Foundation, Inc http://www.fsf.org
  43. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  44. */
  45. class UnboundGroup extends Plugin
  46. {
  47. /**
  48. * @param SettingsTabsType $tabs
  49. */
  50. public function onPopulateSettingsTabs(Request $request, string $section, array &$tabs): EventResult
  51. {
  52. if ($section === 'profile' && $request->get('_route') === 'group_actor_settings') {
  53. $tabs[] = [
  54. 'title' => 'Unbound',
  55. 'desc' => 'Unbound Group state.',
  56. 'id' => 'settings-group-unbound-details',
  57. 'controller' => GroupSettings::groupUnbound($request, Actor::getById((int) $request->get('id')), 'settings-group-unbound-details'),
  58. ];
  59. $tabs[] = [
  60. 'title' => 'Linked',
  61. 'desc' => 'Link to this Group from another Group.',
  62. 'id' => 'settings-group-links-details',
  63. 'controller' => GroupSettings::groupLinks($request, Actor::getById((int) $request->get('id')), 'settings-group-links-details'),
  64. ];
  65. }
  66. return Event::next;
  67. }
  68. public function onActivityStreamsTwoContext(array &$activity_streams_two_context): EventResult
  69. {
  70. $activity_streams_two_context[] = [
  71. 'unbound' => [
  72. '@id' => 'gs:unbound',
  73. '@type' => '@id',
  74. ],
  75. ];
  76. return Event::next;
  77. }
  78. public function onActivityPubAddActivityStreamsTwoData(string $type_name, object &$type): EventResult
  79. {
  80. if ($type_name === 'Group' || $type_name === 'Organization') {
  81. $actor = \Plugin\ActivityPub\Util\Explorer::getOneFromUri($type->getId());
  82. $unbound = DB::findOneBy(activitypubGroupUnbound::class, ['actor_id' => $actor->getId()], return_null: true);
  83. if (!\is_null($unbound_value = $unbound?->getUnbound())) {
  84. $type->set('unbound', $unbound_value);
  85. }
  86. }
  87. return Event::next;
  88. }
  89. public function onActivityPubCreateOrUpdateActor(\ActivityPhp\Type\AbstractObject $object, Actor $actor, ActivitypubActor $ap_actor): EventResult
  90. {
  91. if ($object->has('unbound')) {
  92. $obj = DB::findOneBy(activitypubGroupUnbound::class, ['actor_id' => $actor->getId()], return_null: true);
  93. $update_obj = activitypubGroupUnbound::createOrUpdate(obj: $obj, args: ['actor_id' => $actor->getId(), 'unbound' => $object->get('unbound')]);
  94. if (\is_null($obj)) {
  95. DB::persist($update_obj);
  96. }
  97. DB::flush();
  98. }
  99. return Event::next;
  100. }
  101. }