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