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