2018-07-04 04:21:30 +00:00
|
|
|
import $ from 'cafy';
|
|
|
|
import es from '../../../../db/elasticsearch';
|
2018-11-02 04:47:44 +00:00
|
|
|
import define from '../../define';
|
2019-04-07 12:50:36 +00:00
|
|
|
import { Notes } from '../../../../models';
|
|
|
|
import { In } from 'typeorm';
|
2019-04-24 22:46:39 +00:00
|
|
|
import { ID } from '../../../../misc/cafy-id';
|
2019-08-09 04:04:35 +00:00
|
|
|
import config from '../../../../config';
|
2020-01-29 19:37:25 +00:00
|
|
|
import { makePaginationQuery } from '../../common/make-pagination-query';
|
|
|
|
import { generateVisibilityQuery } from '../../common/generate-visibility-query';
|
2020-07-28 00:38:41 +00:00
|
|
|
import { generateMutedUserQuery } from '../../common/generate-muted-user-query';
|
2018-07-04 04:21:30 +00:00
|
|
|
|
2018-11-02 03:49:08 +00:00
|
|
|
export const meta = {
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '投稿を検索します。',
|
|
|
|
'en-US': 'Search notes.'
|
|
|
|
},
|
|
|
|
|
2019-02-23 02:20:58 +00:00
|
|
|
tags: ['notes'],
|
|
|
|
|
2020-02-15 12:33:32 +00:00
|
|
|
requireCredential: false as const,
|
2018-11-02 03:49:08 +00:00
|
|
|
|
|
|
|
params: {
|
|
|
|
query: {
|
|
|
|
validator: $.str
|
|
|
|
},
|
2018-07-04 04:21:30 +00:00
|
|
|
|
2020-01-29 19:37:25 +00:00
|
|
|
sinceId: {
|
|
|
|
validator: $.optional.type(ID),
|
|
|
|
},
|
|
|
|
|
|
|
|
untilId: {
|
|
|
|
validator: $.optional.type(ID),
|
|
|
|
},
|
|
|
|
|
2018-11-02 03:49:08 +00:00
|
|
|
limit: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.num.range(1, 100),
|
2018-11-02 03:49:08 +00:00
|
|
|
default: 10
|
|
|
|
},
|
2018-07-04 04:21:30 +00:00
|
|
|
|
2019-04-24 22:46:39 +00:00
|
|
|
host: {
|
|
|
|
validator: $.optional.nullable.str,
|
|
|
|
default: undefined
|
|
|
|
},
|
|
|
|
|
|
|
|
userId: {
|
|
|
|
validator: $.optional.nullable.type(ID),
|
|
|
|
default: null
|
|
|
|
},
|
2021-02-19 12:40:09 +00:00
|
|
|
|
|
|
|
channelId: {
|
|
|
|
validator: $.optional.nullable.type(ID),
|
|
|
|
default: null
|
|
|
|
},
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
|
|
|
|
2019-02-23 02:20:58 +00:00
|
|
|
res: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'array' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-02-23 02:20:58 +00:00
|
|
|
items: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
ref: 'Note',
|
|
|
|
}
|
2019-02-23 02:20:58 +00:00
|
|
|
},
|
|
|
|
|
2019-02-22 02:46:58 +00:00
|
|
|
errors: {
|
2018-11-02 03:49:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-22 02:46:58 +00:00
|
|
|
export default define(meta, async (ps, me) => {
|
2020-01-29 19:37:25 +00:00
|
|
|
if (es == null) {
|
2021-02-19 12:40:09 +00: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 19:37:25 +00:00
|
|
|
.andWhere('note.text ILIKE :q', { q: `%${ps.query}%` })
|
2021-03-20 04:54:59 +00:00
|
|
|
.leftJoinAndSelect('note.user', 'user')
|
|
|
|
.leftJoinAndSelect('note.reply', 'reply')
|
|
|
|
.leftJoinAndSelect('note.renote', 'renote')
|
|
|
|
.leftJoinAndSelect('reply.user', 'replyUser')
|
|
|
|
.leftJoinAndSelect('renote.user', 'renoteUser');
|
2018-07-18 23:24:03 +00:00
|
|
|
|
2020-01-29 19:37:25 +00:00
|
|
|
generateVisibilityQuery(query, me);
|
2020-07-28 00:38:41 +00:00
|
|
|
if (me) generateMutedUserQuery(query, me);
|
2020-01-29 19:37:25 +00:00
|
|
|
|
|
|
|
const notes = await query.take(ps.limit!).getMany();
|
|
|
|
|
|
|
|
return await Notes.packMany(notes, me);
|
|
|
|
} else {
|
|
|
|
const userQuery = ps.userId != null ? [{
|
2019-04-24 22:46:39 +00:00
|
|
|
term: {
|
2020-01-29 19:37:25 +00:00
|
|
|
userId: ps.userId
|
2019-04-24 22:46:39 +00:00
|
|
|
}
|
2020-01-29 19:37:25 +00:00
|
|
|
}] : [];
|
|
|
|
|
|
|
|
const hostQuery = ps.userId == null ?
|
|
|
|
ps.host === null ? [{
|
2019-04-24 22:46:39 +00:00
|
|
|
bool: {
|
2020-01-29 19:37:25 +00:00
|
|
|
must_not: {
|
|
|
|
exists: {
|
|
|
|
field: 'userHost'
|
|
|
|
}
|
|
|
|
}
|
2018-07-04 04:21:30 +00:00
|
|
|
}
|
2020-01-29 19:37:25 +00:00
|
|
|
}] : ps.host !== undefined ? [{
|
|
|
|
term: {
|
|
|
|
userHost: ps.host
|
|
|
|
}
|
|
|
|
}] : []
|
|
|
|
: [];
|
|
|
|
|
|
|
|
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(),
|
|
|
|
default_operator: 'and'
|
|
|
|
},
|
|
|
|
}, ...hostQuery, ...userQuery]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
sort: [{
|
|
|
|
_doc: 'desc'
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
});
|
2018-07-04 04:21:30 +00:00
|
|
|
|
2020-01-29 19:37:25 +00:00
|
|
|
const hits = result.body.hits.hits.map((hit: any) => hit._id);
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2020-01-29 19:37:25 +00:00
|
|
|
if (hits.length === 0) return [];
|
2018-07-04 04:21:30 +00:00
|
|
|
|
2020-01-29 19:37:25 +00:00
|
|
|
// Fetch found notes
|
|
|
|
const notes = await Notes.find({
|
|
|
|
where: {
|
|
|
|
id: In(hits)
|
|
|
|
},
|
|
|
|
order: {
|
|
|
|
id: -1
|
|
|
|
}
|
|
|
|
});
|
2019-02-22 02:46:58 +00:00
|
|
|
|
2020-01-29 19:37:25 +00:00
|
|
|
return await Notes.packMany(notes, me);
|
|
|
|
}
|
2019-02-22 02:46:58 +00:00
|
|
|
});
|