groupsalmon.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. if (!defined('GNUSOCIAL')) { exit(1); }
  20. /**
  21. * @package OStatusPlugin
  22. * @author James Walker <james@status.net>
  23. */
  24. class GroupsalmonAction extends SalmonAction
  25. {
  26. var $group = null;
  27. protected function prepare(array $args=array())
  28. {
  29. parent::prepare($args);
  30. $id = $this->trimmed('id');
  31. if (!$id) {
  32. // TRANS: Client error.
  33. $this->clientError(_m('No ID.'));
  34. }
  35. $this->group = User_group::getKV('id', $id);
  36. if (!$this->group instanceof User_group) {
  37. // TRANS: Client error.
  38. $this->clientError(_m('No such group.'));
  39. }
  40. $this->target = $this->group;
  41. $remote_group = Ostatus_profile::getKV('group_id', $id);
  42. if ($remote_group instanceof Ostatus_profile) {
  43. // TRANS: Client error.
  44. $this->clientError(_m('Cannot accept remote posts for a remote group.'));
  45. }
  46. return true;
  47. }
  48. /**
  49. * We've gotten a post event on the Salmon backchannel, probably a reply.
  50. */
  51. function handlePost()
  52. {
  53. // @fixme process all objects?
  54. switch ($this->activity->objects[0]->type) {
  55. case ActivityObject::ARTICLE:
  56. case ActivityObject::BLOGENTRY:
  57. case ActivityObject::NOTE:
  58. case ActivityObject::STATUS:
  59. case ActivityObject::COMMENT:
  60. break;
  61. default:
  62. // TRANS: Client exception.
  63. throw new ClientException('Cannot handle that kind of post.');
  64. }
  65. // Notice must be to the attention of this group
  66. if (empty($this->activity->context->attention)) {
  67. // TRANS: Client exception.
  68. throw new ClientException("Not to the attention of anyone.");
  69. } else {
  70. $uri = common_local_url('groupbyid', array('id' => $this->group->id));
  71. if (!array_key_exists($uri, $this->activity->context->attention)) {
  72. // TRANS: Client exception.
  73. throw new ClientException("Not to the attention of this group.");
  74. }
  75. }
  76. $this->saveNotice();
  77. }
  78. /**
  79. * We've gotten a follow/subscribe notification from a remote user.
  80. * Save a subscription relationship for them.
  81. */
  82. /**
  83. * Postel's law: consider a "follow" notification as a "join".
  84. */
  85. function handleFollow()
  86. {
  87. $this->handleJoin();
  88. }
  89. /**
  90. * Postel's law: consider an "unfollow" notification as a "leave".
  91. */
  92. function handleUnfollow()
  93. {
  94. $this->handleLeave();
  95. }
  96. /**
  97. * A remote user joined our group.
  98. * @fixme move permission checks and event call into common code,
  99. * currently we're doing the main logic in joingroup action
  100. * and so have to repeat it here.
  101. */
  102. function handleJoin()
  103. {
  104. if ($this->oprofile->isGroup()) {
  105. // TRANS: Client error.
  106. $this->clientError(_m('Groups cannot join groups.'));
  107. }
  108. common_log(LOG_INFO, sprintf('Remote profile %s joining local group %s', $this->oprofile->getUri(), $this->group->getNickname()));
  109. if ($this->actor->isMember($this->group)) {
  110. // Already a member; we'll take it silently to aid in resolving
  111. // inconsistencies on the other side.
  112. return true;
  113. }
  114. if (Group_block::isBlocked($this->group, $this->actor)) {
  115. // TRANS: Client error displayed when trying to join a group the user is blocked from by a group admin.
  116. $this->clientError(_m('You have been blocked from that group by the admin.'), 403);
  117. }
  118. try {
  119. $this->actor->joinGroup($this->group);
  120. } catch (Exception $e) {
  121. // TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
  122. $this->serverError(sprintf(_m('Could not join remote user %1$s to group %2$s.'),
  123. $this->oprofile->getUri(), $this->group->getNickname()));
  124. }
  125. }
  126. /**
  127. * A remote user left our group.
  128. */
  129. function handleLeave()
  130. {
  131. if ($this->oprofile->isGroup()) {
  132. // TRANS: Client error displayed when trying to have a group join another group.
  133. throw new AlreadyFulfilledException(_m('Groups cannot be members of groups'));
  134. }
  135. common_log(LOG_INFO, sprintf('Remote profile %s leaving local group %s', $this->oprofile->getUri(), $this->group->getNickname()));
  136. try {
  137. $this->actor->leaveGroup($this->group);
  138. } catch (Exception $e) {
  139. // TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
  140. $this->serverError(sprintf(_m('Could not remove remote user %1$s from group %2$s.'),
  141. $this->oprofile->getUri(), $this->group->getNickname()));
  142. }
  143. }
  144. }