normalize more endpoint filtering code

This commit is contained in:
Hazelnoot 2025-08-13 14:48:39 -04:00
parent a756b309f0
commit 8ca9f90afe
11 changed files with 46 additions and 51 deletions

View file

@ -93,6 +93,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
this.queryService.generateVisibilityQuery(query, me); this.queryService.generateVisibilityQuery(query, me);
this.queryService.generateBlockedHostQueryForNote(query); this.queryService.generateBlockedHostQueryForNote(query);
this.queryService.generateExcludedRepliesQueryForNotes(query, me);
// this.queryService.generateSuspendedUserQueryForNote(query); // To avoid problems with removing notes, ignoring suspended user for now // this.queryService.generateSuspendedUserQueryForNote(query); // To avoid problems with removing notes, ignoring suspended user for now
if (me) { if (me) {
this.queryService.generateMutedUserQueryForNotes(query, me); this.queryService.generateMutedUserQueryForNotes(query, me);

View file

@ -92,7 +92,6 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
.limit(ps.limit); .limit(ps.limit);
this.queryService.generateVisibilityQuery(query, me); this.queryService.generateVisibilityQuery(query, me);
this.queryService.generateBlockedHostQueryForNote(query);
if (me) { if (me) {
this.queryService.generateSilencedUserQueryForNotes(query, me); this.queryService.generateSilencedUserQueryForNotes(query, me);
this.queryService.generateMutedUserQueryForNotes(query, me); this.queryService.generateMutedUserQueryForNotes(query, me);
@ -101,10 +100,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
if (ps.local) { if (ps.local) {
query.andWhere('note.userHost IS NULL'); query.andWhere('note.userHost IS NULL');
} else {
this.queryService.generateBlockedHostQueryForNote(query);
} }
if (ps.reply !== undefined) { if (ps.reply) {
query.andWhere(ps.reply ? 'note.replyId IS NOT NULL' : 'note.replyId IS NULL'); this.queryService.generateExcludedRepliesQueryForNotes(query, me);
} else if (ps.reply === false) {
query.andWhere('note.replyId IS NULL');
} }
if (ps.renote !== undefined) { if (ps.renote !== undefined) {

View file

@ -102,14 +102,6 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
return []; return [];
} }
const [
userIdsWhoMeMuting,
userIdsWhoBlockingMe,
] = me ? await Promise.all([
this.cacheService.userMutingsCache.fetch(me.id),
this.cacheService.userBlockedCache.fetch(me.id),
]) : [new Set<string>(), new Set<string>()];
const query = this.notesRepository.createQueryBuilder('note') const query = this.notesRepository.createQueryBuilder('note')
.where('note.id IN (:...noteIds)', { noteIds: noteIds }) .where('note.id IN (:...noteIds)', { noteIds: noteIds })
.innerJoinAndSelect('note.user', 'user') .innerJoinAndSelect('note.user', 'user')
@ -120,16 +112,17 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
.leftJoinAndSelect('note.channel', 'channel') .leftJoinAndSelect('note.channel', 'channel')
.andWhere('user.isExplorable = TRUE'); .andWhere('user.isExplorable = TRUE');
this.queryService.generateVisibilityQuery(query, me);
this.queryService.generateBlockedHostQueryForNote(query); this.queryService.generateBlockedHostQueryForNote(query);
this.queryService.generateSuspendedUserQueryForNote(query); this.queryService.generateSuspendedUserQueryForNote(query);
this.queryService.generateSilencedUserQueryForNotes(query, me); this.queryService.generateSilencedUserQueryForNotes(query, me);
if (me) {
this.queryService.generateMutedUserQueryForNotes(query, me);
this.queryService.generateBlockedUserQueryForNotes(query, me);
this.queryService.generateMutedNoteThreadQuery(query, me);
}
const notes = (await query.getMany()).filter(note => { const notes = await query.getMany();
if (me && isUserRelated(note, userIdsWhoBlockingMe)) return false;
if (me && isUserRelated(note, userIdsWhoMeMuting)) return false;
return true;
});
notes.sort((a, b) => a.id > b.id ? -1 : 1); notes.sort((a, b) => a.id > b.id ? -1 : 1);

View file

@ -151,11 +151,12 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
query.andWhere('"user"."isSuspended" = false'); query.andWhere('"user"."isSuspended" = false');
this.queryService.generateBlockedHostQueryForNote(query); this.queryService.generateBlockedHostQueryForNote(query);
this.queryService.generateSilencedUserQueryForNotes(query, me, ps.list !== 'followers'); this.queryService.generateSilencedUserQueryForNotes(query, me, ps.list !== 'followers');
this.queryService.generateSuspendedUserQueryForNote(query);
// Respect blocks, mutes, and privacy // Respect blocks, mutes, and privacy
this.queryService.generateVisibilityQuery(query, me); this.queryService.generateVisibilityQuery(query, me);
this.queryService.generateBlockedUserQueryForNotes(query, me); this.queryService.generateBlockedUserQueryForNotes(query, me);
this.queryService.generateMutedUserQueryForNotes(query, me); this.queryService.generateMutedUserQueryForNotes(query, me, ps.list !== 'followers');
// Support pagination // Support pagination
this.queryService this.queryService

View file

@ -87,6 +87,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
this.queryService.generateVisibilityQuery(qb, me); this.queryService.generateVisibilityQuery(qb, me);
this.queryService.generateBlockedHostQueryForNote(qb); this.queryService.generateBlockedHostQueryForNote(qb);
this.queryService.generateSuspendedUserQueryForNote(qb); this.queryService.generateSuspendedUserQueryForNote(qb);
this.queryService.generateSilencedUserQueryForNotes(qb, me);
this.queryService.generateMutedUserQueryForNotes(qb, me); this.queryService.generateMutedUserQueryForNotes(qb, me);
this.queryService.generateMutedNoteThreadQuery(qb, me); this.queryService.generateMutedNoteThreadQuery(qb, me);
this.queryService.generateBlockedUserQueryForNotes(qb, me); this.queryService.generateBlockedUserQueryForNotes(qb, me);

View file

@ -137,10 +137,13 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
//#region block/mute/vis //#region block/mute/vis
this.queryService.generateVisibilityQuery(query, me); this.queryService.generateVisibilityQuery(query, me);
this.queryService.generateSilencedUserQueryForNotes(query, me);
this.queryService.generateSuspendedUserQueryForNote(query);
this.queryService.generateBlockedHostQueryForNote(query); this.queryService.generateBlockedHostQueryForNote(query);
if (me) { if (me) {
this.queryService.generateBlockedUserQueryForNotes(query, me); this.queryService.generateBlockedUserQueryForNotes(query, me);
this.queryService.generateMutedUserQueryForNotes(query, me); this.queryService.generateMutedUserQueryForNotes(query, me);
this.queryService.generateMutedNoteThreadQuery(query, me);
} }
//#endregion //#endregion

View file

@ -97,6 +97,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
if (me) { if (me) {
this.queryService.generateMutedUserQueryForNotes(query, me); this.queryService.generateMutedUserQueryForNotes(query, me);
this.queryService.generateBlockedUserQueryForNotes(query, me); this.queryService.generateBlockedUserQueryForNotes(query, me);
this.queryService.generateMutedNoteThreadQuery(query, me);
} }
const renotes = await query.limit(ps.limit).getMany(); const renotes = await query.limit(ps.limit).getMany();

View file

@ -98,7 +98,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
this.queryService.generateSilencedUserQueryForNotes(query, me); this.queryService.generateSilencedUserQueryForNotes(query, me);
if (me) this.queryService.generateMutedUserQueryForNotes(query, me); if (me) this.queryService.generateMutedUserQueryForNotes(query, me);
if (me) this.queryService.generateBlockedUserQueryForNotes(query, me); if (me) this.queryService.generateBlockedUserQueryForNotes(query, me);
if (me) this.queryService.generateMutedUserRenotesQueryForNotes(query, me); if (me) this.queryService.generateMutedNoteThreadQuery(query, me);
if (!this.serverSettings.enableBotTrending) query.andWhere('user.isBot = FALSE'); if (!this.serverSettings.enableBotTrending) query.andWhere('user.isBot = FALSE');
@ -123,19 +123,24 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
throw e; throw e;
} }
if (ps.reply != null) { if (ps.reply === false) {
query.andWhere('note.replyId IS NULL');
} else {
if (ps.reply) { if (ps.reply) {
query.andWhere('note.replyId IS NOT NULL'); query.andWhere('note.replyId IS NOT NULL');
} else {
query.andWhere('note.replyId IS NULL');
} }
this.queryService.generateExcludedRepliesQueryForNotes(query, me);
} }
if (ps.renote != null) { if (ps.renote === false) {
this.queryService.andIsNotRenote(query, 'note');
} else {
if (ps.renote) { if (ps.renote) {
this.queryService.andIsRenote(query, 'note'); this.queryService.andIsRenote(query, 'note');
} else { }
this.queryService.andIsNotRenote(query, 'note');
if (me) {
this.queryService.generateMutedUserRenotesQueryForNotes(query, me);
} }
} }

View file

@ -78,12 +78,6 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
return []; return [];
} }
const [
userIdsWhoMeMuting,
] = me ? await Promise.all([
this.cacheService.userMutingsCache.fetch(me.id),
]) : [new Set<string>()];
const query = this.notesRepository.createQueryBuilder('note') const query = this.notesRepository.createQueryBuilder('note')
.where('note.id IN (:...noteIds)', { noteIds: noteIds }) .where('note.id IN (:...noteIds)', { noteIds: noteIds })
.innerJoinAndSelect('note.user', 'user') .innerJoinAndSelect('note.user', 'user')
@ -96,13 +90,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
this.queryService.generateBlockedHostQueryForNote(query); this.queryService.generateBlockedHostQueryForNote(query);
this.queryService.generateSuspendedUserQueryForNote(query); this.queryService.generateSuspendedUserQueryForNote(query);
this.queryService.generateSilencedUserQueryForNotes(query, me, true); this.queryService.generateSilencedUserQueryForNotes(query, me, true);
this.queryService.generateExcludedRepliesQueryForNotes(query, me);
if (me) {
this.queryService.generateBlockedUserQueryForNotes(query, me);
this.queryService.generateMutedNoteThreadQuery(query, me);
this.queryService.generateMutedUserQueryForNotes(query, me, true);
}
const notes = (await query.getMany()).filter(note => { const notes = await query.getMany();
if (me && isUserRelated(note, userIdsWhoBlockingMe, false)) return false;
if (me && isUserRelated(note, userIdsWhoMeMuting, true)) return false;
return true;
});
notes.sort((a, b) => a.id > b.id ? -1 : 1); notes.sort((a, b) => a.id > b.id ? -1 : 1);

