apiaccountupdatedeliverydevice.php 5.0 KB

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