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