merge: Experimental improvements to Delete Account process (maybe resolves #1035) (!1021)

View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/1021

Closes #1035

Approved-by: dakkar <dakkar@thenautilus.net>
Approved-by: Marie <github@yuugi.dev>
This commit is contained in:
Hazelnoot 2025-05-15 13:00:47 +00:00
commit a2a38c78db
3 changed files with 240 additions and 60 deletions

View file

@ -63,8 +63,6 @@ export class DeleteAccountService {
// 知り得る全SharedInboxにDelete配信 // 知り得る全SharedInboxにDelete配信
const content = this.apRendererService.addContext(this.apRendererService.renderDelete(this.userEntityService.genLocalUserUri(user.id), user)); const content = this.apRendererService.addContext(this.apRendererService.renderDelete(this.userEntityService.genLocalUserUri(user.id), user));
const queue: string[] = [];
const followings = await this.followingsRepository.find({ const followings = await this.followingsRepository.find({
where: [ where: [
{ followerSharedInbox: Not(IsNull()) }, { followerSharedInbox: Not(IsNull()) },
@ -73,22 +71,17 @@ export class DeleteAccountService {
select: ['followerSharedInbox', 'followeeSharedInbox'], select: ['followerSharedInbox', 'followeeSharedInbox'],
}); });
const inboxes = followings.map(x => x.followerSharedInbox ?? x.followeeSharedInbox); const inboxes = followings.map(x => [x.followerSharedInbox ?? x.followeeSharedInbox as string, true] as const);
const queue = new Map<string, true>(inboxes);
for (const inbox of inboxes) { await this.queueService.deliverMany(user, content, queue);
if (inbox != null && !queue.includes(inbox)) queue.push(inbox);
}
for (const inbox of queue) { await this.queueService.createDeleteAccountJob(user, {
this.queueService.deliver(user, content, inbox, true);
}
this.queueService.createDeleteAccountJob(user, {
soft: false, soft: false,
}); });
} else { } else {
// リモートユーザーの削除は、完全にDBから物理削除してしまうと再度連合してきてアカウントが復活する可能性があるため、soft指定する // リモートユーザーの削除は、完全にDBから物理削除してしまうと再度連合してきてアカウントが復活する可能性があるため、soft指定する
this.queueService.createDeleteAccountJob(user, { await this.queueService.createDeleteAccountJob(user, {
soft: true, soft: true,
}); });
} }

View file

@ -308,9 +308,9 @@ export class ReactionService {
} }
@bindThis @bindThis
public async delete(user: { id: MiUser['id']; host: MiUser['host']; isBot: MiUser['isBot']; }, note: MiNote) { public async delete(user: { id: MiUser['id']; host: MiUser['host']; isBot: MiUser['isBot']; }, note: MiNote, exist?: MiNoteReaction | null) {
// if already unreacted // if already unreacted
const exist = await this.noteReactionsRepository.findOneBy({ exist ??= await this.noteReactionsRepository.findOneBy({
noteId: note.id, noteId: note.id,
userId: user.id, userId: user.id,
}); });

View file

@ -4,9 +4,9 @@
*/ */
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import { MoreThan } from 'typeorm'; import { In, MoreThan } from 'typeorm';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import type { DriveFilesRepository, NoteReactionsRepository, NotesRepository, UserProfilesRepository, UsersRepository, NoteScheduleRepository, MiNoteSchedule } from '@/models/_.js'; import type { DriveFilesRepository, NoteReactionsRepository, NotesRepository, UserProfilesRepository, UsersRepository, NoteScheduleRepository, MiNoteSchedule, FollowingsRepository, FollowRequestsRepository, BlockingsRepository, MutingsRepository, ClipsRepository, ClipNotesRepository, LatestNotesRepository, NoteEditRepository, NoteFavoritesRepository, PollVotesRepository, PollsRepository, SigninsRepository, UserIpsRepository, RegistryItemsRepository } from '@/models/_.js';
import type Logger from '@/logger.js'; import type Logger from '@/logger.js';
import { DriveService } from '@/core/DriveService.js'; import { DriveService } from '@/core/DriveService.js';
import type { MiDriveFile } from '@/models/DriveFile.js'; import type { MiDriveFile } from '@/models/DriveFile.js';
@ -17,10 +17,10 @@ import { bindThis } from '@/decorators.js';
import { SearchService } from '@/core/SearchService.js'; import { SearchService } from '@/core/SearchService.js';
import { ApLogService } from '@/core/ApLogService.js'; import { ApLogService } from '@/core/ApLogService.js';
import { ReactionService } from '@/core/ReactionService.js'; import { ReactionService } from '@/core/ReactionService.js';
import { QueueService } from '@/core/QueueService.js';
import { QueueLoggerService } from '../QueueLoggerService.js'; import { QueueLoggerService } from '../QueueLoggerService.js';
import type * as Bull from 'bullmq'; import type * as Bull from 'bullmq';
import type { DbUserDeleteJobData } from '../types.js'; import type { DbUserDeleteJobData } from '../types.js';
import { QueueService } from '@/core/QueueService.js';
@Injectable() @Injectable()
export class DeleteAccountProcessorService { export class DeleteAccountProcessorService {
@ -45,6 +45,48 @@ export class DeleteAccountProcessorService {
@Inject(DI.noteScheduleRepository) @Inject(DI.noteScheduleRepository)
private noteScheduleRepository: NoteScheduleRepository, private noteScheduleRepository: NoteScheduleRepository,
@Inject(DI.followingsRepository)
private readonly followingsRepository: FollowingsRepository,
@Inject(DI.followRequestsRepository)
private readonly followRequestsRepository: FollowRequestsRepository,
@Inject(DI.blockingsRepository)
private readonly blockingsRepository: BlockingsRepository,
@Inject(DI.mutingsRepository)
private readonly mutingsRepository: MutingsRepository,
@Inject(DI.clipsRepository)
private readonly clipsRepository: ClipsRepository,
@Inject(DI.clipNotesRepository)
private readonly clipNotesRepository: ClipNotesRepository,
@Inject(DI.latestNotesRepository)
private readonly latestNotesRepository: LatestNotesRepository,
@Inject(DI.noteEditRepository)
private readonly noteEditRepository: NoteEditRepository,
@Inject(DI.noteFavoritesRepository)
private readonly noteFavoritesRepository: NoteFavoritesRepository,
@Inject(DI.pollVotesRepository)
private readonly pollVotesRepository: PollVotesRepository,
@Inject(DI.pollsRepository)
private readonly pollsRepository: PollsRepository,
@Inject(DI.signinsRepository)
private readonly signinsRepository: SigninsRepository,
@Inject(DI.userIpsRepository)
private readonly userIpsRepository: UserIpsRepository,
@Inject(DI.registryItemsRepository)
private readonly registryItemsRepository: RegistryItemsRepository,
private queueService: QueueService, private queueService: QueueService,
private driveService: DriveService, private driveService: DriveService,
private emailService: EmailService, private emailService: EmailService,
@ -65,6 +107,140 @@ export class DeleteAccountProcessorService {
return; return;
} }
{ // Delete user clips
const userClips = await this.clipsRepository.find({
select: {
id: true,
},
where: {
userId: user.id,
},
}) as { id: string }[];
// Delete one-at-a-time because there can be a lot
for (const clip of userClips) {
await this.clipNotesRepository.delete({
id: clip.id,
});
}
await this.clipsRepository.delete({
userId: user.id,
});
this.logger.succ('All clips have been deleted.');
}
{ // Delete favorites
await this.noteFavoritesRepository.delete({
userId: user.id,
});
this.logger.succ('All favorites have been deleted.');
}
{ // Delete user relations
await this.followingsRepository.delete({
followerId: user.id,
});
await this.followingsRepository.delete({
followeeId: user.id,
});
await this.followRequestsRepository.delete({
followerId: user.id,
});
await this.followRequestsRepository.delete({
followeeId: user.id,
});
await this.blockingsRepository.delete({
blockerId: user.id,
});
await this.blockingsRepository.delete({
blockeeId: user.id,
});
await this.mutingsRepository.delete({
muterId: user.id,
});
await this.mutingsRepository.delete({
muteeId: user.id,
});
this.logger.succ('All user relations have been deleted.');
}
{ // Delete reactions
let cursor: MiNoteReaction['id'] | null = null;
while (true) {
const reactions = await this.noteReactionsRepository.find({
where: {
userId: user.id,
...(cursor ? { id: MoreThan(cursor) } : {}),
},
take: 100,
order: {
id: 1,
},
relations: {
note: true,
},
}) as MiNoteReaction[];
if (reactions.length === 0) {
break;
}
cursor = reactions.at(-1)?.id ?? null;
for (const reaction of reactions) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const note = reaction.note!;
await this.reactionService.delete(user, note, reaction);
}
}
this.logger.succ('All reactions have been deleted');
}
{ // Poll votes
let cursor: MiNoteReaction['id'] | null = null;
while (true) {
const votes = await this.pollVotesRepository.find({
where: {
userId: user.id,
...(cursor ? { id: MoreThan(cursor) } : {}),
},
select: {
id: true,
},
take: 100,
order: {
id: 1,
},
}) as { id: string }[];
if (votes.length === 0) {
break;
}
cursor = votes.at(-1)?.id ?? null;
await this.pollVotesRepository.delete({
id: In(votes.map(v => v.id)),
});
}
this.logger.succ('All poll votes have been deleted');
}
{ // Delete scheduled notes { // Delete scheduled notes
const scheduledNotes = await this.noteScheduleRepository.findBy({ const scheduledNotes = await this.noteScheduleRepository.findBy({
userId: user.id, userId: user.id,
@ -82,6 +258,10 @@ export class DeleteAccountProcessorService {
} }
{ // Delete notes { // Delete notes
await this.latestNotesRepository.delete({
userId: user.id,
});
let cursor: MiNote['id'] | null = null; let cursor: MiNote['id'] | null = null;
while (true) { while (true) {
@ -102,7 +282,23 @@ export class DeleteAccountProcessorService {
cursor = notes.at(-1)?.id ?? null; cursor = notes.at(-1)?.id ?? null;
await this.notesRepository.delete(notes.map(note => note.id)); // Delete associated polls one-at-a-time, since it can cascade to a LOT of vote entries
for (const note of notes) {
if (note.hasPoll) {
await this.pollsRepository.delete({
noteId: note.id,
});
}
}
const ids = notes.map(note => note.id);
await this.noteEditRepository.delete({
noteId: In(ids),
});
await this.notesRepository.delete({
id: In(ids),
});
for (const note of notes) { for (const note of notes) {
await this.searchService.unindexNote(note); await this.searchService.unindexNote(note);
@ -119,37 +315,6 @@ export class DeleteAccountProcessorService {
this.logger.succ('All of notes deleted'); this.logger.succ('All of notes deleted');
} }
{ // Delete reactions
let cursor: MiNoteReaction['id'] | null = null;
while (true) {
const reactions = await this.noteReactionsRepository.find({
where: {
userId: user.id,
...(cursor ? { id: MoreThan(cursor) } : {}),
},
take: 100,
order: {
id: 1,
},
}) as MiNoteReaction[];
if (reactions.length === 0) {
break;
}
cursor = reactions.at(-1)?.id ?? null;
for (const reaction of reactions) {
const note = await this.notesRepository.findOneBy({ id: reaction.noteId }) as MiNote;
await this.reactionService.delete(user, note);
}
}
this.logger.succ('All reactions have been deleted');
}
{ // Delete files { // Delete files
let cursor: MiDriveFile['id'] | null = null; let cursor: MiDriveFile['id'] | null = null;
@ -191,20 +356,42 @@ export class DeleteAccountProcessorService {
this.logger.succ('All AP logs deleted'); this.logger.succ('All AP logs deleted');
} }
{ // Send email notification // Do this BEFORE deleting the account!
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: user.id }); const profile = await this.userProfilesRepository.findOneBy({ userId: user.id });
if (profile.email && profile.emailVerified) {
this.emailService.sendEmail(profile.email, 'Account deleted', { // Delete the actual account
'Your account has been deleted.', await this.userIpsRepository.delete({
'Your account has been deleted.'); userId: user.id,
} });
}
await this.signinsRepository.delete({
userId: user.id,
});
await this.registryItemsRepository.delete({
userId: user.id,
});
// soft指定されている場合は物理削除しない // soft指定されている場合は物理削除しない
if (job.data.soft) { if (job.data.soft) {
// nop // nop
} else { } else {
await this.usersRepository.delete(job.data.user.id); await this.usersRepository.delete(user.id);
}
this.logger.succ('Account data deleted');
}
{ // Send email notification
if (profile && profile.email && profile.emailVerified) {
try {
await this.emailService.sendEmail(profile.email, 'Account deleted',
'Your account has been deleted.',
'Your account has been deleted.');
} catch (e) {
this.logger.warn('Failed to send account deletion message:', { e });
}
}
} }
return 'Account deleted'; return 'Account deleted';