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

Merge pull request #1866 from visuddhinanda/laravel

站内信支持发送多人
visuddhinanda 2 лет назад
Родитель
Сommit
2592146d77

+ 27 - 30
app/Console/Commands/MqPr.php

@@ -13,6 +13,7 @@ use App\Models\PaliSentence;
 use App\Tools\WebHook as WebHookSend;
 use App\Http\Api\MdRender;
 use App\Http\Api\PaliTextApi;
+use App\Http\Controllers\NotificationController;
 
 class MqPr extends Command
 {
@@ -23,6 +24,7 @@ class MqPr extends Command
      */
     protected $signature = 'mq:pr';
 
+    protected $ver = '2023-12-24';
     /**
      * The console command description.
      *
@@ -52,15 +54,15 @@ class MqPr extends Command
         }
         $exchange = 'router';
         $queue = 'suggestion';
-        $this->info(" [*] Waiting for {$queue}. To exit press CTRL+C");
-        Log::debug("mq:pr start.");
+        $this->info(" [*] Waiting for {$queue}. Ver. ".$this->ver);
+        Log::debug("mq:pr start. ver=".$this->ver);
         Mq::worker($exchange,$queue,function ($message){
             /**生成消息内容 */
 
             $msgTitle = '修改建议';
             $prData = $message->data;
             $sent_num = "{$prData->book}-{$prData->paragraph}-{$prData->word_start}-{$prData->word_end}";
-            $this->info('request'.$sent_num);
+            $this->info('ver='.$this->ver.' request'.$sent_num);
 
             $username = $prData->editor->nickName;
             $palitext = PaliSentence::where('book',$prData->book)
@@ -73,7 +75,7 @@ class MqPr extends Command
                                 ->where('word_start',$prData->word_start)
                                 ->where('word_end',$prData->word_end)
                                 ->where('channel_uid',$prData->channel->id)
-                                ->value('content');
+                                ->first();
             $prtext = mb_substr($prData->content,0,140,"UTF-8");
 
             $link = config('app.url')."/pcd/article/para/{$prData->book}-{$prData->paragraph}";
@@ -84,35 +86,30 @@ class MqPr extends Command
             $msgContent .= ">句子编号:<font color=\"info\">{$sent_num}</font>\n";
             $msgContent .= "欢迎大家[点击链接]({$link})查看并讨论。";
 
-            //标题
-            if($prData->book<1000){
-                $path = json_decode(PaliTextApi::getChapterPath($prData->book,$prData->paragraph)) ;
-                $title = $path[0]->title;
-            }else{
-                $title = '';
-            }
 
-            $notificationContent = "**{$title}**\n\n";
-            $notificationContent .= ">{$orgText}\n\n";
-            $notificationContent .= "{$prtext}";
             $result=0;
             //发送站内信
-            $url = config('app.url').'/api/v2/notification';
-            $response = Http::withToken($message->token)
-                            ->post($url,
-                            [
-                                'to'=> $prData->channel->studio_id,
-                                'title'=> $msgTitle,
-                                'content'=> $notificationContent,
-                                'url'=>$link,
-                                'res_type'=> 'suggestion',
-                                'res_id'=> $prData->uid,
-                                'channel'=>$prData->channel->id,
-                            ]);
-            if($response->failed()){
-                Log::error('send notification failed');
-            }else{
-                $this->info("send notification");
+            try{
+                $sendTo = array();
+                $sendTo[] = $prData->channel->studio_id;
+                if($orgText){
+                    //原文作者
+                    if(!in_array($orgText->editor_uid,$sendTo)){
+                        $sendTo[] = $orgText->editor_uid;
+                    }
+                    //原文采纳者
+                    if(!empty($orgText->acceptor_uid) && !in_array($orgText->acceptor_uid,$sendTo)){
+                        $sendTo[] = $orgText->acceptor_uid;
+                    }
+                }
+                $sendCount = NotificationController::insert($prData->editor->id,
+                                                $sendTo,
+                                                'suggestion',
+                                                $prData->uid,
+                                                $prData->channel->id);
+                $this->info("send notification success to [".$sendCount.'] users');
+            }catch(\Exception $e){
+                Log::error('send notification failed',['exception'=>$e]);
             }
 
             //发送webhook

+ 2 - 1
app/Console/Commands/UpgradeDaily.php

@@ -73,11 +73,12 @@ class UpgradeDaily extends Command
         Log::info('社区术语表完毕 {app.env}',['app.env'=>$env]);
 
         # 导出离线数据
+        /*
         $this->call('export:offline',['format'=>'lzma']);
         $time = time()-$currTime;
         $message .= "export:offline:{$time}; ";
         Log::info('导出离线数据完毕{app.env}',['app.env'=>$env]);
-
+        */
         $time = time()-$start;
         $message .= "总时间:{$time}; ";
 

+ 30 - 6
app/Http/Controllers/NotificationController.php

@@ -79,6 +79,22 @@ class NotificationController extends Controller
         return $this->ok(new NotificationResource($new));
     }
 
+    public static function insert($from,$to,$res_type,$res_id,$channel){
+        foreach ($to as $key => $one) {
+            $new = new Notification;
+            $new->id = Str::uuid();
+            $new->from = $from;
+            $new->to = $one;
+            $new->url = '';
+            $new->content = '';
+            $new->res_type = $res_type;
+            $new->res_id = $res_id;
+            $new->channel = $channel;
+            $new->save();
+        }
+        return count($to);
+    }
+
     /**
      * Display the specified resource.
      *
@@ -101,12 +117,20 @@ class NotificationController extends Controller
     public function update(Request $request, Notification $notification)
     {
         //
-        $notification->status = $request->get('status','read');
-        $notification->save();
-        $unread = Notification::where('to',$notification->to)
-                            ->where('status','unread')
-                            ->count();
-        return $this->ok(['unread'=>$unread]);
+        $user = AuthApi::current($request);
+        if(!$user){
+            return $this->error(__('auth.failed'),401,401);
+        }
+        if($notification->to===$user['user_uid']){
+            $notification->status = $request->get('status','read');
+            $notification->save();
+            $unread = Notification::where('to',$notification->to)
+                                ->where('status','unread')
+                                ->count();
+            return $this->ok(['unread'=>$unread]);
+        }else{
+            return $this->error(__('auth.failed'),403,403);
+        }
     }
 
     /**

+ 36 - 91
app/Http/Controllers/SentPrController.php

@@ -4,16 +4,18 @@
 namespace App\Http\Controllers;
 
 use Illuminate\Support\Str;
-use App\Http\Api\AuthApi;
+use Illuminate\Support\Facades\Http;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Http\Request;
+
 use App\Models\SentPr;
 use App\Models\Channel;
 use App\Models\PaliSentence;
 use App\Models\Sentence;
+use App\Models\Notification;
 use App\Http\Resources\SentPrResource;
-use Illuminate\Http\Request;
-use Illuminate\Support\Facades\Http;
-use Illuminate\Support\Facades\Log;
 use App\Http\Api\Mq;
+use App\Http\Api\AuthApi;
 
 class SentPrController extends Controller
 {
@@ -33,12 +35,26 @@ class SentPrController extends Controller
                                 ->where('word_end',$request->get('end'))
                                 ->where('channel_uid',$request->get('channel'));
                 $all_count = $table->count();
-                $chapters = $table->orderBy('created_at','desc')->get();
+                $result = $table->orderBy('created_at','desc')->get();
 
                 break;
         }
-        if($chapters){
-            return $this->ok(["rows"=>SentPrResource::collection($chapters),"count"=>$all_count]);
+        if($result){
+            //修改notification 已读状态
+            $user = AuthApi::current($request);
+            if($user){
+                $id=array();
+                foreach ($result as $key => $row) {
+                    $id[] = $row->uid;
+                }
+                Notification::whereIn('res_id',$id)
+                            ->where('to',$user['user_uid'])
+                            ->update(['status'=>'read']);
+            }
+            return $this->ok([
+                    "rows"=>SentPrResource::collection($result),
+                    "count"=>$all_count
+                ]);
         }else{
             return $this->error("no data");
         }
@@ -136,88 +152,8 @@ class SentPrController extends Controller
         Mq::publish('suggestion',['data'=>new SentPrResource($new),
                                   'token'=>AuthApi::getToken($request)]);
 
-		$robotMessageOk=false;
+		$robotMessageOk=true;
 		$webHookMessage="";
-		if(app()->isLocal()==false)
-		{
-			/*
-			初译:e5bc5c97-a6fb-4ccb-b7df-be6dcfee9c43
-			模版:#用户名 就“##该句子巴利前20字符##”提出了这样的修改建议:“##PR内容前20字##”,欢迎大家[点击链接](句子/段落链接)前往查看并讨论。
-
-			问题集:8622ad73-deef-4525-8e8e-ba3f1462724e
-			模版:#用户名 就 “##该句子巴利前20字符##”有这样的疑问:“##PR内容前20字##”,欢迎大家[点击链接](句子/段落链接)参与讨论。
-
-			初步答疑:5ab653d7-1ae3-40b0-ae07-c3d530a2a8f8
-			模版:#用户名 就“##该句子巴利前20字符##”中的问题做了这样的回复:“##PR内容前20字##”,欢迎大家[点击链接](句子/段落链接)前往查看并讨论。
-
-			机器人地址:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=25dbd74f-c89c-40e5-8cbc-48b1ef7710b8
-
-			项目范围:
-			book65 par:829-1306
-			book67 par:759-1152
-			*/
-
-			//if(($data['book']==65 && $data['para']>=829 && $data['para']<=1306) || ($data['book']== 67 && $data['para'] >= 759 && $data['para'] <= 1152)){
-				$username = 'nickname';
-				$palitext = PaliSentence::where('book',$data['book'])
-										->where('paragraph',$data['para'])
-										->where('word_begin',$data['begin'])
-										->where('word_end',$data['end'])
-										->value('text');
-				$sent_num = "{$data['book']}-{$data['para']}-{$data['begin']}-{$data['end']}";
-				$palitext = mb_substr($palitext,0,20,"UTF-8");
-				$prtext = mb_substr($data['text'],0,140,"UTF-8");
-				$link = "https://www-hk.wikipali.org/app/article/index.php?view=para&book={$data['book']}&par={$data['para']}&begin={$data['begin']}&end={$data['end']}&channel={$data['channel']}&mode=edit";
-				switch ($data['channel']) {
-					//测试
-					//case '3b0cb0aa-ea88-4ce5-b67d-00a3e76220cc':
-					//正式
-					case 'e5bc5c97-a6fb-4ccb-b7df-be6dcfee9c43':
-						$strMessage = "{$username} 就文句`{$palitext}`提出了修改建议:
-						>内容摘要:<font color=\"comment\">{$prtext}</font>,\n
-						>句子编号:<font color=\"info\">{$sent_num}</font>\n
-						欢迎大家[点击链接]({$link}&channel=e5bc5c97-a6fb-4ccb-b7df-be6dcfee9c43,8622ad73-deef-4525-8e8e-ba3f1462724e,5ab653d7-1ae3-40b0-ae07-c3d530a2a8f8)查看并讨论。";
-						break;
-					case '8622ad73-deef-4525-8e8e-ba3f1462724e':
-						$strMessage = "{$username} 就文句`{$palitext}`有疑问:\n
-						>内容摘要:<font color=\"comment\">{$prtext}</font>,\n
-						>句子编号:<font color=\"info\">{$sent_num}</font>\n
-						欢迎大家[点击链接]({$link}&channel=8622ad73-deef-4525-8e8e-ba3f1462724e,5ab653d7-1ae3-40b0-ae07-c3d530a2a8f8)查看并讨论。";
-						break;
-					case '5ab653d7-1ae3-40b0-ae07-c3d530a2a8f8':
-						$strMessage = "{$username} 就文句`{$palitext}`中的疑问有这样的回复:\n
-						>内容摘要:<font color=\"comment\">{$prtext}</font>,\n
-						>句子编号:<font color=\"info\">{$sent_num}</font>\n
-						欢迎大家[点击链接]({$link}&channel=8622ad73-deef-4525-8e8e-ba3f1462724e,5ab653d7-1ae3-40b0-ae07-c3d530a2a8f8)查看并讨论。";
-						break;
-					default:
-						$strMessage = "";
-						break;
-				}
-				$url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=25dbd74f-c89c-40e5-8cbc-48b1ef7710b8";
-				$param = [
-						"msgtype"=>"markdown",
-						"markdown"=> [
-							"content"=> $strMessage,
-						],
-					];
-				if(!empty($strMessage)){
-					$response = Http::post($url, $param);
-					if($response->successful()){
-						$robotMessageOk = true;
-						$webHookMessage = "消息发送成功";
-					}else{
-						$webHookMessage = "消息发送失败";
-						$robotMessageOk = false;
-					}
-				}else{
-					$webHookMessage = "channel不符";
-					$robotMessageOk = false;
-				}
-			//}else{
-			//	$webHookMessage = "不在段落范围内";
-			//}
-		}
 
 		#同时返回此句子pr数量
 		$info['book_id'] = $data['book'];
@@ -238,17 +174,26 @@ class SentPrController extends Controller
 
     /**
      * Display the specified resource.
-     *
-     * @param  \App\Models\SentPr  $sentPr
+     * @param  \Illuminate\Http\Request  $request
+     * @param  string  $uid
      * @return \Illuminate\Http\Response
      */
-    public function show(string $uid)
+    public function show(Request $request,string $uid)
     {
         //
+
         $pr = SentPr::where('uid',$uid)->first();
         if(!$pr){
             return $this->error('no data',404,404);
         }
+        //修改notification 已读状态
+        $user = AuthApi::current($request);
+        if($user){
+            Notification::where('res_id',$uid)
+                        ->where('to',$user['user_uid'])
+                        ->update(['status'=>'read']);
+        }
+
         return $this->ok(new SentPrResource($pr));
     }