Просмотр исходного кода

fix: 模型写入的术语 editor_id 记 -1

editor_id 存的是人类用户的自增 sn,模型没有。此前落的是模型 token 里的 id,
恒为 0——而 0 同时也是「缺省/未知」的值,落库后分不清是模型写的还是数据有
问题。改记 -1,模型的真实身份仍看 editor_uid。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
visuddhinanda 1 неделя назад
Родитель
Сommit
5c080c59f4

+ 14 - 2
api-v13/app/Http/Controllers/DhammaTermController.php

@@ -295,7 +295,7 @@ class DhammaTermController extends Controller
                 }
                 $term->owner = $studioId;
             }
-            $term->editor_id = $user['user_id'];
+            $term->editor_id = $this->editorId($user);
             $term->editor_uid = $user['user_uid'];
             $term->create_time = time() * 1000;
             $term->modify_time = time() * 1000;
@@ -309,6 +309,18 @@ class DhammaTermController extends Controller
         }
     }
 
+    /**
+     * editor_id 存的是人类用户的自增 sn,模型没有。模型 token 里的 id 恒为 0,
+     * 而 0 同时也是「缺省/未知」的值,落库后分不清是模型写的还是数据有问题,
+     * 故模型一律记 -1;模型的真实身份看 editor_uid。
+     *
+     * @param  array<string, mixed>  $user
+     */
+    private function editorId(array $user): int
+    {
+        return $this->isAiModel($user['user_uid']) ? -1 : (int) $user['user_id'];
+    }
+
     /**
      * 当前身份是不是 AI 模型。模型 token 的 user_id 恒为 0,但人类的旧 cookie
      * 鉴权也可能给出奇怪的值,故直接查表判定,不靠 id。
@@ -398,7 +410,7 @@ class DhammaTermController extends Controller
                 $dhammaTerm->$field = $request->input($field);
             }
         }
-        $dhammaTerm->editor_id = $user['user_id'];
+        $dhammaTerm->editor_id = $this->editorId($user);
         $dhammaTerm->editor_uid = $user['user_uid'];
         // create_time 是创建时刻,改动时不该被刷新
         $dhammaTerm->modify_time = time() * 1000;

+ 5 - 0
api-v13/tests/Feature/TermWriteAsAiModelTest.php

@@ -59,6 +59,8 @@ it('creates a channel term attributed to the ai model', function () {
     // 核心断言:署名归模型,而不是发起操作的人类
     expect($saved->editor_uid)->toBe($model->uid);
     expect($saved->editor_uid)->not->toBe($human);
+    // editor_id 存人类的自增 sn,模型没有;0 与「缺省/未知」撞车,故记 -1
+    expect($saved->editor_id)->toBe(-1);
     // owner 仍是 channel 所属 studio
     expect($saved->owner)->toBe($human);
 });
@@ -133,6 +135,7 @@ it('updates a term as the ai model, leaving unsubmitted fields untouched', funct
     expect($saved->note)->toBe('原有的注解');
     expect($saved->tag)->toBe('abhidhamma');
     expect($saved->editor_uid)->toBe($model->uid);
+    expect($saved->editor_id)->toBe(-1);
     // create_time 是创建时刻,改动不该刷新它
     expect($saved->create_time)->toBe($createTime);
 });
@@ -182,6 +185,8 @@ it('refuses a studio term written into someone elses studio', function () {
     ], authHeader($human))->assertOk();
 
     expect(DhammaTerm::where('owner', $human)->count())->toBe(1);
+    // 人类写入不受影响:editor_id 仍是 token 里的用户 sn
+    expect(DhammaTerm::where('owner', $human)->first()->editor_id)->toBe(1);
 });
 
 it('refuses to create a studio-level term as an ai model', function () {