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