3 * StatusNet, the distributed open-source microblogging tool
5 * Send a direct message via the API
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.
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.
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/>.
24 * @author Adrian Lang <mail@adrianlang.de>
25 * @author Evan Prodromou <evan@status.net>
26 * @author Robin Millette <robin@millette.info>
27 * @author Zach Copley <zach@status.net>
28 * @copyright 2009 StatusNet, Inc.
29 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30 * @link http://status.net/
33 if (!defined('STATUSNET')) {
37 require_once INSTALLDIR . '/lib/apiauth.php';
40 * Creates a new direct message from the authenticating user to
41 * the user specified by id.
45 * @author Adrian Lang <mail@adrianlang.de>
46 * @author Evan Prodromou <evan@status.net>
47 * @author Robin Millette <robin@millette.info>
48 * @author Zach Copley <zach@status.net>
49 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50 * @link http://status.net/
53 class ApiDirectMessageNewAction extends ApiAuthAction
59 * Take arguments for running
61 * @param array $args $_REQUEST args
63 * @return boolean success flag
67 function prepare($args)
69 parent::prepare($args);
71 $this->user = $this->auth_user;
73 if (empty($this->user)) {
74 $this->clientError(_('No such user.'), 404, $this->format);
78 $this->content = $this->trimmed('text');
80 $this->user = $this->auth_user;
82 $user_param = $this->trimmed('user');
83 $user_id = $this->arg('user_id');
84 $screen_name = $this->trimmed('screen_name');
86 if (isset($user_param) || isset($user_id) || isset($screen_name)) {
87 $this->other = $this->getTargetUser($user_param);
96 * Save the new message
98 * @param array $args $_REQUEST data (unused)
103 function handle($args)
105 parent::handle($args);
107 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
109 _('This method requires a POST.'),
116 if (empty($this->content)) {
118 _('No message text!'),
123 $content_shortened = common_shorten_links($this->content);
124 if (Message::contentTooLong($content_shortened)) {
127 _('That\'s too long. Max message size is %d chars.'),
128 Message::maxContent()
137 if (empty($this->other)) {
138 $this->clientError(_('Recipient user not found.'), 403, $this->format);
140 } else if (!$this->user->mutuallySubscribed($this->other)) {
142 _('Can\'t send direct messages to users who aren\'t your friend.'),
147 } else if ($this->user->id == $this->other->id) {
149 // Note: sending msgs to yourself is allowed by Twitter
151 $errmsg = 'Don\'t send a message to yourself; ' .
152 'just say it to yourself quietly instead.';
154 $this->clientError(_($errmsg), 403, $this->format);
158 $message = Message::saveNew(
161 html_entity_decode($this->content, ENT_NOQUOTES, 'UTF-8'),
165 if (is_string($message)) {
166 $this->serverError($message);
172 if ($this->format == 'xml') {
173 $this->showSingleXmlDirectMessage($message);
174 } elseif ($this->format == 'json') {
175 $this->showSingleJsondirectMessage($message);