]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinementions.php
4ba8a4966b195a33043eb6e68219e25571e77111
[quix0rs-gnu-social.git] / actions / apitimelinementions.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show notices mentioning a user (@nickname)
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    Jeffery To <jeffery.to@gmail.com>
27  * @author    mac65 <mac65@mac65.com>
28  * @author    Mike Cochrane <mikec@mikenz.geek.nz>
29  * @author    Robin Millette <robin@millette.info>
30  * @author    Zach Copley <zach@status.net>
31  * @copyright 2009 StatusNet, Inc.
32  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
33  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
34  * @link      http://status.net/
35  */
36
37 if (!defined('STATUSNET')) {
38     exit(1);
39 }
40
41 /**
42  * Returns the most recent (default 20) mentions (status containing @nickname)
43  *
44  * @category API
45  * @package  StatusNet
46  * @author   Craig Andrews <candrews@integralblue.com>
47  * @author   Evan Prodromou <evan@status.net>
48  * @author   Jeffery To <jeffery.to@gmail.com>
49  * @author   mac65 <mac65@mac65.com>
50  * @author   Mike Cochrane <mikec@mikenz.geek.nz>
51  * @author   Robin Millette <robin@millette.info>
52  * @author   Zach Copley <zach@status.net>
53  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
54  * @link     http://status.net/
55  */
56 class ApiTimelineMentionsAction extends ApiBareAuthAction
57 {
58     var $notices = null;
59
60     /**
61      * Take arguments for running
62      *
63      * @param array $args $_REQUEST args
64      *
65      * @return boolean success flag
66      */
67     function prepare($args)
68     {
69         parent::prepare($args);
70
71         $this->user = $this->getTargetUser($this->arg('id'));
72
73         if (empty($this->user)) {
74             // TRANS: Client error displayed when requesting most recent mentions for a non-existing user.
75             $this->clientError(_('No such user.'), 404, $this->format);
76             return;
77         }
78
79         $this->notices = $this->getNotices();
80
81         return true;
82     }
83
84     /**
85      * Handle the request
86      *
87      * Just show the notices
88      *
89      * @param array $args $_REQUEST data (unused)
90      *
91      * @return void
92      */
93     function handle($args)
94     {
95         parent::handle($args);
96         $this->showTimeline();
97     }
98
99     /**
100      * Show the timeline of notices
101      *
102      * @return void
103      */
104     function showTimeline()
105     {
106         $profile = $this->user->getProfile();
107         $avatar     = $profile->getAvatar(AVATAR_PROFILE_SIZE);
108
109         $sitename   = common_config('site', 'name');
110         $title      = sprintf(
111             // TRANS: Title for timeline of most recent mentions of a user.
112             // TRANS: %1$s is the StatusNet sitename, %2$s is a user nickname.
113             _('%1$s / Updates mentioning %2$s'),
114             $sitename, $this->user->nickname
115         );
116         $taguribase = TagURI::base();
117         $id         = "tag:$taguribase:Mentions:" . $this->user->id;
118         $link       = common_local_url(
119             'replies',
120             array('nickname' => $this->user->nickname)
121         );
122
123         $self = $this->getSelfUri();
124
125         $subtitle   = sprintf(
126             // TRANS: Subtitle for timeline of most recent mentions of a user.
127             // TRANS: %1$s is the StatusNet sitename, %2$s is a user nickname,
128             // TRANS: %3$s is a user's full name.
129             _('%1$s updates that reply to updates from %2$s / %3$s.'),
130             $sitename, $this->user->nickname, $profile->getBestName()
131         );
132         $logo = ($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_PROFILE_SIZE);
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             $this->raw($atom->getString());
165
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         if (empty($this->auth_user)) {
195             $profile = null; 
196         } else {
197             $profile = $this->auth_user->getProfile();
198         }
199
200         $stream = new ReplyNoticeStream($this->user->id, $profile);
201
202         $notice = $stream->getNotices(($this->page - 1) * $this->count,
203                                       $this->count,
204                                       $this->since_id,
205                                       $this->max_id);
206
207         while ($notice->fetch()) {
208             $notices[] = clone($notice);
209         }
210
211         return $notices;
212     }
213
214     /**
215      * Is this action read only?
216      *
217      * @param array $args other arguments
218      *
219      * @return boolean true
220      */
221     function isReadOnly($args)
222     {
223         return true;
224     }
225
226     /**
227      * When was this feed last modified?
228      *
229      * @return string datestamp of the latest notice in the stream
230      */
231     function lastModified()
232     {
233         if (!empty($this->notices) && (count($this->notices) > 0)) {
234             return strtotime($this->notices[0]->created);
235         }
236
237         return null;
238     }
239
240     /**
241      * An entity tag for this stream
242      *
243      * Returns an Etag based on the action name, language, user ID, and
244      * timestamps of the first and last notice in the timeline
245      *
246      * @return string etag
247      */
248     function etag()
249     {
250         if (!empty($this->notices) && (count($this->notices) > 0)) {
251
252             $last = count($this->notices) - 1;
253
254             return '"' . implode(
255                 ':',
256                 array($this->arg('action'),
257                       common_user_cache_hash($this->auth_user),
258                       common_language(),
259                       $this->user->id,
260                       strtotime($this->notices[0]->created),
261                       strtotime($this->notices[$last]->created))
262             )
263             . '"';
264         }
265
266         return null;
267     }
268 }