]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapifavorites.php
d7d77907dd0efe4248386b147d1340fe9af00b49
[quix0rs-gnu-social.git] / actions / twitapifavorites.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 TwitapifavoritesAction extends TwitterapiAction {
25
26         function is_readonly() {
27
28                 static $write_methods = array('favorites');
29
30                 $cmdtext = explode('.', $this->arg('method'));
31
32                 if (in_array($cmdtext[0], $write_methods)) {
33                         return false;
34                 }
35
36                 return true;
37         }
38
39         function favorites($args, $apidata) {
40                 parent::handle($args);
41
42                 $user = null;
43
44                 // function was called with an argument /favorites/api_arg.format
45                 if (isset($apidata['api_arg'])) {
46
47                         if (is_numeric($apidata['api_arg'])) {
48                                 $user = User::staticGet($apidata['api_arg']);
49                         } else {
50                                 $nickname = common_canonical_nickname($apidata['api_arg']);
51                                 $user = User::staticGet('nickname', $nickname);
52                         }
53                 } else {
54
55                         // if no user was specified, then we'll use the authenticated user
56                         $user = $apidata['user'];
57                 }
58
59                 if (!$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'];
63                 }
64
65                 $profile = $user->getProfile();
66
67                 if (!$profile) {
68                         common_server_error(_('User has no profile.'));
69                         exit();
70                 }
71
72                 $page = $this->arg('page');
73
74                 if (!$page) {
75                         $page = 1;
76                 }
77
78                 if (!$count) {
79                         $count = 20;
80                 }
81
82                 $notice = $user->favoriteNotices((($page-1)*20), $count);
83
84                 if (!$notice) {
85                         common_server_error(_('Could not retrieve favorite notices.'));
86                         exit();
87                 }
88
89                 $sitename = common_config('site', 'name');
90                 $siteserver = common_config('site', 'server');
91
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);
96
97                 switch($apidata['content-type']) {
98                  case 'xml':
99                         $this->show_xml_timeline($notice);
100                         break;
101                  case 'rss':
102                         $this->show_rss_timeline($notice, $title, $id, $link, $subtitle);
103                         break;
104                  case 'atom':
105                         $this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
106                         break;
107                  case 'json':
108                         $this->show_json_timeline($notice);
109                         break;
110                  default:
111                         common_user_error(_('API method not found!'), $code = 404);
112                 }
113
114                 exit();
115         }
116
117         function create($args, $apidata) {
118                 parent::handle($args);
119
120                 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
121                         common_user_error(_('API method not found!'), $code = 404);
122                         exit;
123                 }
124
125                 // Check for RESTfulness
126                 if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
127                         // XXX: Twitter just prints the err msg, no XML / JSON.
128                         $this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
129                         exit();
130                 }
131
132                 $user = $apidata['user'];
133                 $notice_id = $apidata['api_arg'];
134                 $notice = Notice::staticGet($notice_id);
135
136                 if (!$notice) {
137                         $this->client_error(_('No status found with that ID.'), 404, $apidata['content-type']);
138                         exit();
139                 }
140
141                 // XXX: Twitter lets you fave things repeatedly via api.
142                 if ($user->hasFave($notice)) {
143                         $this->client_error(_('This notice is already a favorite!'), 403, $apidata['content-type']);
144                         exit();
145                 }
146
147                 common_debug("notice: " . $apidata['api_arg']);
148
149                 $fave = Fave::addNew($user, $notice);
150
151                 if (!$fave) {
152                         common_server_error(_('Could not create favorite.'));
153                         exit();
154                 }
155
156                 $this->notify($fave, $notice, $user);
157                 $user->blowFavesCache();
158
159                 if ($apidata['content-type'] == 'xml') {
160                         $this->show_single_xml_status($notice);
161                 } elseif ($apidata['content-type'] == 'json') {
162                         $this->show_single_json_status($notice);
163                 }
164
165                 exit();
166         }
167
168         function destroy($args, $apidata) {
169                 parent::handle($args);
170                 common_server_error(_('API method under construction.'), $code=501);
171                 exit();
172         }
173
174         // XXX: these two funcs swiped from faves.  Maybe put in util.php, or some common base class?
175
176         function notify($fave, $notice, $user) {
177             $other = User::staticGet('id', $notice->profile_id);
178                 if ($other && $other->id != $user->id) {
179                         if ($other->email && $other->emailnotifyfav) {
180                                 $this->notify_mail($other, $user, $notice);
181                         }
182                         # XXX: notify by IM
183                         # XXX: notify by SMS
184                 }
185         }
186
187         function notify_mail($other, $user, $notice) {
188                 $profile = $user->getProfile();
189                 $bestname = $profile->getBestName();
190                 $subject = sprintf(_('%s added your notice as a favorite'), $bestname);
191                 $body = sprintf(_("%1\$s just added your notice from %2\$s as one of their favorites.\n\n" .
192                                                   "In case you forgot, you can see the text of your notice here:\n\n" .
193                                                   "%3\$s\n\n" .
194                                                   "You can see the list of %1\$s's favorites here:\n\n" .
195                                                   "%4\$s\n\n" .
196                                                   "Faithfully yours,\n" .
197                                                   "%5\$s\n"),
198                                                 $bestname,
199                                                 common_exact_date($notice->created),
200                                                 common_local_url('shownotice', array('notice' => $notice->id)),
201                                                 common_local_url('showfavorites', array('nickname' => $user->nickname)),
202                                                 common_config('site', 'name'));
203
204                 mail_to_user($other, $subject, $body);
205         }
206
207 }