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')) {
38 * Creates a new direct message from the authenticating user to
39 * the user specified by id.
43 * @author Adrian Lang <mail@adrianlang.de>
44 * @author Evan Prodromou <evan@status.net>
45 * @author Robin Millette <robin@millette.info>
46 * @author Zach Copley <zach@status.net>
47 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
48 * @link http://status.net/
50 class ApiDirectMessageNewAction extends ApiAuthAction
52 protected $needPost = true;
54 var $other = null; // Profile we're sending to
58 * Take arguments for running
60 * @param array $args $_REQUEST args
62 * @return boolean success flag
64 protected function prepare(array $args=array())
66 parent::prepare($args);
68 if (empty($this->user)) {
69 // TRANS: Client error when user not found for an API direct message action.
70 $this->clientError(_('No such user.'), 404);
73 $this->content = $this->trimmed('text');
75 $user_param = $this->trimmed('user');
76 $user_id = $this->arg('user_id');
77 $screen_name = $this->trimmed('screen_name');
79 if (isset($user_param) || isset($user_id) || isset($screen_name)) {
80 $this->other = $this->getTargetProfile($user_param);
89 * Save the new message
93 protected function handle()
97 if (empty($this->content)) {
98 // TRANS: Client error displayed when no message text was submitted (406).
99 $this->clientError(_('No message text!'), 406);
101 $content_shortened = $this->auth_user->shortenLinks($this->content);
102 if (Message::contentTooLong($content_shortened)) {
103 // TRANS: Client error displayed when message content is too long.
104 // TRANS: %d is the maximum number of characters for a message.
106 sprintf(_m('That\'s too long. Maximum message size is %d character.', 'That\'s too long. Maximum message size is %d characters.', Message::maxContent()), Message::maxContent()),
111 if (!$this->other instanceof Profile) {
112 // TRANS: Client error displayed if a recipient user could not be found (403).
113 $this->clientError(_('Recipient user not found.'), 403);
114 } else if (!$this->user->mutuallySubscribed($this->other)) {
115 // TRANS: Client error displayed trying to direct message another user who's not a friend (403).
116 $this->clientError(_('Cannot send direct messages to users who aren\'t your friend.'), 403);
117 } else if ($this->user->id == $this->other->id) {
119 // Note: sending msgs to yourself is allowed by Twitter
121 // TRANS: Client error displayed trying to direct message self (403).
122 $this->clientError(_('Do not send a message to yourself; just say it to yourself quietly instead.'), 403);
125 $message = Message::saveNew(
128 html_entity_decode($this->content, ENT_NOQUOTES, 'UTF-8'),
134 if ($this->format == 'xml') {
135 $this->showSingleXmlDirectMessage($message);
136 } elseif ($this->format == 'json') {
137 $this->showSingleJsondirectMessage($message);