diff --git a/app/Http/Controllers/Api/DialogController.php b/app/Http/Controllers/Api/DialogController.php index ae9ec458c..e5af03f0b 100755 --- a/app/Http/Controllers/Api/DialogController.php +++ b/app/Http/Controllers/Api/DialogController.php @@ -541,6 +541,8 @@ class DialogController extends AbstractController // if ($session_id > 0) { $builder->whereSessionId($session_id); + } elseif ($dialog->session_id > 0) { + $builder->whereSessionId($dialog->session_id); } if ($msg_type) { if ($msg_type === 'tag') { diff --git a/app/Models/WebSocketDialog.php b/app/Models/WebSocketDialog.php index 2e31d70b0..fc659dc36 100644 --- a/app/Models/WebSocketDialog.php +++ b/app/Models/WebSocketDialog.php @@ -19,6 +19,7 @@ use Illuminate\Support\Facades\DB; * @property int $id * @property string|null $type 对话类型 * @property string|null $group_type 聊天室类型 + * @property int|null $session_id 会话ID(最新) * @property string|null $name 对话名称 * @property string $avatar 头像(群) * @property int|null $owner_id 群主用户ID @@ -48,6 +49,7 @@ use Illuminate\Support\Facades\DB; * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialog whereLinkId($value) * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialog whereName($value) * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialog whereOwnerId($value) + * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialog whereSessionId($value) * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialog whereTopMsgId($value) * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialog whereTopUserid($value) * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialog whereType($value) diff --git a/app/Models/WebSocketDialogSession.php b/app/Models/WebSocketDialogSession.php index f94a13612..bd7bba9f7 100644 --- a/app/Models/WebSocketDialogSession.php +++ b/app/Models/WebSocketDialogSession.php @@ -7,7 +7,6 @@ namespace App\Models; * * @property int $id * @property int $dialog_id 对话ID - * @property int $userid 用户ID * @property string $title 会话标题 * @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $updated_at @@ -27,7 +26,6 @@ namespace App\Models; * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogSession whereId($value) * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogSession whereTitle($value) * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogSession whereUpdatedAt($value) - * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogSession whereUserid($value) * @mixin \Eloquent */ class WebSocketDialogSession extends AbstractModel @@ -50,12 +48,4 @@ class WebSocketDialogSession extends AbstractModel { return $this->belongsTo(WebSocketDialog::class, 'dialog_id'); } - - /** - * 获取关联的用户 - */ - public function user() - { - return $this->belongsTo(User::class, 'userid'); - } } diff --git a/database/migrations/2025_02_07_163329_web_socket_dialogs_add_session_id.php b/database/migrations/2025_02_07_163329_web_socket_dialogs_add_session_id.php new file mode 100644 index 000000000..e5a4d629e --- /dev/null +++ b/database/migrations/2025_02_07_163329_web_socket_dialogs_add_session_id.php @@ -0,0 +1,34 @@ +bigInteger('session_id')->index()->nullable()->default(0)->after('group_type')->comment('会话ID(最新)'); + } + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('web_socket_dialogs', function (Blueprint $table) { + $table->dropColumn('session_id'); + }); + } +} diff --git a/database/migrations/2025_02_07_163701_create_web_socket_dialog_sessions_table.php b/database/migrations/2025_02_07_163701_create_web_socket_dialog_sessions_table.php index 39c7845f8..57def27cc 100644 --- a/database/migrations/2025_02_07_163701_create_web_socket_dialog_sessions_table.php +++ b/database/migrations/2025_02_07_163701_create_web_socket_dialog_sessions_table.php @@ -1,5 +1,9 @@ id(); - $table->bigInteger('dialog_id')->unsigned()->index()->comment('对话ID'); - $table->bigInteger('userid')->unsigned()->index()->comment('用户ID'); - $table->string('title', 255)->default('')->comment('会话标题'); - $table->timestamps(); - }); + if (Schema::hasTable('web_socket_dialog_sessions')) { + return; + } + Schema::create('web_socket_dialog_sessions', function (Blueprint $table) { + $table->id(); + $table->bigInteger('dialog_id')->unsigned()->index()->comment('对话ID'); + $table->string('title', 255)->default('')->comment('会话标题'); + $table->timestamps(); + }); + $list = WebSocketDialog::select(['web_socket_dialogs.*', 'u.email']) + ->join('web_socket_dialog_users as du', 'web_socket_dialogs.id', '=', 'du.dialog_id') + ->join('users as u', 'du.userid', '=', 'u.userid') + ->where('u.email', 'like', 'ai-%@bot.system') + ->where('web_socket_dialogs.type', 'user') + ->get(); + foreach ($list as $item) { + $title = WebSocketDialogMsg::whereDialogId($item->id)->where('key', '!=', '')->orderBy('id')->value('key'); + $session = WebSocketDialogSession::create([ + 'dialog_id' => $item->id, + 'title' => $title ? Base::cutStr($title, 100) : 'Unknown', + ]); + $session->save(); + $item->session_id = $session->id; + $item->save(); + WebSocketDialogMsg::whereDialogId($item->id)->update(['session_id' => $session->id]); } }