2010-02-20 16:12:43 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* StatusNet - the distributed open-source microblogging tool
|
|
|
|
* Copyright (C) 2010, StatusNet, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-05-05 18:25:39 +01:00
|
|
|
if (!defined('GNUSOCIAL')) { exit(1); }
|
2010-02-20 16:12:43 +00:00
|
|
|
|
2010-09-19 14:17:36 +01:00
|
|
|
/**
|
|
|
|
* @package OStatusPlugin
|
|
|
|
* @author James Walker <james@status.net>
|
|
|
|
*/
|
2010-02-20 16:12:43 +00:00
|
|
|
class GroupsalmonAction extends SalmonAction
|
|
|
|
{
|
|
|
|
var $group = null;
|
|
|
|
|
2014-05-05 18:25:39 +01:00
|
|
|
protected function prepare(array $args=array())
|
2010-02-20 16:12:43 +00:00
|
|
|
{
|
|
|
|
parent::prepare($args);
|
|
|
|
|
|
|
|
$id = $this->trimmed('id');
|
|
|
|
|
|
|
|
if (!$id) {
|
2010-09-19 14:17:36 +01:00
|
|
|
// TRANS: Client error.
|
|
|
|
$this->clientError(_m('No ID.'));
|
2010-02-20 16:12:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-18 12:04:58 +01:00
|
|
|
$this->group = User_group::getKV('id', $id);
|
2010-02-20 16:12:43 +00:00
|
|
|
|
2014-05-05 18:25:39 +01:00
|
|
|
if (!$this->group instanceof User_group) {
|
2010-09-19 14:17:36 +01:00
|
|
|
// TRANS: Client error.
|
|
|
|
$this->clientError(_m('No such group.'));
|
2010-02-20 16:12:43 +00:00
|
|
|
}
|
|
|
|
|
2010-12-27 18:51:59 +00:00
|
|
|
|
|
|
|
$this->target = $this->group;
|
|
|
|
|
2013-08-18 12:04:58 +01:00
|
|
|
$oprofile = Ostatus_profile::getKV('group_id', $id);
|
2014-05-05 18:25:39 +01:00
|
|
|
if ($oprofile instanceof Ostatus_profile) {
|
2010-09-19 14:17:36 +01:00
|
|
|
// TRANS: Client error.
|
2011-04-10 23:39:27 +01:00
|
|
|
$this->clientError(_m('Cannot accept remote posts for a remote group.'));
|
2010-02-24 02:19:13 +00:00
|
|
|
}
|
|
|
|
|
2010-02-20 16:12:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We've gotten a post event on the Salmon backchannel, probably a reply.
|
|
|
|
*/
|
|
|
|
function handlePost()
|
|
|
|
{
|
2010-03-23 01:53:09 +00:00
|
|
|
// @fixme process all objects?
|
2010-08-13 22:18:54 +01:00
|
|
|
switch ($this->activity->objects[0]->type) {
|
2010-02-20 16:12:43 +00:00
|
|
|
case ActivityObject::ARTICLE:
|
|
|
|
case ActivityObject::BLOGENTRY:
|
|
|
|
case ActivityObject::NOTE:
|
|
|
|
case ActivityObject::STATUS:
|
|
|
|
case ActivityObject::COMMENT:
|
|
|
|
break;
|
|
|
|
default:
|
2010-09-19 14:17:36 +01:00
|
|
|
// TRANS: Client exception.
|
2011-04-10 23:39:27 +01:00
|
|
|
throw new ClientException('Cannot handle that kind of post.');
|
2010-02-20 16:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notice must be to the attention of this group
|
2013-10-28 21:21:14 +00:00
|
|
|
if (empty($this->activity->context->attention)) {
|
2010-09-19 14:17:36 +01:00
|
|
|
// TRANS: Client exception.
|
2010-02-20 16:12:43 +00:00
|
|
|
throw new ClientException("Not to the attention of anyone.");
|
|
|
|
} else {
|
|
|
|
$uri = common_local_url('groupbyid', array('id' => $this->group->id));
|
2013-10-28 21:21:14 +00:00
|
|
|
|
|
|
|
if (!array_key_exists($uri, $this->activity->context->attention)) {
|
2010-09-19 14:17:36 +01:00
|
|
|
// TRANS: Client exception.
|
2010-02-20 16:12:43 +00:00
|
|
|
throw new ClientException("Not to the attention of this group.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$profile = $this->ensureProfile();
|
2010-02-24 02:19:13 +00:00
|
|
|
$this->saveNotice();
|
2010-02-20 16:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We've gotten a follow/subscribe notification from a remote user.
|
|
|
|
* Save a subscription relationship for them.
|
|
|
|
*/
|
|
|
|
|
2010-02-22 17:43:27 +00:00
|
|
|
/**
|
|
|
|
* Postel's law: consider a "follow" notification as a "join".
|
|
|
|
*/
|
2010-02-20 16:12:43 +00:00
|
|
|
function handleFollow()
|
|
|
|
{
|
2010-02-22 17:43:27 +00:00
|
|
|
$this->handleJoin();
|
2010-02-20 16:12:43 +00:00
|
|
|
}
|
|
|
|
|
2010-02-22 17:43:27 +00:00
|
|
|
/**
|
|
|
|
* Postel's law: consider an "unfollow" notification as a "leave".
|
|
|
|
*/
|
2010-02-20 16:12:43 +00:00
|
|
|
function handleUnfollow()
|
|
|
|
{
|
2010-02-22 17:43:27 +00:00
|
|
|
$this->handleLeave();
|
2010-02-20 16:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A remote user joined our group.
|
2010-02-22 17:43:27 +00:00
|
|
|
* @fixme move permission checks and event call into common code,
|
|
|
|
* currently we're doing the main logic in joingroup action
|
|
|
|
* and so have to repeat it here.
|
2010-02-20 16:12:43 +00:00
|
|
|
*/
|
|
|
|
function handleJoin()
|
|
|
|
{
|
2010-02-22 17:43:27 +00:00
|
|
|
$oprofile = $this->ensureProfile();
|
2014-05-05 18:25:39 +01:00
|
|
|
if (!$oprofile instanceof Ostatus_profile) {
|
2010-09-19 14:17:36 +01:00
|
|
|
// TRANS: Client error.
|
2011-04-10 23:39:27 +01:00
|
|
|
$this->clientError(_m('Cannot read profile to set up group membership.'));
|
2010-02-22 17:43:27 +00:00
|
|
|
}
|
|
|
|
if ($oprofile->isGroup()) {
|
2010-09-19 14:17:36 +01:00
|
|
|
// TRANS: Client error.
|
2011-04-10 23:39:27 +01:00
|
|
|
$this->clientError(_m('Groups cannot join groups.'));
|
2010-02-22 17:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
common_log(LOG_INFO, "Remote profile {$oprofile->uri} joining local group {$this->group->nickname}");
|
|
|
|
$profile = $oprofile->localProfile();
|
|
|
|
|
|
|
|
if ($profile->isMember($this->group)) {
|
|
|
|
// Already a member; we'll take it silently to aid in resolving
|
|
|
|
// inconsistencies on the other side.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Group_block::isBlocked($this->group, $profile)) {
|
2011-04-29 17:59:47 +01:00
|
|
|
// TRANS: Client error displayed when trying to join a group the user is blocked from by a group admin.
|
2010-09-19 14:17:36 +01:00
|
|
|
$this->clientError(_m('You have been blocked from that group by the admin.'), 403);
|
2010-02-22 17:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2011-03-21 21:35:29 +00:00
|
|
|
$profile->joinGroup($this->group);
|
2010-02-22 17:43:27 +00:00
|
|
|
} catch (Exception $e) {
|
2010-09-19 14:17:36 +01:00
|
|
|
// TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
|
2010-02-22 17:43:27 +00:00
|
|
|
$this->serverError(sprintf(_m('Could not join remote user %1$s to group %2$s.'),
|
|
|
|
$oprofile->uri, $this->group->nickname));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A remote user left our group.
|
|
|
|
*/
|
|
|
|
function handleLeave()
|
|
|
|
{
|
|
|
|
$oprofile = $this->ensureProfile();
|
2014-05-05 18:25:39 +01:00
|
|
|
if (!$oprofile instanceof Ostatus_profile) {
|
2011-04-29 17:59:47 +01:00
|
|
|
// TRANS: Client error displayed when group membership cannot be cancelled
|
|
|
|
// TRANS: because the remote profile could not be read.
|
2011-04-10 23:39:27 +01:00
|
|
|
$this->clientError(_m('Cannot read profile to cancel group membership.'));
|
2010-02-22 17:43:27 +00:00
|
|
|
}
|
|
|
|
if ($oprofile->isGroup()) {
|
2011-04-29 17:59:47 +01:00
|
|
|
// TRANS: Client error displayed when trying to have a group join another group.
|
2011-04-10 23:39:27 +01:00
|
|
|
$this->clientError(_m('Groups cannot join groups.'));
|
2010-02-22 17:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
common_log(LOG_INFO, "Remote profile {$oprofile->uri} leaving local group {$this->group->nickname}");
|
|
|
|
$profile = $oprofile->localProfile();
|
|
|
|
|
|
|
|
try {
|
2011-03-21 21:35:29 +00:00
|
|
|
$profile->leaveGroup($this->group);
|
2010-02-22 17:43:27 +00:00
|
|
|
} catch (Exception $e) {
|
2010-09-19 14:17:36 +01:00
|
|
|
// TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
|
2010-02-22 17:43:27 +00:00
|
|
|
$this->serverError(sprintf(_m('Could not remove remote user %1$s from group %2$s.'),
|
|
|
|
$oprofile->uri, $this->group->nickname));
|
|
|
|
}
|
2010-02-20 16:12:43 +00:00
|
|
|
}
|
|
|
|
}
|