]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/deluserqueuehandler.php
Merge remote-tracking branch 'upstream/master'
[quix0rs-gnu-social.git] / lib / deluserqueuehandler.php
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 /**
21  * Background job to delete prolific users without disrupting front-end too much.
22  *
23  * Up to 50 messages are deleted on each run through; when all messages are gone,
24  * the actual account is deleted.
25  *
26  * @package QueueHandler
27  * @maintainer Brion Vibber <brion@status.net>
28  */
29
30 class DelUserQueueHandler extends QueueHandler
31 {
32     const DELETION_WINDOW = 50;
33
34     public function transport()
35     {
36         return 'deluser';
37     }
38
39     public function handle($user)
40     {
41         if (!($user instanceof User)) {
42             common_log(LOG_ERR, "Got a bogus user, not deleting");
43             return true;
44         }
45
46         $user = User::getKV('id', $user->id);
47         if (!$user) {
48             common_log(LOG_INFO, "User {$user->nickname} was deleted before we got here.");
49             return true;
50         }
51
52         try {
53             if (!$user->hasRole(Profile_role::DELETED)) {
54                 common_log(LOG_INFO, "User {$user->nickname} is not pending deletion; aborting.");
55                 return true;
56             }
57         } catch (UserNoProfileException $unp) {
58             common_log(LOG_INFO, "Deleting user {$user->nickname} with no profile... probably a good idea!");
59         }
60
61         $notice = $this->getNextBatch($user);
62         if ($notice->N) {
63             common_log(LOG_INFO, "Deleting next {$notice->N} notices by {$user->nickname}");
64             while ($notice->fetch()) {
65                 $del = clone($notice);
66                 $del->delete();
67             }
68
69             // @todo improve reliability in case we died during the above deletions
70             // with a fatal error. If the job is lost, we should perform some kind
71             // of garbage collection later.
72
73             // Queue up the next batch.
74             $qm = QueueManager::get();
75             $qm->enqueue($user, 'deluser');
76         } else {
77             // Out of notices? Let's finish deleting this profile!
78             try {
79                 $user->getProfile()->delete();
80             } catch (UserNoProfileException $e) {
81                 // in case a profile didn't exist for some reason, just delete the User directly
82                 $user->delete();
83             }
84             common_log(LOG_INFO, "User $user->id $user->nickname deleted.");
85             return true;
86         }
87
88         return true;
89     }
90
91     /**
92      * Fetch the next self::DELETION_WINDOW messages for this user.
93      * @return Notice
94      */
95     protected function getNextBatch(User $user)
96     {
97         $notice = new Notice();
98         $notice->profile_id = $user->id;
99         $notice->limit(self::DELETION_WINDOW);
100         $notice->find();
101         return $notice;
102     }
103
104 }