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