groupsalmon.php 6.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?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/>.
 */

if (!defined('STATUSNET')) {
    exit(1);
}

Siebrand Mazeland's avatar
Siebrand Mazeland committed
24 25 26 27
/**
 * @package OStatusPlugin
 * @author James Walker <james@status.net>
 */
28 29 30 31 32 33 34 35 36 37 38
class GroupsalmonAction extends SalmonAction
{
    var $group = null;

    function prepare($args)
    {
        parent::prepare($args);

        $id = $this->trimmed('id');

        if (!$id) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
39 40
            // TRANS: Client error.
            $this->clientError(_m('No ID.'));
41 42
        }

43
        $this->group = User_group::getKV('id', $id);
44 45

        if (empty($this->group)) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
46 47
            // TRANS: Client error.
            $this->clientError(_m('No such group.'));
48 49
        }

50 51 52

        $this->target = $this->group;

53
        $oprofile = Ostatus_profile::getKV('group_id', $id);
54
        if ($oprofile) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
55
            // TRANS: Client error.
56
            $this->clientError(_m('Cannot accept remote posts for a remote group.'));
57 58
        }

59 60 61 62 63 64 65 66
        return true;
    }

    /**
     * We've gotten a post event on the Salmon backchannel, probably a reply.
     */
    function handlePost()
    {
Zach Copley's avatar
Zach Copley committed
67
        // @fixme process all objects?
68
        switch ($this->activity->objects[0]->type) {
69 70 71 72 73 74 75
        case ActivityObject::ARTICLE:
        case ActivityObject::BLOGENTRY:
        case ActivityObject::NOTE:
        case ActivityObject::STATUS:
        case ActivityObject::COMMENT:
            break;
        default:
Siebrand Mazeland's avatar
Siebrand Mazeland committed
76
            // TRANS: Client exception.
77
            throw new ClientException('Cannot handle that kind of post.');
78 79 80
        }

        // Notice must be to the attention of this group
81
        if (empty($this->activity->context->attention)) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
82
            // TRANS: Client exception.
83 84 85
            throw new ClientException("Not to the attention of anyone.");
        } else {
            $uri = common_local_url('groupbyid', array('id' => $this->group->id));
86 87

            if (!array_key_exists($uri, $this->activity->context->attention)) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
88
                // TRANS: Client exception.
89 90 91 92 93
                throw new ClientException("Not to the attention of this group.");
            }
        }

        $profile = $this->ensureProfile();
94
        $this->saveNotice();
95 96 97 98 99 100 101
    }

    /**
     * We've gotten a follow/subscribe notification from a remote user.
     * Save a subscription relationship for them.
     */

102 103 104
    /**
     * Postel's law: consider a "follow" notification as a "join".
     */
105 106
    function handleFollow()
    {
107
        $this->handleJoin();
108 109
    }

110 111 112
    /**
     * Postel's law: consider an "unfollow" notification as a "leave".
     */
113 114
    function handleUnfollow()
    {
115
        $this->handleLeave();
116 117 118 119
    }

    /**
     * A remote user joined our group.
120 121 122
     * @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.
123 124 125
     */
    function handleJoin()
    {
126 127
        $oprofile = $this->ensureProfile();
        if (!$oprofile) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
128
            // TRANS: Client error.
129
            $this->clientError(_m('Cannot read profile to set up group membership.'));
130 131
        }
        if ($oprofile->isGroup()) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
132
            // TRANS: Client error.
133
            $this->clientError(_m('Groups cannot join groups.'));
134 135 136 137 138 139 140 141 142 143 144 145
        }

        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)) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
146
            // TRANS: Client error displayed when trying to join a group the user is blocked from by a group admin.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
147
            $this->clientError(_m('You have been blocked from that group by the admin.'), 403);
148 149 150 151
            return false;
        }

        try {
152
            $profile->joinGroup($this->group);
153
        } catch (Exception $e) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
154
            // TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
155 156 157 158 159 160 161 162 163 164 165 166
            $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();
        if (!$oprofile) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
167 168
            // TRANS: Client error displayed when group membership cannot be cancelled
            // TRANS: because the remote profile could not be read.
169
            $this->clientError(_m('Cannot read profile to cancel group membership.'));
170 171
        }
        if ($oprofile->isGroup()) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
172
            // TRANS: Client error displayed when trying to have a group join another group.
173
            $this->clientError(_m('Groups cannot join groups.'));
174 175 176 177 178 179
        }

        common_log(LOG_INFO, "Remote profile {$oprofile->uri} leaving local group {$this->group->nickname}");
        $profile = $oprofile->localProfile();

        try {
180
            $profile->leaveGroup($this->group);
181
        } catch (Exception $e) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
182
            // TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
183 184 185 186
            $this->serverError(sprintf(_m('Could not remove remote user %1$s from group %2$s.'),
                                       $oprofile->uri, $this->group->nickname));
            return;
        }
187 188
    }
}