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