]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DirectMessage/actions/newmessage.php
Merge branch 'master' of https://git.gnu.io/gnu/gnu-social into social-master
[quix0rs-gnu-social.git] / plugins / DirectMessage / actions / newmessage.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Handler for posting new messages
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Personal
23  * @package   StatusNet
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/
31  */
32
33 if (!defined('GNUSOCIAL')) { exit(1); }
34
35 /**
36  * Action for posting new direct messages
37  *
38  * @category Personal
39  * @package  StatusNet
40  * @author   Evan Prodromou <evan@status.net>
41  * @author   Zach Copley <zach@status.net>
42  * @author   Sarven Capadisli <csarven@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  */
46
47 class NewmessageAction extends FormAction
48 {
49     var $content = null;
50     var $to = null;
51     var $other = null;
52
53     protected $form = 'Message';    // will become MessageForm later
54
55     /**
56      * Title of the page
57      *
58      * Note that this usually doesn't get called unless something went wrong
59      *
60      * @return string page title
61      */
62
63     function title()
64     {
65         // TRANS: Page title for new direct message page.
66         return _('New message');
67     }
68
69     protected function doPreparation()
70     {
71         $this->content = $this->trimmed('content');
72         $this->to = $this->trimmed('to');
73
74         if ($this->to) {
75
76             $this->other = Profile::getKV('id', $this->to);
77
78             if (!$this->other instanceof Profile) {
79                 // TRANS: Client error displayed trying to send a direct message to a non-existing user.
80                 $this->clientError(_('No such user.'), 404);
81             }
82
83             if (!$this->other->isLocal()) {
84                 // TRANS: Explains that current federation does not support direct, private messages yet.
85                 $this->clientError(_('You cannot send direct messages to federated users yet.'));
86             }
87
88             if (!$this->scoped->mutuallySubscribed($this->other)) {
89                 // TRANS: Client error displayed trying to send a direct message to a user while sender and
90                 // TRANS: receiver are not subscribed to each other.
91                 $this->clientError(_('You cannot send a message to this user.'), 404);
92             }
93         }
94
95         return true;
96     }
97
98     protected function doPost()
99     {
100         assert($this->scoped instanceof Profile); // XXX: maybe an error instead...
101
102         if (empty($this->content)) {
103             // TRANS: Form validator error displayed trying to send a direct message without content.
104             $this->clientError(_('No content!'));
105         }
106
107         $content_shortened = $this->scoped->shortenLinks($this->content);
108
109         if (Message::contentTooLong($content_shortened)) {
110             // TRANS: Form validation error displayed when message content is too long.
111             // TRANS: %d is the maximum number of characters for a message.
112             $this->clientError(sprintf(_m('That\'s too long. Maximum message size is %d character.',
113                                        'That\'s too long. Maximum message size is %d characters.',
114                                        Message::maxContent()),
115                                     Message::maxContent()));
116         }
117
118         if (!$this->other instanceof Profile) {
119             // TRANS: Form validation error displayed trying to send a direct message without specifying a recipient.
120             $this->clientError(_('No recipient specified.'));
121         } else if (!$this->scoped->mutuallySubscribed($this->other)) {
122             // TRANS: Client error displayed trying to send a direct message to a user while sender and
123             // TRANS: receiver are not subscribed to each other.
124             $this->clientError(_('You cannot send a message to this user.'), 404);
125         } else if ($this->scoped->id == $this->other->id) {
126             // TRANS: Client error displayed trying to send a direct message to self.
127             $this->clientError(_('Do not send a message to yourself; ' .
128                 'just say it to yourself quietly instead.'), 403);
129         }
130
131         $message = Message::saveNew($this->scoped->id, $this->other->id, $this->content, 'web');
132         $message->notify();
133
134         if (GNUsocial::isAjax()) {
135             // TRANS: Confirmation text after sending a direct message.
136             // TRANS: %s is the direct message recipient.
137             return sprintf(_('Direct message to %s sent.'), $this->other->getNickname());
138         }
139
140         $url = common_local_url('outbox', array('nickname' => $this->scoped->getNickname()));
141         common_redirect($url, 303);
142     }
143
144     function showNoticeForm()
145     {
146         // Just don't show a NoticeForm
147     }
148 }