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