]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinementions.php
ecead98ccee8e35c2fe6f352783a8ac78ad80f4a
[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    Zach Copley <zach@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/apibareauth.php';
35
36 /**
37  * Returns the most recent (default 20) mentions (status containing @nickname)
38  *
39  * @category API
40  * @package  StatusNet
41  * @author   Zach Copley <zach@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  */
45
46 class ApiTimelineMentionsAction extends ApiBareAuthAction
47 {
48
49     var $user    = null;
50     var $notices = null;
51
52     /**
53      * Take arguments for running
54      *
55      * @param array $args $_REQUEST args
56      *
57      * @return boolean success flag
58      *
59      */
60
61     function prepare($args)
62     {
63         parent::prepare($args);
64
65         $this->page     = (int)$this->arg('page', 1);
66         $this->count    = (int)$this->arg('count', 20);
67         $this->max_id   = (int)$this->arg('max_id', 0);
68         $this->since_id = (int)$this->arg('since_id', 0);
69         $this->since    = $this->arg('since');
70
71         $this->user = $this->getTargetUser($this->arg('id'));
72
73         if (empty($this->user)) {
74             $this->clientError(_('No such user!'), 404, $this->arg('format'));
75             return;
76         }
77
78         $this->notices = $this->getNotices();
79
80         return true;
81     }
82
83     /**
84      * Handle the request
85      *
86      * Just show the notices
87      *
88      * @param array $args $_REQUEST data (unused)
89      *
90      * @return void
91      */
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
105     function showTimeline()
106     {
107         $profile = $this->user->getProfile();
108
109         $sitename   = common_config('site', 'name');
110         $title      = sprintf(
111             _('%1$s / Updates mentioning %2$s'),
112             $sitename, $this->user->nickname
113         );
114         $taguribase = common_config('integration', 'taguri');
115         $id         = "tag:$taguribase:Mentions:" . $this->user->id;
116         $link       = common_local_url(
117             'replies',
118             array('nickname' => $this->user->nickname)
119         );
120         $subtitle   = sprintf(
121             _('%1$s updates that reply to updates from %2$s / %3$s.'),
122             $sitename, $this->user->nickname, $profile->getBestName()
123         );
124
125         switch($this->arg('format')) {
126         case 'xml':
127             $this->show_xml_timeline($this->notices);
128             break;
129         case 'rss':
130             $this->show_rss_timeline($this->notices, $title, $link, $subtitle);
131             break;
132         case 'atom':
133             $selfuri = common_root_url() .
134                 ltrim($_SERVER['QUERY_STRING'], 'p=');
135             $this->show_atom_timeline(
136                 $this->notices, $title, $id, $link, $subtitle,
137                 null, $selfuri
138             );
139             break;
140         case 'json':
141             $this->show_json_timeline($this->notices);
142             break;
143         default:
144             $this->clientError(_('API method not found!'), $code = 404);
145             break;
146         }
147     }
148
149     /**
150      * Get notices
151      *
152      * @return array notices
153      */
154
155     function getNotices()
156     {
157         $notices = array();
158
159         $notice = $this->user->getReplies(
160             ($this->page - 1) * $this->count, $this->count,
161             $this->since_id, $this->max_id, $this->since
162         );
163
164         while ($notice->fetch()) {
165             $notices[] = clone($notice);
166         }
167
168         return $notices;
169     }
170
171     /**
172      * Is this action read only?
173      *
174      * @param array $args other arguments
175      *
176      * @return boolean true
177      */
178
179     function isReadOnly($args)
180     {
181         return true;
182     }
183
184     /**
185      * When was this feed last modified?
186      *
187      * @return string datestamp of the latest notice in the stream
188      */
189
190     function lastModified()
191     {
192         if (!empty($this->notices) && (count($this->notices) > 0)) {
193             return strtotime($this->notices[0]->created);
194         }
195
196         return null;
197     }
198
199     /**
200      * An entity tag for this stream
201      *
202      * Returns an Etag based on the action name, language, user ID, and
203      * timestamps of the first and last notice in the timeline
204      *
205      * @return string etag
206      */
207
208     function etag()
209     {
210         if (!empty($this->notices) && (count($this->notices) > 0)) {
211
212             $last = count($this->notices) - 1;
213
214             return '"' . implode(
215                 ':',
216                 array($this->arg('action'),
217                       common_language(),
218                       $this->user->id,
219                       strtotime($this->notices[0]->created),
220                       strtotime($this->notices[$last]->created))
221             )
222             . '"';
223         }
224
225         return null;
226     }
227
228 }