upstream V3 development https://www.gnusocial.rocks/v3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

114 lines
4.7 KiB

  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;
  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 Plugin\ActivityPub\ActivityPub;
  37. use Symfony\Component\HttpFoundation\Request;
  38. /**
  39. * Provides a response in application/ld+json to GSActivity
  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. abstract class OrderedCollectionController extends GSOrderedCollection
  45. {
  46. protected array $ordered_items = [];
  47. protected string $route;
  48. protected array $route_args = [];
  49. protected int $actor_id;
  50. public static function fromControllerVars(array $vars): array
  51. {
  52. $route = $vars['request']->get('_route');
  53. $route_args = array_merge($vars['request']->query->all(), $vars['request']->attributes->get('_route_params'));
  54. unset($route_args['is_system_path'], $route_args['template'], $route_args['_format'], $route_args['accept'], $route_args['p']);
  55. if (is_subclass_of($vars['controller'][0], FeedController::class)) {
  56. $notes = [];
  57. foreach ($vars['notes'] as $note_replies) {
  58. $notes[] = Router::url('note_view', ['id' => $note_replies['note']->getId()], type: Router::ABSOLUTE_URL);
  59. }
  60. $type = self::setupType(route: $route, route_args: $route_args, ordered_items: $notes);
  61. } elseif (is_subclass_of($vars['controller'][0], CircleController::class)) {
  62. $actors = [];
  63. foreach ($vars['actors'] as $actor) {
  64. $actors[] = Router::url('actor_view_id', ['id' => $actor->getId()], type: Router::ABSOLUTE_URL);
  65. }
  66. $type = self::setupType(route: $route, route_args: $route_args, ordered_items: $actors);
  67. } else {
  68. $type = self::setupType(route: $route, route_args: $route_args, ordered_items: []);
  69. }
  70. return ['type' => $type];
  71. }
  72. protected static function setupType(string $route, array $route_args = [], array $ordered_items = []): OrderedCollectionPage|OrderedCollection
  73. {
  74. $page = $route_args['page'] ?? 0;
  75. $type = $page === 0 ? new OrderedCollection() : new OrderedCollectionPage();
  76. $type->set('@context', ActivityPub::$activity_streams_two_context);
  77. $type->set('items', $ordered_items);
  78. $type->set('orderedItems', $ordered_items);
  79. $type->set('totalItems', \count($ordered_items));
  80. if ($page === 0) {
  81. $route_args['page'] = 1;
  82. $type->set('first', Router::url($route, $route_args, type: ROUTER::ABSOLUTE_URL));
  83. } else {
  84. $type->set('partOf', Router::url($route, $route_args, type: ROUTER::ABSOLUTE_URL));
  85. if ($page + 1 < $total_pages = 1) { // TODO: do proper pagination
  86. $route_args['page'] = ($page + 1 == 1 ? 2 : $page + 1);
  87. $type->set('next', Router::url($route, $route_args, type: ROUTER::ABSOLUTE_URL));
  88. }
  89. if ($page > 1) {
  90. $route_args['page'] = ($page - 1 <= 0 ? 1 : $page - 1);
  91. $type->set('prev', Router::url($route, $route_args, type: ROUTER::ABSOLUTE_URL));
  92. }
  93. }
  94. return $type;
  95. }
  96. public function handle(Request $request): array
  97. {
  98. $type = self::setupType($this->route, $this->route_args, $this->ordered_items, $this->actor_id);
  99. return ['type' => $type];
  100. }
  101. }