apifriendshipscreate.php 3.6 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
 *
 * Subscribe to a user via the 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 follow (subscribe) the user specified in
 * the ID parameter.  Returns the befriended 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 ApiFriendshipsCreateAction extends ApiAuthAction
{
49 50
    protected $needPost = true;

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

    /**
     * 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
        if (!in_array($this->format, array('xml', 'json'))) {
82 83
            // TRANS: Client error displayed when coming across a non-supported API method.
            $this->clientError(_('API method not found.'), 404);
84 85
        }

86
        if (empty($this->other)) {
87 88
            // TRANS: Client error displayed when trying follow who's profile could not be found.
            $this->clientError(_('Could not follow user: profile not found.'), 403);
89 90
        }

91
        if ($this->scoped->isSubscribed($this->other)) {
92
            $errmsg = sprintf(
Siebrand Mazeland's avatar
Siebrand Mazeland committed
93 94
                // TRANS: Client error displayed when trying to follow a user that's already being followed.
                // TRANS: %s is the nickname of the user that is already being followed.
95 96 97
                _('Could not follow user: %s is already on your list.'),
                $this->other->nickname
            );
98
            $this->clientError($errmsg, 403);
99 100
        }

mmn's avatar
mmn committed
101
        try {
102
            Subscription::start($this->scoped, $this->other);
103 104
        } catch (AlreadyFulfilledException $e) {
            $this->clientError($e->getMessage(), 409);
105 106
        }

107 108 109
        $this->initDocument($this->format);
        $this->showProfile($this->other, $this->format);
        $this->endDocument($this->format);
110 111
    }
}