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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29 * @link http://status.net/
32 if (!defined('STATUSNET') && !defined('LACONICA')) {
37 * Action for posting new direct messages
41 * @author Evan Prodromou <evan@status.net>
42 * @author Zach Copley <zach@status.net>
43 * @author Sarven Capadisli <csarven@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
48 class NewmessageAction extends FormAction
57 * Note that this usually doesn't get called unless something went wrong
59 * @return string page title
64 // TRANS: Page title for new direct message page.
65 return _('New message');
69 * Handle input, produce output
71 * @param array $args $_REQUEST contents
76 protected function prepare($args)
78 parent::prepare($args);
80 if (!common_logged_in()) {
84 $user = $this->scoped->getUser();
86 $this->content = $this->trimmed('content');
87 $this->to = $this->trimmed('to');
91 $this->other = User::getKV('id', $this->to);
94 // TRANS: Client error displayed trying to send a direct message to a non-existing user.
95 $this->clientError(_('No such user.'), 404);
98 if (!$user->mutuallySubscribed($this->other)) {
99 // TRANS: Client error displayed trying to send a direct message to a user while sender and
100 // TRANS: receiver are not subscribed to each other.
101 $this->clientError(_('You cannot send a message to this user.'), 404);
108 protected function handlePost()
110 parent::handlePost();
112 assert($this->scoped); // XXX: maybe an error instead...
113 $user = $this->scoped->getUser();
115 if (!$this->content) {
116 // TRANS: Form validator error displayed trying to send a direct message without content.
117 $this->clientError(_('No content!'));
119 $content_shortened = $user->shortenLinks($this->content);
121 if (Message::contentTooLong($content_shortened)) {
122 // TRANS: Form validation error displayed when message content is too long.
123 // TRANS: %d is the maximum number of characters for a message.
124 $this->clientError(sprintf(_m('That\'s too long. Maximum message size is %d character.',
125 'That\'s too long. Maximum message size is %d characters.',
126 Message::maxContent()),
127 Message::maxContent()));
132 // TRANS: Form validation error displayed trying to send a direct message without specifying a recipient.
133 $this->clientError(_('No recipient specified.'));
134 } else if (!$user->mutuallySubscribed($this->other)) {
135 // TRANS: Client error displayed trying to send a direct message to a user while sender and
136 // TRANS: receiver are not subscribed to each other.
137 $this->clientError(_('You cannot send a message to this user.'), 404);
138 } else if ($user->id == $this->other->id) {
139 // TRANS: Client error displayed trying to send a direct message to self.
140 $this->clientError(_('Do not send a message to yourself; ' .
141 'just say it to yourself quietly instead.'), 403);
144 $message = Message::saveNew($user->id, $this->other->id, $this->content, 'web');
147 if ($this->boolean('ajax')) {
148 $this->startHTML('text/xml;charset=utf-8');
149 $this->elementStart('head');
150 // TRANS: Page title after sending a direct message.
151 $this->element('title', null, _('Message sent'));
152 $this->elementEnd('head');
153 $this->elementStart('body');
154 $this->element('p', array('id' => 'command_result'),
155 // TRANS: Confirmation text after sending a direct message.
156 // TRANS: %s is the direct message recipient.
157 sprintf(_('Direct message to %s sent.'),
158 $this->other->nickname));
159 $this->elementEnd('body');
160 $this->elementEnd('html');
162 $url = common_local_url('outbox',
163 array('nickname' => $user->nickname));
164 common_redirect($url, 303);
169 * Show an Ajax-y error message
171 * Goes back to the browser, where it's shown in a popup.
173 * @param string $msg Message to show
178 function ajaxErrorMsg($msg)
180 $this->startHTML('text/xml;charset=utf-8', true);
181 $this->elementStart('head');
182 // TRANS: Page title after an AJAX error occurred on the "send direct message" page.
183 $this->element('title', null, _('Ajax Error'));
184 $this->elementEnd('head');
185 $this->elementStart('body');
186 $this->element('p', array('id' => 'error'), $msg);
187 $this->elementEnd('body');
188 $this->elementEnd('html');
191 function showForm($msg = null)
193 if ($msg && $this->boolean('ajax')) {
194 $this->ajaxErrorMsg($msg);
199 if ($this->trimmed('ajax')) {
200 header('Content-Type: text/xml;charset=utf-8');
201 $this->xw->startDocument('1.0', 'UTF-8');
202 $this->elementStart('html');
203 $this->elementStart('head');
204 // TRANS: Page title on page for sending a direct message.
205 $this->element('title', null, _('New message'));
206 $this->elementEnd('head');
207 $this->elementStart('body');
208 $this->showNoticeForm();
209 $this->elementEnd('body');
217 function showPageNotice()
220 $this->element('p', 'error', $this->msg);
224 // Do nothing (override)
226 function showNoticeForm()
228 $message_form = new MessageForm($this, $this->other, $this->content);
229 $message_form->show();