2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { NotesRepository } from '@/models/index.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2023-05-05 08:52:14 +09:00
|
|
|
import { SearchService } from '@/core/SearchService.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-03-13 17:52:24 +09:00
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
|
|
|
import { ApiError } from '../../error.js';
|
2018-07-04 13:21:30 +09:00
|
|
|
|
2018-11-02 12:49:08 +09:00
|
|
|
export const meta = {
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['notes'],
|
|
|
|
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: false,
|
2018-11-02 12:49:08 +09:00
|
|
|
|
2019-02-23 11:20:58 +09:00
|
|
|
res: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-02-23 11:20:58 +09:00
|
|
|
items: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 22:35:26 +09:00
|
|
|
ref: 'Note',
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2019-02-23 11:20:58 +09:00
|
|
|
},
|
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
errors: {
|
2023-03-13 17:52:24 +09:00
|
|
|
unavailable: {
|
|
|
|
message: 'Search of notes unavailable.',
|
|
|
|
code: 'UNAVAILABLE',
|
|
|
|
id: '0b44998d-77aa-4427-80d0-d2c9b8523011',
|
|
|
|
},
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2018-11-02 12:49:08 +09:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
query: { type: 'string' },
|
|
|
|
sinceId: { type: 'string', format: 'misskey:id' },
|
|
|
|
untilId: { type: 'string', format: 'misskey:id' },
|
|
|
|
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
|
|
|
offset: { type: 'integer', default: 0 },
|
2022-04-03 13:57:26 +09:00
|
|
|
host: {
|
|
|
|
type: 'string',
|
|
|
|
nullable: true,
|
|
|
|
description: 'The local host is represented with `null`.',
|
|
|
|
},
|
2022-02-19 14:05:32 +09:00
|
|
|
userId: { type: 'string', format: 'misskey:id', nullable: true, default: null },
|
|
|
|
channelId: { type: 'string', format: 'misskey:id', nullable: true, default: null },
|
|
|
|
},
|
|
|
|
required: ['query'],
|
|
|
|
} as const;
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// TODO: ロジックをサービスに切り出す
|
2020-01-30 04:37:25 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
private noteEntityService: NoteEntityService,
|
2023-05-05 08:52:14 +09:00
|
|
|
private searchService: SearchService,
|
2023-03-13 17:52:24 +09:00
|
|
|
private roleService: RoleService,
|
2022-09-18 03:27:08 +09:00
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2023-03-13 17:52:24 +09:00
|
|
|
const policies = await this.roleService.getUserPolicies(me ? me.id : null);
|
|
|
|
if (!policies.canSearchNotes) {
|
|
|
|
throw new ApiError(meta.errors.unavailable);
|
|
|
|
}
|
|
|
|
|
2023-05-05 08:52:14 +09:00
|
|
|
const notes = await this.searchService.searchNote(ps.query, me, {
|
|
|
|
userId: ps.userId,
|
|
|
|
channelId: ps.channelId,
|
|
|
|
}, {
|
|
|
|
untilId: ps.untilId,
|
|
|
|
sinceId: ps.sinceId,
|
|
|
|
limit: ps.limit,
|
|
|
|
});
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
return await this.noteEntityService.packMany(notes, me);
|
2020-01-30 04:37:25 +09:00
|
|
|
});
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|