deluserqueuehandler.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. const DELETION_WINDOW = 50;
  31. public function transport()
  32. {
  33. return 'deluser';
  34. }
  35. public function handle($user) : bool
  36. {
  37. if (!($user instanceof User)) {
  38. common_log(LOG_ERR, "Got a bogus user, not deleting");
  39. return true;
  40. }
  41. $user = User::getKV('id', $user->id);
  42. if (!$user) {
  43. common_log(LOG_INFO, "User {$user->nickname} was deleted before we got here.");
  44. return true;
  45. }
  46. try {
  47. if (!$user->hasRole(Profile_role::DELETED)) {
  48. common_log(LOG_INFO, "User {$user->nickname} is not pending deletion; aborting.");
  49. return true;
  50. }
  51. } catch (UserNoProfileException $unp) {
  52. common_log(LOG_INFO, "Deleting user {$user->nickname} with no profile... probably a good idea!");
  53. }
  54. $notice = $this->getNextBatch($user);
  55. if ($notice->N) {
  56. common_log(LOG_INFO, "Deleting next {$notice->N} notices by {$user->nickname}");
  57. while ($notice->fetch()) {
  58. $del = clone($notice);
  59. $del->delete();
  60. }
  61. // @todo improve reliability in case we died during the above deletions
  62. // with a fatal error. If the job is lost, we should perform some kind
  63. // of garbage collection later.
  64. // Queue up the next batch.
  65. $qm = QueueManager::get();
  66. $qm->enqueue($user, 'deluser');
  67. } else {
  68. // Out of notices? Let's finish deleting this profile!
  69. try {
  70. $user->getProfile()->delete();
  71. } catch (UserNoProfileException $e) {
  72. // in case a profile didn't exist for some reason, just delete the User directly
  73. $user->delete();
  74. }
  75. common_log(LOG_INFO, "User $user->id $user->nickname deleted.");
  76. return true;
  77. }
  78. return true;
  79. }
  80. /**
  81. * Fetch the next self::DELETION_WINDOW messages for this user.
  82. * @return Notice
  83. */
  84. protected function getNextBatch(User $user)
  85. {
  86. $notice = new Notice();
  87. $notice->profile_id = $user->id;
  88. $notice->limit(self::DELETION_WINDOW);
  89. $notice->find();
  90. return $notice;
  91. }
  92. }