server: refactor finding delete-cascaded notes

Remove the several filter functions in different places by filtering
directly in the database.

Instead of a QueryBuilder, use the plain find function.

Refactor a for loop awaiting several promises individually, use
Array.map and await Promise.all to make better use of promises.
This commit is contained in:
Johann150 2022-10-31 20:57:45 +01:00
parent ac240eb58d
commit 6df2f7c55c
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -54,7 +54,7 @@ export default async function(user: { id: User['id']; uri: User['uri']; host: Us
}
// also deliever delete activity to cascaded notes
const cascadingNotes = (await findCascadingNotes(note)).filter(note => !note.localOnly); // filter out local-only notes
const cascadingNotes = await findCascadingNotes(note);
for (const cascadingNote of cascadingNotes) {
if (!cascadingNote.user) continue;
if (!Users.isLocalUser(cascadingNote.user)) continue;
@ -89,22 +89,31 @@ async function findCascadingNotes(note: Note): Promise<Note[]> {
const cascadingNotes: Note[] = [];
const recursive = async (noteId: string): Promise<void> => {
const query = Notes.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();
for (const reply of replies) {
// FIXME: use note_replies SQL function? Unclear what to do with 2nd and 3rd parameter, maybe rewrite the function.
const replies = await Notes.find({
where: [{
replyId: noteId,
localOnly: false,
userHost: IsNull(),
}, {
renoteId: noteId,
text: Not(IsNull()),
localOnly: false,
userHost: IsNull(),
}],
relations: {
user: true,
},
});
await Promise.all(replies.map(reply => {
cascadingNotes.push(reply);
await recursive(reply.id);
}
return recursive(reply.id);
}));
};
await recursive(note.id);
return cascadingNotes.filter(note => note.userHost === null); // filter out non-local users
return cascadingNotes;
}
async function getMentionedRemoteUsers(note: Note): Promise<IRemoteUser[]> {