Explorar o código

Merge pull request #1717 from visuddhinanda/laravel

添加system channel 标记
visuddhinanda %!s(int64=2) %!d(string=hai) anos
pai
achega
b4b6a852bb

+ 21 - 5
app/Console/Commands/CopyUserBook.php

@@ -58,6 +58,8 @@ class CopyUserBook extends Command
         }
         }
 
 
         $this->info('给CustomBook 添加channel');
         $this->info('给CustomBook 添加channel');
+        $newChannel = 0;
+
         foreach (CustomBook::get() as $key => $customBook) {
         foreach (CustomBook::get() as $key => $customBook) {
             $this->info('doing book='.$customBook->book_id);
             $this->info('doing book='.$customBook->book_id);
             if(empty($customBook->channel_id)){
             if(empty($customBook->channel_id)){
@@ -82,28 +84,34 @@ class CopyUserBook extends Command
                     $channel->type = 'original';
                     $channel->type = 'original';
                     $channel->lang = $bookLang;
                     $channel->lang = $bookLang;
                     $channel->editor_id = 0;
                     $channel->editor_id = 0;
+                    $channel->is_system = true;
                     $channel->create_time = time()*1000;
                     $channel->create_time = time()*1000;
                     $channel->modify_time = time()*1000;
                     $channel->modify_time = time()*1000;
                     $channel->status = $customBook->status;
                     $channel->status = $customBook->status;
                     if(!$this->option('test')){
                     if(!$this->option('test')){
                         $saveOk = $channel->save();
                         $saveOk = $channel->save();
                         if($saveOk){
                         if($saveOk){
+                            $newChannel++;
                             Log::debug('copy user book : create channel success name='.$channelName);
                             Log::debug('copy user book : create channel success name='.$channelName);
                         }else{
                         }else{
-                            Log::error('copy user book : create channel fail.',['channel'=>$channelName,'book'=>$book->book]);
+                            Log::error('copy user book : create channel fail.',['channel'=>$channelName,'book'=>$customBook->book_id]);
                             $this->error('copy user book : create channel fail.  name='.$channelName);
                             $this->error('copy user book : create channel fail.  name='.$channelName);
                             continue;
                             continue;
                         }
                         }
                     }
                     }
                 }
                 }
                 if(!Str::isUuid($channel->uid)){
                 if(!Str::isUuid($channel->uid)){
-                    Log::error('copy user book : channel id error.',['channel'=>$channelName,'book'=>$book->book]);
+                    Log::error('copy user book : channel id error.',['channel'=>$channelName,'book'=>$customBook->book_id]);
                     $this->error('copy user book : channel id error.  name='.$channelName);
                     $this->error('copy user book : channel id error.  name='.$channelName);
                     continue;
                     continue;
                 }
                 }
                 $customBook->channel_id = $channel->uid;
                 $customBook->channel_id = $channel->uid;
                 if(!$this->option('test')){
                 if(!$this->option('test')){
-                    $customBook->save();
+                    $ok = $customBook->save();
+                    if(!$ok){
+                        Log::error('copy user book : create channel fail.',['book'=>$customBook->book_id]);
+                        continue;
+                    }
                 }
                 }
             }
             }
         }
         }
@@ -111,7 +119,9 @@ class CopyUserBook extends Command
 
 
         $userBooks = CustomBook::get();
         $userBooks = CustomBook::get();
         $this->info('book '. count($userBooks));
         $this->info('book '. count($userBooks));
+        $copySent = 0;
         foreach ($userBooks as $key => $book) {
         foreach ($userBooks as $key => $book) {
+
             $queryBook = $this->option('book');
             $queryBook = $this->option('book');
             if(!empty($queryBook)){
             if(!empty($queryBook)){
                 if($book->book_id != $queryBook){
                 if($book->book_id != $queryBook){
@@ -152,13 +162,19 @@ class CopyUserBook extends Command
                     Log::error('channel uuid is null ',['sentence'=>$sentence->book]);
                     Log::error('channel uuid is null ',['sentence'=>$sentence->book]);
                 }else{
                 }else{
                     if(!$this->option('test')){
                     if(!$this->option('test')){
-                        $newRow->save();
+                        $ok = $newRow->save();
+                        if(!$ok){
+                            Log::error('copy fail ',['sentence'=>$sentence->id]);
+                        }
+                        $copySent++;
                     }
                     }
                 }
                 }
             }
             }
             $this->info("book {$book->book} finished");
             $this->info("book {$book->book} finished");
         }
         }
