apifriendshipsdestroy.php 3.48 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
 *
 * Unsubscribe to a user via API
 *
 * PHP version 5
 *
 * LICENCE: 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/>.
 *
 * @category  API
 * @package   StatusNet
Zach Copley's avatar
Zach Copley committed
24 25
 * @author    Dan Moore <dan@moore.cx>
 * @author    Evan Prodromou <evan@status.net>
26
 * @author    Zach Copley <zach@status.net>
27
 * @copyright 2009-2010 StatusNet, Inc.
28 29 30 31
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://status.net/
 */

32
if (!defined('GNUSOCIAL')) { exit(1); }
33 34 35 36 37 38 39 40

/**
 * Allows the authenticating users to unfollow (unsubscribe) the user specified in
 * the ID parameter.  Returns the unfollowed user in the requested format when
 * successful.  Returns a string describing the failure condition when unsuccessful.
 *
 * @category API
 * @package  StatusNet
Zach Copley's avatar
Zach Copley committed
41 42
 * @author   Dan Moore <dan@moore.cx>
 * @author   Evan Prodromou <evan@status.net>
43 44 45 46 47 48
 * @author   Zach Copley <zach@status.net>
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link     http://status.net/
 */
class ApiFriendshipsDestroyAction extends ApiAuthAction
{
49 50 51
    protected $needPost = true;

    protected $other = null;
52 53 54 55 56 57 58 59 60

    /**
     * Take arguments for running
     *
     * @param array $args $_REQUEST args
     *
     * @return boolean success flag
     *
     */
61
    protected function prepare(array $args=array())
62 63 64
    {
        parent::prepare($args);

65
        $this->other = $this->getTargetProfile($this->arg('id'));
66 67 68 69 70 71 72 73 74 75 76

        return true;
    }

    /**
     * Handle the request
     *
     * Check the format and show the user info
     *
     * @return void
     */
77
    protected function handle()
78
    {
79
        parent::handle();
80

81 82
        if (!in_array($this->format, array('xml', 'json'))) {
            $this->clientError(
83
                // TRANS: Client error displayed when coming across a non-supported API method.
84
                _('API method not found.'),
85
                404
86 87 88
            );
        }

89
        if (!$this->other instanceof Profile) {
90
            $this->clientError(
Siebrand Mazeland's avatar
Siebrand Mazeland committed
91
                // TRANS: Client error displayed when trying to unfollow a user that cannot be found.
92
                _('Could not unfollow user: User not found.'),
93
                403
94 95 96
            );
        }

97
        // Don't allow unsubscribing from yourself!
98

99
        if ($this->scoped->id == $this->other->id) {
100
            $this->clientError(
Siebrand Mazeland's avatar
Siebrand Mazeland committed
101
                // TRANS: Client error displayed when trying to unfollow self.
102
                _("You cannot unfollow yourself."),
103
                403
104 105 106
            );
        }

107
        // throws an exception on error
108
        Subscription::cancel($this->scoped, $this->other);
109

110 111 112
        $this->initDocument($this->format);
        $this->showProfile($this->other, $this->format);
        $this->endDocument($this->format);
113 114
    }
}