]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinefavorites.php
Avatar resizing improvements and better code reuse
[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 /**
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 ApiTimelineFavoritesAction 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 / Favorites from %2$s'),
107             $sitename,
108             $this->user->nickname
109         );
110
111         $taguribase = TagURI::base();
112         $id         = "tag:$taguribase:Favorites:" . $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 favorited 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             'showfavorites',
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         if (!empty($this->auth_user) && $this->auth_user->id == $this->user->id) {
197             $notice = $this->user->favoriteNotices(
198                 true,
199                 ($this->page-1) * $this->count,
200                 $this->count,
201                 $this->since_id,
202                 $this->max_id
203             );
204         } else {
205             $notice = $this->user->favoriteNotices(
206                 false,
207                 ($this->page-1) * $this->count,
208                 $this->count,
209                 $this->since_id,
210                 $this->max_id
211             );
212         }
213
214         while ($notice->fetch()) {
215             $notices[] = clone($notice);
216         }
217
218         return $notices;
219     }
220
221     /**
222      * Is this action read only?
223      *
224      * @param array $args other arguments
225      *
226      * @return boolean true
227      */
228     function isReadOnly($args)
229     {
230         return true;
231     }
232
233     /**
234      * When was this feed last modified?
235      *
236      * @return string datestamp of the latest notice in the stream
237      */
238     function lastModified()
239     {
240         if (!empty($this->notices) && (count($this->notices) > 0)) {
241             return strtotime($this->notices[0]->created);
242         }
243
244         return null;
245     }
246
247     /**
248      * An entity tag for this stream
249      *
250      * Returns an Etag based on the action name, language, user ID, and
251      * timestamps of the first and last notice in the timeline
252      *
253      * @return string etag
254      */
255     function etag()
256     {
257         if (!empty($this->notices) && (count($this->notices) > 0)) {
258
259             $last = count($this->notices) - 1;
260
261             return '"' . implode(
262                 ':',
263                 array($this->arg('action'),
264                       common_user_cache_hash($this->auth_user),
265                       common_language(),
266                       $this->user->id,
267                       strtotime($this->notices[0]->created),
268                       strtotime($this->notices[$last]->created))
269             )
270             . '"';
271         }
272
273         return null;
274     }
275 }