From c388fe373db0d47dde1fab245ee841d3fdcca90a Mon Sep 17 00:00:00 2001 From: kuaifan Date: Sat, 24 May 2025 07:24:07 +0800 Subject: [PATCH] no message --- .../Commands/SyncUserMsgToZincSearch.php | 6 +++ app/Module/Apps.php | 1 + app/Module/ZincSearch/ZincSearchBase.php | 11 +++- app/Module/ZincSearch/ZincSearchDialogMsg.php | 51 ++++++++++++++++++- app/Tasks/ZincSearchSyncTask.php | 6 +++ docker-compose.yml | 17 ------- 6 files changed, 72 insertions(+), 20 deletions(-) diff --git a/app/Console/Commands/SyncUserMsgToZincSearch.php b/app/Console/Commands/SyncUserMsgToZincSearch.php index 222b198a8..411a0546a 100644 --- a/app/Console/Commands/SyncUserMsgToZincSearch.php +++ b/app/Console/Commands/SyncUserMsgToZincSearch.php @@ -3,6 +3,7 @@ namespace App\Console\Commands; use App\Models\WebSocketDialogMsg; +use App\Module\Apps; use App\Module\ZincSearch\ZincSearchKeyValue; use App\Module\ZincSearch\ZincSearchDialogMsg; use Cache; @@ -27,6 +28,11 @@ class SyncUserMsgToZincSearch extends Command */ public function handle(): int { + if (!Apps::isInstalled("search")) { + $this->error("应用「ZincSearch」未安装"); + return 1; + } + // 注册信号处理器(仅在支持pcntl扩展的环境下) if (extension_loaded('pcntl')) { pcntl_async_signals(true); // 启用异步信号处理 diff --git a/app/Module/Apps.php b/app/Module/Apps.php index ee44e554d..299c10930 100644 --- a/app/Module/Apps.php +++ b/app/Module/Apps.php @@ -51,6 +51,7 @@ class Apps 'office' => 'OnlyOffice', 'drawio' => 'Drawio', 'minder' => 'Minder', + 'search' => 'ZincSearch', default => $appId, }; throw new ApiException("应用「{$name}」未安装"); diff --git a/app/Module/ZincSearch/ZincSearchBase.php b/app/Module/ZincSearch/ZincSearchBase.php index 417222d96..60e4d5976 100644 --- a/app/Module/ZincSearch/ZincSearchBase.php +++ b/app/Module/ZincSearch/ZincSearchBase.php @@ -2,6 +2,9 @@ namespace App\Module\ZincSearch; +use App\Module\Apps; +use App\Module\Doo; + /** * ZincSearch 公共类 */ @@ -28,6 +31,13 @@ class ZincSearchBase */ private function request($path, $body = null, $method = 'POST') { + if (!Apps::isInstalled("search")) { + return [ + 'success' => false, + 'error' => Doo::translate("应用「ZincSearch」未安装") + ]; + } + $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://{$this->host}:{$this->port}{$path}"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); @@ -38,7 +48,6 @@ class ZincSearchBase $headers = ['Content-Type: application/json']; if ($method === 'BULK') { $headers = ['Content-Type: text/plain']; - $method = 'POST'; } curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); diff --git a/app/Module/ZincSearch/ZincSearchDialogMsg.php b/app/Module/ZincSearch/ZincSearchDialogMsg.php index c3ead3d19..3e7363c8b 100644 --- a/app/Module/ZincSearch/ZincSearchDialogMsg.php +++ b/app/Module/ZincSearch/ZincSearchDialogMsg.php @@ -4,9 +4,10 @@ namespace App\Module\ZincSearch; use App\Models\WebSocketDialogMsg; use App\Models\WebSocketDialogUser; +use App\Module\Apps; use Carbon\Carbon; +use DB; use Illuminate\Support\Facades\Log; -use Swoole\Coroutine; /** * ZincSearch 会话消息类 @@ -129,6 +130,11 @@ class ZincSearchDialogMsg */ public static function search(string $userid, string $keyword, int $from = 0, int $size = 20): array { + if (!Apps::isInstalled("search")) { + // 如果搜索功能未安装,使用数据库查询 + return self::searchByMysql($userid, $keyword, $from, $size); + } + $searchParams = [ 'query' => [ 'bool' => [ @@ -144,7 +150,6 @@ class ZincSearchDialogMsg ['updated_at' => 'desc'] ] ]; - try { $result = ZincSearchBase::elasticSearch(self::$indexNameMsg, $searchParams); $hits = $result['data']['hits']['hits'] ?? []; @@ -184,6 +189,48 @@ class ZincSearchDialogMsg } } + /** + * 根据用户ID和消息关键词搜索会话(MySQL 版本,主要用于未安装ZincSearch的情况) + * + * @param string $userid 用户ID + * @param string $keyword 消息关键词 + * @param int $from 起始位置 + * @param int $size 返回结果数量 + * @return array + */ + private static function searchByMysql(string $userid, string $keyword, int $from = 0, int $size = 20): array + { + $items = DB::table('web_socket_dialog_users as u') + ->select(['d.*', 'u.top_at', 'u.last_at', 'u.mark_unread', 'u.silence', 'u.hide', 'u.color', 'u.updated_at as user_at', 'm.id as search_msg_id']) + ->join('web_socket_dialogs as d', 'u.dialog_id', '=', 'd.id') + ->join('web_socket_dialog_msgs as m', 'm.dialog_id', '=', 'd.id') + ->where('u.userid', $userid) + ->where('m.bot', 0) + ->whereNull('d.deleted_at') + ->where('m.key', 'like', "%{$keyword}%") + ->orderByDesc('m.id') + ->offset($from) + ->limit($size) + ->get() + ->all(); + $msgs = []; + foreach ($items as $item) { + $msgs[] = [ + 'id' => $item->id, + 'search_msg_id' => $item->search_msg_id, + 'user_at' => Carbon::parse($item->user_at)->format('Y-m-d H:i:s'), + + 'mark_unread' => $item->mark_unread, + 'silence' => $item->silence, + 'hide' => $item->hide, + 'color' => $item->color, + 'top_at' => Carbon::parse($item->top_at)->format('Y-m-d H:i:s'), + 'last_at' => Carbon::parse($item->last_at)->format('Y-m-d H:i:s'), + ]; + } + return $msgs; + } + /** * 根据对话用户ID搜索用户信息 * @param array $dialogUserids diff --git a/app/Tasks/ZincSearchSyncTask.php b/app/Tasks/ZincSearchSyncTask.php index da2a2aa41..904f2f829 100644 --- a/app/Tasks/ZincSearchSyncTask.php +++ b/app/Tasks/ZincSearchSyncTask.php @@ -4,6 +4,7 @@ namespace App\Tasks; use App\Models\WebSocketDialogMsg; use App\Models\WebSocketDialogUser; +use App\Module\Apps; use App\Module\ZincSearch\ZincSearchDialogMsg; use Carbon\Carbon; use Illuminate\Support\Facades\Cache; @@ -26,6 +27,11 @@ class ZincSearchSyncTask extends AbstractTask public function start() { + if (!Apps::isInstalled("search")) { + // 如果没有安装搜索模块,则不执行 + return; + } + switch ($this->action) { case 'sync': // 同步消息数据 diff --git a/docker-compose.yml b/docker-compose.yml index 3a3284950..aed8e8535 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -80,23 +80,6 @@ services: - extnetwork restart: unless-stopped - search: - container_name: "dootask-search-${APP_ID}" - image: "public.ecr.aws/zinclabs/zincsearch:0.4.10" - volumes: - - search_data:/data - environment: - ZINC_DATA_PATH: "/data" - ZINC_FIRST_ADMIN_USER: "${DB_USERNAME}" - ZINC_FIRST_ADMIN_PASSWORD: "${DB_PASSWORD}" - deploy: - resources: - limits: - cpus: '2' - networks: - - extnetwork - restart: unless-stopped - appstore: container_name: "dootask-appstore-${APP_ID}" privileged: true