]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinefavorites.php
Properly unlink all old avatars when deleting/uploading a new
[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
101         $sitename = common_config('site', 'name');
102         $title    = sprintf(
103             // TRANS: Title for timeline of most recent favourite notices by a user.
104             // TRANS: %1$s is the StatusNet sitename, %2$s is a user nickname.
105             _('%1$s / Favorites from %2$s'),
106             $sitename,
107             $this->user->nickname
108         );
109
110         $taguribase = TagURI::base();
111         $id         = "tag:$taguribase:Favorites:" . $this->user->id;
112
113         $subtitle = sprintf(
114             // TRANS: Subtitle for timeline of most recent favourite notices by a user.
115             // TRANS: %1$s is the StatusNet sitename, %2$s is a user's full name,
116             // TRANS: %3$s is a user nickname.
117             _('%1$s updates favorited by %2$s / %3$s.'),
118             $sitename,
119             $profile->getBestName(),
120             $this->user->nickname
121         );
122
123         $logo = $profile->avatarUrl(AVATAR_PROFILE_SIZE);
124         $link = common_local_url('showfavorites',
125                     array('nickname' => $this->user->nickname));
126         $self = $this->getSelfUri();
127
128         switch($this->format) {
129         case 'xml':
130             $this->showXmlTimeline($this->notices);
131             break;
132         case 'rss':
133             $this->showRssTimeline(
134                 $this->notices,
135                 $title,
136                 $link,
137                 $subtitle,
138                 null,
139                 $logo,
140                 $self
141             );
142             break;
143         case 'atom':
144             header('Content-Type: application/atom+xml; charset=utf-8');
145
146             $atom = new AtomNoticeFeed($this->auth_user);
147
148             $atom->setId($id);
149             $atom->setTitle($title);
150             $atom->setSubtitle($subtitle);
151             $atom->setLogo($logo);
152             $atom->setUpdated('now');
153
154             $atom->addLink($link);
155             $atom->setSelfLink($self);
156
157             $atom->addEntryFromNotices($this->notices);
158
159             $this->raw($atom->getString());
160             break;
161         case 'json':
162             $this->showJsonTimeline($this->notices);
163             break;
164         case 'as':
165             header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
166             $doc = new ActivityStreamJSONDocument($this->auth_user);
167             $doc->setTitle($title);
168             $doc->addLink($link,'alternate', 'text/html');
169             $doc->addItemsFromNotices($this->notices);
170             $this->raw($doc->asString());
171             break;
172         default:
173             // TRANS: Client error displayed when coming across a non-supported 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 }