FollowEveryonePlugin.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. /**
  50. * Called when a new user is registered.
  51. *
  52. * We find all users, and try to subscribe them to the new user, and
  53. * the new user to them. Exceptions (like silenced users or whatever)
  54. * are caught, logged, and ignored.
  55. *
  56. * @param Profile $profile The new user's profile
  57. *
  58. * @return boolean hook value
  59. */
  60. public function onEndUserRegister(Profile $profile)
  61. {
  62. $otherUser = new User();
  63. $otherUser->whereAdd('id != ' . $profile->id);
  64. if ($otherUser->find()) {
  65. while ($otherUser->fetch()) {
  66. $otherProfile = $otherUser->getProfile();
  67. try {
  68. if (User_followeveryone_prefs::followEveryone($otherUser->id)) {
  69. Subscription::start($otherProfile, $profile);
  70. }
  71. Subscription::start($profile, $otherProfile);
  72. } catch (Exception $e) {
  73. common_log(LOG_WARNING, $e->getMessage());
  74. continue;
  75. }
  76. }
  77. }
  78. $ufep = new User_followeveryone_prefs();
  79. $ufep->user_id = $profile->id;
  80. $ufep->followeveryone = true;
  81. $ufep->insert();
  82. return true;
  83. }
  84. /**
  85. * Database schema setup
  86. *
  87. * Plugins can add their own tables to the StatusNet database. Plugins
  88. * should use StatusNet's schema interface to add or delete tables. The
  89. * ensureTable() method provides an easy way to ensure a table's structure
  90. * and availability.
  91. *
  92. * By default, the schema is checked every time StatusNet is run (say, when
  93. * a Web page is hit). Admins can configure their systems to only check the
  94. * schema when the checkschema.php script is run, greatly improving performance.
  95. * However, they need to remember to run that script after installing or
  96. * upgrading a plugin!
  97. *
  98. * @see Schema
  99. * @see ColumnDef
  100. *
  101. * @return boolean hook value; true means continue processing, false means stop.
  102. */
  103. function onCheckSchema()
  104. {
  105. $schema = Schema::get();
  106. // For storing user-submitted flags on profiles
  107. $schema->ensureTable('user_followeveryone_prefs', User_followeveryone_prefs::schemaDef());
  108. return true;
  109. }
  110. /**
  111. * Show a checkbox on the profile form to ask whether to follow everyone
  112. *
  113. * @param Action $action The action being executed
  114. *
  115. * @return boolean hook value
  116. */
  117. function onEndProfileFormData($action)
  118. {
  119. $user = common_current_user();
  120. $action->elementStart('li');
  121. // TRANS: Checkbox label in form for profile settings.
  122. $action->checkbox('followeveryone', _m('Follow everyone'),
  123. ($action->arg('followeveryone')) ?
  124. $action->arg('followeveryone') :
  125. User_followeveryone_prefs::followEveryone($user->id));
  126. $action->elementEnd('li');
  127. return true;
  128. }
  129. /**
  130. * Save checkbox value for following everyone
  131. *
  132. * @param Action $action The action being executed
  133. *
  134. * @return boolean hook value
  135. */
  136. function onEndProfileSaveForm($action)
  137. {
  138. $user = common_current_user();
  139. User_followeveryone_prefs::savePref($user->id,
  140. $action->boolean('followeveryone'));
  141. return true;
  142. }
  143. /**
  144. * Provide version information about this plugin.
  145. *
  146. * @param Array &$versions Array of version data
  147. *
  148. * @return boolean hook value
  149. *
  150. */
  151. function onPluginVersion(array &$versions)
  152. {
  153. $versions[] = array('name' => 'FollowEveryone',
  154. 'version' => GNUSOCIAL_VERSION,
  155. 'author' => 'Evan Prodromou',
  156. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/FollowEveryone',
  157. 'rawdescription' =>
  158. // TRANS: Plugin description.
  159. _m('New users follow everyone at registration and are followed in return.'));
  160. return true;
  161. }
  162. }