forked from FoundKeyGang/FoundKey
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:
parent
ac240eb58d
commit
6df2f7c55c
1 changed files with 22 additions and 13 deletions
|
@ -54,7 +54,7 @@ export default async function(user: { id: User['id']; uri: User['uri']; host: Us
|
||||||
}
|
}
|
||||||
|
|
||||||
// also deliever delete activity to cascaded notes
|
// 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) {
|
for (const cascadingNote of cascadingNotes) {
|
||||||
if (!cascadingNote.user) continue;
|
if (!cascadingNote.user) continue;
|
||||||
if (!Users.isLocalUser(cascadingNote.user)) continue;
|
if (!Users.isLocalUser(cascadingNote.user)) continue;
|
||||||
|
@ -89,22 +89,31 @@ async function findCascadingNotes(note: Note): Promise<Note[]> {
|
||||||
const cascadingNotes: Note[] = [];
|
const cascadingNotes: Note[] = [];
|
||||||
|
|
||||||
const recursive = async (noteId: string): Promise<void> => {
|
const recursive = async (noteId: string): Promise<void> => {
|
||||||
const query = Notes.createQueryBuilder('note')
|
// FIXME: use note_replies SQL function? Unclear what to do with 2nd and 3rd parameter, maybe rewrite the function.
|
||||||
.where('note.replyId = :noteId', { noteId })
|
const replies = await Notes.find({
|
||||||
.orWhere(new Brackets(q => {
|
where: [{
|
||||||
q.where('note.renoteId = :noteId', { noteId })
|
replyId: noteId,
|
||||||
.andWhere('note.text IS NOT NULL');
|
localOnly: false,
|
||||||
}))
|
userHost: IsNull(),
|
||||||
.leftJoinAndSelect('note.user', 'user');
|
}, {
|
||||||
const replies = await query.getMany();
|
renoteId: noteId,
|
||||||
for (const reply of replies) {
|
text: Not(IsNull()),
|
||||||
|
localOnly: false,
|
||||||
|
userHost: IsNull(),
|
||||||
|
}],
|
||||||
|
relations: {
|
||||||
|
user: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await Promise.all(replies.map(reply => {
|
||||||
cascadingNotes.push(reply);
|
cascadingNotes.push(reply);
|
||||||
await recursive(reply.id);
|
return recursive(reply.id);
|
||||||
}
|
}));
|
||||||
};
|
};
|
||||||
await recursive(note.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[]> {
|
async function getMentionedRemoteUsers(note: Note): Promise<IRemoteUser[]> {
|
||||||
|
|
Loading…
Reference in a new issue