Activitypub_create.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * ActivityPub implementation for GNU social
  18. *
  19. * @package GNUsocial
  20. * @author Diogo Cordeiro <diogo@fc.up.pt>
  21. * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. * @link http://www.gnu.org/software/social/
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * ActivityPub error representation
  28. *
  29. * @category Plugin
  30. * @package GNUsocial
  31. * @author Diogo Cordeiro <diogo@fc.up.pt>
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. */
  34. class Activitypub_create
  35. {
  36. /**
  37. * Generates an ActivityPub representation of a Create
  38. *
  39. * @param string $actor
  40. * @param array $object
  41. * @param bool $directMessage whether it is a private Create activity or not
  42. * @return array pretty array to be used in a response
  43. * @author Diogo Cordeiro <diogo@fc.up.pt>
  44. */
  45. public static function create_to_array(string $actor, array $object, bool $directMessage = false): array
  46. {
  47. $res = [
  48. '@context' => 'https://www.w3.org/ns/activitystreams',
  49. 'id' => $object['id'] . '/create',
  50. 'type' => 'Create',
  51. 'directMessage' => $directMessage,
  52. 'to' => $object['to'],
  53. 'cc' => $object['cc'],
  54. 'actor' => $actor,
  55. 'object' => $object
  56. ];
  57. return $res;
  58. }
  59. /**
  60. * Verifies if a given object is acceptable for a Create Activity.
  61. *
  62. * @param array $object
  63. * @return bool True if acceptable, false if valid but unsupported
  64. * @throws Exception if invalid
  65. * @author Diogo Cordeiro <diogo@fc.up.pt>
  66. */
  67. public static function validate_object($object): bool
  68. {
  69. if (!is_array($object)) {
  70. common_debug('ActivityPub Create Validator: Rejected because of invalid Object format.');
  71. throw new Exception('Invalid Object Format for Create Activity.');
  72. }
  73. if (!isset($object['type'])) {
  74. common_debug('ActivityPub Create Validator: Rejected because of Type.');
  75. throw new Exception('Object type was not specified for Create Activity.');
  76. }
  77. if (isset($object['directMessage']) && !is_bool($object['directMessage'])) {
  78. common_debug('ActivityPub Create Validator: Rejected because Object directMessage is invalid.');
  79. throw new Exception('Invalid Object directMessage.');
  80. }
  81. switch ($object['type']) {
  82. case 'Note':
  83. // Validate data
  84. return Activitypub_notice::validate_note($object);
  85. break;
  86. default:
  87. throw new Exception('This is not a supported Object Type for Create Activity.');
  88. }
  89. return true;
  90. }
  91. /**
  92. * Verify if received note is private (direct).
  93. * Note that we're conformant with the (yet) non-standard directMessage attribute:
  94. * https://github.com/w3c/activitypub/issues/196#issuecomment-304958984
  95. *
  96. * @param array $activity received Create-Note activity
  97. * @return bool true if note is private, false otherwise
  98. * @author Bruno casteleiro <brunoccast@fc.up.pt>
  99. */
  100. public static function isPrivateNote(array $activity): bool
  101. {
  102. if (isset($activity['directMessage'])) {
  103. return $activity['directMessage'];
  104. }
  105. return empty($activity['cc']) && !in_array('https://www.w3.org/ns/activitystreams#Public', $activity['to']);
  106. }
  107. }