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