Skip to content

Commit

Permalink
노트를 삭제할때 타래의 노트들까지 재귀적으로 삭제하지 않도록 수정.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunochi committed Jun 4, 2024
1 parent b63e7db commit 72feaec
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 35 deletions.
14 changes: 14 additions & 0 deletions packages/backend/migration/1711722198590-no-recursive-delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

export class NoRecursiveDelete1711722198590 {
name = 'NoRecursiveDelete1711722198590'

async up(queryRunner) {
await queryRunner.query(`ALTER TABLE "note" DROP CONSTRAINT "FK_17cb3553c700a4985dff5a30ff5"`);
await queryRunner.query(`ALTER TABLE "note" ADD CONSTRAINT "FK_17cb3553c700a4985dff5a30ff5" FOREIGN KEY ("replyId") REFERENCES "note"("id") ON DELETE SET NULL ON UPDATE NO ACTION`);
}

async down(queryRunner) {
await queryRunner.query(`ALTER TABLE "note" DROP CONSTRAINT "FK_17cb3553c700a4985dff5a30ff5"`);
await queryRunner.query(`ALTER TABLE "note" ADD CONSTRAINT "FK_17cb3553c700a4985dff5a30ff5" FOREIGN KEY ("replyId") REFERENCES "note"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
}
}
34 changes: 0 additions & 34 deletions packages/backend/src/core/NoteDeleteService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export class NoteDeleteService {
*/
async delete(user: { id: MiUser['id']; uri: MiUser['uri']; host: MiUser['host']; isBot: MiUser['isBot']; }, note: MiNote, quiet = false, deleter?: MiUser) {
const deletedAt = new Date();
const cascadingNotes = await this.findCascadingNotes(note);

if (note.replyId) {
await this.notesRepository.decrement({ id: note.replyId }, 'repliesCount', 1);
Expand Down Expand Up @@ -92,14 +91,6 @@ export class NoteDeleteService {
this.deliverToConcerned(user, note, content);
}

// also deliever delete activity to cascaded notes
const federatedLocalCascadingNotes = (cascadingNotes).filter(note => !note.localOnly && note.userHost == null); // filter out local-only notes
for (const cascadingNote of federatedLocalCascadingNotes) {
if (!cascadingNote.user) continue;
if (!this.userEntityService.isLocalUser(cascadingNote.user)) continue;
const content = this.apRendererService.addContext(this.apRendererService.renderDelete(this.apRendererService.renderTombstone(`${this.config.url}/notes/${cascadingNote.id}`), cascadingNote.user));
this.deliverToConcerned(cascadingNote.user, cascadingNote, content);
}
//#endregion

const meta = await this.metaService.fetch();
Expand All @@ -119,9 +110,6 @@ export class NoteDeleteService {
}
}

for (const cascadingNote of cascadingNotes) {
this.searchService.unindexNote(cascadingNote);
}
this.searchService.unindexNote(note);

await this.notesRepository.delete({
Expand All @@ -141,28 +129,6 @@ export class NoteDeleteService {
}
}

@bindThis
private async findCascadingNotes(note: MiNote): Promise<MiNote[]> {
const recursive = async (noteId: string): Promise<MiNote[]> => {
const query = this.notesRepository.createQueryBuilder('note')
.where('note.replyId = :noteId', { noteId })
.orWhere(new Brackets(q => {
q.where('note.renoteId = :noteId', { noteId })
.andWhere('note.text IS NOT NULL');
}))
.leftJoinAndSelect('note.user', 'user');
const replies = await query.getMany();

return [
replies,
...await Promise.all(replies.map(reply => recursive(reply.id))),
].flat();
};

const cascadingNotes: MiNote[] = await recursive(note.id);

return cascadingNotes;
}

@bindThis
private async getMentionedRemoteUsers(note: MiNote) {
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/models/Note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class MiNote {
public replyId: MiNote['id'] | null;

@ManyToOne(type => MiNote, {
onDelete: 'CASCADE',
onDelete: 'SET NULL',
})
@JoinColumn()
public reply: MiNote | null;
Expand Down

0 comments on commit 72feaec

Please sign in to comment.