]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapifriendships.php
eaef6966c8084d1b8f2ac6b0e192323966825397
[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(_('No such user'));
35                         exit();
36                         return;
37                 }
38                 
39                 $user = $apidata['user'];
40                 
41                 if ($user->isSubscribed($other)) {
42                         $this->client_error(_('Already subscribed.'));
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->server_error(_('Could not subscribe'));
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->fetch(TRUE)) {
100                         $sub->query('BEGIN');
101                         $sub->delete();
102                         $sub->query('COMMIT');
103                 } else {
104                         $this->client_error(_('Not subscribed'));
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                 $user_a_id = $this->trimmed('user_a');
131                 $user_b_id = $this->trimmed('user_b');
132                 $user_a = $this->get_profile($user_a_id);
133                 $user_b = $this->get_profile($user_b_id);
134                 
135                 if (!$user_a || !$user_b) {
136                         $this->client_error(_('No such user'));
137                         return;
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                         common_start_xml();
149                         common_element('friends', NULL, $result);
150                         common_end_xml();
151                         break;
152                  case 'json':
153                         print json_encode($result);
154                         print "\n";
155                         break;
156                  default:
157                         print $result;
158                         break;
159                 }
160                 
161         }
162
163         function get_profile($id) {
164                 if (is_numeric($id)) {
165                         return Profile::staticGet($id);
166                 } else {
167                         $user = User::staticGet('nickname', $id);
168                         if ($user) {
169                                 return $user->getProfile();
170                         } else {
171                                 return NULL;
172                         }
173                 }
174         }
175         
176         function get_user($id) {
177                 if (is_numeric($id)) {
178                         return User::staticGet($id);
179                 } else {
180                         return User::staticGet('nickname', $id);
181                 }
182         }
183 }