3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, 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('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/twitterapi.php');
24 class TwitapifavoritesAction extends TwitterapiAction {
26 function is_readonly() {
28 static $write_methods = array('favorites');
30 $cmdtext = explode('.', $this->arg('method'));
32 if (in_array($cmdtext[0], $write_methods)) {
39 function favorites($args, $apidata) {
40 parent::handle($args);
44 // function was called with an argument /favorites/api_arg.format
45 if (isset($apidata['api_arg'])) {
47 if (is_numeric($apidata['api_arg'])) {
48 $user = User::staticGet($apidata['api_arg']);
50 $nickname = common_canonical_nickname($apidata['api_arg']);
51 $user = User::staticGet('nickname', $nickname);
55 // if no user was specified, then we'll use the authenticated user
56 $user = $apidata['user'];
60 // Set the user to be the auth user if asked-for can't be found
61 // honestly! This is what Twitter does, I swear --Zach
62 $user = $apidata['user'];
65 $profile = $user->getProfile();
68 common_server_error(_('User has no profile.'));
72 $page = $this->arg('page');
82 $notice = $user->favoriteNotices((($page-1)*20), $count);
85 common_server_error(_('Could not retrieve favorite notices.'));
89 $sitename = common_config('site', 'name');
90 $siteserver = common_config('site', 'server');
92 $title = sprintf(_('%s / Favorites from %s'), $sitename, $user->nickname);
93 $id = "tag:$siteserver:favorites:".$user->id;
94 $link = common_local_url('favorites', array('nickname' => $user->nickname));
95 $subtitle = sprintf(_('%s updates favorited by %s / %s.'), $sitename, $profile->getBestName(), $user->nickname);
97 switch($apidata['content-type']) {
99 $this->show_xml_timeline($notice);
102 $this->show_rss_timeline($notice, $title, $id, $link, $subtitle);
105 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
108 $this->show_json_timeline($notice);
111 common_user_error(_('API method not found!'), $code = 404);
117 function create($args, $apidata) {
118 parent::handle($args);
120 // Check for RESTfulness
121 if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
122 // XXX: Twitter just prints the err msg, no XML / JSON.
123 $this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
127 $user = $apidata['user'];
128 $notice_id = $apidata['api_arg'];
129 $notice = Notice::staticGet($notice_id);
132 $this->client_error(_('No status found with that ID.'), 404, $apidata['content-type']);
136 // XXX: Twitter lets you fave things repeatedly via api.
137 if ($user->hasFave($notice)) {
138 $this->client_error(_('This notice is already a favorite!'), 403, $apidata['content-type']);
142 common_debug("notice: " . $apidata['api_arg']);
144 $fave = Fave::addNew($user, $notice);
147 common_server_error(_('Could not create favorite.'));
151 $this->notify($fave, $notice, $user);
152 $user->blowFavesCache();
154 if ($apidata['content-type'] == 'xml') {
155 $this->show_single_xml_status($notice);
156 } elseif ($apidata['content-type'] == 'json') {
157 $this->show_single_json_status($notice);
163 function destroy($args, $apidata) {
164 parent::handle($args);
165 common_server_error(_('API method under construction.'), $code=501);
169 // XXX: these two funcs swiped from faves. Maybe put in util.php, or some common base class?
171 function notify($fave, $notice, $user) {
172 $other = User::staticGet('id', $notice->profile_id);
173 if ($other && $other->id != $user->id) {
174 if ($other->email && $other->emailnotifyfav) {
175 $this->notify_mail($other, $user, $notice);
182 function notify_mail($other, $user, $notice) {
183 $profile = $user->getProfile();
184 $bestname = $profile->getBestName();
185 $subject = sprintf(_('%s added your notice as a favorite'), $bestname);
186 $body = sprintf(_("%1\$s just added your notice from %2\$s as one of their favorites.\n\n" .
187 "In case you forgot, you can see the text of your notice here:\n\n" .
189 "You can see the list of %1\$s's favorites here:\n\n" .
191 "Faithfully yours,\n" .
194 common_exact_date($notice->created),
195 common_local_url('shownotice', array('notice' => $notice->id)),
196 common_local_url('showfavorites', array('nickname' => $user->nickname)),
197 common_config('site', 'name'));
199 mail_to_user($other, $subject, $body);