]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apidirectmessagenew.php
$format is used by every API action. Set it in the base class.
[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    Zach Copley <zach@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/apiauth.php';
35
36 /**
37  * Creates a new direct message from the authenticating user to
38  * the user specified by id.
39  *
40  * @category API
41  * @package  StatusNet
42  * @author   Zach Copley <zach@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 ApiDirectMessageNewAction extends ApiAuthAction
48 {
49     var $source  = null;
50     var $user    = null;
51     var $other   = null;
52     var $content = null;
53
54     /**
55      * Take arguments for running
56      *
57      * @param array $args $_REQUEST args
58      *
59      * @return boolean success flag
60      *
61      */
62
63     function prepare($args)
64     {
65         parent::prepare($args);
66
67         if ($this->requiresAuth()) {
68             if ($this->checkBasicAuthUser() == false) {
69                 return;
70             }
71         }
72
73         $this->user = $this->auth_user;
74
75         if (empty($this->user)) {
76             $this->clientError(_('No such user!'), 404, $this->format);
77             return;
78         }
79
80         $this->source = $this->trimmed('source'); // Not supported by Twitter.
81
82         $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
83         if (empty($thtis->source) || in_array($this->source, $reserved_sources)) {
84             $source = 'api';
85         }
86
87         $this->content = $this->trimmed('text');
88
89         $this->user  = $this->auth_user;
90
91         $user_param  = $this->trimmed('user');
92         $user_id     = $this->arg('user_id');
93         $screen_name = $this->trimmed('screen_name');
94
95         if (isset($user_param) || isset($user_id) || isset($screen_name)) {
96             $this->other = $this->getTargetUser($user_param);
97         }
98
99         return true;
100     }
101
102     /**
103      * Handle the request
104      *
105      * Save the new message
106      *
107      * @param array $args $_REQUEST data (unused)
108      *
109      * @return void
110      */
111
112     function handle($args)
113     {
114         parent::handle($args);
115
116         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
117             $this->clientError(
118                 _('This method requires a POST.'),
119                 400,
120                 $this->format
121             );
122             return;
123         }
124
125         if (empty($this->content)) {
126             $this->clientError(
127                 _('No message text!'),
128                 406,
129                 $this->format
130             );
131         } else {
132             $content_shortened = common_shorten_links($this->content);
133             if (Message::contentTooLong($content_shortened)) {
134                 $this->clientError(
135                     sprintf(
136                         _('That\'s too long. Max message size is %d chars.'),
137                         Message::maxContent()
138                     ),
139                     406,
140                     $this->format
141                 );
142                 return;
143             }
144         }
145
146         if (empty($this->other)) {
147             $this->clientError(_('Recipient user not found.'), 403, $this->format);
148             return;
149         } else if (!$this->user->mutuallySubscribed($this->other)) {
150             $this->clientError(
151                 _('Can\'t send direct messages to users who aren\'t your friend.'),
152                 403,
153                 $this->format
154             );
155             return;
156         } else if ($this->user->id == $this->other->id) {
157
158             // Note: sending msgs to yourself is allowed by Twitter
159
160             $errmsg = 'Don\'t send a message to yourself; ' . 
161                    'just say it to yourself quietly instead.'
162
163             $this->clientError(_($errmsg), 403, $this->format);
164             return;
165         }
166
167         $message = Message::saveNew(
168             $this->user->id,
169             $this->other->id,
170             html_entity_decode($this->content, ENT_NOQUOTES, 'UTF-8'),
171             $this->source
172         );
173
174         if (is_string($message)) {
175             $this->serverError($message);
176             return;
177         }
178
179         mail_notify_message($message, $this->user, $this->other);
180
181         if ($this->format == 'xml') {
182             $this->showSingleXmlDirectMessage($message);
183         } elseif ($this->format == 'json') {
184             $this->showSingleJsondirectMessage($message);
185         }
186     }
187
188 }
189