groupsalmon.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. try {
  77. $this->saveNotice();
  78. } catch (AlreadyFulfilledException $e) {
  79. return;
  80. }
  81. }
  82. /**
  83. * We've gotten a follow/subscribe notification from a remote user.
  84. * Save a subscription relationship for them.
  85. */
  86. /**
  87. * Postel's law: consider a "follow" notification as a "join".
  88. */
  89. function handleFollow()
  90. {
  91. $this->handleJoin();
  92. }
  93. /**
  94. * Postel's law: consider an "unfollow" notification as a "leave".
  95. */
  96. function handleUnfollow()
  97. {
  98. $this->handleLeave();
  99. }
  100. /**
  101. * A remote user joined our group.
  102. * @fixme move permission checks and event call into common code,
  103. * currently we're doing the main logic in joingroup action
  104. * and so have to repeat it here.
  105. */
  106. function handleJoin()
  107. {
  108. if ($this->oprofile->isGroup()) {
  109. // TRANS: Client error.
  110. $this->clientError(_m('Groups cannot join groups.'));
  111. }
  112. common_log(LOG_INFO, sprintf('Remote profile %s joining local group %s', $this->oprofile->getUri(), $this->group->getNickname()));
  113. if ($this->actor->isMember($this->group)) {
  114. // Already a member; we'll take it silently to aid in resolving
  115. // inconsistencies on the other side.
  116. return true;
  117. }
  118. if (Group_block::isBlocked($this->group, $this->actor)) {
  119. // TRANS: Client error displayed when trying to join a group the user is blocked from by a group admin.
  120. $this->clientError(_m('You have been blocked from that group by the admin.'), 403);
  121. }
  122. try {
  123. $this->actor->joinGroup($this->group);
  124. } catch (Exception $e) {
  125. // TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
  126. $this->serverError(sprintf(_m('Could not join remote user %1$s to group %2$s.'),
  127. $this->oprofile->getUri(), $this->group->getNickname()));
  128. }
  129. }
  130. /**
  131. * A remote user left our group.
  132. */
  133. function handleLeave()
  134. {
  135. if ($this->oprofile->isGroup()) {
  136. // TRANS: Client error displayed when trying to have a group join another group.
  137. throw new AlreadyFulfilledException(_m('Groups cannot be members of groups'));
  138. }
  139. common_log(LOG_INFO, sprintf('Remote profile %s leaving local group %s', $this->oprofile->getUri(), $this->group->getNickname()));
  140. try {
  141. $this->actor->leaveGroup($this->group);
  142. } catch (Exception $e) {
  143. // TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
  144. $this->serverError(sprintf(_m('Could not remove remote user %1$s from group %2$s.'),
  145. $this->oprofile->getUri(), $this->group->getNickname()));
  146. }
  147. }
  148. }