]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apimentions.php
Fix header comment
[quix0rs-gnu-social.git] / actions / apimentions.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 ApiMentionsAction 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         if ($this->requiresAuth()) {
72             if ($this->checkBasicAuthUser() == false) {
73                 return;
74             }
75         }
76
77         $this->user = $this->getTargetUser($this->arg('id'));
78
79         if (empty($this->user)) {
80             $this->clientError(_('No such user!'), 404, $this->arg('format'));
81             return;
82         }
83
84         $this->notices = $this->getNotices();
85
86         return true;
87     }
88
89     /**
90      * Handle the request
91      *
92      * Just show the notices
93      *
94      * @param array $args $_REQUEST data (unused)
95      *
96      * @return void
97      */
98
99     function handle($args)
100     {
101         parent::handle($args);
102         $this->showTimeline();
103     }
104
105     /**
106      * Show the timeline of notices
107      *
108      * @return void
109      */
110
111     function showTimeline()
112     {
113         $profile = $this->user->getProfile();
114
115         $sitename   = common_config('site', 'name');
116         $title      = sprintf(
117             _('%1$s / Updates mentioning %2$s'),
118             $sitename, $this->user->nickname
119         );
120         $taguribase = common_config('integration', 'taguri');
121         $id         = "tag:$taguribase:Mentions:" . $this->user->id;
122         $link       = common_local_url(
123             'replies',
124             array('nickname' => $this->user->nickname)
125         );
126         $subtitle   = sprintf(
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->arg('format')) {
132         case 'xml':
133             $this->show_xml_timeline($this->notices);
134             break;
135         case 'rss':
136             $this->show_rss_timeline($this->notices, $title, $link, $subtitle);
137             break;
138         case 'atom':
139             $selfuri = common_root_url() .
140                 ltrim($_SERVER['QUERY_STRING'], 'p=');
141             $this->show_atom_timeline(
142                 $this->notices, $title, $id, $link, $subtitle,
143                 null, $selfuri
144             );
145             break;
146         case 'json':
147             $this->show_json_timeline($this->notices);
148             break;
149         default:
150             $this->clientError(_('API method not found!'), $code = 404);
151             break;
152         }
153     }
154
155     /**
156      * Get notices
157      *
158      * @return array notices
159      */
160
161     function getNotices()
162     {
163         $notices = array();
164
165         $notice = $this->user->getReplies(
166             ($this->page - 1) * $this->count, $this->count,
167             $this->since_id, $this->max_id, $this->since
168         );
169
170         while ($notice->fetch()) {
171             $notices[] = clone($notice);
172         }
173
174         return $notices;
175     }
176
177     /**
178      * Is this action read only?
179      *
180      * @param array $args other arguments
181      *
182      * @return boolean true
183      */
184
185     function isReadOnly($args)
186     {
187         return true;
188     }
189
190     /**
191      * When was this feed last modified?
192      *
193      * @return string datestamp of the latest notice in the stream
194      */
195
196     function lastModified()
197     {
198         if (!empty($this->notices) && (count($this->notices) > 0)) {
199             return strtotime($this->notices[0]->created);
200         }
201
202         return null;
203     }
204
205     /**
206      * An entity tag for this stream
207      *
208      * Returns an Etag based on the action name, language, user ID, and
209      * timestamps of the first and last notice in the timeline
210      *
211      * @return string etag
212      */
213
214     function etag()
215     {
216         if (!empty($this->notices) && (count($this->notices) > 0)) {
217
218             $last = count($this->notices) - 1;
219
220             return '"' . implode(
221                 ':',
222                 array($this->arg('action'),
223                       common_language(),
224                       $this->user->id,
225                       strtotime($this->notices[0]->created),
226                       strtotime($this->notices[$last]->created))
227             )
228             . '"';
229         }
230
231         return null;
232     }
233
234 }