2022-09-17 20:27:08 +02:00
|
|
|
import { DeepPartial } from 'typeorm';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { NoteReactionsRepository } from '@/models/index.js';
|
|
|
|
import type { NoteReaction } from '@/models/entities/NoteReaction.js';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { NoteReactionEntityService } from '@/core/entities/NoteReactionEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-04-17 06:01:30 +02:00
|
|
|
import { ApiError } from '../../error.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import type { FindOptionsWhere } from 'typeorm';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-07-16 21:36:44 +02:00
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['notes', 'reactions'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: false,
|
2018-07-16 21:36:44 +02:00
|
|
|
|
2022-07-05 12:16:21 +02:00
|
|
|
allowGet: true,
|
|
|
|
cacheSec: 60,
|
|
|
|
|
2019-02-26 21:08:42 +01:00
|
|
|
res: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-02-26 21:08:42 +01:00
|
|
|
items: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 15:35:26 +02:00
|
|
|
ref: 'NoteReaction',
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2019-02-26 21:08:42 +01:00
|
|
|
},
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
errors: {
|
|
|
|
noSuchNote: {
|
|
|
|
message: 'No such note.',
|
|
|
|
code: 'NO_SUCH_NOTE',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: '263fff3d-d0e1-4af4-bea7-8408059b451a',
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
noteId: { type: 'string', format: 'misskey:id' },
|
|
|
|
type: { type: 'string', nullable: true },
|
|
|
|
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
|
|
|
offset: { type: 'integer', default: 0 },
|
|
|
|
sinceId: { type: 'string', format: 'misskey:id' },
|
|
|
|
untilId: { type: 'string', format: 'misskey:id' },
|
|
|
|
},
|
|
|
|
required: ['noteId'],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 18:12:50 +01:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.noteReactionsRepository)
|
|
|
|
private noteReactionsRepository: NoteReactionsRepository,
|
2019-09-03 00:25:02 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
private noteReactionEntityService: NoteReactionEntityService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const query = {
|
|
|
|
noteId: ps.noteId,
|
|
|
|
} as FindOptionsWhere<NoteReaction>;
|
2018-10-29 02:52:36 +01:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
if (ps.type) {
|
|
|
|
// ローカルリアクションはホスト名が . とされているが
|
|
|
|
// DB 上ではそうではないので、必要に応じて変換
|
|
|
|
const suffix = '@.:';
|
|
|
|
const type = ps.type.endsWith(suffix) ? ps.type.slice(0, ps.type.length - suffix.length) + ':' : ps.type;
|
|
|
|
query.reaction = type;
|
|
|
|
}
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
const reactions = await this.noteReactionsRepository.find({
|
|
|
|
where: query,
|
|
|
|
take: ps.limit,
|
|
|
|
skip: ps.offset,
|
|
|
|
order: {
|
|
|
|
id: -1,
|
|
|
|
},
|
|
|
|
relations: ['user', 'user.avatar', 'user.banner', 'note'],
|
|
|
|
});
|
|
|
|
|
|
|
|
return await Promise.all(reactions.map(reaction => this.noteReactionEntityService.pack(reaction, me)));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|