3 * StatusNet, the distributed open-source microblogging tool
5 * Handler for posting new messages
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 Evan Prodromou <evan@status.net>
25 * @author Zach Copley <zach@status.net>
26 * @author Sarven Capadisli <csarven@status.net>
27 * @copyright 2008-2009 StatusNet, Inc.
28 * @copyright 2013 Free Software Foundation, 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 * Action for posting new direct messages
42 * @author Evan Prodromou <evan@status.net>
43 * @author Zach Copley <zach@status.net>
44 * @author Sarven Capadisli <csarven@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/
49 class NewmessageAction extends FormAction
58 * Note that this usually doesn't get called unless something went wrong
60 * @return string page title
65 // TRANS: Page title for new direct message page.
66 return _('New message');
70 * Handle input, produce output
72 * @param array $args $_REQUEST contents
77 protected function prepare($args)
79 parent::prepare($args);
81 $user = $this->scoped->getUser();
83 $this->content = $this->trimmed('content');
84 $this->to = $this->trimmed('to');
88 $this->other = User::getKV('id', $this->to);
91 // TRANS: Client error displayed trying to send a direct message to a non-existing user.
92 $this->clientError(_('No such user.'), 404);
95 if (!$user->mutuallySubscribed($this->other)) {
96 // TRANS: Client error displayed trying to send a direct message to a user while sender and
97 // TRANS: receiver are not subscribed to each other.
98 $this->clientError(_('You cannot send a message to this user.'), 404);
105 protected function handlePost()
107 parent::handlePost();
109 assert($this->scoped); // XXX: maybe an error instead...
110 $user = $this->scoped->getUser();
112 if (!$this->content) {
113 // TRANS: Form validator error displayed trying to send a direct message without content.
114 $this->clientError(_('No content!'));
116 $content_shortened = $user->shortenLinks($this->content);
118 if (Message::contentTooLong($content_shortened)) {
119 // TRANS: Form validation error displayed when message content is too long.
120 // TRANS: %d is the maximum number of characters for a message.
121 $this->clientError(sprintf(_m('That\'s too long. Maximum message size is %d character.',
122 'That\'s too long. Maximum message size is %d characters.',
123 Message::maxContent()),
124 Message::maxContent()));
129 // TRANS: Form validation error displayed trying to send a direct message without specifying a recipient.
130 $this->clientError(_('No recipient specified.'));
131 } else if (!$user->mutuallySubscribed($this->other)) {
132 // TRANS: Client error displayed trying to send a direct message to a user while sender and
133 // TRANS: receiver are not subscribed to each other.
134 $this->clientError(_('You cannot send a message to this user.'), 404);
135 } else if ($this->scoped->id == $this->other->id) {
136 // TRANS: Client error displayed trying to send a direct message to self.
137 $this->clientError(_('Do not send a message to yourself; ' .
138 'just say it to yourself quietly instead.'), 403);
141 $message = Message::saveNew($this->scoped->id, $this->other->id, $this->content, 'web');
144 if ($this->boolean('ajax')) {
145 $this->startHTML('text/xml;charset=utf-8');
146 $this->elementStart('head');
147 // TRANS: Page title after sending a direct message.
148 $this->element('title', null, _('Message sent'));
149 $this->elementEnd('head');
150 $this->elementStart('body');
151 $this->element('p', array('id' => 'command_result'),
152 // TRANS: Confirmation text after sending a direct message.
153 // TRANS: %s is the direct message recipient.
154 sprintf(_('Direct message to %s sent.'),
155 $this->other->nickname));
156 $this->elementEnd('body');
157 $this->elementEnd('html');
159 $url = common_local_url('outbox',
160 array('nickname' => $this->scoped->nickname));
161 common_redirect($url, 303);
166 * Show an Ajax-y error message
168 * Goes back to the browser, where it's shown in a popup.
170 * @param string $msg Message to show
175 function ajaxErrorMsg($msg)
177 $this->startHTML('text/xml;charset=utf-8', true);
178 $this->elementStart('head');
179 // TRANS: Page title after an AJAX error occurred on the "send direct message" page.
180 $this->element('title', null, _('Ajax Error'));
181 $this->elementEnd('head');
182 $this->elementStart('body');
183 $this->element('p', array('id' => 'error'), $msg);
184 $this->elementEnd('body');
185 $this->elementEnd('html');
188 function showForm($msg = null)
190 if ($msg && $this->boolean('ajax')) {
191 $this->ajaxErrorMsg($msg);
196 if ($this->trimmed('ajax')) {
197 $this->startHTML('text/xml;charset=utf-8');
198 $this->elementStart('head');
199 // TRANS: Page title on page for sending a direct message.
200 $this->element('title', null, _('New message'));
201 $this->elementEnd('head');
202 $this->elementStart('body');
203 $this->showNoticeForm();
204 $this->elementEnd('body');
212 function showPageNotice()
215 $this->element('p', 'error', $this->msg);
219 // Do nothing (override)
221 function showNoticeForm()
223 $message_form = new MessageForm($this, $this->other, $this->content);
224 $message_form->show();