RSSCloudPlugin.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. * Plugin to support RSSCloud
  18. *
  19. * @category Plugin
  20. * @package GNUsocial
  21. * @author Zach Copley <zach@status.net>
  22. * @copyright 2009 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. define('RSSCLOUDPLUGIN_VERSION', '0.1.0');
  27. /**
  28. * Plugin class for adding RSSCloud capabilities to StatusNet
  29. *
  30. * @category Plugin
  31. * @package StatusNet
  32. * @author Zach Copley <zach@status.net>
  33. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  34. */
  35. class RSSCloudPlugin extends Plugin
  36. {
  37. /**
  38. * Our friend, the constructor
  39. *
  40. * @return void
  41. */
  42. public function __construct()
  43. {
  44. parent::__construct();
  45. }
  46. /**
  47. * Setup the info for the subscription handler. Allow overriding
  48. * to point at another cloud hub (not currently used).
  49. *
  50. * @return void
  51. */
  52. public function onInitializePlugin(): void
  53. {
  54. $this->domain = common_config('rsscloud', 'domain');
  55. $this->port = common_config('rsscloud', 'port');
  56. $this->path = common_config('rsscloud', 'path');
  57. $this->funct = common_config('rsscloud', 'function');
  58. $this->protocol = common_config('rsscloud', 'protocol');
  59. // set defaults
  60. $local_server = parse_url(common_path('main/rsscloud/request_notify'));
  61. if (empty($this->domain)) {
  62. $this->domain = $local_server['host'];
  63. }
  64. if (empty($this->port)) {
  65. $this->port = '80';
  66. }
  67. if (empty($this->path)) {
  68. $this->path = $local_server['path'];
  69. }
  70. if (empty($this->funct)) {
  71. $this->funct = '';
  72. }
  73. if (empty($this->protocol)) {
  74. $this->protocol = 'http-post';
  75. }
  76. }
  77. /**
  78. * Add RSSCloud-related paths to the router table
  79. *
  80. * Hook for RouterInitialized event.
  81. *
  82. * @param URLMapper $m URL parser and mapper
  83. * @return bool hook return
  84. */
  85. public function onRouterInitialized(URLMapper $m): bool
  86. {
  87. $m->connect(
  88. 'main/rsscloud/request_notify',
  89. ['action' => 'RSSCloudRequestNotify']
  90. );
  91. // XXX: This is just for end-to-end testing. Uncomment if you need to pretend
  92. // to be a cloud hub for some reason.
  93. //$m->connect(
  94. // 'main/rsscloud/notify',
  95. // ['action' => 'LoggingAggregator']
  96. //);
  97. return true;
  98. }
  99. /**
  100. * Add a <cloud> element to the RSS feed (after the rss <channel>
  101. * element is started).
  102. *
  103. * @param Action $action the ApiAction
  104. * @return void
  105. */
  106. public function onStartApiRss(Action $action): void
  107. {
  108. if (get_class($action) == 'ApiTimelineUserAction') {
  109. $attrs = [
  110. 'domain' => $this->domain,
  111. 'port' => $this->port,
  112. 'path' => $this->path,
  113. 'registerProcedure' => $this->funct,
  114. 'protocol' => $this->protocol,
  115. ];
  116. // Dipping into XMLWriter to avoid a full end element (</cloud>).
  117. $action->xw->startElement('cloud');
  118. foreach ($attrs as $name => $value) {
  119. $action->xw->writeAttribute($name, $value);
  120. }
  121. $action->xw->endElement();
  122. }
  123. }
  124. /**
  125. * Add an RSSCloud queue item for each notice
  126. *
  127. * @param Notice $notice the notice
  128. * @param array &$transports the list of transports (queues)
  129. * @return bool hook return
  130. */
  131. public function onStartEnqueueNotice(
  132. Notice $notice,
  133. array &$transports
  134. ): bool {
  135. if ($notice->isLocal()) {
  136. array_push($transports, 'rsscloud');
  137. }
  138. return true;
  139. }
  140. /**
  141. * Create the rsscloud_subscription table if it's not
  142. * already in the DB
  143. *
  144. * @return bool hook return
  145. */
  146. public function onCheckSchema(): bool
  147. {
  148. $schema = Schema::get();
  149. $schema->ensureTable(
  150. 'rsscloud_subscription',
  151. RSSCloudSubscription::schemaDef()
  152. );
  153. return true;
  154. }
  155. /**
  156. * Register RSSCloud notice queue handler
  157. *
  158. * @param QueueManager $manager
  159. * @return bool hook return
  160. */
  161. public function onEndInitializeQueueManager(QueueManager $manager): bool
  162. {
  163. $manager->connect('rsscloud', 'RSSCloudQueueHandler');
  164. return true;
  165. }
  166. /**
  167. * Ensure that subscriptions for a user are deleted
  168. * when that user gets deleted.
  169. *
  170. * @param User $user
  171. * @param array &$related list of related tables
  172. *
  173. * @return bool hook result
  174. */
  175. public function onUserDeleteRelated(User $user, array &$related): bool
  176. {
  177. $sub = new RSSCloudSubscription();
  178. $sub->subscribed = $user->id;
  179. if ($sub->find()) {
  180. while ($sub->fetch()) {
  181. $sub->delete();
  182. }
  183. }
  184. $sub->free();
  185. return true;
  186. }
  187. public function onPluginVersion(array &$versions): bool
  188. {
  189. $versions[] = [
  190. 'name' => 'RSSCloud',
  191. 'version' => RSSCLOUDPLUGIN_VERSION,
  192. 'author' => 'Zach Copley',
  193. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/RSSCloud',
  194. 'rawdescription' =>
  195. // TRANS: Plugin description.
  196. _m('The RSSCloud plugin enables your GNU social instance to '
  197. . 'publish real-time updates for profile RSS feeds using the '
  198. . '<a href="http://rsscloud.co/">rssCloud protocol</a>.'),
  199. ];
  200. return true;
  201. }
  202. }