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