]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinefavorites.php
Fix for ticket 2756 - Calls to OAuth endpoints are redirected to the
[quix0rs-gnu-social.git] / actions / apitimelinefavorites.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show a user's favorite notices
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  API
23  * @package   StatusNet
24  * @author    Craig Andrews <candrews@integralblue.com>
25  * @author    Evan Prodromou <evan@status.net>
26  * @author    Zach Copley <zach@status.net>
27  * @copyright 2009-2010 StatusNet, Inc.
28  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
29  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30  * @link      http://status.net/
31  */
32
33 if (!defined('STATUSNET')) {
34     exit(1);
35 }
36
37 require_once INSTALLDIR.'/lib/apibareauth.php';
38
39 /**
40  * Returns the 20 most recent favorite notices for the authenticating user or user
41  * specified by the ID parameter in the requested format.
42  *
43  * @category API
44  * @package  StatusNet
45  * @author   Craig Andrews <candrews@integralblue.com>
46  * @author   Evan Prodromou <evan@status.net>
47  * @author   Zach Copley <zach@status.net>
48  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
49  * @link     http://status.net/
50  */
51
52 class ApiTimelineFavoritesAction extends ApiBareAuthAction
53 {
54     var $notices  = null;
55
56     /**
57      * Take arguments for running
58      *
59      * @param array $args $_REQUEST args
60      *
61      * @return boolean success flag
62      *
63      */
64
65     function prepare($args)
66     {
67         parent::prepare($args);
68
69         $this->user = $this->getTargetUser($this->arg('id'));
70
71         if (empty($this->user)) {
72             $this->clientError(_('No such user.'), 404, $this->format);
73             return;
74         }
75
76         $this->notices = $this->getNotices();
77
78         return true;
79     }
80
81     /**
82      * Handle the request
83      *
84      * Just show the notices
85      *
86      * @param array $args $_REQUEST data (unused)
87      *
88      * @return void
89      */
90
91     function handle($args)
92     {
93         parent::handle($args);
94         $this->showTimeline();
95     }
96
97     /**
98      * Show the timeline of notices
99      *
100      * @return void
101      */
102
103     function showTimeline()
104     {
105         $profile  = $this->user->getProfile();
106         $avatar   = $profile->getAvatar(AVATAR_PROFILE_SIZE);
107
108         $sitename = common_config('site', 'name');
109         $title    = sprintf(
110             _('%1$s / Favorites from %2$s'),
111             $sitename,
112             $this->user->nickname
113         );
114
115         $taguribase = TagURI::base();
116         $id         = "tag:$taguribase:Favorites:" . $this->user->id;
117
118         $subtitle = sprintf(
119             _('%1$s updates favorited by %2$s / %2$s.'),
120             $sitename,
121             $profile->getBestName(),
122             $this->user->nickname
123         );
124         $logo = !empty($avatar)
125             ? $avatar->displayUrl()
126             : Avatar::defaultImage(AVATAR_PROFILE_SIZE);
127
128         $link = common_local_url(
129             'showfavorites',
130             array('nickname' => $this->user->nickname)
131         );
132
133         $self = $this->getSelfUri();
134
135         switch($this->format) {
136         case 'xml':
137             $this->showXmlTimeline($this->notices);
138             break;
139         case 'rss':
140             $this->showRssTimeline(
141                 $this->notices,
142                 $title,
143                 $link,
144                 $subtitle,
145                 null,
146                 $logo,
147                 $self
148             );
149             break;
150         case 'atom':
151
152             header('Content-Type: application/atom+xml; charset=utf-8');
153
154             $atom = new AtomNoticeFeed($this->auth_user);
155
156             $atom->setId($id);
157             $atom->setTitle($title);
158             $atom->setSubtitle($subtitle);
159             $atom->setLogo($logo);
160             $atom->setUpdated('now');
161
162             $atom->addLink($link);
163             $atom->setSelfLink($self);
164
165             $atom->addEntryFromNotices($this->notices);
166
167             $this->raw($atom->getString());
168
169             break;
170         case 'json':
171             $this->showJsonTimeline($this->notices);
172             break;
173         default:
174             $this->clientError(_('API method not found.'), $code = 404);
175             break;
176         }
177     }
178
179     /**
180      * Get notices
181      *
182      * @return array notices
183      */
184
185     function getNotices()
186     {
187         $notices = array();
188
189         common_debug("since id = " . $this->since_id . " max id = " . $this->max_id);
190
191         if (!empty($this->auth_user) && $this->auth_user->id == $this->user->id) {
192             $notice = $this->user->favoriteNotices(
193                 true,
194                 ($this->page-1) * $this->count,
195                 $this->count,
196                 $this->since_id,
197                 $this->max_id
198             );
199         } else {
200             $notice = $this->user->favoriteNotices(
201                 false,
202                 ($this->page-1) * $this->count,
203                 $this->count,
204                 $this->since_id,
205                 $this->max_id
206             );
207         }
208
209         while ($notice->fetch()) {
210             $notices[] = clone($notice);
211         }
212
213         return $notices;
214     }
215
216     /**
217      * Is this action read only?
218      *
219      * @param array $args other arguments
220      *
221      * @return boolean true
222      */
223
224     function isReadOnly($args)
225     {
226         return true;
227     }
228
229     /**
230      * When was this feed last modified?
231      *
232      * @return string datestamp of the latest notice in the stream
233      */
234
235     function lastModified()
236     {
237         if (!empty($this->notices) && (count($this->notices) > 0)) {
238             return strtotime($this->notices[0]->created);
239         }
240
241         return null;
242     }
243
244     /**
245      * An entity tag for this stream
246      *
247      * Returns an Etag based on the action name, language, user ID, and
248      * timestamps of the first and last notice in the timeline
249      *
250      * @return string etag
251      */
252
253     function etag()
254     {
255         if (!empty($this->notices) && (count($this->notices) > 0)) {
256
257             $last = count($this->notices) - 1;
258
259             return '"' . implode(
260                 ':',
261                 array($this->arg('action'),
262                       common_language(),
263                       $this->user->id,
264                       strtotime($this->notices[0]->created),
265                       strtotime($this->notices[$last]->created))
266             )
267             . '"';
268         }
269
270         return null;
271     }
272
273 }