delete fetch logs when a note or user is deleted

This commit is contained in:
Hazelnoot 2025-02-02 23:19:41 -05:00
parent dbaeb7f2ac
commit 9de5ecae51
3 changed files with 50 additions and 2 deletions

View file

@ -5,7 +5,7 @@
import { createHash } from 'crypto'; import { createHash } from 'crypto';
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import { LessThan } from 'typeorm'; import { In, LessThan } from 'typeorm';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import { SkApFetchLog, SkApInboxLog, SkApContext } from '@/models/_.js'; import { SkApFetchLog, SkApInboxLog, SkApContext } from '@/models/_.js';
import type { ApContextsRepository, ApFetchLogsRepository, ApInboxLogsRepository } from '@/models/_.js'; import type { ApContextsRepository, ApFetchLogsRepository, ApInboxLogsRepository } from '@/models/_.js';
@ -121,6 +121,24 @@ export class ApLogService {
.execute(); .execute();
} }
/**
* Deletes all logged copies of an object or objects
* @param objectUris URIs / AP IDs of the objects to delete
*/
public async deleteObjectLogs(objectUris: string | string[]): Promise<number> {
if (Array.isArray(objectUris)) {
const logsDeleted = await this.apFetchLogsRepository.delete({
objectUri: In(objectUris),
});
return logsDeleted.affected ?? 0;
} else {
const logsDeleted = await this.apFetchLogsRepository.delete({
objectUri: objectUris,
});
return logsDeleted.affected ?? 0;
}
}
/** /**
* Deletes all expired AP logs and garbage-collects the AP context cache. * Deletes all expired AP logs and garbage-collects the AP context cache.
* Returns the total number of deleted rows. * Returns the total number of deleted rows.

View file

@ -24,9 +24,14 @@ import { SearchService } from '@/core/SearchService.js';
import { ModerationLogService } from '@/core/ModerationLogService.js'; import { ModerationLogService } from '@/core/ModerationLogService.js';
import { isQuote, isRenote } from '@/misc/is-renote.js'; import { isQuote, isRenote } from '@/misc/is-renote.js';
import { LatestNoteService } from '@/core/LatestNoteService.js'; import { LatestNoteService } from '@/core/LatestNoteService.js';
import { ApLogService } from '@/core/ApLogService.js';
import Logger from '@/logger.js';
import { LoggerService } from './LoggerService.js';
@Injectable() @Injectable()
export class NoteDeleteService { export class NoteDeleteService {
private readonly logger: Logger;
constructor( constructor(
@Inject(DI.config) @Inject(DI.config)
private config: Config, private config: Config,
@ -55,7 +60,11 @@ export class NoteDeleteService {
private perUserNotesChart: PerUserNotesChart, private perUserNotesChart: PerUserNotesChart,
private instanceChart: InstanceChart, private instanceChart: InstanceChart,
private latestNoteService: LatestNoteService, private latestNoteService: LatestNoteService,
) {} private readonly apLogService: ApLogService,
loggerService: LoggerService,
) {
this.logger = loggerService.getLogger('note-delete-service');
}
/** /**
* 稿 * 稿
@ -156,6 +165,11 @@ export class NoteDeleteService {
note: note, note: note,
}); });
} }
if (note.uri) {
this.apLogService.deleteObjectLogs(note.uri)
.catch(err => this.logger.error(err, `Failed to delete AP logs for note '${note.uri}'`));
}
} }
@bindThis @bindThis

View file

@ -15,6 +15,7 @@ import type { MiNoteReaction } from '@/models/NoteReaction.js';
import { EmailService } from '@/core/EmailService.js'; import { EmailService } from '@/core/EmailService.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
import { SearchService } from '@/core/SearchService.js'; import { SearchService } from '@/core/SearchService.js';
import { ApLogService } from '@/core/ApLogService.js';
import { ReactionService } from '@/core/ReactionService.js'; import { ReactionService } from '@/core/ReactionService.js';
import { QueueLoggerService } from '../QueueLoggerService.js'; import { QueueLoggerService } from '../QueueLoggerService.js';
import type * as Bull from 'bullmq'; import type * as Bull from 'bullmq';
@ -45,6 +46,7 @@ export class DeleteAccountProcessorService {
private queueLoggerService: QueueLoggerService, private queueLoggerService: QueueLoggerService,
private searchService: SearchService, private searchService: SearchService,
private reactionService: ReactionService, private reactionService: ReactionService,
private readonly apLogService: ApLogService,
) { ) {
this.logger = this.queueLoggerService.logger.createSubLogger('delete-account'); this.logger = this.queueLoggerService.logger.createSubLogger('delete-account');
} }
@ -84,6 +86,13 @@ export class DeleteAccountProcessorService {
for (const note of notes) { for (const note of notes) {
await this.searchService.unindexNote(note); await this.searchService.unindexNote(note);
} }
// Delete note AP logs
const noteUris = notes.map(n => n.uri).filter(u => !!u) as string[];
if (noteUris.length > 0) {
await this.apLogService.deleteObjectLogs(noteUris)
.catch(err => this.logger.error(err, `Failed to delete AP logs for notes of user '${user.uri ?? user.id}'`));
}
} }
this.logger.succ('All of notes deleted'); this.logger.succ('All of notes deleted');
@ -149,6 +158,13 @@ export class DeleteAccountProcessorService {
this.logger.succ('All of files deleted'); this.logger.succ('All of files deleted');
} }
{ // Delete actor logs
if (user.uri) {
await this.apLogService.deleteObjectLogs(user.uri)
.catch(err => this.logger.error(err, `Failed to delete AP logs for user '${user.uri}'`));
}
}
{ // Send email notification { // Send email notification
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: user.id }); const profile = await this.userProfilesRepository.findOneByOrFail({ userId: user.id });
if (profile.email && profile.emailVerified) { if (profile.email && profile.emailVerified) {