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