]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apidirectmessagenew.php
Introduced common_location_shared() to check if location sharing is always,
[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('GNUSOCIAL')) { exit(1); }
34
35 /**
36  * Creates a new direct message from the authenticating user to
37  * the user specified by id.
38  *
39  * @category API
40  * @package  StatusNet
41  * @author   Adrian Lang <mail@adrianlang.de>
42  * @author   Evan Prodromou <evan@status.net>
43  * @author   Robin Millette <robin@millette.info>
44  * @author   Zach Copley <zach@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46  * @link     http://status.net/
47  */
48 class ApiDirectMessageNewAction extends ApiAuthAction
49 {
50     protected $needPost = true;
51
52     var $other   = null;    // Profile we're sending to
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     protected function prepare(array $args=array())
63     {
64         parent::prepare($args);
65
66         if (!$this->scoped instanceof Profile) {
67             // TRANS: Client error when user not found for an API direct message action.
68             $this->clientError(_('No such user.'), 404);
69         }
70
71         $this->content = $this->trimmed('text');
72
73         $user_param  = $this->trimmed('user');
74         $user_id     = $this->arg('user_id');
75         $screen_name = $this->trimmed('screen_name');
76
77         if (isset($user_param) || isset($user_id) || isset($screen_name)) {
78             $this->other = $this->getTargetProfile($user_param);
79         }
80
81         return true;
82     }
83
84     /**
85      * Handle the request
86      *
87      * Save the new message
88      *
89      * @return void
90      */
91     protected function handle()
92     {
93         parent::handle();
94
95         if (empty($this->content)) {
96             // TRANS: Client error displayed when no message text was submitted (406).
97             $this->clientError(_('No message text!'), 406);
98         } else {
99             $content_shortened = $this->auth_user->shortenLinks($this->content);
100             if (Message::contentTooLong($content_shortened)) {
101                 // TRANS: Client error displayed when message content is too long.
102                 // TRANS: %d is the maximum number of characters for a message.
103                 $this->clientError(
104                     sprintf(_m('That\'s too long. Maximum message size is %d character.', 'That\'s too long. Maximum message size is %d characters.', Message::maxContent()), Message::maxContent()),
105                     406);
106             }
107         }
108
109         if (!$this->other instanceof Profile) {
110             // TRANS: Client error displayed if a recipient user could not be found (403).
111             $this->clientError(_('Recipient user not found.'), 403);
112         } else if (!$this->scoped->mutuallySubscribed($this->other)) {
113             // TRANS: Client error displayed trying to direct message another user who's not a friend (403).
114             $this->clientError(_('Cannot send direct messages to users who aren\'t your friend.'), 403);
115         } else if ($this->scoped->getID() === $this->other->getID()) {
116
117             // Note: sending msgs to yourself is allowed by Twitter
118
119             // TRANS: Client error displayed trying to direct message self (403).
120             $this->clientError(_('Do not send a message to yourself; just say it to yourself quietly instead.'), 403);
121         }
122
123         $message = Message::saveNew(
124             $this->scoped->getID(),
125             $this->other->getID(),
126             html_entity_decode($this->content, ENT_NOQUOTES, 'UTF-8'),
127             $this->source
128         );
129
130         $message->notify();
131
132         if ($this->format == 'xml') {
133             $this->showSingleXmlDirectMessage($message);
134         } elseif ($this->format == 'json') {
135             $this->showSingleJsondirectMessage($message);
136         }
137     }
138 }