Activitypub_like.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 Like 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_like
  35. {
  36. /**
  37. * Generates an ActivityPub representation of a Like
  38. *
  39. * @param string $actor Actor URI
  40. * @param Notice $notice Notice URI
  41. * @return array pretty array to be used in a response
  42. * @author Diogo Cordeiro <diogo@fc.up.pt>
  43. */
  44. public static function like_to_array(string $actor, Notice $notice): array
  45. {
  46. $res = [
  47. '@context' => 'https://www.w3.org/ns/activitystreams',
  48. 'id' => Activitypub_notice::getUri($notice),
  49. 'type' => 'Like',
  50. 'actor' => $actor,
  51. 'object' => Activitypub_notice::getUri($notice->getParent()),
  52. ];
  53. return $res;
  54. }
  55. /**
  56. * Save a favorite record.
  57. *
  58. * @param string $uri
  59. * @param Profile $actor the local or remote Profile who favorites
  60. * @param Notice $target the notice that is favorited
  61. * @return Notice record on success
  62. * @throws AlreadyFulfilledException
  63. * @throws ClientException
  64. * @throws NoticeSaveException
  65. * @throws ServerException
  66. */
  67. public static function addNew(string $uri, Profile $actor, Notice $target): Notice
  68. {
  69. if (Fave::existsForProfile($target, $actor)) {
  70. // TRANS: Client error displayed when trying to mark a notice as favorite that already is a favorite.
  71. throw new AlreadyFulfilledException(_m('You have already favorited this!'));
  72. }
  73. $act = new Activity();
  74. $act->type = ActivityObject::ACTIVITY;
  75. $act->verb = ActivityVerb::FAVORITE;
  76. $act->time = time();
  77. $act->id = $uri;
  78. $act->title = _m('Favor');
  79. // TRANS: Message that is the "content" of a favorite (%1$s is the actor's nickname, %2$ is the favorited
  80. // notice's nickname and %3$s is the content of the favorited notice.)
  81. $act->content = sprintf(
  82. _m('%1$s favorited something by %2$s: %3$s'),
  83. $actor->getNickname(),
  84. $target->getProfile()->getNickname(),
  85. $target->getRendered()
  86. );
  87. $act->actor = $actor->asActivityObject();
  88. $act->target = $target->asActivityObject();
  89. $act->objects = [clone($act->target)];
  90. $url = common_local_url('AtomPubShowFavorite', ['profile'=>$actor->id, 'notice'=>$target->id]);
  91. $act->selfLink = $url;
  92. $act->editLink = $url;
  93. $options = [
  94. 'source' => 'ActivityPub',
  95. 'uri' => $act->id,
  96. 'url' => $url,
  97. 'is_local' => ($actor->isLocal() ? Notice::LOCAL_PUBLIC : Notice::REMOTE),
  98. 'scope' => $target->getScope(),
  99. ];
  100. // saveActivity will in turn also call Fave::saveActivityObject
  101. return Notice::saveActivity($act, $actor, $options);
  102. }
  103. }