2022-09-17 21:27:08 +03:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import type { Note } from '@/models/entities/Note.js';
|
2022-09-20 23:33:11 +03:00
|
|
|
import type { NotesRepository } from '@/models/index.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-02-27 04:07:39 +02:00
|
|
|
import { ApiError } from '../../error.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import { GetterService } from '../../common/GetterService.js';
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
export const meta = {
|
2019-02-23 04:20:58 +02:00
|
|
|
tags: ['notes'],
|
|
|
|
|
2022-01-18 15:27:10 +02:00
|
|
|
requireCredential: false,
|
2018-11-01 20:32:24 +02:00
|
|
|
|
2019-02-23 04:20:58 +02:00
|
|
|
res: {
|
2022-01-18 15:27:10 +02:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-02-23 04:20:58 +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: 'Note',
|
2021-12-09 16:58:30 +02:00
|
|
|
},
|
2019-02-23 04:20:58 +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: 'e1035875-9551-45ec-afa8-1ded1fcb53c8',
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 15:27:10 +02:00
|
|
|
} as const;
|
2018-11-01 20:32:24 +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' },
|
|
|
|
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
|
|
|
offset: { type: 'integer', default: 0 },
|
|
|
|
},
|
|
|
|
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.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
private noteEntityService: NoteEntityService,
|
|
|
|
private getterService: GetterService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const note = await this.getterService.getNote(ps.noteId).catch(err => {
|
|
|
|
if (err.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(meta.errors.noSuchNote);
|
|
|
|
throw err;
|
|
|
|
});
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
const conversation: Note[] = [];
|
|
|
|
let i = 0;
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
const get = async (id: any) => {
|
|
|
|
i++;
|
|
|
|
const p = await this.notesRepository.findOneBy({ id });
|
|
|
|
if (p == null) return;
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
if (i > ps.offset!) {
|
|
|
|
conversation.push(p);
|
|
|
|
}
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
if (conversation.length === ps.limit) {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
if (p.replyId) {
|
|
|
|
await get(p.replyId);
|
|
|
|
}
|
|
|
|
};
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
if (note.replyId) {
|
|
|
|
await get(note.replyId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await this.noteEntityService.packMany(conversation, me);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|