-        $this->info('all done');
+        $this->info('all done ');
+        $this->info('channel create '.$newChannel);
+        $this->info('sentence copy '.$copySent);
         return 0;
         return 0;
     }
     }
 }
 }

+ 6 - 5
app/Http/Api/ChannelApi.php

@@ -76,7 +76,7 @@ class ChannelApi{
      * 获取某个studio 的某个语言的自定义书的channel
      * 获取某个studio 的某个语言的自定义书的channel
      * 如果没有,建立
      * 如果没有,建立
      */
      */
-    public static function userBookGetOrCreate($studioId,$lang){
+    public static function userBookGetOrCreate($studioId,$lang,$status){
         $channelName = '_user_book_'.$lang;
         $channelName = '_user_book_'.$lang;
         $channel = Channel::where('owner_uid',$studioId)
         $channel = Channel::where('owner_uid',$studioId)
                         ->where('name',$channelName)->first();
                         ->where('name',$channelName)->first();
@@ -87,20 +87,21 @@ class ChannelApi{
         $channel = new Channel;
         $channel = new Channel;
         $channel->id = app('snowflake')->id();
         $channel->id = app('snowflake')->id();
         $channel->uid = $channelUuid;
         $channel->uid = $channelUuid;
-        $channel->owner_uid = $customBook->owner;
+        $channel->owner_uid = $studioId;
         $channel->name = $channelName;
         $channel->name = $channelName;
         $channel->type = 'original';
         $channel->type = 'original';
-        $channel->lang = $bookLang;
+        $channel->lang = $lang;
         $channel->editor_id = 0;
         $channel->editor_id = 0;
+        $channel->is_system = true;
         $channel->create_time = time()*1000;
         $channel->create_time = time()*1000;
         $channel->modify_time = time()*1000;
         $channel->modify_time = time()*1000;
-        $channel->status = $customBook->status;
+        $channel->status = $status;
         $saveOk = $channel->save();
         $saveOk = $channel->save();
         if($saveOk){
         if($saveOk){
             Log::debug('copy user book : create channel success name='.$channelName);
             Log::debug('copy user book : create channel success name='.$channelName);
             return $channel->uid;
             return $channel->uid;
         }else{
         }else{
-            Log::error('copy user book : create channel fail.',['channel'=>$channelName,'book'=>$book->book]);
+            Log::error('copy user book : create channel fail.',['channel'=>$channelName,'studioId'=>$studioId]);
             return false;
             return false;
         }
         }
     }
     }

+ 1 - 1
app/Http/Controllers/ArticleController.php

@@ -511,7 +511,7 @@ class ArticleController extends Controller
             $newBook->lang = $anthology->lang;
             $newBook->lang = $anthology->lang;
             $newBook->status = $anthology->status;
             $newBook->status = $anthology->status;
             //查询anthology所在的studio有没有符合要求的channel 没有的话,建立
             //查询anthology所在的studio有没有符合要求的channel 没有的话,建立
-            $channelId = ChannelApi::userBookGetOrCreate($anthology->owner,$anthology->lang);
+            $channelId = ChannelApi::userBookGetOrCreate($anthology->owner,$anthology->lang,$anthology->status);
             if($channelId === false){
             if($channelId === false){
                 throw new \Exception('user book get fail studio='.$anthology->owner.' language='.$anthology->lang);
                 throw new \Exception('user book get fail studio='.$anthology->owner.' language='.$anthology->lang);
             }
             }

+ 10 - 4
app/Http/Controllers/ChannelController.php

@@ -36,7 +36,7 @@ class ChannelController extends Controller
 		$result=false;
 		$result=false;
 		$indexCol = ['uid','name','summary',
 		$indexCol = ['uid','name','summary',
                     'type','owner_uid','lang',
                     'type','owner_uid','lang',
-                    'status','updated_at','created_at'];
+                    'status','is_system','updated_at','created_at'];
 		switch ($request->get('view')) {
 		switch ($request->get('view')) {
             case 'public':
             case 'public':
                 $table = Channel::select($indexCol)
                 $table = Channel::select($indexCol)
@@ -510,7 +510,7 @@ class ChannelController extends Controller
     public function show($id)
     public function show($id)
     {
     {
         //
         //
-        $indexCol = ['uid','name','summary','type','owner_uid','lang','status','updated_at','created_at'];
+        $indexCol = ['uid','name','summary','type','owner_uid','lang','is_system','status','updated_at','created_at'];
 		$channel = Channel::where("uid",$id)->select($indexCol)->first();
 		$channel = Channel::where("uid",$id)->select($indexCol)->first();
         $studio = StudioApi::getById($channel->owner_uid);
         $studio = StudioApi::getById($channel->owner_uid);
         $channel->studio = $studio;
         $channel->studio = $studio;
@@ -530,13 +530,16 @@ class ChannelController extends Controller
         //鉴权
         //鉴权
         $user = AuthApi::current($request);
         $user = AuthApi::current($request);
         if(!$user){
         if(!$user){
-            return $this->error(__('auth.failed'),[],401);
+            return $this->error(__('auth.failed'),401,401);
+        }
+        if($channel->is_system){
+            return $this->error('system channel',403,403);
         }
         }
         if($channel->owner_uid !== $user["user_uid"]){
         if($channel->owner_uid !== $user["user_uid"]){
             //判断是否为协作
             //判断是否为协作
             $power = ShareApi::getResPower($user["user_uid"],$request->get('id'));
             $power = ShareApi::getResPower($user["user_uid"],$request->get('id'));
             if($power < 30){
             if($power < 30){
-                return $this->error(__('auth.failed'),[],403);
+                return $this->error(__('auth.failed'),403,403);
             }
             }
         }
         }
         $channel->name = $request->get('name');
         $channel->name = $request->get('name');
@@ -561,6 +564,9 @@ class ChannelController extends Controller
         if(!$user){
         if(!$user){
             return $this->error(__('auth.failed'),[],401);
             return $this->error(__('auth.failed'),[],401);
         }
         }
+        if($channel->is_system){
+            return $this->error('system channel',403,403);
+        }
         if($channel->owner_uid !== $user["user_uid"]){
         if($channel->owner_uid !== $user["user_uid"]){
             //判断是否为协作
             //判断是否为协作
             $power = ShareApi::getResPower($user["user_uid"],$request->get('id'));
             $power = ShareApi::getResPower($user["user_uid"],$request->get('id'));

+ 7 - 0
app/Http/Resources/ArticleMapResource.php

@@ -40,6 +40,13 @@ class ArticleMapResource extends JsonResource
         }
         }
         $mdRender = new MdRender(['format'=>'simple']);
         $mdRender = new MdRender(['format'=>'simple']);
         $data['title_text'] = $mdRender->convert($this->title,$channels);
         $data['title_text'] = $mdRender->convert($this->title,$channels);
+        if ($request->get('view') === 'article') {
+            $collection = Collection::where('uid',$this->collect_id)->first();
+            if($collection){
+                $data['collection']['id']=$collection->uid;
+                $data['collection']['title']=$collection->title;
+            }
+        }
         return $data;
         return $data;
     }
     }
 }
 }

+ 19 - 12
app/Http/Resources/ArticleResource.php

@@ -44,12 +44,6 @@ class ArticleResource extends JsonResource
             "created_at" => $this->created_at,
             "created_at" => $this->created_at,
             "updated_at" => $this->updated_at,
             "updated_at" => $this->updated_at,
         ];
         ];
