2020-01-29 19:37:25 +00:00
|
|
|
import $ from 'cafy';
|
|
|
|
import { ID } from '../../../../misc/cafy-id';
|
|
|
|
import define from '../../define';
|
2020-11-15 03:04:54 +00:00
|
|
|
import { ClipNotes, Clips, Notes } from '../../../../models';
|
2020-01-29 19:37:25 +00:00
|
|
|
import { makePaginationQuery } from '../../common/make-pagination-query';
|
|
|
|
import { generateVisibilityQuery } from '../../common/generate-visibility-query';
|
2020-07-28 00:38:41 +00:00
|
|
|
import { generateMutedUserQuery } from '../../common/generate-muted-user-query';
|
2020-11-15 03:04:54 +00:00
|
|
|
import { ApiError } from '../../error';
|
2020-01-29 19:37:25 +00:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['account', 'notes', 'clips'],
|
|
|
|
|
2020-11-15 08:35:40 +00:00
|
|
|
requireCredential: false as const,
|
2020-01-29 19:37:25 +00:00
|
|
|
|
|
|
|
kind: 'read:account',
|
|
|
|
|
|
|
|
params: {
|
2020-11-15 03:04:54 +00:00
|
|
|
clipId: {
|
|
|
|
validator: $.type(ID),
|
|
|
|
},
|
|
|
|
|
2020-01-29 19:37:25 +00:00
|
|
|
limit: {
|
|
|
|
validator: $.optional.num.range(1, 100),
|
|
|
|
default: 10
|
|
|
|
},
|
|
|
|
|
|
|
|
sinceId: {
|
|
|
|
validator: $.optional.type(ID),
|
|
|
|
},
|
|
|
|
|
|
|
|
untilId: {
|
|
|
|
validator: $.optional.type(ID),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchClip: {
|
2020-11-15 03:04:54 +00:00
|
|
|
message: 'No such clip.',
|
2020-01-29 19:37:25 +00:00
|
|
|
code: 'NO_SUCH_CLIP',
|
|
|
|
id: '1d7645e6-2b6d-4635-b0fe-fe22b0e72e00'
|
|
|
|
}
|
2021-03-06 13:34:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
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'
|
|
|
|
}
|
2020-01-29 19:37:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default define(meta, async (ps, user) => {
|
|
|
|
const clip = await Clips.findOne({
|
|
|
|
id: ps.clipId,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (clip == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchClip);
|
|
|
|
}
|
|
|
|
|
2020-11-15 08:35:40 +00:00
|
|
|
if (!clip.isPublic && (user == null || (clip.userId !== user.id))) {
|
2020-11-15 08:32:29 +00:00
|
|
|
throw new ApiError(meta.errors.noSuchClip);
|
|
|
|
}
|
|
|
|
|
2020-01-29 19:37:25 +00:00
|
|
|
const clipQuery = ClipNotes.createQueryBuilder('joining')
|
|
|
|
.select('joining.noteId')
|
|
|
|
.where('joining.clipId = :clipId', { clipId: clip.id });
|
|
|
|
|
|
|
|
const query = makePaginationQuery(Notes.createQueryBuilder('note'), ps.sinceId, ps.untilId)
|
|
|
|
.andWhere(`note.id IN (${ clipQuery.getQuery() })`)
|
2021-03-21 03:31:56 +00:00
|
|
|
.leftJoinAndSelect('note.user', 'user')
|
|
|
|
.leftJoinAndSelect('note.reply', 'reply')
|
|
|
|
.leftJoinAndSelect('note.renote', 'renote')
|
|
|
|
.leftJoinAndSelect('reply.user', 'replyUser')
|
|
|
|
.leftJoinAndSelect('renote.user', 'renoteUser')
|
2020-01-29 19:37:25 +00:00
|
|
|
.setParameters(clipQuery.getParameters());
|
|
|
|
|
2020-11-15 08:35:40 +00:00
|
|
|
if (user) {
|
|
|
|
generateVisibilityQuery(query, user);
|
|
|
|
generateMutedUserQuery(query, user);
|
|
|
|
}
|
2020-01-29 19:37:25 +00:00
|
|
|
|
|
|
|
const notes = await query
|
|
|
|
.take(ps.limit!)
|
|
|
|
.getMany();
|
|
|
|
|
|
|
|
return await Notes.packMany(notes, user);
|
|
|
|
});
|