From af806352a1c3701b5d8b2088dcf2154732b00152 Mon Sep 17 00:00:00 2001
From: taichan <40626578+taichanNE30@users.noreply.github.com>
Date: Sun, 20 Aug 2023 13:39:37 +0900
Subject: [PATCH] =?UTF-8?q?feat:=20=E3=83=AD=E3=83=BC=E3=82=AB=E3=83=AB?=
 =?UTF-8?q?=E3=81=AE=E3=81=BF=E3=83=8E=E3=83=BC=E3=83=88=E6=A4=9C=E7=B4=A2?=
 =?UTF-8?q?=20(#11451)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Add local search

* Update CHANGELOG

* lint

* Remove TODO comment

* lint

* Update packages/backend/src/core/SearchService.ts

---------

Co-authored-by: syuilo <Syuilotan@yahoo.co.jp>
---
 CHANGELOG.md                                |  2 ++
 packages/backend/src/core/SearchService.ts  | 14 ++++++++++-
 packages/frontend/src/pages/search.note.vue | 28 +++++++++++++--------
 3 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 02a0fa16d6..2782dcf450 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,6 +27,7 @@
 - 絵文字ピッカーの検索の表示件数を100件に増加
 - Enhance: ユーザーメニューでスイッチでユーザーリストに追加・削除できるように
 - Enhance: 自分が押したリアクションのデザインを改善
+- Enhance: ノート検索にローカルのみ検索可能なオプションの追加
 - Fix: サーバー情報画面(`/instance-info/{domain}`)でブロックができないのを修正
 - Fix: 未読のお知らせの「わかった」をクリック・タップしてもその場で「わかった」が消えない問題を修正
 - Fix: iOSで画面を回転させるとテキストサイズが変わる問題を修正
@@ -35,6 +36,7 @@
 - Fix: Misskeyプラグインをインストールする際のAiScriptバージョンのチェックが0.14.0以降に対応していない問題を修正
 
 ### Server
+- Fix: ノート検索 `notes/search` にてhostを指定した際に検索結果に反映されるように
 - cacheRemoteFilesの初期値はfalseになりました
 - ファイルアップロード時等にファイル名の拡張子を修正する関数(correctFilename)の挙動を改善
 - Fix: 一部のfeatured noteを照会できない問題を修正
diff --git a/packages/backend/src/core/SearchService.ts b/packages/backend/src/core/SearchService.ts
index ce80744c6d..f21bd790be 100644
--- a/packages/backend/src/core/SearchService.ts
+++ b/packages/backend/src/core/SearchService.ts
@@ -25,6 +25,8 @@ type Q =
 	{ op: '<', k: K, v: number } |
 	{ op: '>=', k: K, v: number } |
 	{ op: '<=', k: K, v: number } |
+	{ op: 'is null', k: K} |
+	{ op: 'is not null', k: K} |
 	{ op: 'and', qs: Q[] } |
 	{ op: 'or', qs: Q[] } |
 	{ op: 'not', q: Q };
@@ -50,6 +52,8 @@ function compileQuery(q: Q): string {
 		case '<=': return `(${q.k} <= ${compileValue(q.v)})`;
 		case 'and': return q.qs.length === 0 ? '' : `(${ q.qs.map(_q => compileQuery(_q)).join(' AND ') })`;
 		case 'or': return q.qs.length === 0 ? '' : `(${ q.qs.map(_q => compileQuery(_q)).join(' OR ') })`;
+		case 'is null': return `(${q.k} IS NULL)`;
+		case 'is not null': return `(${q.k} IS NOT NULL)`;
 		case 'not': return `(NOT ${compileQuery(q.q)})`;
 		default: throw new Error('unrecognized query operator');
 	}
@@ -170,7 +174,7 @@ export class SearchService {
 			if (opts.channelId) filter.qs.push({ op: '=', k: 'channelId', v: opts.channelId });
 			if (opts.host) {
 				if (opts.host === '.') {
-					// TODO: Meilisearchが2023/05/07現在値がNULLかどうかのクエリが書けない
+					filter.qs.push({ op: 'is null', k: 'userHost' });
 				} else {
 					filter.qs.push({ op: '=', k: 'userHost', v: opts.host });
 				}
@@ -204,6 +208,14 @@ export class SearchService {
 				.leftJoinAndSelect('reply.user', 'replyUser')
 				.leftJoinAndSelect('renote.user', 'renoteUser');
 
+			if (opts.host) {
+				if (opts.host === '.') {
+					query.andWhere('user.host IS NULL');
+				} else {
+					query.andWhere('user.host = :host', { host: opts.host });
+				}
+			}
+
 			this.queryService.generateVisibilityQuery(query, me);
 			if (me) this.queryService.generateMutedUserQuery(query, me);
 			if (me) this.queryService.generateBlockedUserQuery(query, me);
diff --git a/packages/frontend/src/pages/search.note.vue b/packages/frontend/src/pages/search.note.vue
index 799dac2096..b0881fdedb 100644
--- a/packages/frontend/src/pages/search.note.vue
+++ b/packages/frontend/src/pages/search.note.vue
@@ -12,18 +12,22 @@ SPDX-License-Identifier: AGPL-3.0-only
 		<MkFolder>
 			<template #label>{{ i18n.ts.options }}</template>
 
-			<MkFolder>
-				<template #label>{{ i18n.ts.specifyUser }}</template>
-				<template v-if="user" #suffix>@{{ user.username }}</template>
+			<div class="_gaps_m">
+				<MkSwitch v-model="isLocalOnly">{{ i18n.ts.localOnly }}</MkSwitch>
 
-				<div style="text-align: center;" class="_gaps">
-					<div v-if="user">@{{ user.username }}</div>
-					<div>
-						<MkButton v-if="user == null" primary rounded inline @click="selectUser">{{ i18n.ts.selectUser }}</MkButton>
-						<MkButton v-else danger rounded inline @click="user = null">{{ i18n.ts.remove }}</MkButton>
+				<MkFolder>
+					<template #label>{{ i18n.ts.specifyUser }}</template>
+					<template v-if="user" #suffix>@{{ user.username }}</template>
+
+					<div style="text-align: center;" class="_gaps">
+						<div v-if="user">@{{ user.username }}</div>
+						<div>
+							<MkButton v-if="user == null" primary rounded inline @click="selectUser">{{ i18n.ts.selectUser }}</MkButton>
+							<MkButton v-else danger rounded inline @click="user = null">{{ i18n.ts.remove }}</MkButton>
+						</div>
 					</div>
-				</div>
-			</MkFolder>
+				</MkFolder>
+			</div>
 		</MkFolder>
 		<div>
 			<MkButton large primary gradate rounded style="margin: 0 auto;" @click="search">{{ i18n.ts.search }}</MkButton>
@@ -43,6 +47,7 @@ import MkNotes from '@/components/MkNotes.vue';
 import MkInput from '@/components/MkInput.vue';
 import MkRadios from '@/components/MkRadios.vue';
 import MkButton from '@/components/MkButton.vue';
+import MkSwitch from '@/components/MkSwitch.vue';
 import { i18n } from '@/i18n';
 import * as os from '@/os';
 import MkFoldableSection from '@/components/MkFoldableSection.vue';
@@ -59,6 +64,7 @@ let searchQuery = $ref('');
 let searchOrigin = $ref('combined');
 let notePagination = $ref();
 let user = $ref(null);
+let isLocalOnly = $ref(false);
 
 function selectUser() {
 	os.selectUser().then(_user => {
@@ -98,6 +104,8 @@ async function search() {
 		},
 	};
 
+	if (isLocalOnly) notePagination.params.host = '.';
+
 	key++;
 }
 </script>