fix: perform visibility query in second stage

This commit is contained in:
Johann150 2022-10-28 23:39:39 +02:00
parent 8f5952bb7d
commit c777c2ed04
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1
16 changed files with 42 additions and 55 deletions

View file

@ -1,12 +1,17 @@
import { Brackets, SelectQueryBuilder } from 'typeorm';
import { SelectQueryBuilder } from 'typeorm';
import { User } from '@/models/entities/user.js';
import { Followings } from '@/models/index.js';
import { Note } from '@/models/entities/note.js';
import { Notes } from '@/models/index.js';
export function generateVisibilityQuery(q: SelectQueryBuilder<any>, me?: { id: User['id'] } | null) {
if (me == null) {
q.andWhere('note_visible(note.id, null)');
export function visibilityQuery(q: SelectQueryBuilder<Note>, meId?: User['id'] | null = null): SelectQueryBuilder<Note> {
const superQuery = Notes.createQueryBuilder()
.from(() => q, 'note');
if (meId == null) {
superQuery.where('note_visible(note.id, null);');
} else {
q.andWhere('note_visible(note.id, :meId)');
q.setParameters({ meId: me.id });
superQuery.where('note_visible(note.id, :meId)', { meId });
}
return q;
}

View file

@ -2,7 +2,7 @@ import { IdentifiableError } from '@/misc/identifiable-error.js';
import { User } from '@/models/entities/user.js';
import { Note } from '@/models/entities/note.js';
import { Notes, Users } from '@/models/index.js';
import { generateVisibilityQuery } from './generate-visibility-query.js';
import { visibilityQuery } from './generate-visibility-query.js';
/**
* Get note for API processing, taking into account visibility.
@ -13,9 +13,7 @@ export async function getNote(noteId: Note['id'], me: { id: User['id'] } | null)
id: noteId,
});
generateVisibilityQuery(query, me);
const note = await query.getOne();
const note = await visibilityQuery(query, me).getOne();
if (note == null) {
throw new IdentifiableError('9725d0ce-ba28-4dde-95a7-2cbb2c15de24', 'No such note.');

View file

@ -1,7 +1,7 @@
import { readNote } from '@/services/note/read.js';
import { Antennas, Notes, AntennaNotes } from '@/models/index.js';
import { makePaginationQuery } from '@/server/api/common/make-pagination-query.js';
import { generateVisibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { visibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { generateMutedUserQuery } from '@/server/api/common/generate-muted-user-query.js';
import { generateBlockedUserQuery } from '@/server/api/common/generate-block-query.js';
import define from '@/server/api/define.js';
@ -65,11 +65,10 @@ export default define(meta, paramDef, async (ps, user) => {
.leftJoinAndSelect('renoteUser.banner', 'renoteUserBanner')
.andWhere('antennaNote.antennaId = :antennaId', { antennaId: antenna.id });
generateVisibilityQuery(query, user);
generateMutedUserQuery(query, user);
generateBlockedUserQuery(query, user);
const notes = await query
const notes = await visibilityQuery(query, user)
.take(ps.limit)
.getMany();

View file

@ -1,7 +1,7 @@
import { ClipNotes, Clips, Notes } from '@/models/index.js';
import define from '@/server/api/define.js';
import { makePaginationQuery } from '@/server/api/common/make-pagination-query.js';
import { generateVisibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { visibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { generateMutedUserQuery } from '@/server/api/common/generate-muted-user-query.js';
import { ApiError } from '@/server/api/error.js';
import { generateBlockedUserQuery } from '@/server/api/common/generate-block-query.js';
@ -65,12 +65,11 @@ export default define(meta, paramDef, async (ps, user) => {
.andWhere('clipNote.clipId = :clipId', { clipId: clip.id });
if (user) {
generateVisibilityQuery(query, user);
generateMutedUserQuery(query, user);
generateBlockedUserQuery(query, user);
}
const notes = await query
const notes = await visibilityQuery(query, user)
.take(ps.limit)
.getMany();

View file

@ -1,7 +1,7 @@
import { Notes } from '@/models/index.js';
import define from '@/server/api/define.js';
import { makePaginationQuery } from '@/server/api/common/make-pagination-query.js';
import { generateVisibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { visibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { generateMutedUserQuery } from '@/server/api/common/generate-muted-user-query.js';
import { generateBlockedUserQuery } from '@/server/api/common/generate-block-query.js';
@ -59,13 +59,12 @@ export default define(meta, paramDef, async (ps, user) => {
.andWhere('note.id IN (SELECT id FROM note_replies(:noteId, :depth, :limit))', { noteId: ps.noteId, depth: ps.depth, limit: ps.limit })
.innerJoinAndSelect('note.user', 'user');
generateVisibilityQuery(query, user);
if (user) {
generateMutedUserQuery(query, user);
generateBlockedUserQuery(query, user);
}
const notes = await query.getMany();
const notes = await visibilityQuery(query, user).getMany();
return await Notes.packMany(notes, user, { detail: false });
});

View file

@ -5,7 +5,7 @@ import { activeUsersChart } from '@/services/chart/index.js';
import define from '@/server/api/define.js';
import { ApiError } from '@/server/api/error.js';
import { makePaginationQuery } from '@/server/api/common/make-pagination-query.js';
import { generateVisibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { visibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { generateMutedUserQuery } from '@/server/api/common/generate-muted-user-query.js';
import { generateRepliesQuery } from '@/server/api/common/generate-replies-query.js';
import { generateMutedNoteQuery } from '@/server/api/common/generate-muted-note-query.js';
@ -84,7 +84,6 @@ export default define(meta, paramDef, async (ps, user) => {
generateChannelQuery(query, user);
generateRepliesQuery(query, user);
generateVisibilityQuery(query, user);
generateMutedUserQuery(query, user);
generateMutedNoteQuery(query, user);
generateBlockedUserQuery(query, user);
@ -125,7 +124,7 @@ export default define(meta, paramDef, async (ps, user) => {
}
//#endregion
const timeline = await query.take(ps.limit).getMany();
const timeline = await visibilityQuery(query, user).take(ps.limit).getMany();
process.nextTick(() => {
activeUsersChart.read(user);

View file

@ -6,7 +6,7 @@ import define from '@/server/api/define.js';
import { ApiError } from '@/server/api/error.js';
import { generateMutedUserQuery } from '@/server/api/common/generate-muted-user-query.js';
import { makePaginationQuery } from '@/server/api/common/make-pagination-query.js';
import { generateVisibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { visibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { generateRepliesQuery } from '@/server/api/common/generate-replies-query.js';
import { generateMutedNoteQuery } from '@/server/api/common/generate-muted-note-query.js';
import { generateChannelQuery } from '@/server/api/common/generate-channel-query.js';
@ -77,7 +77,6 @@ export default define(meta, paramDef, async (ps, user) => {
generateChannelQuery(query, user);
generateRepliesQuery(query, user);
generateVisibilityQuery(query, user);
if (user) generateMutedUserQuery(query, user);
if (user) generateMutedNoteQuery(query, user);
if (user) generateBlockedUserQuery(query, user);
@ -103,7 +102,7 @@ export default define(meta, paramDef, async (ps, user) => {
}
//#endregion
const timeline = await query.take(ps.limit).getMany();
const timeline = await visibilityQuery(query, user).take(ps.limit).getMany();
process.nextTick(() => {
if (user) {

View file

@ -3,7 +3,7 @@ import { noteVisibilities } from 'foundkey-js';
import { readNote } from '@/services/note/read.js';
import { Notes, Followings } from '@/models/index.js';
import define from '@/server/api/define.js';
import { generateVisibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { visibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { generateMutedUserQuery } from '@/server/api/common/generate-muted-user-query.js';
import { makePaginationQuery } from '@/server/api/common/make-pagination-query.js';
import { generateBlockedUserQuery } from '@/server/api/common/generate-block-query.js';
@ -63,7 +63,6 @@ export default define(meta, paramDef, async (ps, user) => {
.leftJoinAndSelect('renoteUser.avatar', 'renoteUserAvatar')
.leftJoinAndSelect('renoteUser.banner', 'renoteUserBanner');
generateVisibilityQuery(query, user);
generateMutedUserQuery(query, user);
generateMutedNoteThreadQuery(query, user);
generateBlockedUserQuery(query, user);
@ -77,7 +76,7 @@ export default define(meta, paramDef, async (ps, user) => {
query.setParameters(followingQuery.getParameters());
}
const mentions = await query.take(ps.limit).getMany();
const mentions = await visibilityQuery(query, user).take(ps.limit).getMany();
readNote(user.id, mentions);

View file

@ -2,7 +2,7 @@ import { Notes } from '@/models/index.js';
import define from '@/server/api/define.js';
import { getNote } from '@/server/api/common/getters.js';
import { ApiError } from '@/server/api/error.js';
import { generateVisibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { visibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { generateMutedUserQuery } from '@/server/api/common/generate-muted-user-query.js';
import { makePaginationQuery } from '@/server/api/common/make-pagination-query.js';
import { generateBlockedUserQuery } from '@/server/api/common/generate-block-query.js';
@ -63,11 +63,10 @@ export default define(meta, paramDef, async (ps, user) => {
.leftJoinAndSelect('renoteUser.avatar', 'renoteUserAvatar')
.leftJoinAndSelect('renoteUser.banner', 'renoteUserBanner');
generateVisibilityQuery(query, user);
if (user) generateMutedUserQuery(query, user);
if (user) generateBlockedUserQuery(query, user);
const renotes = await query.take(ps.limit).getMany();
const renotes = await visibilityQuery(query, user).take(ps.limit).getMany();
return await Notes.packMany(renotes, user);
});

View file

@ -2,7 +2,7 @@ import { Notes } from '@/models/index.js';
import { ApiError } from '@/server/api/error.js';
import define from '@/server/api/define.js';
import { makePaginationQuery } from '@/server/api/common/make-pagination-query.js';
import { generateVisibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { visibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { generateMutedUserQuery } from '@/server/api/common/generate-muted-user-query.js';
import { generateBlockedUserQuery } from '@/server/api/common/generate-block-query.js';
import { getNote } from '@/server/api/common/getters.js';
@ -63,11 +63,10 @@ export default define(meta, paramDef, async (ps, user) => {
.leftJoinAndSelect('renoteUser.avatar', 'renoteUserAvatar')
.leftJoinAndSelect('renoteUser.banner', 'renoteUserBanner');
generateVisibilityQuery(query, user);
if (user) generateMutedUserQuery(query, user);
if (user) generateBlockedUserQuery(query, user);
const timeline = await query.take(ps.limit).getMany();
const timeline = await visibilityQuery(query, user).take(ps.limit).getMany();
return await Notes.packMany(timeline, user);
});

View file

@ -4,7 +4,7 @@ import { normalizeForSearch } from '@/misc/normalize-for-search.js';
import define from '@/server/api/define.js';
import { makePaginationQuery } from '@/server/api/common/make-pagination-query.js';
import { generateMutedUserQuery } from '@/server/api/common/generate-muted-user-query.js';
import { generateVisibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { visibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { generateBlockedUserQuery } from '@/server/api/common/generate-block-query.js';
export const meta = {
@ -79,7 +79,6 @@ export default define(meta, paramDef, async (ps, me) => {
.leftJoinAndSelect('renoteUser.avatar', 'renoteUserAvatar')
.leftJoinAndSelect('renoteUser.banner', 'renoteUserBanner');
generateVisibilityQuery(query, me);
if (me) generateMutedUserQuery(query, me);
if (me) generateBlockedUserQuery(query, me);
@ -132,7 +131,7 @@ export default define(meta, paramDef, async (ps, me) => {
}
// Search notes
const notes = await query.take(ps.limit).getMany();
const notes = await visibilityQuery(query, me).take(ps.limit).getMany();
return await Notes.packMany(notes, me);
});

View file

@ -4,7 +4,7 @@ import config from '@/config/index.js';
import es from '@/db/elasticsearch.js';
import define from '@/server/api/define.js';
import { makePaginationQuery } from '@/server/api/common/make-pagination-query.js';
import { generateVisibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { visibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { generateMutedUserQuery } from '@/server/api/common/generate-muted-user-query.js';
import { generateBlockedUserQuery } from '@/server/api/common/generate-block-query.js';
@ -70,11 +70,10 @@ export default define(meta, paramDef, async (ps, me) => {
.leftJoinAndSelect('renoteUser.avatar', 'renoteUserAvatar')
.leftJoinAndSelect('renoteUser.banner', 'renoteUserBanner');
generateVisibilityQuery(query, me);
if (me) generateMutedUserQuery(query, me);
if (me) generateBlockedUserQuery(query, me);
const notes = await query.take(ps.limit).getMany();
const notes = await visibilityQuery(query, me).take(ps.limit).getMany();
return await Notes.packMany(notes, me);
} else {

View file

@ -3,7 +3,7 @@ import { Notes, Followings } from '@/models/index.js';
import { activeUsersChart } from '@/services/chart/index.js';
import define from '@/server/api/define.js';
import { makePaginationQuery } from '@/server/api/common/make-pagination-query.js';
import { generateVisibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { visibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { generateMutedUserQuery } from '@/server/api/common/generate-muted-user-query.js';
import { generateRepliesQuery } from '@/server/api/common/generate-replies-query.js';
import { generateMutedNoteQuery } from '@/server/api/common/generate-muted-note-query.js';
@ -82,7 +82,6 @@ export default define(meta, paramDef, async (ps, user) => {
generateChannelQuery(query, user);
generateRepliesQuery(query, user);
generateVisibilityQuery(query, user);
generateMutedUserQuery(query, user);
generateMutedNoteQuery(query, user);
generateBlockedUserQuery(query, user);
@ -123,7 +122,7 @@ export default define(meta, paramDef, async (ps, user) => {
}
//#endregion
const timeline = await query.take(ps.limit).getMany();
const timeline = await visibilityQuery(query, user).take(ps.limit).getMany();
process.nextTick(() => {
activeUsersChart.read(user);

View file

@ -4,7 +4,7 @@ import { activeUsersChart } from '@/services/chart/index.js';
import define from '@/server/api/define.js';
import { ApiError } from '@/server/api/error.js';
import { makePaginationQuery } from '@/server/api/common/make-pagination-query.js';
import { generateVisibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { visibilityQuery } from '@/server/api/common/generate-visibility-query.js';
export const meta = {
tags: ['notes', 'lists'],
@ -70,8 +70,6 @@ export default define(meta, paramDef, async (ps, user) => {
.leftJoinAndSelect('renoteUser.banner', 'renoteUserBanner')
.andWhere('userListJoining.userListId = :userListId', { userListId: list.id });
generateVisibilityQuery(query, user);
if (ps.includeMyRenotes === false) {
query.andWhere(new Brackets(qb => {
qb.orWhere('note.userId != :meId', { meId: user.id });
@ -107,7 +105,7 @@ export default define(meta, paramDef, async (ps, user) => {
}
//#endregion
const timeline = await query.take(ps.limit).getMany();
const timeline = await visibilityQuery(query, user).take(ps.limit).getMany();
activeUsersChart.read(user);

View file

@ -4,7 +4,7 @@ import define from '@/server/api/define.js';
import { ApiError } from '@/server/api/error.js';
import { getUser } from '@/server/api/common/getters.js';
import { makePaginationQuery } from '@/server/api/common/make-pagination-query.js';
import { generateVisibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { visibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { generateMutedUserQuery } from '@/server/api/common/generate-muted-user-query.js';
import { generateBlockedUserQuery } from '@/server/api/common/generate-block-query.js';
@ -69,7 +69,6 @@ export default define(meta, paramDef, async (ps, me) => {
.leftJoinAndSelect('renoteUser.avatar', 'renoteUserAvatar')
.leftJoinAndSelect('renoteUser.banner', 'renoteUserBanner');
generateVisibilityQuery(query, me);
if (me) {
generateMutedUserQuery(query, me);
generateBlockedUserQuery(query, me);
@ -110,7 +109,7 @@ export default define(meta, paramDef, async (ps, me) => {
//#endregion
const timeline = await query.take(ps.limit).getMany();
const timeline = await visibilityQuery(query, me).take(ps.limit).getMany();
return await Notes.packMany(timeline, me);
});

View file

@ -1,7 +1,7 @@
import { NoteReactions, UserProfiles } from '@/models/index.js';
import define from '@/server/api/define.js';
import { makePaginationQuery } from '@/server/api/common/make-pagination-query.js';
import { generateVisibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { visibilityQuery } from '@/server/api/common/generate-visibility-query.js';
import { ApiError } from '@/server/api/error.js';
export const meta = {
@ -50,9 +50,7 @@ export default define(meta, paramDef, async (ps, me) => {
.andWhere('reaction.userId = :userId', { userId: ps.userId })
.leftJoinAndSelect('reaction.note', 'note');
generateVisibilityQuery(query, me);
const reactions = await query
const reactions = await visibilityQuery(query, me)
.take(ps.limit)
.getMany();