]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinefavorites.php
35a996c9cb3f7373b4a2836db8f1e8ddca2d94a1
[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    Zach Copley <zach@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/apibareauth.php';
35
36 /**
37  * Returns the 20 most recent favorite notices for the authenticating user or user
38  * specified by the ID parameter in the requested format.
39  *
40  * @category API
41  * @package  StatusNet
42  * @author   Zach Copley <zach@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  */
46
47 class ApiTimelineFavoritesAction extends ApiBareAuthAction
48 {
49
50     var $user     = null;
51     var $notices  = null;
52     var $format   = null;
53     var $page     = null;
54     var $count    = null;
55     var $max_id   = null;
56     var $since_id = null;
57     var $since    = null;
58
59     /**
60      * Take arguments for running
61      *
62      * @param array $args $_REQUEST args
63      *
64      * @return boolean success flag
65      *
66      */
67
68     function prepare($args)
69     {
70         parent::prepare($args);
71
72         $this->page     = (int)$this->arg('page', 1);
73         $this->count    = (int)$this->arg('count', 20);
74         $this->max_id   = (int)$this->arg('max_id', 0);
75         $this->since_id = (int)$this->arg('since_id', 0);
76         $this->since    = $this->arg('since');
77         $this->format   = $this->arg('format');
78
79         $this->user = $this->getTargetUser($this->arg('id'));
80
81         if (empty($this->user)) {
82             $this->clientError(_('No such user!'), 404, $this->format);
83             return;
84         }
85
86         $this->notices = $this->getNotices();
87
88         return true;
89     }
90
91     /**
92      * Handle the request
93      *
94      * Just show the notices
95      *
96      * @param array $args $_REQUEST data (unused)
97      *
98      * @return void
99      */
100
101     function handle($args)
102     {
103         parent::handle($args);
104         $this->showTimeline();
105     }
106
107     /**
108      * Show the timeline of notices
109      *
110      * @return void
111      */
112
113     function showTimeline()
114     {
115         $profile = $this->user->getProfile();
116
117         $sitename   = common_config('site', 'name');
118         $title      = sprintf(
119             _('%s / Favorites from %s'),
120             $sitename,
121             $this->user->nickname
122         );
123
124         $taguribase = common_config('integration', 'taguri');
125         $id         = "tag:$taguribase:Favorites:" . $this->user->id;
126         $link       = common_local_url(
127             'favorites',
128             array('nickname' => $this->user->nickname)
129         );
130         $subtitle   = sprintf(
131             _('%s updates favorited by %s / %s.'),
132             $sitename,
133             $profile->getBestName(),
134             $this->user->nickname
135         );
136
137         switch($this->format) {
138         case 'xml':
139             $this->show_xml_timeline($this->notices);
140             break;
141         case 'rss':
142             $this->show_rss_timeline($this->notices, $title, $link, $subtitle);
143             break;
144         case 'atom':
145             $selfuri = common_root_url() .
146                 ltrim($_SERVER['QUERY_STRING'], 'p=');
147             $this->show_atom_timeline(
148                 $this->notices, $title, $id, $link, $subtitle,
149                 null, $selfuri
150             );
151             break;
152         case 'json':
153             $this->show_json_timeline($this->notices);
154             break;
155         default:
156             $this->clientError(_('API method not found!'), $code = 404);
157             break;
158         }
159     }
160
161     /**
162      * Get notices
163      *
164      * @return array notices
165      */
166
167     function getNotices()
168     {
169         $notices = array();
170
171         if (!empty($this->auth_user) && $this->auth_user->id == $this->user->id) {
172             $notice = $this->user->favoriteNotices(
173                 ($this->page-1) * $this->count,
174                 $this->count,
175                 true
176             );
177         } else {
178             $notice = $this->user->favoriteNotices(
179                 ($this->page-1) * $this->count,
180                 $this->count,
181                 false
182             );
183         }
184
185         while ($notice->fetch()) {
186             $notices[] = clone($notice);
187         }
188
189         return $notices;
190     }
191
192     /**
193      * Is this action read only?
194      *
195      * @param array $args other arguments
196      *
197      * @return boolean true
198      */
199
200     function isReadOnly($args)
201     {
202         return true;
203     }
204
205     /**
206      * When was this feed last modified?
207      *
208      * @return string datestamp of the latest notice in the stream
209      */
210
211     function lastModified()
212     {
213         if (!empty($this->notices) && (count($this->notices) > 0)) {
214             return strtotime($this->notices[0]->created);
215         }
216
217         return null;
218     }
219
220     /**
221      * An entity tag for this stream
222      *
223      * Returns an Etag based on the action name, language, user ID, and
224      * timestamps of the first and last notice in the timeline
225      *
226      * @return string etag
227      */
228
229     function etag()
230     {
231         if (!empty($this->notices) && (count($this->notices) > 0)) {
232
233             $last = count($this->notices) - 1;
234
235             return '"' . implode(
236                 ':',
237                 array($this->arg('action'),
238                       common_language(),
239                       $this->user->id,
240                       strtotime($this->notices[0]->created),
241                       strtotime($this->notices[$last]->created))
242             )
243             . '"';
244         }
245
246         return null;
247     }
248
249 }