]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapifriendships.php
Twitter-compatible API: /friendship/exists always failed - fixed!
[quix0rs-gnu-social.git] / actions / twitapifriendships.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/twitterapi.php');
23
24 class TwitapifriendshipsAction extends TwitterapiAction {
25
26         function create($args, $apidata) {
27                 parent::handle($args);
28
29                 $id = $apidata['api_arg'];
30
31                 $other = $this->get_user($id);
32
33                 if (!$other) {
34                         $this->client_error(_('Could not follow user: User not found.'), 403, $apidata['content-type']);
35                         exit();
36                 }
37                 
38                 $user = $apidata['user'];
39                 
40                 if ($user->isSubscribed($other)) {
41                         $this->client_error("Could not follow user: $other->nickname is already on your list.", 403, $apidata['content-type']);
42                         exit();
43                 }
44                 
45                 $sub = new Subscription();
46                 
47                 $sub->query('BEGIN');
48                 
49                 $sub->subscriber = $user->id;
50                 $sub->subscribed = $other->id;
51                 $sub->created = DB_DataObject_Cast::dateTime(); # current time
52                   
53                 $result = $sub->insert();
54
55                 if (!$result) {
56                         $this->client_error("Could not follow user: $other->nickname.", 400, $apidata['content-type']);                 
57                         exit();
58                 }
59                 
60                 $sub->query('COMMIT');
61                 
62                 mail_subscribe_notify($other, $user);
63
64                 $type = $apidata['content-type'];
65                 $this->init_document($type);
66                 $this->show_profile($other, $type);
67                 $this->end_document($type);
68                 exit();
69         }
70         
71         //destroy
72         //
73         //Discontinues friendship with the user specified in the ID parameter as the authenticating user.  Returns the un-friended user in the requested format when successful.  Returns a string describing the failure condition when unsuccessful. 
74         //
75         //URL: http://twitter.com/friendships/destroy/id.format
76         //
77         //Formats: xml, json
78         //
79         //Parameters:
80         //
81         //* id.  Required.  The ID or screen name of the user with whom to discontinue friendship.  Ex: http://twitter.com/friendships/destroy/12345.json or http://twitter.com/friendships/destroy/bob.xml
82         
83         function destroy($args, $apidata) {
84                 parent::handle($args);
85                 $id = $apidata['api_arg'];
86
87                 # We can't subscribe to a remote person, but we can unsub
88                 
89                 $other = $this->get_profile($id);
90                 $user = $apidata['user'];
91                 
92                 $sub = new Subscription();
93                 $sub->subscriber = $user->id;
94                 $sub->subscribed = $other->id;
95                 
96                 if ($sub->find(TRUE)) {
97                         $sub->query('BEGIN');
98                         $sub->delete();
99                         $sub->query('COMMIT');
100                 } else {
101                         $this->client_error(_('You are not friends with the specified user.'), 403, $apidata['content-type']);                  
102                         exit();
103                 }
104
105                 $type = $apidata['content-type'];
106                 $this->init_document($type);    
107                 $this->show_profile($other, $type);
108                 $this->end_document($type);
109                 exit();
110         }
111
112         //      Tests if a friendship exists between two users.
113         //        
114         //        
115         //        URL: http://twitter.com/friendships/exists.format
116         //      
117         //      Formats: xml, json, none
118         //        
119         //        Parameters:
120         //      
121         //          * user_a.  Required.  The ID or screen_name of the first user to test friendship for.
122         //            * user_b.  Required.  The ID or screen_name of the second user to test friendship for.
123         //        * Ex: http://twitter.com/friendships/exists.xml?user_a=alice&user_b=bob
124         
125         function exists($args, $apidata) {
126                 parent::handle($args);
127                 
128                 
129                 $user_a_id = $this->trimmed('user_a');
130                 $user_b_id = $this->trimmed('user_b');
131                 
132                 $user_a = $this->get_user($user_a_id);
133                 $user_b = $this->get_user($user_b_id);
134                 
135                 if (!$user_a || !$user_b) {
136                         $this->client_error(_('Two user ids or screen_names must be supplied.'), 400, $apidata['content-type']);
137                         exit();
138                 }
139                 
140                 if ($user_a->isSubscribed($user_b)) {
141                         $result = 'true';
142                 } else {
143                         $result = 'false';
144                 }
145                 
146                 switch ($apidata['content-type']) {
147                  case 'xml':
148                         $this->init_document('xml');
149                         common_element('friends', NULL, $result);
150                         $this->end_document('xml');
151                         break;
152                  case 'json':
153                         $this->init_document('json');
154                         print json_encode($result);
155                         $this->end_document('json');
156                         break;
157                  default:
158                         print $result;  // Really? --Zach
159                         break;
160                 }
161                 
162                 exit();
163         }
164
165         function get_profile($id) {
166                 if (is_numeric($id)) {
167                         return Profile::staticGet($id);
168                 } else {
169                         $user = User::staticGet('nickname', $id);                       
170                         if ($user) {
171                                 return $user->getProfile();
172                         } else {
173                                 return NULL;
174                         }
175                 }
176         }
177         
178         function get_user($id) {
179                 if (is_numeric($id)) {
180                         return User::staticGet($id);
181                 } else {
182                         return User::staticGet('nickname', $id);
183                 }
184         }
185 }