]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Favorite/actions/apitimelinefavorites.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / Favorite / 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     protected function prepare(array $args=array())
61     {
62         parent::prepare($args);
63
64         $this->target = $this->getTargetProfile($this->arg('id'));
65
66         if (!($this->target instanceof Profile)) {
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);
69         }
70
71         $this->notices = $this->getNotices();
72
73         return true;
74     }
75
76     /**
77      * Handle the request
78      *
79      * Just show the notices
80      *
81      * @return void
82      */
83     protected function handle()
84     {
85         parent::handle();
86         $this->showTimeline();
87     }
88
89     /**
90      * Show the timeline of notices
91      *
92      * @return void
93      */
94     function showTimeline()
95     {
96         $sitename = common_config('site', 'name');
97         $title    = sprintf(
98             // TRANS: Title for timeline of most recent favourite notices by a user.
99             // TRANS: %1$s is the StatusNet sitename, %2$s is a user nickname.
100             _('%1$s / Favorites from %2$s'),
101             $sitename,
102             $this->target->nickname
103         );
104
105         $taguribase = TagURI::base();
106         $id         = "tag:$taguribase:Favorites:" . $this->target->id;
107
108         $subtitle = sprintf(
109             // TRANS: Subtitle for timeline of most recent favourite notices by a user.
110             // TRANS: %1$s is the StatusNet sitename, %2$s is a user's full name,
111             // TRANS: %3$s is a user nickname.
112             _('%1$s updates favorited by %2$s / %3$s.'),
113             $sitename,
114             $this->target->getBestName(),
115             $this->target->nickname
116         );
117
118         $logo = $this->target->avatarUrl(AVATAR_PROFILE_SIZE);
119         $link = common_local_url('showfavorites',
120                     array('nickname' => $this->target->nickname));
121         $self = $this->getSelfUri();
122
123         switch($this->format) {
124         case 'xml':
125             $this->showXmlTimeline($this->notices);
126             break;
127         case 'rss':
128             $this->showRssTimeline(
129                 $this->notices,
130                 $title,
131                 $link,
132                 $subtitle,
133                 null,
134                 $logo,
135                 $self
136             );
137             break;
138         case 'atom':
139             header('Content-Type: application/atom+xml; charset=utf-8');
140
141             $atom = new AtomNoticeFeed($this->auth_user);
142
143             $atom->setId($id);
144             $atom->setTitle($title);
145             $atom->setSubtitle($subtitle);
146             $atom->setLogo($logo);
147             $atom->setUpdated('now');
148
149             $atom->addLink($link);
150             $atom->setSelfLink($self);
151
152             $atom->addEntryFromNotices($this->notices);
153
154             $this->raw($atom->getString());
155             break;
156         case 'json':
157             $this->showJsonTimeline($this->notices);
158             break;
159         case 'as':
160             header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
161             $doc = new ActivityStreamJSONDocument($this->auth_user);
162             $doc->setTitle($title);
163             $doc->addLink($link,'alternate', 'text/html');
164             $doc->addItemsFromNotices($this->notices);
165             $this->raw($doc->asString());
166             break;
167         default:
168             // TRANS: Client error displayed when coming across a non-supported API method.
169             $this->clientError(_('API method not found.'), 404);
170         }
171     }
172
173     /**
174      * Get notices
175      *
176      * @return array notices
177      */
178     function getNotices()
179     {
180         $notices = array();
181
182         common_debug("since id = " . $this->since_id . " max id = " . $this->max_id);
183
184         $notice = Fave::stream($this->target->id,
185                                ($this->page-1) * $this->count,  // offset
186                                $this->count,                    // limit
187                                (!empty($this->auth_user) && $this->auth_user->id == $this->target->id),   // own feed?
188                                $this->since_id,
189                                $this->max_id
190                               );
191
192         while ($notice->fetch()) {
193             $notices[] = clone($notice);
194         }
195
196         return $notices;
197     }
198
199     /**
200      * Is this action read only?
201      *
202      * @param array $args other arguments
203      *
204      * @return boolean true
205      */
206     function isReadOnly(array $args=array())
207     {
208         return true;
209     }
210
211     /**
212      * When was this feed last modified?
213      *
214      * @return string datestamp of the latest notice in the stream
215      */
216     function lastModified()
217     {
218         if (!empty($this->notices) && (count($this->notices) > 0)) {
219             return strtotime($this->notices[0]->created);
220         }
221
222         return null;
223     }
224
225     /**
226      * An entity tag for this stream
227      *
228      * Returns an Etag based on the action name, language, user ID, and
229      * timestamps of the first and last notice in the timeline
230      *
231      * @return string etag
232      */
233     function etag()
234     {
235         if (!empty($this->notices) && (count($this->notices) > 0)) {
236
237             $last = count($this->notices) - 1;
238
239             return '"' . implode(
240                 ':',
241                 array($this->arg('action'),
242                       common_user_cache_hash($this->auth_user),
243                       common_language(),
244                       $this->target->id,
245                       strtotime($this->notices[0]->created),
246                       strtotime($this->notices[$last]->created))
247             )
248             . '"';
249         }
250
251         return null;
252     }
253 }