]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinefavorites.php
New actions for favorites via the API
[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         if ($this->requiresAuth()) {
73             if ($this->checkBasicAuthUser() == false) {
74                 return;
75             }
76         }
77
78         $this->page     = (int)$this->arg('page', 1);
79         $this->count    = (int)$this->arg('count', 20);
80         $this->max_id   = (int)$this->arg('max_id', 0);
81         $this->since_id = (int)$this->arg('since_id', 0);
82         $this->since    = $this->arg('since');
83         $this->format   = $this->arg('format');
84
85         $this->user = $this->getTargetUser($this->arg('id'));
86
87         if (empty($this->user)) {
88             $this->clientError(_('No such user!'), 404, $this->format);
89             return;
90         }
91
92         $this->notices = $this->getNotices();
93
94         return true;
95     }
96
97     /**
98      * Handle the request
99      *
100      * Just show the notices
101      *
102      * @param array $args $_REQUEST data (unused)
103      *
104      * @return void
105      */
106
107     function handle($args)
108     {
109         parent::handle($args);
110         $this->showTimeline();
111     }
112
113     /**
114      * Show the timeline of notices
115      *
116      * @return void
117      */
118
119     function showTimeline()
120     {
121         $profile = $this->user->getProfile();
122
123         $sitename   = common_config('site', 'name');
124         $title      = sprintf(
125             _('%s / Favorites from %s'),
126             $sitename,
127             $this->user->nickname
128         );
129
130         $taguribase = common_config('integration', 'taguri');
131         $id         = "tag:$taguribase:Favorites:" . $this->user->id;
132         $link       = common_local_url(
133             'favorites',
134             array('nickname' => $this->user->nickname)
135         );
136         $subtitle   = sprintf(
137             _('%s updates favorited by %s / %s.'),
138             $sitename,
139             $profile->getBestName(),
140             $this->user->nickname
141         );
142
143         switch($this->format) {
144         case 'xml':
145             $this->show_xml_timeline($this->notices);
146             break;
147         case 'rss':
148             $this->show_rss_timeline($this->notices, $title, $link, $subtitle);
149             break;
150         case 'atom':
151             $selfuri = common_root_url() .
152                 ltrim($_SERVER['QUERY_STRING'], 'p=');
153             $this->show_atom_timeline(
154                 $this->notices, $title, $id, $link, $subtitle,
155                 null, $selfuri
156             );
157             break;
158         case 'json':
159             $this->show_json_timeline($this->notices);
160             break;
161         default:
162             $this->clientError(_('API method not found!'), $code = 404);
163             break;
164         }
165     }
166
167     /**
168      * Get notices
169      *
170      * @return array notices
171      */
172
173     function getNotices()
174     {
175         $notices = array();
176
177         if (!empty($this->auth_user) && $this->auth_user->id == $this->user->id) {
178             $notice = $this->user->favoriteNotices(
179                 ($this->page-1) * $this->count,
180                 $this->count,
181                 true
182             );
183         } else {
184             $notice = $this->user->favoriteNotices(
185                 ($this->page-1) * $this->count,
186                 $this->count,
187                 false
188             );
189         }
190
191         while ($notice->fetch()) {
192             $notices[] = clone($notice);
193         }
194
195         return $notices;
196     }
197
198     /**
199      * Is this action read only?
200      *
201      * @param array $args other arguments
202      *
203      * @return boolean true
204      */
205
206     function isReadOnly($args)
207     {
208         return true;
209     }
210
211     /**
212      * When was this feed last modified?
213      *
214      * @return string datestamp of the latest notice in the stream
215      */
216
217     function lastModified()
218     {
219         if (!empty($this->notices) && (count($this->notices) > 0)) {
220             return strtotime($this->notices[0]->created);
221         }
222
223         return null;
224     }
225
226     /**
227      * An entity tag for this stream
228      *
229      * Returns an Etag based on the action name, language, user ID, and
230      * timestamps of the first and last notice in the timeline
231      *
232      * @return string etag
233      */
234
235     function etag()
236     {
237         if (!empty($this->notices) && (count($this->notices) > 0)) {
238
239             $last = count($this->notices) - 1;
240
241             return '"' . implode(
242                 ':',
243                 array($this->arg('action'),
244                       common_language(),
245                       $this->user->id,
246                       strtotime($this->notices[0]->created),
247                       strtotime($this->notices[$last]->created))
248             )
249             . '"';
250         }
251
252         return null;
253     }
254
255 }