SubMirrorPlugin.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2009-2010, StatusNet, Inc.
  5. *
  6. * This program 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. * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
  20. /**
  21. * @package SubMirrorPlugin
  22. * @maintainer Brion Vibber <brion@status.net>
  23. */
  24. class SubMirrorPlugin extends Plugin
  25. {
  26. const PLUGIN_VERSION = '2.0.0';
  27. /**
  28. * Hook for RouterInitialized event.
  29. *
  30. * @param URLMapper $m path-to-action mapper
  31. * @return boolean hook return
  32. */
  33. public function onRouterInitialized(URLMapper $m)
  34. {
  35. $m->connect('settings/mirror',
  36. ['action' => 'mirrorsettings']);
  37. $m->connect('settings/mirror/add/:provider',
  38. ['action' => 'mirrorsettings'],
  39. ['provider' => '[A-Za-z0-9_-]+']);
  40. $m->connect('settings/mirror/add',
  41. ['action' => 'addmirror']);
  42. $m->connect('settings/mirror/edit',
  43. ['action' => 'editmirror']);
  44. return true;
  45. }
  46. function handle($notice) : bool
  47. {
  48. // Is anybody mirroring?
  49. $mirror = new SubMirror();
  50. $mirror->subscribed = $notice->profile_id;
  51. if ($mirror->find()) {
  52. while ($mirror->fetch()) {
  53. $mirror->repeat($notice);
  54. }
  55. }
  56. }
  57. function onPluginVersion(array &$versions)
  58. {
  59. $versions[] = array('name' => 'SubMirror',
  60. 'version' => self::PLUGIN_VERSION,
  61. 'author' => 'Brion Vibber',
  62. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/SubMirror',
  63. 'rawdescription' =>
  64. // TRANS: Plugin description.
  65. _m('Pull feeds into your timeline!'));
  66. return true;
  67. }
  68. /**
  69. * Menu item for personal subscriptions/groups area
  70. *
  71. * @param Action $action action being executed
  72. *
  73. * @return boolean hook return
  74. */
  75. function onEndAccountSettingsNav($action)
  76. {
  77. $action_name = $action->trimmed('action');
  78. common_debug("ACTION NAME = " . $action_name);
  79. $action->menuItem(common_local_url('mirrorsettings'),
  80. // TRANS: SubMirror plugin menu item on user settings page.
  81. _m('MENU', 'Mirroring'),
  82. // TRANS: SubMirror plugin tooltip for user settings menu item.
  83. _m('Configure mirroring of posts from other feeds'),
  84. $action_name === 'mirrorsettings');
  85. return true;
  86. }
  87. function onCheckSchema()
  88. {
  89. $schema = Schema::get();
  90. $schema->ensureTable('submirror', SubMirror::schemaDef());
  91. // @hack until key definition support is merged
  92. SubMirror::fixIndexes($schema);
  93. return true;
  94. }
  95. /**
  96. * Set up queue handlers for outgoing hub pushes
  97. * @param QueueManager $qm
  98. * @return boolean hook return
  99. */
  100. function onEndInitializeQueueManager(QueueManager $qm)
  101. {
  102. // After each notice save, check if there's any repeat mirrors.
  103. $qm->connect('mirror', 'MirrorQueueHandler');
  104. return true;
  105. }
  106. function onStartEnqueueNotice($notice, &$transports)
  107. {
  108. $transports[] = 'mirror';
  109. }
  110. /**
  111. * Let the OStatus subscription garbage collection know if we're
  112. * making use of a remote feed, so it doesn't get dropped out
  113. * from under us.
  114. *
  115. * @param Ostatus_profile $oprofile
  116. * @param int $count in/out
  117. * @return mixed hook return value
  118. */
  119. function onOstatus_profileSubscriberCount(Ostatus_profile $oprofile, &$count)
  120. {
  121. try {
  122. $profile = $oprofile->localProfile();
  123. $mirror = new SubMirror();
  124. $mirror->subscribed = $profile->id;
  125. if ($mirror->find()) {
  126. while ($mirror->fetch()) {
  127. $count++;
  128. }
  129. }
  130. } catch (NoProfileException $e) {
  131. // We can't handle this kind of Ostatus_profile since it has no
  132. // local profile
  133. }
  134. return true;
  135. }
  136. /**
  137. * Add a count of mirrored feeds into a user's profile sidebar stats.
  138. *
  139. * @param Profile $profile
  140. * @param array $stats
  141. * @return boolean hook return value
  142. */
  143. function onProfileStats($profile, &$stats)
  144. {
  145. $cur = common_current_user();
  146. if (!empty($cur) && $cur->id == $profile->id) {
  147. $mirror = new SubMirror();
  148. $mirror->subscriber = $profile->id;
  149. $entry = array(
  150. 'id' => 'mirrors',
  151. // TRANS: Label in profile statistics section, followed by a count.
  152. 'label' => _m('Mirrored feeds'),
  153. 'link' => common_local_url('mirrorsettings'),
  154. 'value' => $mirror->count(),
  155. );
  156. $insertAt = count($stats);
  157. foreach ($stats as $i => $row) {
  158. if ($row['id'] == 'groups') {
  159. // Slip us in after them.
  160. $insertAt = $i + 1;
  161. break;
  162. }
  163. }
  164. array_splice($stats, $insertAt, 0, array($entry));
  165. }
  166. return true;
  167. }
  168. }