Model.php 4.4 KB

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