3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) {
24 require_once(INSTALLDIR.'/lib/twitterapi.php');
26 class TwitapifavoritesAction extends TwitterapiAction
29 function favorites($args, $apidata)
31 parent::handle($args);
33 $this->auth_user = $apidata['user'];
34 $user = $this->get_user($apidata['api_arg'], $apidata);
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);
42 $this->clientError('Not Found', 404, $apidata['content-type']);
46 $profile = $user->getProfile();
48 $sitename = common_config('site', 'name');
49 $title = sprintf(_('%s / Favorites from %s'), $sitename,
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);
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');
64 if (!empty($this->auth_user) && $this->auth_user->id == $user->id) {
65 $notice = $user->favoriteNotices(($page-1)*$count, $count, true);
67 $notice = $user->favoriteNotices(($page-1)*$count, $count, false);
70 switch($apidata['content-type']) {
72 $this->show_xml_timeline($notice);
75 $this->show_rss_timeline($notice, $title, $link, $subtitle);
78 if (isset($apidata['api_arg'])) {
79 $selfuri = $selfuri = common_root_url() .
80 'api/favorites/' . $apidata['api_arg'] . '.atom';
82 $selfuri = $selfuri = common_root_url() .
85 $this->show_atom_timeline($notice, $title, $id, $link,
86 $subtitle, null, $selfuri);
89 $this->show_json_timeline($notice);
92 $this->clientError(_('API method not found!'), $code = 404);
97 function create($args, $apidata)
99 parent::handle($args);
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']);
108 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
109 $this->clientError(_('API method not found!'), $code = 404);
113 $user = $apidata['user']; // Always the auth user
114 $notice_id = $apidata['api_arg'];
115 $notice = Notice::staticGet($notice_id);
117 if (empty($notice)) {
118 $this->clientError(_('No status found with that ID.'),
119 404, $apidata['content-type']);
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']);
130 $fave = Fave::addNew($user, $notice);
133 $this->clientError(_('Could not create favorite.'));
137 $this->notify($fave, $notice, $user);
138 $user->blowFavesCache();
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);
148 function destroy($args, $apidata)
150 parent::handle($args);
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']);
159 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
160 $this->clientError(_('API method not found!'), $code = 404);
164 $user = $apidata['user']; // Always the auth user
165 $notice_id = $apidata['api_arg'];
166 $notice = Notice::staticGet($notice_id);
168 if (empty($notice)) {
169 $this->clientError(_('No status found with that ID.'),
170 404, $apidata['content-type']);
175 $fave->user_id = $this->id;
176 $fave->notice_id = $notice->id;
178 if (!$fave->find(true)) {
179 $this->clientError(_('That status is not a favorite!'),
180 403, $apidata['content-type']);
184 $result = $fave->delete();
187 common_log_db_error($fave, 'DELETE', __FILE__);
188 $this->clientError(_('Could not delete favorite.'), 404);
192 $user->blowFavesCache();
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);
202 // XXX: these two funcs swiped from faves.
203 // Maybe put in util.php, or some common base class?
205 function notify($fave, $notice, $user)
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);