]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinementions.php
Properly unlink all old avatars when deleting/uploading a new
[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
108         $sitename   = common_config('site', 'name');
109         $title      = sprintf(
110             // TRANS: Title for timeline of most recent mentions of a user.
111             // TRANS: %1$s is the StatusNet sitename, %2$s is a user nickname.
112             _('%1$s / Updates mentioning %2$s'),
113             $sitename, $this->user->nickname
114         );
115         $taguribase = TagURI::base();
116         $id         = "tag:$taguribase:Mentions:" . $this->user->id;
117
118         $logo = $profile->avatarUrl(AVATAR_PROFILE_SIZE);
119         $link = common_local_url('replies',
120                     array('nickname' => $this->user->nickname));
121         $self = $this->getSelfUri();
122
123         $subtitle   = sprintf(
124             // TRANS: Subtitle for timeline of most recent mentions of a user.
125             // TRANS: %1$s is the StatusNet sitename, %2$s is a user nickname,
126             // TRANS: %3$s is a user's full name.
127             _('%1$s updates that reply to updates from %2$s / %3$s.'),
128             $sitename, $this->user->nickname, $profile->getBestName()
129         );
130
131         switch($this->format) {
132         case 'xml':
133             $this->showXmlTimeline($this->notices);
134             break;
135         case 'rss':
136             $this->showRssTimeline(
137                 $this->notices,
138                 $title,
139                 $link,
140                 $subtitle,
141                 null,
142                 $logo,
143                 $self
144             );
145             break;
146         case 'atom':
147             header('Content-Type: application/atom+xml; charset=utf-8');
148
149             $atom = new AtomNoticeFeed($this->auth_user);
150
151             $atom->setId($id);
152             $atom->setTitle($title);
153             $atom->setSubtitle($subtitle);
154             $atom->setLogo($logo);
155             $atom->setUpdated('now');
156
157             $atom->addLink($link);
158             $atom->setSelfLink($self);
159
160             $atom->addEntryFromNotices($this->notices);
161             $this->raw($atom->getString());
162
163             break;
164         case 'json':
165             $this->showJsonTimeline($this->notices);
166             break;
167         case 'as':
168             header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
169             $doc = new ActivityStreamJSONDocument($this->auth_user);
170             $doc->setTitle($title);
171             $doc->addLink($link, 'alternate', 'text/html');
172             $doc->addItemsFromNotices($this->notices);
173             $this->raw($doc->asString());
174             break;
175         default:
176             // TRANS: Client error displayed when coming across a non-supported API method.
177             $this->clientError(_('API method not found.'), $code = 404);
178             break;
179         }
180     }
181
182     /**
183      * Get notices
184      *
185      * @return array notices
186      */
187     function getNotices()
188     {
189         $notices = array();
190
191         if (empty($this->auth_user)) {
192             $profile = null; 
193         } else {
194             $profile = $this->auth_user->getProfile();
195         }
196
197         $stream = new ReplyNoticeStream($this->user->id, $profile);
198
199         $notice = $stream->getNotices(($this->page - 1) * $this->count,
200                                       $this->count,
201                                       $this->since_id,
202                                       $this->max_id);
203
204         while ($notice->fetch()) {
205             $notices[] = clone($notice);
206         }
207
208         return $notices;
209     }
210
211     /**
212      * Is this action read only?
213      *
214      * @param array $args other arguments
215      *
216      * @return boolean true
217      */
218     function isReadOnly($args)
219     {
220         return true;
221     }
222
223     /**
224      * When was this feed last modified?
225      *
226      * @return string datestamp of the latest notice in the stream
227      */
228     function lastModified()
229     {
230         if (!empty($this->notices) && (count($this->notices) > 0)) {
231             return strtotime($this->notices[0]->created);
232         }
233
234         return null;
235     }
236
237     /**
238      * An entity tag for this stream
239      *
240      * Returns an Etag based on the action name, language, user ID, and
241      * timestamps of the first and last notice in the timeline
242      *
243      * @return string etag
244      */
245     function etag()
246     {
247         if (!empty($this->notices) && (count($this->notices) > 0)) {
248
249             $last = count($this->notices) - 1;
250
251             return '"' . implode(
252                 ':',
253                 array($this->arg('action'),
254                       common_user_cache_hash($this->auth_user),
255                       common_language(),
256                       $this->user->id,
257                       strtotime($this->notices[0]->created),
258                       strtotime($this->notices[$last]->created))
259             )
260             . '"';
261         }
262
263         return null;
264     }
265 }