]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinefavorites.php
Merge branch '1.0.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>
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 class ApiTimelineFavoritesAction extends ApiBareAuthAction
52 {
53     var $notices  = null;
54
55     /**
56      * Take arguments for running
57      *
58      * @param array $args $_REQUEST args
59      *
60      * @return boolean success flag
61      */
62     function prepare($args)
63     {
64         parent::prepare($args);
65
66         $this->user = $this->getTargetUser($this->arg('id'));
67
68         if (empty($this->user)) {
69             // TRANS: Client error displayed when requesting most recent favourite notices by a user for a non-existing 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     function handle($args)
89     {
90         parent::handle($args);
91         $this->showTimeline();
92     }
93
94     /**
95      * Show the timeline of notices
96      *
97      * @return void
98      */
99     function showTimeline()
100     {
101         $profile  = $this->user->getProfile();
102         $avatar   = $profile->getAvatar(AVATAR_PROFILE_SIZE);
103
104         $sitename = common_config('site', 'name');
105         $title    = sprintf(
106             // TRANS: Title for timeline of most recent favourite notices by a user.
107             // TRANS: %1$s is the StatusNet sitename, %2$s is a user nickname.
108             _('%1$s / Favorites from %2$s'),
109             $sitename,
110             $this->user->nickname
111         );
112
113         $taguribase = TagURI::base();
114         $id         = "tag:$taguribase:Favorites:" . $this->user->id;
115
116         $subtitle = sprintf(
117             // TRANS: Subtitle for timeline of most recent favourite notices by a user.
118             // TRANS: %1$s is the StatusNet sitename, %2$s is a user's full name,
119             // TRANS: %3$s is a user nickname.
120             _('%1$s updates favorited by %2$s / %3$s.'),
121             $sitename,
122             $profile->getBestName(),
123             $this->user->nickname
124         );
125         $logo = !empty($avatar)
126             ? $avatar->displayUrl()
127             : Avatar::defaultImage(AVATAR_PROFILE_SIZE);
128
129         $link = common_local_url(
130             'showfavorites',
131             array('nickname' => $this->user->nickname)
132         );
133
134         $self = $this->getSelfUri();
135
136         switch($this->format) {
137         case 'xml':
138             $this->showXmlTimeline($this->notices);
139             break;
140         case 'rss':
141             $this->showRssTimeline(
142                 $this->notices,
143                 $title,
144                 $link,
145                 $subtitle,
146                 null,
147                 $logo,
148                 $self
149             );
150             break;
151         case 'atom':
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             break;
169         case 'json':
170             $this->showJsonTimeline($this->notices);
171             break;
172         case 'as':
173             header('Content-Type: application/json; charset=utf-8');
174             $doc = new ActivityStreamJSONDocument($this->auth_user);
175             $doc->setTitle($title);
176             $doc->addLink($link,'alternate', 'text/html');
177             $doc->addItemsFromNotices($this->notices);
178             $this->raw($doc->asString());
179             break;
180         default:
181             // TRANS: Client error displayed when coming across a non-supported API method.
182             $this->clientError(_('API method not found.'), $code = 404);
183             break;
184         }
185     }
186
187     /**
188      * Get notices
189      *
190      * @return array notices
191      */
192     function getNotices()
193     {
194         $notices = array();
195
196         common_debug("since id = " . $this->since_id . " max id = " . $this->max_id);
197
198         if (!empty($this->auth_user) && $this->auth_user->id == $this->user->id) {
199             $notice = $this->user->favoriteNotices(
200                 true,
201                 ($this->page-1) * $this->count,
202                 $this->count,
203                 $this->since_id,
204                 $this->max_id
205             );
206         } else {
207             $notice = $this->user->favoriteNotices(
208                 false,
209                 ($this->page-1) * $this->count,
210                 $this->count,
211                 $this->since_id,
212                 $this->max_id
213             );
214         }
215
216         while ($notice->fetch()) {
217             $notices[] = clone($notice);
218         }
219
220         return $notices;
221     }
222
223     /**
224      * Is this action read only?
225      *
226      * @param array $args other arguments
227      *
228      * @return boolean true
229      */
230     function isReadOnly($args)
231     {
232         return true;
233     }
234
235     /**
236      * When was this feed last modified?
237      *
238      * @return string datestamp of the latest notice in the stream
239      */
240     function lastModified()
241     {
242         if (!empty($this->notices) && (count($this->notices) > 0)) {
243             return strtotime($this->notices[0]->created);
244         }
245
246         return null;
247     }
248
249     /**
250      * An entity tag for this stream
251      *
252      * Returns an Etag based on the action name, language, user ID, and
253      * timestamps of the first and last notice in the timeline
254      *
255      * @return string etag
256      */
257     function etag()
258     {
259         if (!empty($this->notices) && (count($this->notices) > 0)) {
260
261             $last = count($this->notices) - 1;
262
263             return '"' . implode(
264                 ':',
265                 array($this->arg('action'),
266                       common_user_cache_hash($this->auth_user),
267                       common_language(),
268                       $this->user->id,
269                       strtotime($this->notices[0]->created),
270                       strtotime($this->notices[$last]->created))
271             )
272             . '"';
273         }
274
275         return null;
276     }
277 }