GroupSettings.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. declare(strict_types = 1);
  3. namespace Plugin\UnboundGroup\Controller;
  4. use App\Core\Controller;
  5. use App\Core\DB;
  6. use App\Core\Form;
  7. use function App\Core\I18n\_m;
  8. use App\Entity as E;
  9. use App\Util\Common;
  10. use App\Util\Exception\ClientException;
  11. use Component\Subscription\Subscription;
  12. use Plugin\ActivityPub\Util\Explorer;
  13. use Plugin\UnboundGroup\Entity\activitypubGroupUnbound;
  14. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  15. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  16. use Symfony\Component\Form\Extension\Core\Type\TextType;
  17. use Symfony\Component\HttpFoundation\Request;
  18. class GroupSettings extends Controller
  19. {
  20. /**
  21. * Manage the gs:unbound state
  22. */
  23. public static function groupUnbound(Request $request, E\Actor $target, string $details_id)
  24. {
  25. $actor = Common::ensureLoggedIn()->getActor();
  26. if (!$actor->canModerate($target)) {
  27. throw new ClientException(_m('You don\'t have enough permissions to edit {nickname}\'s settings', ['{nickname}' => $target->getNickname()]));
  28. }
  29. $unbound_options = [
  30. _m('Default') => null,
  31. _m('True') => true,
  32. _m('False') => false,
  33. ];
  34. $obj = DB::findOneBy(activitypubGroupUnbound::class, ['actor_id' => $target->getId()], return_null: true);
  35. $form_definition = [
  36. ['new_state', ChoiceType::class, ['label' => _m('Unbound:'), 'multiple' => false, 'expanded' => false, 'choices' => $unbound_options, 'data' => $obj?->getUnbound()]],
  37. [$save_unbound_state_form_name = 'save_group_links', SubmitType::class, ['label' => _m('Save state')]],
  38. ];
  39. $unbound_state_form = Form::create($form_definition);
  40. if ($request->getMethod() === 'POST' && $request->request->has($save_unbound_state_form_name)) {
  41. $unbound_state_form->handleRequest($request);
  42. if ($unbound_state_form->isSubmitted() && $unbound_state_form->isValid()) {
  43. $new_state = $unbound_state_form->getData()['new_state'];
  44. $update_obj = activitypubGroupUnbound::createOrUpdate(obj: $obj, args: [
  45. 'actor_id' => $target->getId(),
  46. 'unbound' => $new_state,
  47. ]);
  48. if (\is_null($obj)) {
  49. DB::persist($update_obj);
  50. }
  51. DB::flush();
  52. Form::forceRedirect($unbound_state_form, $request);
  53. }
  54. }
  55. return [
  56. '_template' => 'self_tags_settings.fragment.html.twig',
  57. 'add_self_tags_form' => $unbound_state_form->createView(),
  58. ];
  59. }
  60. /**
  61. * Manage the linksTo collection of a Group or Organisation Actor
  62. */
  63. public static function groupLinks(Request $request, E\Actor $target, string $details_id)
  64. {
  65. $actor = Common::ensureLoggedIn()->getActor();
  66. if (!$actor->canModerate($target)) {
  67. throw new ClientException(_m('You don\'t have enough permissions to edit {nickname}\'s settings', ['{nickname}' => $target->getNickname()]));
  68. }
  69. $form_definition = [
  70. ['new_link', TextType::class, ['label' => _m('Link to'), 'required' => true, 'help' => _m('Enter the URI.')]],
  71. [$add_link_to_form_name = 'save_group_links', SubmitType::class, ['label' => _m('Add link')]],
  72. ];
  73. $add_link_to_form = Form::create($form_definition);
  74. if ($request->getMethod() === 'POST' && $request->request->has($add_link_to_form_name)) {
  75. $add_link_to_form->handleRequest($request);
  76. if ($add_link_to_form->isSubmitted() && $add_link_to_form->isValid()) {
  77. if (Common::isValidHttpUrl($new_uri = $add_link_to_form->getData()['new_link'])) {
  78. $new_link = Explorer::getOneFromUri($new_uri);
  79. $unbound = DB::findOneBy(activitypubGroupUnbound::class, ['actor_id' => $new_link->getId()], return_null: true);
  80. if ($unbound?->getUnbound() ?? true) {
  81. if (\is_null(Subscription::subscribe(subject: $target, object: $new_link, source: 'ActivityPub via FEP-2100'))) {
  82. throw new ClientException(_m('This group is already linked from {nickname}', ['{nickname}' => $new_link->getNickname()]));
  83. }
  84. Subscription::refreshSubscriptionCount($target, $new_link);
  85. }
  86. Form::forceRedirect($add_link_to_form, $request);
  87. } else {
  88. throw new ClientException(_m('Invalid URI given.'));
  89. }
  90. }
  91. }
  92. return [
  93. '_template' => 'self_tags_settings.fragment.html.twig',
  94. 'add_self_tags_form' => $add_link_to_form->createView(),
  95. ];
  96. }
  97. }