This commit is contained in:
ThatOneCalculator 2022-07-28 23:53:42 -07:00
parent e639496be2
commit 15ad79c3de
1 changed files with 40 additions and 58 deletions

View File

@ -1,12 +1,12 @@
import { Brackets } from 'typeorm'; import { Brackets } from 'typeorm';
import { fetchMeta } from '@/misc/fetch-meta.js'; import { fetchMeta } from '@/misc/fetch-meta.js';
import { Metas, Notes } from '@/models/index.js'; import { Notes, Users } from '@/models/index.js';
import { activeUsersChart } from '@/services/chart/index.js'; import { activeUsersChart } from '@/services/chart/index.js';
import define from '../../define.js'; import define from '../../define.js';
import { ApiError } from '../../error.js'; import { ApiError } from '../../error.js';
import { generateMutedUserQuery } from '../../common/generate-muted-user-query.js';
import { makePaginationQuery } from '../../common/make-pagination-query.js'; import { makePaginationQuery } from '../../common/make-pagination-query.js';
import { generateVisibilityQuery } from '../../common/generate-visibility-query.js'; import { generateVisibilityQuery } from '../../common/generate-visibility-query.js';
import { generateMutedUserQuery } from '../../common/generate-muted-user-query.js';
import { generateRepliesQuery } from '../../common/generate-replies-query.js'; import { generateRepliesQuery } from '../../common/generate-replies-query.js';
import { generateMutedNoteQuery } from '../../common/generate-muted-note-query.js'; import { generateMutedNoteQuery } from '../../common/generate-muted-note-query.js';
import { generateChannelQuery } from '../../common/generate-channel-query.js'; import { generateChannelQuery } from '../../common/generate-channel-query.js';
@ -14,8 +14,7 @@ import { generateBlockedUserQuery } from '../../common/generate-block-query.js';
export const meta = { export const meta = {
tags: ['notes'], tags: ['notes'],
requireCredentialPrivateMode: true,
requireCredential: true,
res: { res: {
type: 'array', type: 'array',
@ -31,7 +30,7 @@ export const meta = {
rtlDisabled: { rtlDisabled: {
message: 'Recommended timeline has been disabled.', message: 'Recommended timeline has been disabled.',
code: 'RTL_DISABLED', code: 'RTL_DISABLED',
id: '620763f4-f621-4533-ab33-0577a1a3c343', id: '45a6eb02-7695-4393-b023-dd3be9aaaefe',
}, },
}, },
} as const; } as const;
@ -39,19 +38,20 @@ export const meta = {
export const paramDef = { export const paramDef = {
type: 'object', type: 'object',
properties: { properties: {
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
sinceId: { type: 'string', format: 'misskey:id' },
untilId: { type: 'string', format: 'misskey:id' },
sinceDate: { type: 'integer' },
untilDate: { type: 'integer' },
includeMyRenotes: { type: 'boolean', default: true },
includeRenotedMyNotes: { type: 'boolean', default: true },
includeLocalRenotes: { type: 'boolean', default: true },
withFiles: { withFiles: {
type: 'boolean', type: 'boolean',
default: false, default: false,
description: 'Only show notes that have attached files.', description: 'Only show notes that have attached files.',
}, },
fileType: { type: 'array', items: {
type: 'string',
} },
excludeNsfw: { type: 'boolean', default: false },
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
sinceId: { type: 'string', format: 'misskey:id' },
untilId: { type: 'string', format: 'misskey:id' },
sinceDate: { type: 'integer' },
untilDate: { type: 'integer' },
}, },
required: [], required: [],
} as const; } as const;
@ -59,20 +59,16 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export // eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => { export default define(meta, paramDef, async (ps, user) => {
const m = await fetchMeta(); const m = await fetchMeta();
if (m.disableLocalTimeline && (!user.isAdmin && !user.isModerator)) { if (m.disableRecommendedTimeline) {
if (user == null || (!user.isAdmin && !user.isModerator)) {
throw new ApiError(meta.errors.rtlDisabled); throw new ApiError(meta.errors.rtlDisabled);
} }
}
//#region Construct query //#region Construct query
const recommendedInstancesQuery = Metas.createQueryBuilder('meta')
.select('meta.recommendedInstances');
const query = makePaginationQuery(Notes.createQueryBuilder('note'), const query = makePaginationQuery(Notes.createQueryBuilder('note'),
ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate) ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate)
.andWhere(new Brackets(qb => { .andWhere('(note.visibility = \'public\') AND (note.userHost = ANY(meta.recommendedInstances))')
qb.where(`(note.userHost IN (${ recommendedInstancesQuery.getQuery() })`)
.orWhere('(note.visibility = \'public\') AND (note.userHost IS NULL)');
}))
.innerJoinAndSelect('note.user', 'user') .innerJoinAndSelect('note.user', 'user')
.leftJoinAndSelect('user.avatar', 'avatar') .leftJoinAndSelect('user.avatar', 'avatar')
.leftJoinAndSelect('user.banner', 'banner') .leftJoinAndSelect('user.banner', 'banner')
@ -83,55 +79,41 @@ export default define(meta, paramDef, async (ps, user) => {
.leftJoinAndSelect('replyUser.banner', 'replyUserBanner') .leftJoinAndSelect('replyUser.banner', 'replyUserBanner')
.leftJoinAndSelect('renote.user', 'renoteUser') .leftJoinAndSelect('renote.user', 'renoteUser')
.leftJoinAndSelect('renoteUser.avatar', 'renoteUserAvatar') .leftJoinAndSelect('renoteUser.avatar', 'renoteUserAvatar')
.leftJoinAndSelect('renoteUser.banner', 'renoteUserBanner') .leftJoinAndSelect('renoteUser.banner', 'renoteUserBanner');
.setParameters(recommendedInstancesQuery.getParameters());
generateChannelQuery(query, user); generateChannelQuery(query, user);
generateRepliesQuery(query, user); generateRepliesQuery(query, user);
generateVisibilityQuery(query, user); generateVisibilityQuery(query, user);
generateMutedUserQuery(query, user); if (user) generateMutedUserQuery(query, user);
generateMutedNoteQuery(query, user); if (user) generateMutedNoteQuery(query, user);
generateBlockedUserQuery(query, user); if (user) generateBlockedUserQuery(query, user);
if (ps.includeMyRenotes === false) {
query.andWhere(new Brackets(qb => {
qb.orWhere('note.userId != :meId', { meId: user.id });
qb.orWhere('note.renoteId IS NULL');
qb.orWhere('note.text IS NOT NULL');
qb.orWhere('note.fileIds != \'{}\'');
qb.orWhere('0 < (SELECT COUNT(*) FROM poll WHERE poll."noteId" = note.id)');
}));
}
if (ps.includeRenotedMyNotes === false) {
query.andWhere(new Brackets(qb => {
qb.orWhere('note.renoteUserId != :meId', { meId: user.id });
qb.orWhere('note.renoteId IS NULL');
qb.orWhere('note.text IS NOT NULL');
qb.orWhere('note.fileIds != \'{}\'');
qb.orWhere('0 < (SELECT COUNT(*) FROM poll WHERE poll."noteId" = note.id)');
}));
}
if (ps.includeLocalRenotes === false) {
query.andWhere(new Brackets(qb => {
qb.orWhere('note.renoteUserHost IS NOT NULL');
qb.orWhere('note.renoteId IS NULL');
qb.orWhere('note.text IS NOT NULL');
qb.orWhere('note.fileIds != \'{}\'');
qb.orWhere('0 < (SELECT COUNT(*) FROM poll WHERE poll."noteId" = note.id)');
}));
}
if (ps.withFiles) { if (ps.withFiles) {
query.andWhere('note.fileIds != \'{}\''); query.andWhere('note.fileIds != \'{}\'');
} }
if (ps.fileType != null) {
query.andWhere('note.fileIds != \'{}\'');
query.andWhere(new Brackets(qb => {
for (const type of ps.fileType!) {
const i = ps.fileType!.indexOf(type);
qb.orWhere(`:type${i} = ANY(note.attachedFileTypes)`, { [`type${i}`]: type });
}
}));
if (ps.excludeNsfw) {
query.andWhere('note.cw IS NULL');
query.andWhere('0 = (SELECT COUNT(*) FROM drive_file df WHERE df.id = ANY(note."fileIds") AND df."isSensitive" = TRUE)');
}
}
//#endregion //#endregion
const timeline = await query.take(ps.limit).getMany(); const timeline = await query.take(ps.limit).getMany();
process.nextTick(() => { process.nextTick(() => {
if (user) {
activeUsersChart.read(user); activeUsersChart.read(user);
}
}); });
return await Notes.packMany(timeline, user); return await Notes.packMany(timeline, user);