]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apidirectmessagenew.php
* improve L10n consistency for English. For example proper punctuation for all button...
[quix0rs-gnu-social.git] / actions / apidirectmessagenew.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Send a direct message via the API
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  API
23  * @package   StatusNet
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/
31  */
32
33 if (!defined('STATUSNET')) {
34     exit(1);
35 }
36
37 require_once INSTALLDIR . '/lib/apiauth.php';
38
39 /**
40  * Creates a new direct message from the authenticating user to
41  * the user specified by id.
42  *
43  * @category API
44  * @package  StatusNet
45  * @author   Adrian Lang <mail@adrianlang.de>
46  * @author   Evan Prodromou <evan@status.net>
47  * @author   Robin Millette <robin@millette.info>
48  * @author   Zach Copley <zach@status.net>
49  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50  * @link     http://status.net/
51  */
52 class ApiDirectMessageNewAction extends ApiAuthAction
53 {
54     var $other   = null;
55     var $content = null;
56
57     /**
58      * Take arguments for running
59      *
60      * @param array $args $_REQUEST args
61      *
62      * @return boolean success flag
63      */
64     function prepare($args)
65     {
66         parent::prepare($args);
67
68         $this->user = $this->auth_user;
69
70         if (empty($this->user)) {
71             // TRANS: Client error when user not found for an API direct message action.
72             $this->clientError(_('No such user.'), 404, $this->format);
73             return;
74         }
75
76         $this->content = $this->trimmed('text');
77
78         $this->user  = $this->auth_user;
79
80         $user_param  = $this->trimmed('user');
81         $user_id     = $this->arg('user_id');
82         $screen_name = $this->trimmed('screen_name');
83
84         if (isset($user_param) || isset($user_id) || isset($screen_name)) {
85             $this->other = $this->getTargetUser($user_param);
86         }
87
88         return true;
89     }
90
91     /**
92      * Handle the request
93      *
94      * Save the new message
95      *
96      * @param array $args $_REQUEST data (unused)
97      *
98      * @return void
99      */
100     function handle($args)
101     {
102         parent::handle($args);
103
104         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
105             $this->clientError(
106                 // TRANS: Client error. POST is a HTTP command. It should not be translated.
107                 _('This method requires a POST.'),
108                 400,
109                 $this->format
110             );
111             return;
112         }
113
114         if (empty($this->content)) {
115             $this->clientError(
116                 // TRANS: Client error displayed when no message text was submitted (406).
117                 _('No message text!'),
118                 406,
119                 $this->format
120             );
121         } else {
122             $content_shortened = $this->auth_user->shortenLinks($this->content);
123             if (Message::contentTooLong($content_shortened)) {
124                 $this->clientError(
125                     // TRANS: Client error displayed when message content is too long.
126                     // TRANS: %d is the maximum number of characters for a message.
127                     sprintf(_m('That\'s too long. Maximum message size is %d character.', 'That\'s too long. Maximum message size is %d characters.', Message::maxContent()),
128                         Message::maxContent()
129                     ),
130                     406,
131                     $this->format
132                 );
133                 return;
134             }
135         }
136
137         if (empty($this->other)) {
138             // TRANS: Client error displayed if a recipient user could not be found (403).
139             $this->clientError(_('Recipient user not found.'), 403, $this->format);
140             return;
141         } else if (!$this->user->mutuallySubscribed($this->other)) {
142             $this->clientError(
143                 // TRANS: Client error displayed trying to direct message another user who's not a friend (403).
144                 _('Cannot send direct messages to users who aren\'t your friend.'),
145                 403,
146                 $this->format
147             );
148             return;
149         } else if ($this->user->id == $this->other->id) {
150
151             // Note: sending msgs to yourself is allowed by Twitter
152
153             // TRANS: Client error displayed trying to direct message self (403).
154             $this->clientError(_('Do not send a message to yourself; ' .
155                    'just say it to yourself quietly instead.'), 403, $this->format);
156             return;
157         }
158
159         $message = Message::saveNew(
160             $this->user->id,
161             $this->other->id,
162             html_entity_decode($this->content, ENT_NOQUOTES, 'UTF-8'),
163             $this->source
164         );
165
166         if (is_string($message)) {
167             $this->serverError($message);
168             return;
169         }
170
171         $message->notify();
172
173         if ($this->format == 'xml') {
174             $this->showSingleXmlDirectMessage($message);
175         } elseif ($this->format == 'json') {
176             $this->showSingleJsondirectMessage($message);
177         }
178     }
179 }