]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinefavorites.php
HTML and style cleanup for EmailSummary plugin.
[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         default:
173             // TRANS: Client error displayed when trying to handle an unknown API method.
174             $this->clientError(_('API method not found.'), $code = 404);
175             break;
176         }
177     }
178
179     /**
180      * Get notices
181      *
182      * @return array notices
183      */
184     function getNotices()
185     {
186         $notices = array();
187
188         common_debug("since id = " . $this->since_id . " max id = " . $this->max_id);
189
190         if (!empty($this->auth_user) && $this->auth_user->id == $this->user->id) {
191             $notice = $this->user->favoriteNotices(
192                 true,
193                 ($this->page-1) * $this->count,
194                 $this->count,
195                 $this->since_id,
196                 $this->max_id
197             );
198         } else {
199             $notice = $this->user->favoriteNotices(
200                 false,
201                 ($this->page-1) * $this->count,
202                 $this->count,
203                 $this->since_id,
204                 $this->max_id
205             );
206         }
207
208         while ($notice->fetch()) {
209             $notices[] = clone($notice);
210         }
211
212         return $notices;
213     }
214
215     /**
216      * Is this action read only?
217      *
218      * @param array $args other arguments
219      *
220      * @return boolean true
221      */
222     function isReadOnly($args)
223     {
224         return true;
225     }
226
227     /**
228      * When was this feed last modified?
229      *
230      * @return string datestamp of the latest notice in the stream
231      */
232     function lastModified()
233     {
234         if (!empty($this->notices) && (count($this->notices) > 0)) {
235             return strtotime($this->notices[0]->created);
236         }
237
238         return null;
239     }
240
241     /**
242      * An entity tag for this stream
243      *
244      * Returns an Etag based on the action name, language, user ID, and
245      * timestamps of the first and last notice in the timeline
246      *
247      * @return string etag
248      */
249     function etag()
250     {
251         if (!empty($this->notices) && (count($this->notices) > 0)) {
252
253             $last = count($this->notices) - 1;
254
255             return '"' . implode(
256                 ':',
257                 array($this->arg('action'),
258                       common_user_cache_hash($this->auth_user),
259                       common_language(),
260                       $this->user->id,
261                       strtotime($this->notices[0]->created),
262                       strtotime($this->notices[$last]->created))
263             )
264             . '"';
265         }
266
267         return null;
268     }
269 }