]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apifriendshipsshow.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / actions / apifriendshipsshow.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show information about the relationship between two users
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  API
23  * @package   StatusNet
24  * @author    Dan Moore <dan@moore.cx>
25  * @author    Evan Prodromou <evan@status.net>
26  * @author    Zach Copley <zach@status.net>
27  * @copyright 2009 StatusNet, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29  * @link      http://status.net/
30  */
31
32 if (!defined('GNUSOCIAL')) { exit(1); }
33
34 /**
35  * Outputs detailed information about the relationship between two users
36  *
37  * @category API
38  * @package  StatusNet
39  * @author   Dan Moore <dan@moore.cx>
40  * @author   Evan Prodromou <evan@status.net>
41  * @author   Zach Copley <zach@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  */
45 class ApiFriendshipsShowAction extends ApiBareAuthAction
46 {
47     var $source = null;
48     var $target = null;
49
50     /**
51      * Take arguments for running
52      *
53      * @param array $args $_REQUEST args
54      *
55      * @return boolean success flag
56      */
57     protected function prepare(array $args=array())
58     {
59         parent::prepare($args);
60
61         $source_id          = (int)$this->trimmed('source_id');
62         $source_screen_name = $this->trimmed('source_screen_name');
63         $target_id          = (int)$this->trimmed('target_id');
64         $target_screen_name = $this->trimmed('target_screen_name');
65
66         if (!empty($source_id)) {
67             $this->source = User::getKV($source_id);
68         } elseif (!empty($source_screen_name)) {
69             $this->source = User::getKV('nickname', $source_screen_name);
70         } else {
71             $this->source = $this->auth_user;
72         }
73
74         if (!empty($target_id)) {
75             $this->target = User::getKV($target_id);
76         } elseif (!empty($target_screen_name)) {
77             $this->target = User::getKV('nickname', $target_screen_name);
78         }
79
80         return true;
81     }
82
83     /**
84      * Determines whether this API resource requires auth.  Overloaded to look
85      * return true in case source_id and source_screen_name are both empty
86      *
87      * @return boolean true or false
88      */
89     function requiresAuth()
90     {
91         if (common_config('site', 'private')) {
92             return true;
93         }
94
95         $source_id          = $this->trimmed('source_id');
96         $source_screen_name = $this->trimmed('source_screen_name');
97
98         if (empty($source_id) && empty($source_screen_name)) {
99             return true;
100         }
101
102         return false;
103     }
104
105     /**
106      * Handle the request
107      *
108      * Check the format and show the user info
109      *
110      * @return void
111      */
112     protected function handle()
113     {
114         parent::handle();
115
116         if (!in_array($this->format, array('xml', 'json'))) {
117             // TRANS: Client error displayed when coming across a non-supported API method.
118             $this->clientError(_('API method not found.'), 404);
119         }
120
121         if (empty($this->source)) {
122             $this->clientError(
123                 // TRANS: Client error displayed when a source user could not be determined showing friendship.
124                 _('Could not determine source user.'),
125                 404
126              );
127         }
128
129         if (empty($this->target)) {
130             $this->clientError(
131                 // TRANS: Client error displayed when a target user could not be determined showing friendship.
132                 _('Could not find target user.'),
133                 404
134             );
135         }
136
137         $result = $this->twitterRelationshipArray($this->source, $this->target);
138
139         switch ($this->format) {
140         case 'xml':
141             $this->initDocument('xml');
142             $this->showTwitterXmlRelationship($result[relationship]);
143             $this->endDocument('xml');
144             break;
145         case 'json':
146             $this->initDocument('json');
147             print json_encode($result);
148             $this->endDocument('json');
149             break;
150         default:
151             break;
152         }
153     }
154
155     /**
156      * Return true if read only.
157      *
158      * MAY override
159      *
160      * @param array $args other arguments
161      *
162      * @return boolean is read only action?
163      */
164
165     function isReadOnly(array $args=array())
166     {
167         return true;
168     }
169 }