View file

@ -215,13 +215,13 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
query.andWhere('note.channelId IS NULL'); query.andWhere('note.channelId IS NULL');
} }
this.queryService.generateVisibilityQuery(query, me);
this.queryService.generateBlockedHostQueryForNote(query, true); this.queryService.generateBlockedHostQueryForNote(query, true);
this.queryService.generateSuspendedUserQueryForNote(query, true); this.queryService.generateSuspendedUserQueryForNote(query, true);
this.queryService.generateSilencedUserQueryForNotes(query, me, true); this.queryService.generateSilencedUserQueryForNotes(query, me, true);
if (me) { if (me) {
this.queryService.generateMutedUserQueryForNotes(query, me, true); this.queryService.generateMutedUserQueryForNotes(query, me, true);
this.queryService.generateBlockedUserQueryForNotes(query, me); this.queryService.generateBlockedUserQueryForNotes(query, me);
this.queryService.generateMutedNoteThreadQuery(query, me);
} }
if (ps.withFiles) { if (ps.withFiles) {
@ -239,13 +239,15 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
if (!ps.withRepliesToOthers && !ps.withRepliesToSelf) { if (!ps.withRepliesToOthers && !ps.withRepliesToSelf) {
query.andWhere('reply.id IS NULL'); query.andWhere('reply.id IS NULL');
} else if (!ps.withRepliesToOthers) { } else if (!ps.withRepliesToOthers) {
query.andWhere('(reply.id IS NULL OR reply."userId" = note."userId")'); this.queryService.generateExcludedRepliesQueryForNotes(query, me);
} else if (!ps.withRepliesToSelf) { } else if (!ps.withRepliesToSelf) {
query.andWhere('(reply.id IS NULL OR reply."userId" != note."userId")'); query.andWhere('(reply.id IS NULL OR reply."userId" != note."userId")');
} }
if (!ps.withNonPublic) { if (!ps.withNonPublic) {
query.andWhere('note.visibility = \'public\''); query.andWhere('note.visibility = \'public\'');
} else {
this.queryService.generateVisibilityQuery(query, me);
} }
if (!ps.withBots) { if (!ps.withBots) {

View file

@ -100,8 +100,6 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
} }
} }
const userIdsWhoMeMuting = me ? await this.cacheService.userMutingsCache.fetch(me.id) : new Set<string>();
const query = this.queryService.makePaginationQuery(this.noteReactionsRepository.createQueryBuilder('reaction'), const query = this.queryService.makePaginationQuery(this.noteReactionsRepository.createQueryBuilder('reaction'),
ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate) ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate)
.andWhere('reaction.userId = :userId', { userId: ps.userId }) .andWhere('reaction.userId = :userId', { userId: ps.userId })
@ -117,20 +115,12 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
this.queryService.generateSuspendedUserQueryForNote(query); this.queryService.generateSuspendedUserQueryForNote(query);
this.queryService.generateSilencedUserQueryForNotes(query, me, true); this.queryService.generateSilencedUserQueryForNotes(query, me, true);
if (me) { if (me) {
this.queryService.generateMutedUserQueryForNotes(query, me); this.queryService.generateMutedUserQueryForNotes(query, me, true);
this.queryService.generateBlockedUserQueryForNotes(query, me); this.queryService.generateBlockedUserQueryForNotes(query, me);
this.queryService.generateMutedUserRenotesQueryForNotes(query, me); this.queryService.generateMutedUserRenotesQueryForNotes(query, me);
} }
const reactions = (await query const reactions = await query.limit(ps.limit).getMany();
.limit(ps.limit)
.getMany()).filter(reaction => {
if (reaction.note?.userId === ps.userId) return true; // we can see reactions to note of requesting user
if (me && isUserRelated(reaction.note, userIdsWhoBlockingMe)) return false;
if (me && isUserRelated(reaction.note, userIdsWhoMeMuting)) return false;
return true;
});
return await this.noteReactionEntityService.packMany(reactions, me, { withNote: true }); return await this.noteReactionEntityService.packMany(reactions, me, { withNote: true });
}); });