]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapifriendships.php
Twitter-compatible API - running all strings through gettext() now
[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 is_readonly() {
27                 
28                 static $write_methods = array(  'create',
29                                                                                 'destroy');
30                 
31                 $cmdtext = explode('.', $this->arg('method'));          
32                 
33                 if (in_array($cmdtext[0], $write_methods)) {                    
34                         return false;
35                 }
36                                 
37                 return true;
38         }
39
40         function create($args, $apidata) {
41                 parent::handle($args);
42
43                 $id = $apidata['api_arg'];
44
45                 $other = $this->get_user($id);
46
47                 if (!$other) {
48                         $this->client_error(_('Could not follow user: User not found.'), 403, $apidata['content-type']);
49                         exit();
50                 }
51                 
52                 $user = $apidata['user'];
53                 
54                 if ($user->isSubscribed($other)) {
55                         $errmsg = sprintf(_('Could not follow user: %s is already on your list.'), $other->nickname);
56                         $this->client_error($errmsg, 403, $apidata['content-type']);
57                         exit();
58                 }
59                 
60                 $sub = new Subscription();
61                 
62                 $sub->query('BEGIN');
63                 
64                 $sub->subscriber = $user->id;
65                 $sub->subscribed = $other->id;
66                 $sub->created = DB_DataObject_Cast::dateTime(); # current time
67                   
68                 $result = $sub->insert();
69
70                 if (!$result) {
71                         $errmsg = sprintf(_('Could not follow user: %s is already on your list.'), $other->nickname);
72                         $this->client_error($errmsg, 400, $apidata['content-type']);                    
73                         exit();
74                 }
75                 
76                 $sub->query('COMMIT');
77                 
78                 mail_subscribe_notify($other, $user);
79
80                 $type = $apidata['content-type'];
81                 $this->init_document($type);
82                 $this->show_profile($other, $type);
83                 $this->end_document($type);
84                 exit();
85         }
86         
87         //destroy
88         //
89         //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. 
90         //
91         //URL: http://twitter.com/friendships/destroy/id.format
92         //
93         //Formats: xml, json
94         //
95         //Parameters:
96         //
97         //* 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
98         
99         function destroy($args, $apidata) {
100                 parent::handle($args);
101                 $id = $apidata['api_arg'];
102
103                 # We can't subscribe to a remote person, but we can unsub
104                 
105                 $other = $this->get_profile($id);
106                 $user = $apidata['user'];
107                 
108                 $sub = new Subscription();
109                 $sub->subscriber = $user->id;
110                 $sub->subscribed = $other->id;
111                 
112                 if ($sub->find(TRUE)) {
113                         $sub->query('BEGIN');
114                         $sub->delete();
115                         $sub->query('COMMIT');
116                 } else {
117                         $this->client_error(_('You are not friends with the specified user.'), 403, $apidata['content-type']);                  
118                         exit();
119                 }
120
121                 $type = $apidata['content-type'];
122                 $this->init_document($type);    
123                 $this->show_profile($other, $type);
124                 $this->end_document($type);
125                 exit();
126         }
127
128         //      Tests if a friendship exists between two users.
129         //        
130         //        
131         //        URL: http://twitter.com/friendships/exists.format
132         //      
133         //      Formats: xml, json, none
134         //        
135         //        Parameters:
136         //      
137         //          * user_a.  Required.  The ID or screen_name of the first user to test friendship for.
138         //            * user_b.  Required.  The ID or screen_name of the second user to test friendship for.
139         //        * Ex: http://twitter.com/friendships/exists.xml?user_a=alice&user_b=bob
140         
141         function exists($args, $apidata) {
142                 parent::handle($args);
143                 
144                 
145                 $user_a_id = $this->trimmed('user_a');
146                 $user_b_id = $this->trimmed('user_b');
147                 
148                 $user_a = $this->get_user($user_a_id);
149                 $user_b = $this->get_user($user_b_id);
150                 
151                 if (!$user_a || !$user_b) {
152                         $this->client_error(_('Two user ids or screen_names must be supplied.'), 400, $apidata['content-type']);
153                         exit();
154                 }
155                 
156                 if ($user_a->isSubscribed($user_b)) {
157                         $result = 'true';
158                 } else {
159                         $result = 'false';
160                 }
161                 
162                 switch ($apidata['content-type']) {
163                  case 'xml':
164                         $this->init_document('xml');
165                         common_element('friends', NULL, $result);
166                         $this->end_document('xml');
167                         break;
168                  case 'json':
169                         $this->init_document('json');
170                         print json_encode($result);
171                         $this->end_document('json');
172                         break;
173                  default:
174                         print $result;  // Really? --Zach
175                         break;
176                 }
177                 
178                 exit();
179         }
180
181         function get_profile($id) {
182                 if (is_numeric($id)) {
183                         return Profile::staticGet($id);
184                 } else {
185                         $user = User::staticGet('nickname', $id);                       
186                         if ($user) {
187                                 return $user->getProfile();
188                         } else {
189                                 return NULL;
190                         }
191                 }
192         }
193         
194         function get_user($id) {
195                 if (is_numeric($id)) {
196                         return User::staticGet($id);
197                 } else {
198                         return User::staticGet('nickname', $id);
199                 }
200         }
201 }