import $ from 'cafy'; import { ID } from '../../../../misc/cafy-id'; import define from '../../define'; import { ApiError } from '../../error'; import { UserLists, UserListJoinings, Notes } from '../../../../models'; import { makePaginationQuery } from '../../common/make-pagination-query'; import { generateVisibilityQuery } from '../../common/generate-visibility-query'; import { activeUsersChart } from '../../../../services/chart'; import { Brackets } from 'typeorm'; export const meta = { desc: { 'ja-JP': '指定したユーザーリストのタイムラインを取得します。', 'en-US': 'Get timeline of a user list.' }, tags: ['notes', 'lists'], requireCredential: true as const, params: { listId: { validator: $.type(ID), desc: { 'ja-JP': 'リストのID' } }, limit: { validator: $.optional.num.range(1, 100), default: 10, desc: { 'ja-JP': '最大数' } }, sinceId: { validator: $.optional.type(ID), desc: { 'ja-JP': '指定すると、その投稿を基点としてより新しい投稿を取得します' } }, untilId: { validator: $.optional.type(ID), desc: { 'ja-JP': '指定すると、その投稿を基点としてより古い投稿を取得します' } }, sinceDate: { validator: $.optional.num, desc: { 'ja-JP': '指定した時間を基点としてより新しい投稿を取得します。数値は、1970年1月1日 00:00:00 UTC から指定した日時までの経過時間をミリ秒単位で表します。' } }, untilDate: { validator: $.optional.num, desc: { 'ja-JP': '指定した時間を基点としてより古い投稿を取得します。数値は、1970年1月1日 00:00:00 UTC から指定した日時までの経過時間をミリ秒単位で表します。' } }, includeMyRenotes: { validator: $.optional.bool, default: true, desc: { 'ja-JP': '自分の行ったRenoteを含めるかどうか' } }, includeRenotedMyNotes: { validator: $.optional.bool, default: true, desc: { 'ja-JP': 'Renoteされた自分の投稿を含めるかどうか' } }, includeLocalRenotes: { validator: $.optional.bool, default: true, desc: { 'ja-JP': 'Renoteされたローカルの投稿を含めるかどうか' } }, withFiles: { validator: $.optional.bool, desc: { 'ja-JP': 'true にすると、ファイルが添付された投稿だけ取得します' } }, }, res: { type: 'array' as const, optional: false as const, nullable: false as const, items: { type: 'object' as const, optional: false as const, nullable: false as const, ref: 'Note', } }, errors: { noSuchList: { message: 'No such list.', code: 'NO_SUCH_LIST', id: '8fb1fbd5-e476-4c37-9fb0-43d55b63a2ff' } } }; export default define(meta, async (ps, user) => { const list = await UserLists.findOne({ id: ps.listId, userId: user.id }); if (list == null) { throw new ApiError(meta.errors.noSuchList); } //#region Construct query const listQuery = UserListJoinings.createQueryBuilder('joining') .select('joining.userId') .where('joining.userListId = :userListId', { userListId: list.id }); const query = makePaginationQuery(Notes.createQueryBuilder('note'), ps.sinceId, ps.untilId) .andWhere(`note.userId IN (${ listQuery.getQuery() })`) .leftJoinAndSelect('note.user', 'user') .setParameters(listQuery.getParameters()); generateVisibilityQuery(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) { query.andWhere('note.fileIds != \'{}\''); } //#endregion const timeline = await query.take(ps.limit!).getMany(); activeUsersChart.update(user); return await Notes.packMany(timeline, user); });