apifriendshipsexists.php 3.41 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
 *
 * Show whether there is a friendship between two users
 *
 * 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

/**
Zach Copley's avatar
Zach Copley committed
35
 * Tests for the existence of friendship between two users. Will return true if
36 37 38 39
 * user_a follows user_b, otherwise will return false.
 *
 * @category API
 * @package  StatusNet
Zach Copley's avatar
Zach Copley committed
40 41
 * @author   Dan Moore <dan@moore.cx>
 * @author   Evan Prodromou <evan@status.net>
42 43 44 45
 * @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/
 */
46
class ApiFriendshipsExistsAction extends ApiPrivateAuthAction
47
{
48 49
    var $profile_a = null;
    var $profile_b = null;
50 51 52 53 54 55 56 57

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

62 63
        $this->profile_a = $this->getTargetProfile($this->trimmed('user_a'));
        $this->profile_b = $this->getTargetProfile($this->trimmed('user_b'));
64 65 66 67 68 69 70 71 72 73 74

        return true;
    }

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

79
        if (empty($this->profile_a) || empty($this->profile_b)) {
80
            $this->clientError(
Siebrand Mazeland's avatar
Siebrand Mazeland committed
81
                // TRANS: Client error displayed when supplying invalid parameters to an API call checking if a friendship exists.
82
                _('Two valid IDs or nick names must be supplied.'),
83
                400
84 85 86
            );
        }

87
        $result = Subscription::exists($this->profile_a, $this->profile_b);
88 89 90

        switch ($this->format) {
        case 'xml':
91
            $this->initDocument('xml');
92
            $this->element('friends', null, $result);
93
            $this->endDocument('xml');
94 95
            break;
        case 'json':
96
            $this->initDocument('json');
97
            print json_encode($result);
98
            $this->endDocument('json');
99 100 101 102 103 104
            break;
        default:
            break;
        }
    }

105 106 107 108 109 110 111 112 113 114 115 116 117
    /**
     * Return true if read only.
     *
     * MAY override
     *
     * @param array $args other arguments
     *
     * @return boolean is read only action?
     */
    function isReadOnly($args)
    {
        return true;
    }
118
}