ForceGroupPlugin.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /**
  27. * Members of these groups will have all their posts mirrored into
  28. * the group even if they don't explicitly mention it.
  29. *
  30. * List by local nickname.
  31. */
  32. public $post = array();
  33. /**
  34. * New user registrations will automatically join these groups on
  35. * registration. They're not prevented from leaving, however.
  36. *
  37. * List by local nickname.
  38. */
  39. public $join = array();
  40. /**
  41. * If poster is in one of the forced groups, make sure their notice
  42. * gets saved into that group even if not explicitly mentioned.
  43. *
  44. * @param Notice $notice
  45. * @return boolean event hook return
  46. */
  47. function onStartNoticeDistribute($notice)
  48. {
  49. $profile = $notice->getProfile();
  50. $isRemote = !(User::getKV('id', $profile->id));
  51. if ($isRemote) {
  52. /*
  53. * Notices from remote users on other sites
  54. * will normally not end up here unless they're
  55. * specifically directed here, e.g.: via explicit
  56. * post to a remote (to them) group. But remote
  57. * notices can also be `pulled in' as a result of
  58. * local users subscribing to the remote user;
  59. * from the remote user's perspective, this results
  60. * in group-forcing appearing effectively random.
  61. * So let's be consistent, and just never force
  62. * incoming remote notices into a ForceGroup:
  63. */
  64. return true;
  65. }
  66. foreach ($this->post as $nickname) {
  67. $group = User_group::getForNickname($nickname);
  68. if ($group && $profile->isMember($group)) {
  69. $notice->addToGroupInbox($group);
  70. }
  71. }
  72. return true;
  73. }
  74. public function onEndUserRegister(Profile $profile)
  75. {
  76. foreach ($this->join as $nickname) {
  77. $group = User_group::getForNickname($nickname);
  78. if ($group && !$profile->isMember($group)) {
  79. try {
  80. $profile->joinGroup($group);
  81. } catch (Exception $e) {
  82. // TRANS: Server exception.
  83. // TRANS: %1$s is a user nickname, %2$s is a group nickname.
  84. throw new ServerException(sprintf(_m('Could not join user %1$s to group %2$s.'),
  85. $profile->nickname, $group->nickname));
  86. }
  87. }
  88. }
  89. }
  90. /**
  91. * Provide plugin version information.
  92. *
  93. * This data is used when showing the version page.
  94. *
  95. * @param array &$versions array of version data arrays; see EVENTS.txt
  96. *
  97. * @return boolean hook value
  98. */
  99. function onPluginVersion(array &$versions)
  100. {
  101. $url = 'http://status.net/wiki/Plugin:ForceGroup';
  102. $versions[] = array('name' => 'ForceGroup',
  103. 'version' => GNUSOCIAL_VERSION,
  104. 'author' => 'Brion Vibber',
  105. 'homepage' => $url,
  106. 'rawdescription' =>
  107. // TRANS: Plugin description.
  108. _m('Allows forced group memberships and forces all notices to appear in groups that users were forced in.'));
  109. return true;
  110. }
  111. }