ForceGroupPlugin.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * @package ForceGroupPlugin
  21. * @maintainer Brion Vibber <brion@status.net>
  22. */
  23. if (!defined('STATUSNET')) { exit(1); }
  24. class ForceGroupPlugin extends Plugin
  25. {
  26. const PLUGIN_VERSION = '2.0.0';
  27. /**
  28. * Members of these groups will have all their posts mirrored into
  29. * the group even if they don't explicitly mention it.
  30. *
  31. * List by local nickname.
  32. */
  33. public $post = array();
  34. /**
  35. * New user registrations will automatically join these groups on
  36. * registration. They're not prevented from leaving, however.
  37. *
  38. * List by local nickname.
  39. */
  40. public $join = array();
  41. /**
  42. * If poster is in one of the forced groups, make sure their notice
  43. * gets saved into that group even if not explicitly mentioned.
  44. *
  45. * @param Notice $notice
  46. * @return boolean event hook return
  47. */
  48. function onStartNoticeDistribute($notice)
  49. {
  50. $profile = $notice->getProfile();
  51. $isRemote = !(User::getKV('id', $profile->id));
  52. if ($isRemote) {
  53. /*
  54. * Notices from remote users on other sites
  55. * will normally not end up here unless they're
  56. * specifically directed here, e.g.: via explicit
  57. * post to a remote (to them) group. But remote
  58. * notices can also be `pulled in' as a result of
  59. * local users subscribing to the remote user;
  60. * from the remote user's perspective, this results
  61. * in group-forcing appearing effectively random.
  62. * So let's be consistent, and just never force
  63. * incoming remote notices into a ForceGroup:
  64. */
  65. return true;
  66. }
  67. foreach ($this->post as $nickname) {
  68. $group = User_group::getForNickname($nickname);
  69. if ($group && $profile->isMember($group)) {
  70. $notice->addToGroupInbox($group);
  71. }
  72. }
  73. return true;
  74. }
  75. public function onEndUserRegister(Profile $profile)
  76. {
  77. foreach ($this->join as $nickname) {
  78. $group = User_group::getForNickname($nickname);
  79. if ($group && !$profile->isMember($group)) {
  80. try {
  81. $profile->joinGroup($group);
  82. } catch (Exception $e) {
  83. // TRANS: Server exception.
  84. // TRANS: %1$s is a user nickname, %2$s is a group nickname.
  85. throw new ServerException(sprintf(_m('Could not join user %1$s to group %2$s.'),
  86. $profile->nickname, $group->nickname));
  87. }
  88. }
  89. }
  90. }
  91. /**
  92. * Provide plugin version information.
  93. *
  94. * This data is used when showing the version page.
  95. *
  96. * @param array &$versions array of version data arrays; see EVENTS.txt
  97. *
  98. * @return boolean hook value
  99. */
  100. function onPluginVersion(array &$versions)
  101. {
  102. $url = 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/ForceGroup';
  103. $versions[] = array('name' => 'ForceGroup',
  104. 'version' => self::PLUGIN_VERSION,
  105. 'author' => 'Brion Vibber',
  106. 'homepage' => $url,
  107. 'rawdescription' =>
  108. // TRANS: Plugin description.
  109. _m('Allows forced group memberships and forces all notices to appear in groups that users were forced in.'));
  110. return true;
  111. }
  112. }