2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { NotesRepository } from '@/models/index.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { QueryService } from '@/core/QueryService.js';
|
|
|
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-01-09 00:58:16 +01:00
|
|
|
import { sqlLikeEscape } from '@/misc/sql-like-escape.js';
|
2023-03-13 09:52:24 +01:00
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
|
|
|
import { ApiError } from '../../error.js';
|
2018-07-04 06:21:30 +02:00
|
|
|
|
2018-11-02 04:49:08 +01:00
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['notes'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: false,
|
2018-11-02 04:49:08 +01:00
|
|
|
|
2019-02-23 03:20:58 +01:00
|
|
|
res: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-02-23 03:20:58 +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: 'Note',
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2019-02-23 03:20:58 +01:00
|
|
|
},
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
errors: {
|
2023-03-13 09:52:24 +01:00
|
|
|
unavailable: {
|
|
|
|
message: 'Search of notes unavailable.',
|
|
|
|
code: 'UNAVAILABLE',
|
|
|
|
id: '0b44998d-77aa-4427-80d0-d2c9b8523011',
|
|
|
|
},
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2018-11-02 04:49:08 +01:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01: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 06:57:26 +02:00
|
|
|
host: {
|
|
|
|
type: 'string',
|
|
|
|
nullable: true,
|
|
|
|
description: 'The local host is represented with `null`.',
|
|
|
|
},
|
2022-02-19 06:05:32 +01: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-17 20:27:08 +02:00
|
|
|
// TODO: ロジックをサービスに切り出す
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2022-09-17 20:27:08 +02: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,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
private noteEntityService: NoteEntityService,
|
|
|
|
private queryService: QueryService,
|
2023-03-13 09:52:24 +01:00
|
|
|
private roleService: RoleService,
|
2022-09-17 20:27:08 +02:00
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2023-03-13 09:52:24 +01:00
|
|
|
const policies = await this.roleService.getUserPolicies(me ? me.id : null);
|
|
|
|
if (!policies.canSearchNotes) {
|
|
|
|
throw new ApiError(meta.errors.unavailable);
|
|
|
|
}
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), ps.sinceId, ps.untilId);
|
|
|
|
|
|
|
|
if (ps.userId) {
|
|
|
|
query.andWhere('note.userId = :userId', { userId: ps.userId });
|
|
|
|
} else if (ps.channelId) {
|
|
|
|
query.andWhere('note.channelId = :channelId', { channelId: ps.channelId });
|
|
|
|
}
|
|
|
|
|
|
|
|
query
|
2023-01-08 12:32:17 +01:00
|
|
|
.andWhere('note.text ILIKE :q', { q: `%${ sqlLikeEscape(ps.query) }%` })
|
2022-09-17 20:27:08 +02:00
|
|
|
.innerJoinAndSelect('note.user', 'user')
|
|
|
|
.leftJoinAndSelect('note.reply', 'reply')
|
|
|
|
.leftJoinAndSelect('note.renote', 'renote')
|
|
|
|
.leftJoinAndSelect('reply.user', 'replyUser')
|
2023-04-06 12:48:24 +02:00
|
|
|
.leftJoinAndSelect('renote.user', 'renoteUser');
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
this.queryService.generateVisibilityQuery(query, me);
|
|
|
|
if (me) this.queryService.generateMutedUserQuery(query, me);
|
|
|
|
if (me) this.queryService.generateBlockedUserQuery(query, me);
|
|
|
|
|
|
|
|
const notes = await query.take(ps.limit).getMany();
|
|
|
|
|
|
|
|
return await this.noteEntityService.packMany(notes, me);
|
2020-01-29 20:37:25 +01:00
|
|
|
});
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|