-        $channels = [];
-        if($request->has('channel')){
-            $channels = explode('_',$request->get('channel')) ;
-        }
-        $mdRender = new MdRender(['format'=>'simple']);
-        $data['title_text'] = $mdRender->convert($this->title,$channels);
 
 
         $user = AuthApi::current($request);
         $user = AuthApi::current($request);
         if($user){
         if($user){
@@ -66,13 +60,23 @@ class ArticleResource extends JsonResource
             $collection = ArticleCollection::where('article_id',$this->uid)->first();
             $collection = ArticleCollection::where('article_id',$this->uid)->first();
             $data['anthology_first'] = Collection::find($collection->collect_id);
             $data['anthology_first'] = Collection::find($collection->collect_id);
         }
         }
+        if($request->has('anthology') && Str::isUuid($request->get('anthology'))){
+            $anthology = Collection::where('uid',$request->get('anthology'))->first();
+        }
+        //渲染简化版标题
+        $channels = [];
+        if($request->has('channel')){
+            $channels = explode('_',$request->get('channel')) ;
+        }else if(isset($anthology) && $anthology && !empty($anthology->default_channel)){
+            //使用文集channel
+            $channels[] = $anthology->default_channel;
+        }
+        $mdRender = new MdRender(['format'=>'simple']);
+
         //path
         //path
         if($request->has('anthology') && Str::isUuid($request->get('anthology'))){
         if($request->has('anthology') && Str::isUuid($request->get('anthology'))){
             $data['path'] = array();
             $data['path'] = array();
-
-            //anthology title
-            $anthology = Collection::where('uid',$request->get('anthology'))->first();
-            if($anthology){
+            if(isset($anthology) && $anthology){
                 $data['path'][] = ['key'=>$anthology->uid,
                 $data['path'][] = ['key'=>$anthology->uid,
                             'title'=>$anthology->title,
                             'title'=>$anthology->title,
                             'level'=>0];
                             'level'=>0];
@@ -88,7 +92,7 @@ class ArticleResource extends JsonResource
                 ($currLevel >= 0 && $article->level < $currLevel)){
                 ($currLevel >= 0 && $article->level < $currLevel)){
                     $currLevel = $article->level;
                     $currLevel = $article->level;
                     $path[] = ['key'=>$article->article_id,
                     $path[] = ['key'=>$article->article_id,
-                                'title'=>$article->title,
+                                'title'=>$mdRender->convert($article->title,$channels),
                                 'level'=>$article->level];
                                 'level'=>$article->level];
                 }
                 }
             }
             }
