2022-09-17 21:27:08 +03:00
|
|
|
import { DeepPartial } from 'typeorm';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2022-09-20 23:33:11 +03:00
|
|
|
import type { NoteReactionsRepository } from '@/models/index.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
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 07:01:30 +03:00
|
|
|
import { ApiError } from '../../error.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import type { FindOptionsWhere } from 'typeorm';
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-07-16 22:36:44 +03:00
|
|
|
export const meta = {
|
2019-02-23 04:20:58 +02:00
|
|
|
tags: ['notes', 'reactions'],
|
|
|
|
|
2022-01-18 15:27:10 +02:00
|
|
|
requireCredential: false,
|
2018-07-16 22:36:44 +03:00
|
|
|
|
2022-07-05 13:16:21 +03:00
|
|
|
allowGet: true,
|
|
|
|
cacheSec: 60,
|
|
|
|
|
2019-02-26 22:08:42 +02:00
|
|
|
res: {
|
2022-01-18 15:27:10 +02:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-02-26 22:08:42 +02:00
|
|
|
items: {
|
2022-01-18 15:27:10 +02:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 16:35:26 +03:00
|
|
|
ref: 'NoteReaction',
|
2021-12-09 16:58:30 +02:00
|
|
|
},
|
2019-02-26 22:08:42 +02:00
|
|
|
},
|
|
|
|
|
2019-02-22 04:46:58 +02:00
|
|
|
errors: {
|
|
|
|
noSuchNote: {
|
|
|
|
message: 'No such note.',
|
|
|
|
code: 'NO_SUCH_NOTE',
|
2021-12-09 16:58:30 +02:00
|
|
|
id: '263fff3d-d0e1-4af4-bea7-8408059b451a',
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 15:27:10 +02:00
|
|
|
} as const;
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2022-02-20 06:15:40 +02:00
|
|
|
export const paramDef = {
|
2022-02-19 07:05:32 +02: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 19:12:50 +02:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 21:27:08 +03:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.noteReactionsRepository)
|
|
|
|
private noteReactionsRepository: NoteReactionsRepository,
|
2019-09-03 01:25:02 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
private noteReactionEntityService: NoteReactionEntityService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const query = {
|
|
|
|
noteId: ps.noteId,
|
|
|
|
} as FindOptionsWhere<NoteReaction>;
|
2018-10-29 03:52:36 +02:00
|
|
|
|
2022-09-17 21:27:08 +03: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-29 00:49:51 +02:00
|
|
|
|
2022-09-17 21:27:08 +03: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)));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|