FollowEveryonePlugin.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * When a new user registers, all existing users follow them automatically.
  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 Community
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2010 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. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Plugin to make all users follow each other at registration
  37. *
  38. * Users can unfollow afterwards if they want.
  39. *
  40. * @category Sample
  41. * @package StatusNet
  42. * @author Evan Prodromou <evan@status.net>
  43. * @copyright 2010 StatusNet, Inc.
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  45. * @link http://status.net/
  46. */
  47. class FollowEveryonePlugin extends Plugin
  48. {
  49. const PLUGIN_VERSION = '2.0.0';
  50. /**
  51. * Called when a new user is registered.
  52. *
  53. * We find all users, and try to subscribe them to the new user, and
  54. * the new user to them. Exceptions (like silenced users or whatever)
  55. * are caught, logged, and ignored.
  56. *
  57. * @param Profile $profile The new user's profile
  58. *
  59. * @return boolean hook value
  60. */
  61. public function onEndUserRegister(Profile $profile)
  62. {
  63. $otherUser = new User();
  64. $otherUser->whereAdd('id != ' . $profile->id);
  65. if ($otherUser->find()) {
  66. while ($otherUser->fetch()) {
  67. $otherProfile = $otherUser->getProfile();
  68. try {
  69. if (User_followeveryone_prefs::followEveryone($otherUser->id)) {
  70. Subscription::start($otherProfile, $profile);
  71. }
  72. Subscription::start($profile, $otherProfile);
  73. } catch (Exception $e) {
  74. common_log(LOG_WARNING, $e->getMessage());
  75. continue;
  76. }
  77. }
  78. }
  79. $ufep = new User_followeveryone_prefs();
  80. $ufep->user_id = $profile->id;
  81. $ufep->followeveryone = true;
  82. $ufep->insert();
  83. return true;
  84. }
  85. /**
  86. * Database schema setup
  87. *
  88. * Plugins can add their own tables to the StatusNet database. Plugins
  89. * should use StatusNet's schema interface to add or delete tables. The
  90. * ensureTable() method provides an easy way to ensure a table's structure
  91. * and availability.
  92. *
  93. * By default, the schema is checked every time StatusNet is run (say, when
  94. * a Web page is hit). Admins can configure their systems to only check the
  95. * schema when the checkschema.php script is run, greatly improving performance.
  96. * However, they need to remember to run that script after installing or
  97. * upgrading a plugin!
  98. *
  99. * @see Schema
  100. * @see ColumnDef
  101. *
  102. * @return boolean hook value; true means continue processing, false means stop.
  103. */
  104. function onCheckSchema()
  105. {
  106. $schema = Schema::get();
  107. // For storing user-submitted flags on profiles
  108. $schema->ensureTable('user_followeveryone_prefs', User_followeveryone_prefs::schemaDef());
  109. return true;
  110. }
  111. /**
  112. * Show a checkbox on the profile form to ask whether to follow everyone
  113. *
  114. * @param Action $action The action being executed
  115. *
  116. * @return boolean hook value
  117. */
  118. function onEndProfileFormData($action)
  119. {
  120. $user = common_current_user();
  121. $action->elementStart('li');
  122. // TRANS: Checkbox label in form for profile settings.
  123. $action->checkbox('followeveryone', _m('Follow everyone'),
  124. ($action->arg('followeveryone')) ?
  125. $action->arg('followeveryone') :
  126. User_followeveryone_prefs::followEveryone($user->id));
  127. $action->elementEnd('li');
  128. return true;
  129. }
  130. /**
  131. * Save checkbox value for following everyone
  132. *
  133. * @param Action $action The action being executed
  134. *
  135. * @return boolean hook value
  136. */
  137. function onEndProfileSaveForm($action)
  138. {
  139. $user = common_current_user();
  140. User_followeveryone_prefs::savePref($user->id,
  141. $action->boolean('followeveryone'));
  142. return true;
  143. }
  144. /**
  145. * Provide version information about this plugin.
  146. *
  147. * @param Array &$versions Array of version data
  148. *
  149. * @return boolean hook value
  150. *
  151. */
  152. function onPluginVersion(array &$versions)
  153. {
  154. $versions[] = array('name' => 'FollowEveryone',
  155. 'version' => self::PLUGIN_VERSION,
  156. 'author' => 'Evan Prodromou',
  157. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/FollowEveryone',
  158. 'rawdescription' =>
  159. // TRANS: Plugin description.
  160. _m('New users follow everyone at registration and are followed in return.'));
  161. return true;
  162. }
  163. }