ActivityStreamsTwo.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace Plugin\ActivityStreamsTwo;
  3. use App\Core\Event;
  4. use App\Core\Modules\Plugin;
  5. use App\Core\Router\RouteLoader;
  6. use Plugin\ActivityStreamsTwo\Util\Response\NoteResponse;
  7. use Plugin\ActivityStreamsTwo\Util\Response\TypeResponse;
  8. class ActivityStreamsTwo extends Plugin
  9. {
  10. public function version(): string
  11. {
  12. return '0.1.0';
  13. }
  14. public array $accept = [
  15. 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  16. 'application/activity+json',
  17. 'application/json',
  18. 'application/ld+json',
  19. ];
  20. /**
  21. * @param string $route
  22. * @param array $accept
  23. * @param array $vars
  24. * @param null|TypeResponse $response
  25. *
  26. * @throws \Exception
  27. *
  28. * @return bool
  29. */
  30. public function onRouteInFormat(string $route, array $accept, array $vars, ?TypeResponse &$response = null): bool
  31. {
  32. if (empty(array_intersect($this->accept, $accept))) {
  33. return Event::next;
  34. }
  35. switch ($route) {
  36. case 'note_show':
  37. $response = NoteResponse::handle($vars['note']);
  38. return Event::stop;
  39. default:
  40. return Event::next;
  41. }
  42. }
  43. /**
  44. * This code executes when GNU social creates the page routing, and we hook
  45. * on this event to add our action handler for Embed.
  46. *
  47. * @param RouteLoader $r the router that was initialized.
  48. *
  49. * @return bool
  50. */
  51. public function onAddRoute(RouteLoader $r): bool
  52. {
  53. $r->connect('note_view_as2',
  54. '/note/{id<\d+>}',
  55. [NoteResponse::class, 'handle'],
  56. options: ['accept' => $this->accept]
  57. );
  58. return Event::next;
  59. }
  60. }