@@ -105,7 +109,7 @@ class ArticleResource extends JsonResource
                     if($article->level>$level){
                     if($article->level>$level){
                         $subToc[] =[
                         $subToc[] =[
                             "key"=>$article->article_id,
                             "key"=>$article->article_id,
-                            "title"=>$article->title,
+                            "title"=>$mdRender->convert($article->title,$channels),
                             "level"=>$article->level
                             "level"=>$article->level
                         ];
                         ];
                     }else{
                     }else{
@@ -119,6 +123,9 @@ class ArticleResource extends JsonResource
             $data['toc'] = $subToc;
             $data['toc'] = $subToc;
         }
         }
 
 
+
+        $data['title_text'] = $mdRender->convert($this->title,$channels);
+
         //render html
         //render html
         $channels = array();
         $channels = array();
         if(isset($this->content) && !empty($this->content)){
         if(isset($this->content) && !empty($this->content)){

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

@@ -22,6 +22,7 @@ class ChannelResource extends JsonResource
             "type" => $this->type,
             "type" => $this->type,
             "studio" => StudioApi::getById($this->owner_uid),
             "studio" => StudioApi::getById($this->owner_uid),
             "lang" => $this->lang,
             "lang" => $this->lang,
+            "is_system" => $this->is_system,
             "status" => $this->status,
             "status" => $this->status,
             "created_at" => $this->created_at,
             "created_at" => $this->created_at,
             "updated_at" => $this->updated_at,
             "updated_at" => $this->updated_at,

+ 32 - 0
database/migrations/2023_11_16_103304_add_is_system_in_channels.php

@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddIsSystemInChannels extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('channels', function (Blueprint $table) {
+            $table->boolean('is_system')->index()->default(false);
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('channels', function (Blueprint $table) {
+            $table->dropColumn('is_system');
+        });
+    }
+}