Repository navigation
Expand file tree
/
Copy pathDeleteUser.php
More file actions
251 lines (223 loc) · 11.1 KB
/
Copy pathDeleteUser.php
File metadata and controls
251 lines (223 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
declare(strict_types=1);
namespace App\Actions;
use App\Enums\CommitUpdate;
use App\Enums\ConversationKind;
use App\Enums\UserUpdate;
use App\Events\CommitsUpdated;
use App\Events\UsersUpdated;
use App\Models\Commit;
use App\Models\CommitImage;
use App\Models\CommitReport;
use App\Models\Conversation;
use App\Models\ConversationParticipant;
use App\Models\Guild;
use App\Models\GuildSubmission;
use App\Models\Headline;
use App\Models\Message;
use App\Models\MessageImage;
use App\Models\MessageVoice;
use App\Models\Notification;
use App\Models\ScheduledCommit;
use App\Models\User;
use App\Queries\AdminsQuery;
use App\Queries\CommitBuilder;
use App\Services\CommitTranslations;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
final readonly class DeleteUser
{
public function __construct(private UpdateCommitHotScores $updateCommitHotScores, private DeleteOrgs $deleteOrgs)
{
//
}
/**
* The user's top-level commits are erased with every reply under them, and so are their replies, except the ones
* other people replied to: those stay behind as tombstones with no author or body, so the replies under them keep their thread.
* What they posted for organizations stays, and only forgets who posted it.
* The database cascades the image rows of erased commits and the user's scheduled commits, and the voice notes of their
* messages, but the files are erased here. Orgs the user was the last one in go too, with everything sent in them,
* as they do when the last member leaves. Only their GitHub ID is kept, so looking their handle up never brings their
* page back as an unclaimed one, until they sign in again.
*/
public function handle(User $user): void
{
DB::transaction(function () use ($user): void {
User::query()->whereKey($user->id)->lockForUpdate()->firstOrFail();
$this->updateCommitHotScores->handle($this->engagedCommitIds($user));
$translationKeys = $this->touchedCommits($user)
->select(['id', 'body'])
->lazyById(500)
->map(resolve(CommitTranslations::class)->key(...))->unique()->all();
// Read before anything goes, to tell whoever showed or heard about it once it is gone.
$vanishing = $this->touchedCommits($user)->approved()->get(['id', 'parent_id', 'quoted_commit_id']);
$notified = Notification::query()->where('actor_id', $user->id)->distinct()->pluck('recipient_id');
$partners = ConversationParticipant::query()
->whereIn('conversation_id', ConversationParticipant::query()->where('user_id', $user->id)->select('conversation_id'))
->whereNot('user_id', $user->id)
->distinct()
->pluck('user_id');
// Locked first, in statements of their own, so a topic lookup running meanwhile either links before this deletes its
// links or finds the commit erased. The threads are locked before their replies, so no reply joins them in between.
$user->commits()->withTrashed()->topLevel()->orderBy('id')->lockForUpdate()->pluck('id');
$this->touchedCommits($user)->orderBy('id')->lockForUpdate()->pluck('id');
// Report creation holds shared reporter and commit locks until its attachment paths are saved.
$guilds = Guild::query()->where('user_id', $user->id)->orderBy('id')->lockForUpdate()->get();
$guildMembers = DB::table('guild_preferences')->whereIn('guild_id', $guilds->modelKeys())->whereNot('user_id', $user->id)->pluck('user_id');
$imagePaths = [...$this->reportImagePaths($user), ...$this->imagePaths($user), ...$this->scheduledImagePaths($user), ...$this->messageImagePaths($user), ...$this->messageVoicePaths($user), ...$this->guildSubmissionIconPaths($user)];
resolve(DeleteStoredFiles::class)->retain($imagePaths);
DB::table('commit_topics')->whereIn('commit_id', $this->touchedCommits($user)->select('id'))->delete();
Headline::query()->whereIn('id', DB::table('commit_headline')->select('headline_id')
->whereIn('commit_id', $this->touchedCommits($user)->select('id')))
->update(['summary' => null, 'summary_created_at' => null]);
DB::table('commit_headline')->whereIn('commit_id', $this->touchedCommits($user)->select('id'))->delete();
$user->commits()->withTrashed()->topLevel()->forceDelete();
CommitImage::query()->whereIn('commit_id', $this->replies($user)->select('id'))->delete();
CommitReport::query()->whereIn('commit_id', $this->replies($user)->select('id'))->delete();
while ($this->replies($user)->whereDoesntHave('replies', fn (CommitBuilder $replies): CommitBuilder => $replies->withTrashed())->forceDelete() > 0) {
//
}
$user->commits()->delete();
$user->commits()->withTrashed()->update(['user_id' => null, 'author_id' => null, 'body' => '', 'mentions' => null, 'embedding' => null, 'link_preview_id' => null, 'headlines_classified_at' => null]);
// What they posted for organizations is the organizations' own, so it stays, without who posted it.
Commit::query()->withTrashed()->where('user_id', $user->id)->update(['user_id' => null]);
$this->deleteOrgs->handle($this->emptyOrgs($user));
$user->organizations()->detach();
$user->followedOrganizations()->detach();
DB::table('user_active_days')->where('user_id', $user->id)->delete();
$user->delete();
$user->profile->delete();
DB::table('deleted_github_accounts')->insertOrIgnore(['github_id' => $user->github_id]);
$vanishing->chunk(100)->each(fn (Collection $commits) => event(new CommitsUpdated($commits, CommitUpdate::Visibility)));
event(new UsersUpdated($notified, UserUpdate::Notifications));
event(new UsersUpdated($partners, UserUpdate::Inbox));
event(new UsersUpdated($guildMembers, UserUpdate::Guilds));
event(new UsersUpdated($guildMembers, UserUpdate::Relationships));
event(new UsersUpdated(resolve(AdminsQuery::class)->ids(), UserUpdate::Moderation));
foreach ($translationKeys as $key) {
Cache::forget($key);
}
});
}
/**
* The commits the user starred, and their replies and quotes, whose threads rank lower once the user's stars,
* replies and quotes are gone.
*
* @return array<int, string>
*/
private function engagedCommitIds(User $user): array
{
return [
...$user->starredCommits()->get(['commits.id'])->map(fn (Commit $commit): string => $commit->id)->all(),
...$user->commits()
->withTrashed()
->where(fn (CommitBuilder $query): CommitBuilder => $query->whereNotNull('root_id')->orWhereNotNull('quoted_commit_id'))
->get(['id'])
->map(fn (Commit $commit): string => $commit->id)
->all(),
];
}
/**
* The user's commits and every commit in the threads they started: all the erasure deletes or leaves as a tombstone.
*/
private function touchedCommits(User $user): CommitBuilder
{
return Commit::query()->withTrashed()
->where(fn (CommitBuilder $query): CommitBuilder => $query->where('author_id', $user->id)
->orWhereIn('root_id', $user->commits()->withTrashed()->topLevel()->select('id')));
}
/**
* @return HasMany<Commit, User>
*/
private function replies(User $user): HasMany
{
return $user->commits()->withTrashed()->whereNotNull('parent_id');
}
/** @return array<int, string> */
private function reportImagePaths(User $user): array
{
return CommitReport::query()
->where('reporter_id', $user->id)
->orWhereIn('commit_id', $user->commits()->withTrashed()->select('id'))
->orWhereIn('commit_id', Commit::query()->withTrashed()->whereIn('root_id', $user->commits()->withTrashed()->topLevel()->select('id'))->select('id'))
->get(['images'])
->flatMap(fn (CommitReport $report): array => array_column($report->images ?? [], 'path'))
->all();
}
/**
* The images of the user's commits and of every reply in the threads they started, which go along with them.
*
* @return array<int, string>
*/
private function imagePaths(User $user): array
{
return CommitImage::query()
->whereIn('commit_id', $user->commits()->withTrashed()->select('id'))
->orWhereIn('commit_id', Commit::query()->withTrashed()->whereIn('root_id', $user->commits()->withTrashed()->topLevel()->select('id'))->select('id'))
->get(['path'])
->map(fn (CommitImage $image): string => $image->path)
->all();
}
/**
* @return array<int, string>
*/
private function guildSubmissionIconPaths(User $user): array
{
return GuildSubmission::query()
->where('user_id', $user->id)
->get(['icon_path'])
->flatMap(fn (GuildSubmission $submission): array => $submission->icon_path === null ? [] : [$submission->icon_path])
->all();
}
/**
* @return array<int, string>
*/
private function scheduledImagePaths(User $user): array
{
return ScheduledCommit::query()
->where('user_id', $user->id)
->whereNotNull('images')
->get(['images'])
->flatMap(fn (ScheduledCommit $scheduled): array => array_column($scheduled->images ?? [], 'path'))
->all();
}
/**
* The images the user sent in remotes, whose messages go along with them.
*
* @return array<int, string>
*/
private function messageImagePaths(User $user): array
{
return MessageImage::query()
->whereIn('message_id', Message::query()->where('user_id', $user->id)->select('id'))
->get(['path'])
->map(fn (MessageImage $image): string => $image->path)
->all();
}
/**
* The voice notes the user sent in remotes, whose messages go along with them.
*
* @return array<int, string>
*/
private function messageVoicePaths(User $user): array
{
return MessageVoice::query()
->whereIn('message_id', Message::query()->where('user_id', $user->id)->select('id'))
->get(['path'])
->map(fn (MessageVoice $voice): string => $voice->path)
->all();
}
/**
* @return Builder<Conversation>
*/
private function emptyOrgs(User $user): Builder
{
return Conversation::query()
->where('kind', ConversationKind::Org)
->whereHas('participants', fn (Builder $query): Builder => $query->where('user_id', $user->id))
->whereDoesntHave('participants', fn (Builder $query): Builder => $query->where('user_id', '!=', $user->id));
}
}