]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apistatusesupdate.php
Implemented reply # command, allowing users to favorite specific notices by the notice id
[quix0rs-gnu-social.git] / actions / apistatusesupdate.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Post a notice (update your status) through the API
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    Tom Blankenship <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  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
33  * @link      http://status.net/
34  */
35
36 if (!defined('STATUSNET')) {
37     exit(1);
38 }
39
40 require_once INSTALLDIR . '/lib/apiauth.php';
41
42 /**
43  * Updates the authenticating user's status (posts a notice).
44  *
45  * @category API
46  * @package  StatusNet
47  * @author   Craig Andrews <candrews@integralblue.com>
48  * @author   Evan Prodromou <evan@status.net>
49  * @author   Jeffery To <jeffery.to@gmail.com>
50  * @author   Tom Blankenship <mac65@mac65.com>
51  * @author   Mike Cochrane <mikec@mikenz.geek.nz>
52  * @author   Robin Millette <robin@millette.info>
53  * @author   Zach Copley <zach@status.net>
54  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
55  * @link     http://status.net/
56  */
57
58 class ApiStatusesUpdateAction extends ApiAuthAction
59 {
60     var $source                = null;
61     var $status                = null;
62     var $in_reply_to_status_id = null;
63
64     static $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
65
66     /**
67      * Take arguments for running
68      *
69      * @param array $args $_REQUEST args
70      *
71      * @return boolean success flag
72      *
73      */
74
75     function prepare($args)
76     {
77         parent::prepare($args);
78
79         $this->user = $this->auth_user;
80
81         if (empty($this->user)) {
82             $this->clientError(_('No such user!'), 404, $this->format);
83             return false;
84         }
85
86         $this->status = $this->trimmed('status');
87
88         if (empty($this->status)) {
89             $this->clientError(
90                 'Client must provide a \'status\' parameter with a value.',
91                 400,
92                 $this->format
93             );
94
95             return false;
96         }
97
98         $this->source = $this->trimmed('source');
99
100         if (empty($this->source) || in_array($source, $this->reserved_sources)) {
101             $this->source = 'api';
102         }
103
104         $this->in_reply_to_status_id
105             = intval($this->trimmed('in_reply_to_status_id'));
106
107         return true;
108     }
109
110     /**
111      * Handle the request
112      *
113      * Make a new notice for the update, save it, and show it
114      *
115      * @param array $args $_REQUEST data (unused)
116      *
117      * @return void
118      */
119
120     function handle($args)
121     {
122         parent::handle($args);
123
124         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
125             $this->clientError(
126                 _('This method requires a POST.'),
127                 400, $this->format
128             );
129             return;
130         }
131
132         $status_shortened = common_shorten_links($this->status);
133
134         if (Notice::contentTooLong($status_shortened)) {
135
136             // Note: Twitter truncates anything over 140, flags the status
137             // as "truncated."
138
139             $this->clientError(
140                 sprintf(
141                     _('That\'s too long. Max notice size is %d chars.'),
142                     Notice::maxContent()
143                 ),
144                 406,
145                 $this->format
146             );
147
148             return;
149         }
150
151         // Check for commands
152
153         $inter = new CommandInterpreter();
154         $cmd = $inter->handle_command($this->user, $status_shortened);
155
156         if ($cmd) {
157
158             if ($this->supported($cmd)) {
159                 $cmd->execute(new Channel());
160             }
161
162             // Cmd not supported?  Twitter just returns your latest status.
163             // And, it returns your last status whether the cmd was successful
164             // or not!
165
166             $this->notice = $this->user->getCurrentNotice();
167
168         } else {
169
170             $reply_to = null;
171
172             if (!empty($this->in_reply_to_status_id)) {
173
174                 // Check whether notice actually exists
175
176                 $reply = Notice::staticGet($this->in_reply_to_status_id);
177
178                 if ($reply) {
179                     $reply_to = $this->in_reply_to_status_id;
180                 } else {
181                     $this->clientError(
182                         _('Not found'),
183                         $code = 404,
184                         $this->format
185                     );
186                     return;
187                 }
188             }
189
190             $this->notice = Notice::saveNew(
191                 $this->user->id,
192                 html_entity_decode($this->status, ENT_NOQUOTES, 'UTF-8'),
193                 $this->source,
194                 1,
195                 $reply_to
196             );
197
198             common_broadcast_notice($this->notice);
199         }
200
201         $this->showNotice();
202     }
203
204     /**
205      * Show the resulting notice
206      *
207      * @return void
208      */
209
210     function showNotice()
211     {
212         if (!empty($this->notice)) {
213             if ($this->format == 'xml') {
214                 $this->showSingleXmlStatus($this->notice);
215             } elseif ($this->format == 'json') {
216                 $this->show_single_json_status($this->notice);
217             }
218         }
219     }
220
221     /**
222      * Is this command supported when doing an update from the API?
223      *
224      * @param string $cmd the command to check for
225      *
226      * @return boolean true or false
227      */
228
229     function supported($cmd)
230     {
231         static $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand',
232             'FavCommand', 'OnCommand', 'OffCommand');
233
234         if (in_array(get_class($cmd), $cmdlist)) {
235             return true;
236         }
237
238         return false;
239     }
240
241 }