FollowEveryonePlugin.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * When a new user registers, all existing users follow them automatically.
  18. *
  19. * @category Community
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2010 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Plugin to make all users follow each other at registration
  28. *
  29. * Users can unfollow afterwards if they want.
  30. *
  31. * @category Sample
  32. * @package GNUsocial
  33. * @author Evan Prodromou <evan@status.net>
  34. * @copyright 2010 StatusNet, Inc.
  35. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  36. */
  37. class FollowEveryonePlugin extends Plugin
  38. {
  39. const PLUGIN_VERSION = '2.0.0';
  40. /**
  41. * Called when a new user is registered.
  42. *
  43. * We find all users, and try to subscribe them to the new user, and
  44. * the new user to them. Exceptions (like silenced users or whatever)
  45. * are caught, logged, and ignored.
  46. *
  47. * @param Profile $profile The new user's profile
  48. *
  49. * @return boolean hook value
  50. */
  51. public function onEndUserRegister(Profile $profile)
  52. {
  53. $otherUser = new User();
  54. $otherUser->whereAdd('id <> ' . $profile->id);
  55. if ($otherUser->find()) {
  56. while ($otherUser->fetch()) {
  57. $otherProfile = $otherUser->getProfile();
  58. try {
  59. if (User_followeveryone_prefs::followEveryone($otherUser->id)) {
  60. Subscription::start($otherProfile, $profile);
  61. }
  62. Subscription::start($profile, $otherProfile);
  63. } catch (Exception $e) {
  64. common_log(LOG_WARNING, $e->getMessage());
  65. continue;
  66. }
  67. }
  68. }
  69. $ufep = new User_followeveryone_prefs();
  70. $ufep->user_id = $profile->id;
  71. $ufep->followeveryone = true;
  72. $ufep->insert();
  73. return true;
  74. }
  75. /**
  76. * Database schema setup
  77. *
  78. * Plugins can add their own tables to the StatusNet database. Plugins
  79. * should use StatusNet's schema interface to add or delete tables. The
  80. * ensureTable() method provides an easy way to ensure a table's structure
  81. * and availability.
  82. *
  83. * By default, the schema is checked every time StatusNet is run (say, when
  84. * a Web page is hit). Admins can configure their systems to only check the
  85. * schema when the checkschema.php script is run, greatly improving performance.
  86. * However, they need to remember to run that script after installing or
  87. * upgrading a plugin!
  88. *
  89. * @see Schema
  90. * @see ColumnDef
  91. *
  92. * @return boolean hook value; true means continue processing, false means stop.
  93. */
  94. public function onCheckSchema()
  95. {
  96. $schema = Schema::get();
  97. // For storing user-submitted flags on profiles
  98. $schema->ensureTable('user_followeveryone_prefs', User_followeveryone_prefs::schemaDef());
  99. return true;
  100. }
  101. /**
  102. * Show a checkbox on the profile form to ask whether to follow everyone
  103. *
  104. * @param Action $action The action being executed
  105. *
  106. * @return boolean hook value
  107. */
  108. public function onEndProfileFormData($action)
  109. {
  110. $user = common_current_user();
  111. $action->elementStart('li');
  112. // TRANS: Checkbox label in form for profile settings.
  113. $action->checkbox(
  114. 'followeveryone',
  115. _m('Follow everyone'),
  116. ($action->arg('followeveryone') ?? User_followeveryone_prefs::followEveryone($user->id))
  117. );
  118. $action->elementEnd('li');
  119. return true;
  120. }
  121. /**
  122. * Save checkbox value for following everyone
  123. *
  124. * @param Action $action The action being executed
  125. *
  126. * @return boolean hook value
  127. */
  128. public function onEndProfileSaveForm($action)
  129. {
  130. $user = common_current_user();
  131. User_followeveryone_prefs::savePref(
  132. $user->id,
  133. $action->boolean('followeveryone')
  134. );
  135. return true;
  136. }
  137. /**
  138. * Provide version information about this plugin.
  139. *
  140. * @param Array &$versions Array of version data
  141. *
  142. * @return boolean hook value
  143. *
  144. */
  145. public function onPluginVersion(array &$versions): bool
  146. {
  147. $versions[] = array('name' => 'FollowEveryone',
  148. 'version' => self::PLUGIN_VERSION,
  149. 'author' => 'Evan Prodromou',
  150. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/FollowEveryone',
  151. 'rawdescription' =>
  152. // TRANS: Plugin description.
  153. _m('New users follow everyone at registration and are followed in return.'));
  154. return true;
  155. }
  156. }