RemoteFollowPlugin.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. * Remote Follow implementation for GNU social
  18. *
  19. * @package GNUsocial
  20. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  21. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. */
  24. defined('GNUSOCIAL') || die();
  25. class RemoteFollowPlugin extends Plugin
  26. {
  27. const PLUGIN_VERSION = '0.1.0';
  28. /**
  29. * Route/Reroute urls
  30. *
  31. * @param URLMapper $m
  32. * @return void
  33. */
  34. public function onRouterInitialized(URLMapper $m): void
  35. {
  36. // discovery
  37. $m->connect('main/remotefollow/nickname/:nickname',
  38. ['action' => 'RemoteFollowInit'],
  39. ['nickname' => Nickname::DISPLAY_FMT]);
  40. $m->connect('main/remotefollow',
  41. ['action' => 'RemoteFollowInit']);
  42. // remote follow
  43. $m->connect('main/remotefollowsub',
  44. ['action' => 'RemoteFollowSub']);
  45. }
  46. /**
  47. * Add remote-follow button to someone's profile
  48. *
  49. * @param HTMLOutputter $out
  50. * @param Profile $target
  51. * @return bool hook return value
  52. */
  53. public function onStartProfileRemoteSubscribe(HTMLOutputter $out, Profile $target): bool
  54. {
  55. if (common_logged_in() || !$target->isLocal()) {
  56. return true;
  57. }
  58. $out->elementStart('li', 'entity_subscribe');
  59. $url = common_local_url('RemoteFollowInit', ['nickname' => $target->getNickname()]);
  60. $out->element('a',
  61. ['href' => $url,
  62. 'class' => 'entity_remote_subscribe'],
  63. // TRANS: Link text for the follow button
  64. _m('Subscribe'));
  65. $out->elementEnd('li');
  66. return true;
  67. }
  68. /**
  69. * Add remote-follow button in the subscriptions list
  70. *
  71. * @param Action $action
  72. * @return bool hook return value
  73. */
  74. public function onStartShowSubscriptionsContent(Action $action): bool
  75. {
  76. $this->showEntityRemoteSubscribe($action);
  77. return true;
  78. }
  79. /**
  80. * Add remote-follow button to the profile subscriptions minilist
  81. *
  82. * @param Action $action
  83. * @return bool hook return value
  84. */
  85. public function onEndShowSubscriptionsMiniList(Action $action): bool
  86. {
  87. $this->showEntityRemoteSubscribe($action);
  88. return true;
  89. }
  90. /**
  91. * Add webfinger profile link for remote subscription
  92. */
  93. function onEndWebFingerProfileLinks(XML_XRD $xrd, Profile $target): bool
  94. {
  95. $xrd->links[] = new XML_XRD_Element_Link('http://ostatus.org/schema/1.0/subscribe',
  96. common_local_url('RemoteFollowSub') . '?profile={uri}',
  97. null, // type not set
  98. true); // isTemplate
  99. return true;
  100. }
  101. /**
  102. * Plugin version information
  103. *
  104. * @param array $versions
  105. * @return bool hook return value
  106. */
  107. public function onPluginVersion(array &$versions): bool
  108. {
  109. $versions[] = [
  110. 'name' => 'RemoteFollow',
  111. 'version' => self::PLUGIN_VERSION,
  112. 'author' => 'Bruno Casteleiro',
  113. 'homepage' => 'https://notabug.org/diogo/gnu-social/src/nightly/plugins/RemoteFollow',
  114. // TRANS: Plugin description.
  115. 'rawdescription' => _m('Add remote-follow button support to GNU social')
  116. ];
  117. return true;
  118. }
  119. /**
  120. * Add remote-follow button to some required action
  121. *
  122. * @param Action $action
  123. * @return void
  124. */
  125. public function showEntityRemoteSubscribe(Action $action): void
  126. {
  127. if (!$action->getScoped() instanceof Profile) {
  128. // not logged in
  129. return;
  130. }
  131. if ($action->getScoped()->sameAs($action->getTarget())) {
  132. $action->elementStart('div', 'entity_actions');
  133. $action->elementStart('p', ['id' => 'entity_remote_subscribe',
  134. 'class' => 'entity_subscribe']);
  135. $action->element('a',
  136. ['href' => common_local_url('RemoteFollowSub'),
  137. 'class' => 'entity_remote_subscribe'],
  138. // TRANS: Link text for link to remote subscribe.
  139. _m('Remote'));
  140. $action->elementEnd('p');
  141. $action->elementEnd('div');
  142. }
  143. }
  144. }