]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiaccountupdatedeliverydevice.php
Using GNUSOCIAL_VERSION instead of STATUSNET_VERSION
[quix0rs-gnu-social.git] / actions / apiaccountupdatedeliverydevice.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Update the authenticating user notification channels
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    Siebrand Mazeland <s.mazeland@xs4all.nl>
25  * @author    Zach Copley <zach@status.net>
26  * @copyright 2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * Sets which channel (device) StatusNet delivers updates to for
37  * the authenticating user. Sending none as the device parameter
38  * will disable IM and/or SMS updates.
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 class ApiAccountUpdateDeliveryDeviceAction extends ApiAuthAction
47 {
48     protected $needPost = true;
49
50     /**
51      * Take arguments for running
52      *
53      * @param array $args $_REQUEST args
54      *
55      * @return boolean success flag
56      */
57     function prepare($args)
58     {
59         parent::prepare($args);
60
61         $this->user   = $this->auth_user;
62         $this->device = $this->trimmed('device');
63
64         return true;
65     }
66
67     /**
68      * Handle the request
69      *
70      * See which request params have been set, and update the user settings
71      *
72      * @param array $args $_REQUEST data (unused)
73      *
74      * @return void
75      */
76     function handle($args)
77     {
78         parent::handle($args);
79
80         if (!in_array($this->format, array('xml', 'json'))) {
81             $this->clientError(
82                 // TRANS: Client error displayed when coming across a non-supported API method.
83                 _('API method not found.'),
84                 404,
85                 $this->format
86             );
87             return;
88         }
89
90         // Note: Twitter no longer supports IM
91
92         if (!in_array(strtolower($this->device), array('sms', 'im', 'none'))) {
93             // TRANS: Client error displayed when no valid device parameter is provided for a user's delivery device setting.
94             $this->clientError(_( 'You must specify a parameter named ' .
95                                   '\'device\' with a value of one of: sms, im, none.' ));
96             return;
97         }
98
99         if (empty($this->user)) {
100             // TRANS: Client error displayed when no existing user is provided for a user's delivery device setting.
101             $this->clientError(_('No such user.'), 404);
102         }
103
104         $original = clone($this->user);
105
106         if (strtolower($this->device) == 'sms') {
107             $this->user->smsnotify = true;
108         } elseif (strtolower($this->device) == 'im') {
109             //TODO IM is pluginized now, so what should we do?
110             //Enable notifications for all IM plugins?
111             //For now, don't do anything
112             //$this->user->jabbernotify = true;
113         } elseif (strtolower($this->device == 'none')) {
114             $this->user->smsnotify    = false;
115             //TODO IM is pluginized now, so what should we do?
116             //Disable notifications for all IM plugins?
117             //For now, don't do anything
118             //$this->user->jabbernotify = false;
119         }
120
121         $result = $this->user->update($original);
122
123         if ($result === false) {
124             common_log_db_error($this->user, 'UPDATE', __FILE__);
125             // TRANS: Server error displayed when a user's delivery device cannot be updated.
126             $this->serverError(_('Could not update user.'));
127             return;
128         }
129
130         $profile = $this->user->getProfile();
131
132         $twitter_user = $this->twitterUserArray($profile, true);
133
134         // Note: this Twitter API method is retarded because it doesn't give
135         // any success/failure information. Twitter's docs claim that the
136         // notification field will change to reflect notification choice,
137         // but that's not true; notification> is used to indicate
138         // whether the auth user is following the user in question.
139
140         if ($this->format == 'xml') {
141             $this->initDocument('xml');
142             $this->showTwitterXmlUser($twitter_user, 'user', true);
143             $this->endDocument('xml');
144         } elseif ($this->format == 'json') {
145             $this->initDocument('json');
146             $this->showJsonObjects($twitter_user);
147             $this->endDocument('json');
148         }
149     }
150 }