]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapifavorites.php
a distributed -> the distributed
[quix0rs-gnu-social.git] / actions / twitapifavorites.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, 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')) {
21     exit(1);
22 }
23
24 require_once(INSTALLDIR.'/lib/twitterapi.php');
25
26 class TwitapifavoritesAction extends TwitterapiAction
27 {
28
29     function favorites($args, $apidata)
30     {
31         parent::handle($args);
32
33         $this->auth_user = $apidata['user'];
34         $user = $this->get_user($apidata['api_arg'], $apidata);
35
36         if (empty($user)) {
37         if ($apidata['content-type'] == 'xml') {
38             $this->show_single_xml_status($notice);
39         } elseif ($apidata['content-type'] == 'json') {
40             $this->show_single_json_status($notice);
41         }
42             $this->clientError('Not Found', 404, $apidata['content-type']);
43             return;
44         }
45
46         $profile = $user->getProfile();
47
48         $sitename   = common_config('site', 'name');
49         $title      = sprintf(_('%s / Favorites from %s'), $sitename,
50             $user->nickname);
51         $taguribase = common_config('integration', 'taguri');
52         $id         = "tag:$taguribase:Favorites:".$user->id;
53         $link       = common_local_url('favorites',
54             array('nickname' => $user->nickname));
55         $subtitle   = sprintf(_('%s updates favorited by %s / %s.'), $sitename,
56             $profile->getBestName(), $user->nickname);
57
58         $page     = (int)$this->arg('page', 1);
59         $count    = (int)$this->arg('count', 20);
60         $max_id   = (int)$this->arg('max_id', 0);
61         $since_id = (int)$this->arg('since_id', 0);
62         $since    = $this->arg('since');
63
64         if (!empty($this->auth_user) && $this->auth_user->id == $user->id) {
65             $notice = $user->favoriteNotices(($page-1)*$count, $count, true);
66         } else {
67             $notice = $user->favoriteNotices(($page-1)*$count, $count, false);
68         }
69
70         switch($apidata['content-type']) {
71         case 'xml':
72             $this->show_xml_timeline($notice);
73             break;
74         case 'rss':
75             $this->show_rss_timeline($notice, $title, $link, $subtitle);
76             break;
77         case 'atom':
78             if (isset($apidata['api_arg'])) {
79                  $selfuri = $selfuri = common_root_url() .
80                      'api/favorites/' . $apidata['api_arg'] . '.atom';
81             } else {
82                  $selfuri = $selfuri = common_root_url() .
83                   'api/favorites.atom';
84             }
85             $this->show_atom_timeline($notice, $title, $id, $link,
86                 $subtitle, null, $selfuri);
87             break;
88         case 'json':
89             $this->show_json_timeline($notice);
90             break;
91         default:
92             $this->clientError(_('API method not found!'), $code = 404);
93         }
94
95     }
96
97     function create($args, $apidata)
98     {
99         parent::handle($args);
100
101         // Check for RESTfulness
102         if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
103             $this->clientError(_('This method requires a POST or DELETE.'),
104                 400, $apidata['content-type']);
105             return;
106         }
107
108         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
109             $this->clientError(_('API method not found!'), $code = 404);
110             return;
111         }
112
113         $user      = $apidata['user']; // Always the auth user
114         $notice_id = $apidata['api_arg'];
115         $notice    = Notice::staticGet($notice_id);
116
117         if (empty($notice)) {
118             $this->clientError(_('No status found with that ID.'),
119                 404, $apidata['content-type']);
120             return;
121         }
122
123         // XXX: Twitter lets you fave things repeatedly via api.
124         if ($user->hasFave($notice)) {
125             $this->clientError(_('This status is already a favorite!'),
126                 403, $apidata['content-type']);
127             return;
128         }
129
130         $fave = Fave::addNew($user, $notice);
131
132         if (empty($fave)) {
133             $this->clientError(_('Could not create favorite.'));
134             return;
135         }
136
137         $this->notify($fave, $notice, $user);
138         $user->blowFavesCache();
139
140         if ($apidata['content-type'] == 'xml') {
141             $this->show_single_xml_status($notice);
142         } elseif ($apidata['content-type'] == 'json') {
143             $this->show_single_json_status($notice);
144         }
145
146     }
147
148     function destroy($args, $apidata)
149     {
150         parent::handle($args);
151
152         // Check for RESTfulness
153         if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
154             $this->clientError(_('This method requires a POST or DELETE.'),
155                 400, $apidata['content-type']);
156             return;
157         }
158
159         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
160             $this->clientError(_('API method not found!'), $code = 404);
161             return;
162         }
163
164         $user      = $apidata['user']; // Always the auth user
165         $notice_id = $apidata['api_arg'];
166         $notice    = Notice::staticGet($notice_id);
167
168         if (empty($notice)) {
169             $this->clientError(_('No status found with that ID.'),
170                 404, $apidata['content-type']);
171             return;
172         }
173
174         $fave            = new Fave();
175         $fave->user_id   = $this->id;
176         $fave->notice_id = $notice->id;
177
178         if (!$fave->find(true)) {
179             $this->clientError(_('That status is not a favorite!'),
180                 403, $apidata['content-type']);
181             return;
182         }
183
184         $result = $fave->delete();
185
186         if (!$result) {
187             common_log_db_error($fave, 'DELETE', __FILE__);
188             $this->clientError(_('Could not delete favorite.'), 404);
189             return;
190         }
191
192         $user->blowFavesCache();
193
194         if ($apidata['content-type'] == 'xml') {
195             $this->show_single_xml_status($notice);
196         } elseif ($apidata['content-type'] == 'json') {
197             $this->show_single_json_status($notice);
198         }
199
200     }
201
202     // XXX: these two funcs swiped from faves.
203     // Maybe put in util.php, or some common base class?
204
205     function notify($fave, $notice, $user)
206     {
207         $other = User::staticGet('id', $notice->profile_id);
208         if ($other && $other->id != $user->id) {
209             if ($other->email && $other->emailnotifyfav) {
210                 mail_notify_fave($other, $user, $notice);
211             }
212             # XXX: notify by IM
213             # XXX: notify by SMS
214         }
215     }
216 }