]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/deluserqueuehandler.php
Merge branch 'testing' into 0.9.x
[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::staticGet('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         if (!$user->hasRole(Profile_role::DELETED)) {
53             common_log(LOG_INFO, "User {$user->nickname} is not pending deletion; aborting.");
54             return true;
55         }
56
57         $notice = $this->getNextBatch($user);
58         if ($notice->N) {
59             common_log(LOG_INFO, "Deleting next {$notice->N} notices by {$user->nickname}");
60             while ($notice->fetch()) {
61                 $del = clone($notice);
62                 $del->delete();
63             }
64
65             // @todo improve reliability in case we died during the above deletions
66             // with a fatal error. If the job is lost, we should perform some kind
67             // of garbage collection later.
68
69             // Queue up the next batch.
70             $qm = QueueManager::get();
71             $qm->enqueue($user, 'deluser');
72         } else {
73             // Out of notices? Let's finish deleting this guy!
74             $user->delete();
75             common_log(LOG_INFO, "User $user->id $user->nickname deleted.");
76             return true;
77         }
78
79         return true;
80     }
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
95 }