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