ForceGroupPlugin.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * The ForceGroup plugin allows forced group memberships and allows forcing all notices
  18. * to appear in some groups.
  19. *
  20. * @package ForceGroupPlugin
  21. * @maintainer Brion Vibber <brion@status.net>
  22. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die;
  26. class ForceGroupPlugin extends Plugin
  27. {
  28. const PLUGIN_VERSION = '2.0.0';
  29. // settings which can be set in config.php with addModule('ForceGroup', ['param'=>'value', ...]);
  30. /**
  31. * Members of these groups will have all their posts mirrored into
  32. * the group even if they don't explicitly mention it.
  33. *
  34. * List by local nickname.
  35. */
  36. public $post = [];
  37. /**
  38. * New user registrations will automatically join these groups on
  39. * registration. They're not prevented from leaving, however.
  40. *
  41. * List by local nickname.
  42. */
  43. public $join = [];
  44. /**
  45. * If poster is in one of the $post groups, make sure their notice
  46. * gets saved into that group even if not explicitly mentioned.
  47. *
  48. * @param Notice $notice
  49. * @return bool event hook return
  50. * @throws ServerException
  51. */
  52. public function onStartNoticeDistribute($notice): bool
  53. {
  54. $profile = $notice->getProfile();
  55. $isRemote = !(User::getKV('id', $profile->id));
  56. if ($isRemote) {
  57. /*
  58. * Notices from remote users on other sites
  59. * will normally not end up here unless they're
  60. * specifically directed here, e.g.: via explicit
  61. * post to a remote (to them) group. But remote
  62. * notices can also be `pulled in' as a result of
  63. * local users subscribing to the remote user;
  64. * from the remote user's perspective, this results
  65. * in group-forcing appearing effectively random.
  66. * So let's be consistent, and just never force
  67. * incoming remote notices into a ForceGroup:
  68. */
  69. return true;
  70. }
  71. foreach ($this->post as $nickname) {
  72. $group = User_group::getForNickname($nickname);
  73. if ($group && $profile->isMember($group)) {
  74. $notice->addToGroupInbox($group);
  75. }
  76. }
  77. return true;
  78. }
  79. /**
  80. * Make sure that this new user is added to all of the $join groups
  81. *
  82. * @param Profile $profile
  83. * @return bool event hook return
  84. * @throws ServerException
  85. */
  86. public function onEndUserRegister(Profile $profile)
  87. {
  88. foreach ($this->join as $nickname) {
  89. $group = User_group::getForNickname($nickname);
  90. if ($group && !$profile->isMember($group)) {
  91. try {
  92. $profile->joinGroup($group);
  93. } catch (Exception $e) {
  94. // TRANS: Server exception.
  95. // TRANS: %1$s is a user nickname, %2$s is a group nickname.
  96. throw new ServerException(sprintf(
  97. _m('Could not join user %1$s to group %2$s.'),
  98. $profile->nickname,
  99. $group->nickname
  100. ));
  101. }
  102. }
  103. }
  104. return true;
  105. }
  106. /**
  107. * Provide plugin version information.
  108. *
  109. * This data is used when showing the version page.
  110. *
  111. * @param array &$versions array of version data arrays; see EVENTS.txt
  112. *
  113. * @return bool hook value
  114. * @throws Exception
  115. */
  116. public function onPluginVersion(array &$versions): bool
  117. {
  118. $url = GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/ForceGroup';
  119. $versions[] = array('name' => 'ForceGroup',
  120. 'version' => self::PLUGIN_VERSION,
  121. 'author' => 'Brion Vibber',
  122. 'homepage' => $url,
  123. 'rawdescription' =>
  124. // TRANS: Module description.
  125. _m('Allows forced group memberships and forces all notices to appear in groups that users were forced in.'));
  126. return true;
  127. }
  128. }