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