2022-02-27 11:07:39 +09:00
|
|
|
import es from '../../../../db/elasticsearch.js';
|
|
|
|
import define from '../../define.js';
|
|
|
|
import { Notes } from '@/models/index.js';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { In } from 'typeorm';
|
2022-02-27 11:07:39 +09:00
|
|
|
import config from '@/config/index.js';
|
|
|
|
import { makePaginationQuery } from '../../common/make-pagination-query.js';
|
|
|
|
import { generateVisibilityQuery } from '../../common/generate-visibility-query.js';
|
|
|
|
import { generateMutedUserQuery } from '../../common/generate-muted-user-query.js';
|
|
|
|
import { generateBlockedUserQuery } from '../../common/generate-block-query.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: {
|
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-02 16:47:53 +09:00
|
|
|
host: { type: 'string', nullable: true },
|
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-01-03 02:12:50 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 14:05:32 +09:00
|
|
|
export default define(meta, paramDef, async (ps, me) => {
|
2020-01-30 04:37:25 +09:00
|
|
|
if (es == null) {
|
2021-02-19 21:40:09 +09:00
|
|
|
const query = makePaginationQuery(Notes.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
|
2020-01-30 04:37:25 +09:00
|
|
|
.andWhere('note.text ILIKE :q', { q: `%${ps.query}%` })
|
2021-03-21 12:33:37 +09:00
|
|
|
.innerJoinAndSelect('note.user', 'user')
|
2022-02-27 13:59:10 +09:00
|
|
|
.leftJoinAndSelect('user.avatar', 'avatar')
|
|
|
|
.leftJoinAndSelect('user.banner', 'banner')
|
2021-03-21 12:31:56 +09:00
|
|
|
.leftJoinAndSelect('note.reply', 'reply')
|
|
|
|
.leftJoinAndSelect('note.renote', 'renote')
|
|
|
|
.leftJoinAndSelect('reply.user', 'replyUser')
|
2022-02-27 13:59:10 +09:00
|
|
|
.leftJoinAndSelect('replyUser.avatar', 'replyUserAvatar')
|
|
|
|
.leftJoinAndSelect('replyUser.banner', 'replyUserBanner')
|
|
|
|
.leftJoinAndSelect('renote.user', 'renoteUser')
|
|
|
|
.leftJoinAndSelect('renoteUser.avatar', 'renoteUserAvatar')
|
|
|
|
.leftJoinAndSelect('renoteUser.banner', 'renoteUserBanner');
|
2018-07-19 08:24:03 +09:00
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
generateVisibilityQuery(query, me);
|
2020-07-28 09:38:41 +09:00
|
|
|
if (me) generateMutedUserQuery(query, me);
|
2021-08-17 21:48:59 +09:00
|
|
|
if (me) generateBlockedUserQuery(query, me);
|
2020-01-30 04:37:25 +09:00
|
|
|
|
2022-02-19 14:05:32 +09:00
|
|
|
const notes = await query.take(ps.limit).getMany();
|
2020-01-30 04:37:25 +09:00
|
|
|
|
|
|
|
return await Notes.packMany(notes, me);
|
|
|
|
} else {
|
|
|
|
const userQuery = ps.userId != null ? [{
|
2019-04-25 07:46:39 +09:00
|
|
|
term: {
|
2021-12-09 23:58:30 +09:00
|
|
|
userId: ps.userId,
|
|
|
|
},
|
2020-01-30 04:37:25 +09:00
|
|
|
}] : [];
|
|
|
|
|
|
|
|
const hostQuery = ps.userId == null ?
|
|
|
|
ps.host === null ? [{
|
2019-04-25 07:46:39 +09:00
|
|
|
bool: {
|
2020-01-30 04:37:25 +09:00
|
|
|
must_not: {
|
|
|
|
exists: {
|
2021-12-09 23:58:30 +09:00
|
|
|
field: 'userHost',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-01-30 04:37:25 +09:00
|
|
|
}] : ps.host !== undefined ? [{
|
|
|
|
term: {
|
2021-12-09 23:58:30 +09:00
|
|
|
userHost: ps.host,
|
|
|
|
},
|
2020-01-30 04:37:25 +09:00
|
|
|
}] : []
|
|
|
|
: [];
|
|
|
|
|
|
|
|
const result = await es.search({
|
|
|
|
index: config.elasticsearch.index || 'misskey_note',
|
|
|
|
body: {
|
2022-02-19 14:05:32 +09:00
|
|
|
size: ps.limit,
|
2020-01-30 04:37:25 +09:00
|
|
|
from: ps.offset,
|
|
|
|
query: {
|
|
|
|
bool: {
|
|
|
|
must: [{
|
|
|
|
simple_query_string: {
|
|
|
|
fields: ['text'],
|
|
|
|
query: ps.query.toLowerCase(),
|
2021-12-09 23:58:30 +09:00
|
|
|
default_operator: 'and',
|
2020-01-30 04:37:25 +09:00
|
|
|
},
|
2021-12-09 23:58:30 +09:00
|
|
|
}, ...hostQuery, ...userQuery],
|
|
|
|
},
|
2020-01-30 04:37:25 +09:00
|
|
|
},
|
|
|
|
sort: [{
|
2021-12-09 23:58:30 +09:00
|
|
|
_doc: 'desc',
|
|
|
|
}],
|
|
|
|
},
|
2020-01-30 04:37:25 +09:00
|
|
|
});
|
2018-07-04 13:21:30 +09:00
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
const hits = result.body.hits.hits.map((hit: any) => hit._id);
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
if (hits.length === 0) return [];
|
2018-07-04 13:21:30 +09:00
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
// Fetch found notes
|
|
|
|
const notes = await Notes.find({
|
|
|
|
where: {
|
2021-12-09 23:58:30 +09:00
|
|
|
id: In(hits),
|
2020-01-30 04:37:25 +09:00
|
|
|
},
|
|
|
|
order: {
|
2021-12-09 23:58:30 +09:00
|
|
|
id: -1,
|
|
|
|
},
|
2020-01-30 04:37:25 +09:00
|
|
|
});
|
2019-02-22 11:46:58 +09:00
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
return await Notes.packMany(notes, me);
|
|
|
|
}
|
2019-02-22 11:46:58 +09:00
|
|
|
});
|