]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapifriendships.php
beware of shadows
[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 = $this->trimmed('id');
30
31                 $other = $this->get_user($id);
32
33                 if (!$other) {
34                         $this->client_error(_('No such user'));
35                         return;
36                 }
37                 
38                 $user = $apidata['user'];
39                 
40                 if ($user->isSubscribed($other)) {
41                         $this->client_error(_('Already subscribed.'));
42                         return;
43                 }
44                 
45                 $sub = new Subscription();
46                 $sub->subscriber = $user->id;
47                 $sub->subscribed = $other->id;
48
49                 $result = $sub->insert();
50
51                 if (!$result) {
52                         $this->server_error(_('Could not subscribe'));
53                         return;
54                 }
55                 
56                 mail_subscribe_notify($other, $user);
57
58                 $this->show_profile($other);
59                 exit();
60         }
61         
62         //destroy
63         //
64         //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. 
65         //
66         //URL: http://twitter.com/friendships/destroy/id.format
67         //
68         //Formats: xml, json
69         //
70         //Parameters:
71         //
72         //* 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
73         
74         function destroy($args, $apidata) {
75                 parent::handle($args);
76                 $id = $this->trimmed('id');
77
78                 # We can't subscribe to a remote person, but we can unsub
79                 
80                 $other = $this->get_profile($id);
81                 $user = $apidata['user'];
82                 
83                 $sub = new Subscription();
84                 $sub->subscriber = $user->id;
85                 $sub->subscribed = $other->id;
86                 
87                 if ($sub->fetch(TRUE)) {
88                         $sub->delete();
89                 }
90
91                 $this->show_profile($other);
92                 exit();
93         }
94
95         //      Tests if a friendship exists between two users.
96         //        
97         //        
98         //        URL: http://twitter.com/friendships/exists.format
99         //      
100         //      Formats: xml, json, none
101         //        
102         //        Parameters:
103         //      
104         //          * user_a.  Required.  The ID or screen_name of the first user to test friendship for.
105         //            * user_b.  Required.  The ID or screen_name of the second user to test friendship for.
106         //        * Ex: http://twitter.com/friendships/exists.xml?user_a=alice&user_b=bob
107         
108         function exists($args, $apidata) {
109                 parent::handle($args);
110                 $user_a_id = $this->trimmed('user_a');
111                 $user_b_id = $this->trimmed('user_b');
112                 $user_a = $this->get_profile($user_a_id);
113                 $user_b = $this->get_profile($user_b_id);
114                 
115                 if (!$user_a || !$user_b) {
116                         $this->client_error(_('No such user'));
117                         return;
118                 }
119                 
120                 if ($user_a->isSubscribed($user_b)) {
121                         $result = 'true';
122                 } else {
123                         $result = 'false';
124                 }
125                 
126                 switch ($apidata['content-type']) {
127                  case 'xml':
128                         common_start_xml();
129                         common_element('friends', NULL, $result);
130                         common_end_xml();
131                         break;
132                  case 'json':
133                         print json_encode($result);
134                         print "\n";
135                         break;
136                  default:
137                         print $result;
138                         break;
139                 }
140                 
141         }
142
143         function get_profile($id) {
144                 if (is_numeric($id)) {
145                         return Profile::staticGet($id);
146                 } else {
147                         $user = User::staticGet('nickname', $id);
148                         return $user->getProfile();
149                 }
150         }
151         
152         function get_user($id) {
153                 if (is_numeric($id)) {
154                         return User::staticGet($id);
155                 } else {
156                         $user = User::staticGet('nickname', $id);
157                         return $user->getProfile();
158                 }
159         }
160 }