deluserqueuehandler.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * Background job to delete prolific users without disrupting front-end too much.
  21. *
  22. * Up to 50 messages are deleted on each run through; when all messages are gone,
  23. * the actual account is deleted.
  24. *
  25. * @package QueueHandler
  26. * @maintainer Brion Vibber <brion@status.net>
  27. */
  28. class DelUserQueueHandler extends QueueHandler
  29. {
  30. public $widgetOpts;
  31. public $scoped;
  32. const DELETION_WINDOW = 50;
  33. public function transport()
  34. {
  35. return 'deluser';
  36. }
  37. public function handle($user) : bool
  38. {
  39. if (!($user instanceof User)) {
  40. common_log(LOG_ERR, "Got a bogus user, not deleting");
  41. return true;
  42. }
  43. $user = User::getKV('id', $user->id);
  44. if (!$user) {
  45. common_log(LOG_INFO, "User {$user->nickname} was deleted before we got here.");
  46. return true;
  47. }
  48. try {
  49. if (!$user->hasRole(Profile_role::DELETED)) {
  50. common_log(LOG_INFO, "User {$user->nickname} is not pending deletion; aborting.");
  51. return true;
  52. }
  53. } catch (UserNoProfileException $unp) {
  54. common_log(LOG_INFO, "Deleting user {$user->nickname} with no profile... probably a good idea!");
  55. }
  56. $notice = $this->getNextBatch($user);
  57. if ($notice->N) {
  58. common_log(LOG_INFO, "Deleting next {$notice->N} notices by {$user->nickname}");
  59. while ($notice->fetch()) {
  60. $del = clone($notice);
  61. $del->delete();
  62. }
  63. // @todo improve reliability in case we died during the above deletions
  64. // with a fatal error. If the job is lost, we should perform some kind
  65. // of garbage collection later.
  66. // Queue up the next batch.
  67. $qm = QueueManager::get();
  68. $qm->enqueue($user, 'deluser');
  69. } else {
  70. // Out of notices? Let's finish deleting this profile!
  71. try {
  72. $user->getProfile()->delete();
  73. } catch (UserNoProfileException $e) {
  74. // in case a profile didn't exist for some reason, just delete the User directly
  75. $user->delete();
  76. }
  77. common_log(LOG_INFO, "User $user->id $user->nickname deleted.");
  78. return true;
  79. }
  80. return true;
  81. }
  82. /**
  83. * Fetch the next self::DELETION_WINDOW messages for this user.
  84. * @return Notice
  85. */
  86. protected function getNextBatch(User $user)
  87. {
  88. $notice = new Notice();
  89. $notice->profile_id = $user->id;
  90. $notice->limit(self::DELETION_WINDOW);
  91. $notice->find();
  92. return $notice;
  93. }
  94. }