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

Merge pull request #1920 from visuddhinanda/laravel

添加 fork_from 支持
visuddhinanda 2 лет назад
Родитель
Сommit
7fe689f55d

+ 2 - 0
app/Http/Controllers/CorpusController.php

@@ -61,6 +61,7 @@ class CorpusController extends Controller
         'editor_uid',
         'acceptor_uid',
         'pr_edit_at',
+        'fork_at',
         'create_time',
         'modify_time',
         'created_at',
@@ -651,6 +652,7 @@ class CorpusController extends Controller
                      * TODO 刷库改数据
                      * 旧版api没有更新updated_at所以造成旧版的数据updated_at数据比modify_time 要晚
                      */
+                    $newSent['forkAt'] =  $row->fork_at; //
                     $newSent['updateAt'] =  $row->updated_at; //
                     $newSent['updateAt'] = date("Y-m-d H:i:s.",$row->modify_time/1000).($row->modify_time%1000)." UTC";
 

+ 4 - 1
app/Http/Controllers/SentHistoryController.php

@@ -25,11 +25,14 @@ class SentHistoryController extends Controller
                 return $this->error('known view');
                 break;
         }
+        if($request->has('fork')){
+            $table = $table->whereNotNull('fork_from');
+        }
         $count = $table->count();
         $table->orderBy($request->get('order','created_at'),
                         $request->get('dir','desc'));
         $table->skip($request->get("offset",0))
-              ->take($request->get('limit',1000));
+              ->take($request->get('limit',100));
 
         $result = $table->get();
 		return $this->ok(["rows"=>SentHistoryResource::collection($result),"count"=>$count]);

+ 17 - 6
app/Http/Controllers/SentenceController.php

@@ -31,7 +31,7 @@ class SentenceController extends Controller
         $result=false;
 		$indexCol = ['id','uid','book_id','paragraph',
                     'word_start','word_end','content','content_type',
-                    'channel_uid','editor_uid','acceptor_uid','pr_edit_at','updated_at'];
+                    'channel_uid','editor_uid','fork_at','acceptor_uid','pr_edit_at','updated_at'];
 
 		switch ($request->get('view')) {
             case 'public':
@@ -278,6 +278,9 @@ class SentenceController extends Controller
                 $row->editor_uid = $sent["editor_uid"];
                 $row->acceptor_uid = $user["user_uid"];
                 $row->pr_edit_at = $sent["updated_at"];
+                if($request->has('fork_from')){
+                    $row->fork_at = now();
+                }
             }else{
                 $row->editor_uid = $user["user_uid"];
                 $row->acceptor_uid = null;
@@ -289,9 +292,14 @@ class SentenceController extends Controller
 
             //保存历史记录
             if($request->has('copy')){
-                $this->saveHistory($row->uid,$sent["editor_uid"],$sent['content']);
+                $fork_from = $request->get('fork_from',null);
+                $this->saveHistory($row->uid,
+                                $sent["editor_uid"],
+                                $sent['content'],
+                                $user["user_uid"],
+                                $fork_from);
             }else{
-                $this->saveHistory($row->uid,$user["user_uid"],$sent['content']);
+                $this->saveHistory($row->uid,$user["user_uid"],$sent['content'],$user["user_uid"]);
             }
             //清除缓存
             $sentId = "{$sent['book_id']}-{$sent['paragraph']}-{$sent['word_start']}-{$sent['word_end']}";
@@ -307,7 +315,7 @@ class SentenceController extends Controller
         return $this->ok(count($request->get('sentences')));
     }
 
-    private function saveHistory($uid,$editor,$content){
+    private function saveHistory($uid,$editor,$content,$user_uid,$fork_from=null,$pr_from=null){
         $newHis = new SentHistory();
         $newHis->id = app('snowflake')->id();
         $newHis->sent_uid = $uid;
@@ -317,7 +325,10 @@ class SentenceController extends Controller
         }else{
             $newHis->content = $content;
         }
-
+        if($fork_from){
+            $newHis->fork_from = $fork_from;
+            $newHis->accepter_uid = $user_uid;
+        }
         $newHis->create_time = time()*1000;
         $newHis->save();
     }
@@ -406,7 +417,7 @@ class SentenceController extends Controller
         $currSentId = "{$param[0]}-{$param[1]}-{$param[2]}-{$param[3]}";
         RedisClusters::forget("/sent/{$channelId}/{$currSentId}");
         //保存历史记录
-        $this->saveHistory($sent->uid,$user["user_uid"],$request->get('content'));
+        $this->saveHistory($sent->uid,$user["user_uid"],$request->get('content'),$user["user_uid"]);
         Mq::publish('progress',['book'=>$param[0],
                             'para'=>$param[1],
                             'channel'=>$channelId,

+ 14 - 0
app/Http/Resources/SentHistoryResource.php

@@ -4,6 +4,8 @@ namespace App\Http\Resources;
 
 use Illuminate\Http\Resources\Json\JsonResource;
 use App\Http\Api\UserApi;
+use App\Http\Api\ChannelApi;
+use App\Http\Api\StudioApi;
 
 class SentHistoryResource extends JsonResource
 {
@@ -21,8 +23,20 @@ class SentHistoryResource extends JsonResource
             "editor" => UserApi::getByUuId($this->user_uid),
             'content' => $this->content,
             'landmark' => $this->landmark,
+            'pr_from' => $this->pr_from,
             'created_at' => $this->created_at
         ];
+        if($this->fork_from){
+            $fork_from = ChannelApi::getById($this->fork_from);
+            if($fork_from){
+                $data['fork_from'] = $fork_from;
+                $data['fork_studio'] = StudioApi::getById($fork_from['studio_id']);
+
+            }
+        }
+        if($this->accepter_uid){
+            $data['accepter'] = UserApi::getByUuId($this->accepter_uid);
+        }
         return $data;
     }
 }

+ 1 - 0
app/Http/Resources/SentResource.php

@@ -38,6 +38,7 @@ class SentResource extends JsonResource
                 "editor"=> UserApi::getByUuid($this->editor_uid),
                 "channel"=> $channel,
                 "studio" => StudioApi::getById($channel["studio_id"]),
+                'fork_at' => $this->fork_at,
                 "updated_at"=> $this->updated_at,
             ];
         if($request->has('channels')){

+ 38 - 0
database/migrations/2024_01_20_023258_add_fork_in_sent_histories.php

@@ -0,0 +1,38 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddForkInSentHistories extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('sent_histories', function (Blueprint $table) {
+            //
+            $table->uuid('fork_from')->nullable()->index();
+            $table->uuid('pr_from')->nullable()->index();
+            $table->uuid('accepter_uid')->nullable()->index();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('sent_histories', function (Blueprint $table) {
+            //
+            $table->dropColumn('fork_from');
+            $table->dropColumn('pr_from');
+            $table->dropColumn('accepter_uid');
+        });
+    }
+}

+ 36 - 0
database/migrations/2024_01_20_024558_add_fork_in_sentences.php

@@ -0,0 +1,36 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddForkInSentences extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('sentences', function (Blueprint $table) {
+            //
+            $table->dateTime('fork_at')->nullable()->index();
+            $table->integer('collaborator')->index()->default(0);
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('sentences', function (Blueprint $table) {
+            //
+            $table->dropColumn('fork_at');
+            $table->dropColumn('collaborator');
+        });
+    }
+}