Sharkey/packages/backend/src/server/api/endpoints/notes/conversation.ts
2022-09-24 07:15:16 +09:00

88 lines
2.1 KiB
TypeScript

import { Inject, Injectable } from '@nestjs/common';
import type { Note } from '@/models/entities/Note.js';
import type { NotesRepository } from '@/models/index.js';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
import { DI } from '@/di-symbols.js';
import { ApiError } from '../../error.js';
import { GetterService } from '@/server/api/GetterService.js';
export const meta = {
tags: ['notes'],
requireCredential: false,
res: {
type: 'array',
optional: false, nullable: false,
items: {
type: 'object',
optional: false, nullable: false,
ref: 'Note',
},
},
errors: {
noSuchNote: {
message: 'No such note.',
code: 'NO_SUCH_NOTE',
id: 'e1035875-9551-45ec-afa8-1ded1fcb53c8',
},
},
} as const;
export const paramDef = {
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;
// eslint-disable-next-line import/no-default-export
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> {
constructor(
@Inject(DI.notesRepository)
private notesRepository: NotesRepository,
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;
});
const conversation: Note[] = [];
let i = 0;
const get = async (id: any) => {
i++;
const p = await this.notesRepository.findOneBy({ id });
if (p == null) return;
if (i > ps.offset!) {
conversation.push(p);
}
if (conversation.length === ps.limit) {
return;
}
if (p.replyId) {
await get(p.replyId);
}
};
if (note.replyId) {
await get(note.replyId);
}
return await this.noteEntityService.packMany(conversation, me);
});
}
}