TagSubPlugin.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * A plugin to enable local tab subscription
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category TagSubPlugin
  24. * @package StatusNet
  25. * @author Brion Vibber <brion@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. exit(1);
  32. }
  33. /**
  34. * TagSub plugin main class
  35. *
  36. * @category TagSubPlugin
  37. * @package StatusNet
  38. * @author Brion Vibber <brionv@status.net>
  39. * @copyright 2011 StatusNet, Inc.
  40. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  41. * @link http://status.net/
  42. */
  43. class TagSubPlugin extends Plugin
  44. {
  45. const PLUGIN_VERSION = '0.1.1';
  46. /**
  47. * Database schema setup
  48. *
  49. * @return bool hook value; true means continue processing, false means stop.
  50. * @see Schema
  51. *
  52. */
  53. public function onCheckSchema()
  54. {
  55. $schema = Schema::get();
  56. $schema->ensureTable('tagsub', TagSub::schemaDef());
  57. return true;
  58. }
  59. /**
  60. * Map URLs to actions
  61. *
  62. * @param URLMapper $m path-to-action mapper
  63. *
  64. * @return boolean hook value; true means continue processing, false means stop.
  65. */
  66. public function onRouterInitialized(URLMapper $m)
  67. {
  68. $m->connect('tag/:tag/subscribe',
  69. ['action' => 'tagsub'],
  70. ['tag' => Router::REGEX_TAG]);
  71. $m->connect('tag/:tag/unsubscribe',
  72. ['action' => 'tagunsub'],
  73. ['tag' => Router::REGEX_TAG]);
  74. $m->connect(':nickname/tag-subscriptions',
  75. ['action' => 'tagsubs'],
  76. ['nickname' => Nickname::DISPLAY_FMT]);
  77. return true;
  78. }
  79. /**
  80. * Plugin version data
  81. *
  82. * @param array &$versions array of version data
  83. *
  84. * @return bool true hook value
  85. */
  86. public function onPluginVersion(array &$versions): bool
  87. {
  88. $versions[] = ['name' => 'TagSub',
  89. 'version' => self::PLUGIN_VERSION,
  90. 'author' => 'Brion Vibber',
  91. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/TagSub',
  92. 'rawdescription' =>
  93. // TRANS: Plugin description.
  94. _m('Plugin to allow following all messages with a given tag.')];
  95. return true;
  96. }
  97. /**
  98. * Hook inbox delivery setup so tag subscribers receive all
  99. * notices with that tag in their inbox.
  100. *
  101. * Currently makes no distinction between local messages and
  102. * remote ones which happen to come in to the system. Remote
  103. * notices that don't come in at all won't ever reach this.
  104. *
  105. * @param Notice $notice
  106. * @param array $ni in/out map of profile IDs to inbox constants
  107. * @return boolean hook result
  108. */
  109. public function onStartNoticeWhoGets(Notice $notice, array &$ni)
  110. {
  111. foreach ($notice->getTags() as $tag) {
  112. $tagsub = new TagSub();
  113. $tagsub->tag = $tag;
  114. $tagsub->find();
  115. while ($tagsub->fetch()) {
  116. // These constants are currently not actually used, iirc
  117. $ni[$tagsub->profile_id] = NOTICE_INBOX_SOURCE_SUB;
  118. }
  119. }
  120. return true;
  121. }
  122. /**
  123. *
  124. * @param TagAction $action
  125. * @return boolean hook result
  126. */
  127. public function onStartTagShowContent(TagAction $action)
  128. {
  129. $user = common_current_user();
  130. if ($user) {
  131. $tag = $action->trimmed('tag');
  132. $tagsub = TagSub::pkeyGet(array('tag' => $tag,
  133. 'profile_id' => $user->id));
  134. if ($tagsub) {
  135. $form = new TagUnsubForm($action, $tag);
  136. } else {
  137. $form = new TagSubForm($action, $tag);
  138. }
  139. $action->elementStart('div', 'entity_actions');
  140. $action->elementStart('ul');
  141. $action->elementStart('li', 'entity_subscribe');
  142. $form->show();
  143. $action->elementEnd('li');
  144. $action->elementEnd('ul');
  145. $action->elementEnd('div');
  146. }
  147. return true;
  148. }
  149. /**
  150. * Menu item for personal subscriptions/groups area
  151. *
  152. * @param Widget $widget Widget being executed
  153. *
  154. * @return boolean hook return
  155. * @throws Exception
  156. */
  157. public function onEndSubGroupNav($widget)
  158. {
  159. $action = $widget->out;
  160. $action_name = $action->trimmed('action');
  161. $action->menuItem(
  162. common_local_url('tagsubs', array('nickname' => $action->user->nickname)),
  163. // TRANS: SubMirror plugin menu item on user settings page.
  164. _m('MENU', 'Tags'),
  165. // TRANS: SubMirror plugin tooltip for user settings menu item.
  166. _m('Configure tag subscriptions'),
  167. $action_name == 'tagsubs' && $action->arg('nickname') == $action->user->nickname
  168. );
  169. return true;
  170. }
  171. public function onEndDefaultLocalNav($menu, $user)
  172. {
  173. $user = $user ? $user : common_current_user();
  174. if (!empty($user)) {
  175. $tags = TagSub::forProfile($user->getProfile());
  176. if (!empty($tags) && count($tags) > 0) {
  177. $tagSubMenu = new TagSubMenu($menu->out, $user, $tags);
  178. // TRANS: Menu item text for tags submenu.
  179. $menu->submenu(_m('Tags'), $tagSubMenu);
  180. }
  181. }
  182. return true;
  183. }
  184. }