OrderedCollectionController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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;
  30. use ActivityPhp\Type\Core\OrderedCollection;
  31. use ActivityPhp\Type\Core\OrderedCollectionPage;
  32. use App\Core\Router\Router;
  33. use Component\Collection\Util\Controller\CircleController;
  34. use Component\Collection\Util\Controller\FeedController;
  35. use Component\Collection\Util\Controller\OrderedCollection as GSOrderedCollection;
  36. use Symfony\Component\HttpFoundation\Request;
  37. /**
  38. * Provides a response in application/ld+json to GSActivity
  39. *
  40. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  41. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  42. */
  43. abstract class OrderedCollectionController extends GSOrderedCollection
  44. {
  45. protected array $ordered_items = [];
  46. protected string $route;
  47. protected array $route_args = [];
  48. protected int $actor_id;
  49. public static function fromControllerVars(array $vars): array
  50. {
  51. $route = $vars['request']->get('_route');
  52. $route_args = array_merge($vars['request']->query->all(), $vars['request']->attributes->get('_route_params'));
  53. unset($route_args['is_system_path'], $route_args['template'], $route_args['_format'], $route_args['accept'], $route_args['p']);
  54. if (is_subclass_of($vars['controller'][0], FeedController::class)) {
  55. $notes = [];
  56. foreach ($vars['notes'] as $note_replies) {
  57. $notes[] = Router::url('note_view', ['id' => $note_replies['note']->getId()], type: Router::ABSOLUTE_URL);
  58. }
  59. $type = self::setupType(route: $route, route_args: $route_args, ordered_items: $notes);
  60. } elseif (is_subclass_of($vars['controller'][0], CircleController::class)) {
  61. $actors = [];
  62. foreach ($vars['actors'] as $actor) {
  63. $actors[] = Router::url('actor_view_id', ['id' => $actor->getId()], type: Router::ABSOLUTE_URL);
  64. }
  65. $type = self::setupType(route: $route, route_args: $route_args, ordered_items: $actors);
  66. } else {
  67. $type = self::setupType(route: $route, route_args: $route_args, ordered_items: []);
  68. }
  69. return ['type' => $type];
  70. }
  71. protected static function setupType(string $route, array $route_args = [], array $ordered_items = []): OrderedCollectionPage|OrderedCollection
  72. {
  73. $page = $route_args['page'] ?? 0;
  74. $type = $page === 0 ? new OrderedCollection() : new OrderedCollectionPage();
  75. $type->set('@context', 'https://www.w3.org/ns/activitystreams');
  76. $type->set('items', $ordered_items);
  77. $type->set('orderedItems', $ordered_items);
  78. $type->set('totalItems', \count($ordered_items));
  79. if ($page === 0) {
  80. $route_args['page'] = 1;
  81. $type->set('first', Router::url($route, $route_args, type: ROUTER::ABSOLUTE_URL));
  82. } else {
  83. $type->set('partOf', Router::url($route, $route_args, type: ROUTER::ABSOLUTE_URL));
  84. if ($page + 1 < $total_pages = 1) { // TODO: do proper pagination
  85. $route_args['page'] = ($page + 1 == 1 ? 2 : $page + 1);
  86. $type->set('next', Router::url($route, $route_args, type: ROUTER::ABSOLUTE_URL));
  87. }
  88. if ($page > 1) {
  89. $route_args['page'] = ($page - 1 <= 0 ? 1 : $page - 1);
  90. $type->set('prev', Router::url($route, $route_args, type: ROUTER::ABSOLUTE_URL));
  91. }
  92. }
  93. return $type;
  94. }
  95. public function handle(Request $request): array
  96. {
  97. $type = self::setupType($this->route, $this->route_args, $this->ordered_items, $this->actor_id);
  98. return ['type' => $type];
  99. }
  100. }