]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapifavorites.php
71b65461582305f3dff3d3780f4fb2457279df32
[quix0rs-gnu-social.git] / actions / twitapifavorites.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Control Yourself, 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')) {
21     exit(1);
22 }
23
24 require_once(INSTALLDIR.'/lib/twitterapi.php');
25
26 class TwitapifavoritesAction extends TwitterapiAction
27 {
28
29     function favorites($args, $apidata)
30     {
31         parent::handle($args);
32
33         $this->auth_user = $apidata['user'];
34         $user = $this->get_user($apidata['api_arg'], $apidata);
35
36         if (empty($user)) {
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);
41         }
42             $this->clientError('Not Found', 404, $apidata['content-type']);
43             return;
44         }
45
46         $profile = $user->getProfile();
47
48         $sitename   = common_config('site', 'name');
49         $title      = sprintf(_('%s / Favorites from %s'), $sitename,
50             $user->nickname);
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);
57
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');
63
64         $notice = $user->favoriteNotices(($page-1)*$count, $count);
65
66         switch($apidata['content-type']) {
67         case 'xml':
68             $this->show_xml_timeline($notice);
69             break;
70         case 'rss':
71             $this->show_rss_timeline($notice, $title, $link, $subtitle);
72             break;
73         case 'atom':
74             if (isset($apidata['api_arg'])) {
75                  $selfuri = $selfuri = common_root_url() .
76                      'api/favorites/' . $apidata['api_arg'] . '.atom';
77             } else {
78                  $selfuri = $selfuri = common_root_url() .
79                   'api/favorites.atom';
80             }
81             $this->show_atom_timeline($notice, $title, $id, $link,
82                 $subtitle, null, $selfuri);
83             break;
84         case 'json':
85             $this->show_json_timeline($notice);
86             break;
87         default:
88             $this->clientError(_('API method not found!'), $code = 404);
89         }
90
91     }
92
93     function create($args, $apidata)
94     {
95         parent::handle($args);
96
97         // Check for RESTfulness
98         if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
99             $this->clientError(_('This method requires a POST or DELETE.'),
100                 400, $apidata['content-type']);
101             return;
102         }
103
104         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
105             $this->clientError(_('API method not found!'), $code = 404);
106             return;
107         }
108
109         $user      = $apidata['user']; // Always the auth user
110         $notice_id = $apidata['api_arg'];
111         $notice    = Notice::staticGet($notice_id);
112
113         if (empty($notice)) {
114             $this->clientError(_('No status found with that ID.'),
115                 404, $apidata['content-type']);
116             return;
117         }
118
119         // XXX: Twitter lets you fave things repeatedly via api.
120         if ($user->hasFave($notice)) {
121             $this->clientError(_('This status is already a favorite!'),
122                 403, $apidata['content-type']);
123             return;
124         }
125
126         $fave = Fave::addNew($user, $notice);
127
128         if (empty($fave)) {
129             $this->clientError(_('Could not create favorite.'));
130             return;
131         }
132
133         $this->notify($fave, $notice, $user);
134         $user->blowFavesCache();
135
136         if ($apidata['content-type'] == 'xml') {
137             $this->show_single_xml_status($notice);
138         } elseif ($apidata['content-type'] == 'json') {
139             $this->show_single_json_status($notice);
140         }
141
142     }
143
144     function destroy($args, $apidata)
145     {
146         parent::handle($args);
147
148         // Check for RESTfulness
149         if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
150             $this->clientError(_('This method requires a POST or DELETE.'),
151                 400, $apidata['content-type']);
152             return;
153         }
154
155         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
156             $this->clientError(_('API method not found!'), $code = 404);
157             return;
158         }
159
160         $user      = $apidata['user']; // Always the auth user
161         $notice_id = $apidata['api_arg'];
162         $notice    = Notice::staticGet($notice_id);
163
164         if (empty($notice)) {
165             $this->clientError(_('No status found with that ID.'),
166                 404, $apidata['content-type']);
167             return;
168         }
169
170         $fave            = new Fave();
171         $fave->user_id   = $this->id;
172         $fave->notice_id = $notice->id;
173
174         if (!$fave->find(true)) {
175             $this->clientError(_('That status is not a favorite!'),
176                 403, $apidata['content-type']);
177             return;
178         }
179
180         $result = $fave->delete();
181
182         if (!$result) {
183             common_log_db_error($fave, 'DELETE', __FILE__);
184             $this->clientError(_('Could not delete favorite.'), 404);
185             return;
186         }
187
188         $user->blowFavesCache();
189
190         if ($apidata['content-type'] == 'xml') {
191             $this->show_single_xml_status($notice);
192         } elseif ($apidata['content-type'] == 'json') {
193             $this->show_single_json_status($notice);
194         }
195
196     }
197
198     // XXX: these two funcs swiped from faves.
199     // Maybe put in util.php, or some common base class?
200
201     function notify($fave, $notice, $user)
202     {
203         $other = User::staticGet('id', $notice->profile_id);
204         if ($other && $other->id != $user->id) {
205             if ($other->email && $other->emailnotifyfav) {
206                 $this->notify_mail($other, $user, $notice);
207             }
208             # XXX: notify by IM
209             # XXX: notify by SMS
210         }
211     }
212
213     function notify_mail($other, $user, $notice)
214     {
215         $profile = $user->getProfile();
216         $bestname = $profile->getBestName();
217         $subject = sprintf(_('%s added your notice as a favorite'), $bestname);
218         $body = sprintf(_("%1\$s just added your notice from %2\$s as one of their favorites.\n\n" .
219                           "In case you forgot, you can see the text of your notice here:\n\n" .
220                           "%3\$s\n\n" .
221                           "You can see the list of %1\$s's favorites here:\n\n" .
222                           "%4\$s\n\n" .
223                           "Faithfully yours,\n" .
224                           "%5\$s\n"),
225                         $bestname,
226                         common_exact_date($notice->created),
227                         common_local_url('shownotice', array('notice' => $notice->id)),
228                         common_local_url('showfavorites', array('nickname' => $user->nickname)),
229                         common_config('site', 'name'));
230
231         mail_to_user($other, $subject, $body);
232     }
233
234 }