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