SubMirrorPlugin.php 5.8 KB

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