]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Bookmark/apitimelinebookmarks.php
Merge branch '1.1.x' of gitorious.org:statusnet/mainline into 1.1.x
[quix0rs-gnu-social.git] / plugins / Bookmark / apitimelinebookmarks.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 require_once 'bookmarksnoticestream.php';
39
40 /**
41  * Returns the 20 most recent favorite notices for the authenticating user or user
42  * specified by the ID parameter in the requested format.
43  *
44  * @category API
45  * @package  StatusNet
46  * @author   Craig Andrews <candrews@integralblue.com>
47  * @author   Evan Prodromou <evan@status.net>
48  * @author   Zach Copley <zach@status.net>
49  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50  * @link     http://status.net/
51  */
52 class ApiTimelineBookmarksAction extends ApiBareAuthAction
53 {
54     var $notices  = null;
55
56     /**
57      * Take arguments for running
58      *
59      * @param array $args $_REQUEST args
60      *
61      * @return boolean success flag
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             // TRANS: Client error displayed when requesting most recent favourite notices by a user for a non-existing user.
71             $this->clientError(_('No such user.'), 404, $this->format);
72             return;
73         }
74
75         $this->notices = $this->getNotices();
76
77         return true;
78     }
79
80     /**
81      * Handle the request
82      *
83      * Just show the notices
84      *
85      * @param array $args $_REQUEST data (unused)
86      *
87      * @return void
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     function showTimeline()
101     {
102         $profile  = $this->user->getProfile();
103         $avatar   = $profile->getAvatar(AVATAR_PROFILE_SIZE);
104
105         $sitename = common_config('site', 'name');
106         $title    = sprintf(
107             // TRANS: Title for timeline of most recent favourite notices by a user.
108             // TRANS: %1$s is the StatusNet sitename, %2$s is a user nickname.
109             _('%1$s / Bookmarks from %2$s'),
110             $sitename,
111             $this->user->nickname
112         );
113
114         $taguribase = TagURI::base();
115         $id         = "tag:$taguribase:Bookmarks:" . $this->user->id;
116
117         $subtitle = sprintf(
118             // TRANS: Subtitle for timeline of most recent favourite notices by a user.
119             // TRANS: %1$s is the StatusNet sitename, %2$s is a user's full name,
120             // TRANS: %3$s is a user nickname.
121             _('%1$s updates bookmarked by %2$s / %3$s.'),
122             $sitename,
123             $profile->getBestName(),
124             $this->user->nickname
125         );
126         $logo = !empty($avatar)
127             ? $avatar->displayUrl()
128             : Avatar::defaultImage(AVATAR_PROFILE_SIZE);
129
130         $link = common_local_url(
131             'bookmarks',
132             array('nickname' => $this->user->nickname)
133         );
134
135         $self = $this->getSelfUri();
136
137         switch($this->format) {
138         case 'xml':
139             $this->showXmlTimeline($this->notices);
140             break;
141         case 'rss':
142             $this->showRssTimeline(
143                 $this->notices,
144                 $title,
145                 $link,
146                 $subtitle,
147                 null,
148                 $logo,
149                 $self
150             );
151             break;
152         case 'atom':
153             header('Content-Type: application/atom+xml; charset=utf-8');
154
155             $atom = new AtomNoticeFeed($this->auth_user);
156
157             $atom->setId($id);
158             $atom->setTitle($title);
159             $atom->setSubtitle($subtitle);
160             $atom->setLogo($logo);
161             $atom->setUpdated('now');
162
163             $atom->addLink($link);
164             $atom->setSelfLink($self);
165
166             $atom->addEntryFromNotices($this->notices);
167
168             $this->raw($atom->getString());
169             break;
170         case 'json':
171             $this->showJsonTimeline($this->notices);
172             break;
173         case 'as':
174             header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
175             $doc = new ActivityStreamJSONDocument($this->auth_user);
176             $doc->setTitle($title);
177             $doc->addLink($link,'alternate', 'text/html');
178             $doc->addItemsFromNotices($this->notices);
179             $this->raw($doc->asString());
180             break;
181         default:
182             // TRANS: Client error displayed when coming across a non-supported API method.
183             $this->clientError(_('API method not found.'), $code = 404);
184             break;
185         }
186     }
187
188     /**
189      * Get notices
190      *
191      * @return array notices
192      */
193     function getNotices()
194     {
195         $notices = array();
196
197         common_debug("since id = " . $this->since_id . " max id = " . $this->max_id);
198
199         $notice = new BookmarksNoticeStream($this->user->id, true);
200         $notice = $notice->getNotices(
201             ($this->page-1) * $this->count,
202             $this->count,
203             $this->since_id,
204             $this->max_id
205         );
206
207         while ($notice->fetch()) {
208             $notices[] = clone($notice);
209         }
210
211         return $notices;
212     }
213
214     /**
215      * Is this action read only?
216      *
217      * @param array $args other arguments
218      *
219      * @return boolean true
220      */
221     function isReadOnly($args)
222     {
223         return true;
224     }
225
226     /**
227      * When was this feed last modified?
228      *
229      * @return string datestamp of the latest notice in the stream
230      */
231     function lastModified()
232     {
233         if (!empty($this->notices) && (count($this->notices) > 0)) {
234             return strtotime($this->notices[0]->created);
235         }
236
237         return null;
238     }
239
240     /**
241      * An entity tag for this stream
242      *
243      * Returns an Etag based on the action name, language, user ID, and
244      * timestamps of the first and last notice in the timeline
245      *
246      * @return string etag
247      */
248     function etag()
249     {
250         if (!empty($this->notices) && (count($this->notices) > 0)) {
251
252             $last = count($this->notices) - 1;
253
254             return '"' . implode(
255                 ':',
256                 array($this->arg('action'),
257                       common_user_cache_hash($this->auth_user),
258                       common_language(),
259                       $this->user->id,
260                       strtotime($this->notices[0]->created),
261                       strtotime($this->notices[$last]->created))
262             )
263             . '"';
264         }
265
266         return null;
267     }
268 }