Model.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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;
  31. use ActivityPhp\Type\TypeConfiguration;
  32. use ActivityPhp\Type\TypeResolver;
  33. use App\Core\Entity;
  34. use App\Core\Event;
  35. use App\Util\Exception\ClientException;
  36. use Exception;
  37. use InvalidArgumentException;
  38. use Plugin\ActivityPub\Util\Model\Activity;
  39. use Plugin\ActivityPub\Util\Model\Note;
  40. /**
  41. * This class handles translation between JSON and GS Entities
  42. *
  43. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  44. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  45. */
  46. abstract class Model
  47. {
  48. /**
  49. * Create a Type from an ActivityStreams 2.0 JSON string
  50. *
  51. * @throws Exception
  52. */
  53. public static function jsonToType(string|array $data): Type\AbstractObject
  54. {
  55. if (\is_string($data)) {
  56. $attributes = json_decode($data, true);
  57. if (json_last_error() !== \JSON_ERROR_NONE
  58. || !\is_array($attributes)
  59. ) {
  60. throw new Exception(
  61. sprintf(
  62. "An error occurred during the JSON decoding.\n '%s'",
  63. $data,
  64. ),
  65. );
  66. }
  67. } else {
  68. $attributes = $data;
  69. }
  70. if (!\array_key_exists('type', $attributes)) {
  71. throw new InvalidArgumentException('Missing "type" attribute in $data: ' . var_export($data, true));
  72. }
  73. unset($data);
  74. try {
  75. $type = TypeResolver::getClass($attributes['type']);
  76. } catch (Exception $e) {
  77. $message = json_encode($attributes, \JSON_PRETTY_PRINT);
  78. throw new Exception(
  79. $e->getMessage() . "\n{$message}",
  80. );
  81. }
  82. if (\is_string($type)) {
  83. $type = new $type();
  84. }
  85. // Add our own extensions
  86. $validators = [];
  87. if (Event::handle('ActivityPubValidateActivityStreamsTwoData', [$attributes['type'], &$validators]) === Event::next) {
  88. foreach ($validators as $name => $class) {
  89. Type::addValidator($name, $class);
  90. $type->extend($name);
  91. }
  92. }
  93. TypeConfiguration::set('undefined_properties', 'include');
  94. foreach ($attributes as $name => $value) {
  95. $type->set($name, $value);
  96. }
  97. return $type;
  98. }
  99. /**
  100. * Create an Entity from an ActivityStreams 2.0 JSON string
  101. */
  102. abstract public static function fromJson(string|Type\AbstractObject $json, array $options = []): Entity;
  103. /**
  104. * Get a JSON
  105. *
  106. * @param ?int $options PHP JSON options
  107. *
  108. * @throws ClientException
  109. */
  110. public static function toJson(mixed $object, ?int $options = null): string
  111. {
  112. switch ($object::class) {
  113. case \App\Entity\Activity::class:
  114. return Activity::toJson($object, $options);
  115. case \App\Entity\Note::class:
  116. return Note::toJson($object, $options);
  117. default:
  118. $type = self::jsonToType($object);
  119. Event::handle('ActivityPubAddActivityStreamsTwoData', [$type->get('type'), &$type]);
  120. return $type->toJson($options);
  121. }
  122. }
  123. }