Browse Source

支持 notification webhook

visuddhinanda 1 year ago
parent
commit
3c784b192a
1 changed files with 114 additions and 107 deletions
  1. 114 107
      api-v8/app/Http/Controllers/SentPrController.php

+ 114 - 107
api-v8/app/Http/Controllers/SentPrController.php

@@ -29,59 +29,60 @@ class SentPrController extends Controller
         //
         switch ($request->get('view')) {
             case 'sent-info':
-                $table = SentPr::where('book_id',$request->get('book'))
-                                ->where('paragraph',$request->get('para'))
-                                ->where('word_start',$request->get('start'))
-                                ->where('word_end',$request->get('end'))
-                                ->where('channel_uid',$request->get('channel'));
+                $table = SentPr::where('book_id', $request->get('book'))
+                    ->where('paragraph', $request->get('para'))
+                    ->where('word_start', $request->get('start'))
+                    ->where('word_end', $request->get('end'))
+                    ->where('channel_uid', $request->get('channel'));
                 $all_count = $table->count();
-                $result = $table->orderBy('created_at','desc')->get();
+                $result = $table->orderBy('created_at', 'desc')->get();
 
                 break;
         }
-        if($result){
+        if ($result) {
             //修改notification 已读状态
             $user = AuthApi::current($request);
-            if($user){
-                $id=array();
+            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']);
+                Notification::whereIn('res_id', $id)
+                    ->where('to', $user['user_uid'])
+                    ->update(['status' => 'read']);
             }
             return $this->ok([
-                    "rows"=>SentPrResource::collection($result),
-                    "count"=>$all_count
-                ]);
-        }else{
+                "rows" => SentPrResource::collection($result),
+                "count" => $all_count
+            ]);
+        } else {
             return $this->error("no data");
         }
     }
 
-    public function pr_tree(Request $request){
+    public function pr_tree(Request $request)
+    {
         $output = [];
         $sentences = $request->get("data");
         foreach ($sentences as $key => $sentence) {
             # 先查句子信息
-            $sentInfo = Sentence::where('book_id',$sentence['book'])
-                                ->where('paragraph',$sentence['paragraph'])
-                                ->where('word_start',$sentence['word_start'])
-                                ->where('word_end',$sentence['word_end'])
-                                ->where('channel_uid',$sentence['channel_id'])
-                                ->first();
-            $sentPr = SentPr::where('book_id',$sentence['book'])
-                            ->where('paragraph',$sentence['paragraph'])
-                            ->where('word_start',$sentence['word_start'])
-                            ->where('word_end',$sentence['word_end'])
-                            ->where('channel_uid',$sentence['channel_id'])
-                            ->select('content','editor_uid')
-                            ->orderBy('created_at','desc')->get();
-            if(count($sentPr)>0){
-                if($sentInfo){
+            $sentInfo = Sentence::where('book_id', $sentence['book'])
+                ->where('paragraph', $sentence['paragraph'])
+                ->where('word_start', $sentence['word_start'])
+                ->where('word_end', $sentence['word_end'])
+                ->where('channel_uid', $sentence['channel_id'])
+                ->first();
+            $sentPr = SentPr::where('book_id', $sentence['book'])
+                ->where('paragraph', $sentence['paragraph'])
+                ->where('word_start', $sentence['word_start'])
+                ->where('word_end', $sentence['word_end'])
+                ->where('channel_uid', $sentence['channel_id'])
+                ->select('content', 'editor_uid')
+                ->orderBy('created_at', 'desc')->get();
+            if (count($sentPr) > 0) {
+                if ($sentInfo) {
                     $content = $sentInfo->content;
-                }else{
+                } else {
                     $content = "null";
                 }
                 $output[] = [
@@ -97,9 +98,8 @@ class SentPrController extends Controller
                     'pr' => $sentPr,
                 ];
             }
-
         }
-        return $this->ok(['rows'=>$output,'count'=>count($output)]);
+        return $this->ok(['rows' => $output, 'count' => count($output)]);
     }
     /**
      * Store a newly created resource in storage.
@@ -111,25 +111,25 @@ class SentPrController extends Controller
     {
         //
         $user = AuthApi::current($request);
-        if(!$user){
-            return $this->error(__('auth.failed'),401,401);
+        if (!$user) {
+            return $this->error(__('auth.failed'), 401, 401);
         }
         $user_uid = $user['user_uid'];
 
         $data = $request->all();
 
 
-		#查询是否存在
-		#同样的内容只能提交一次
-		$exists = SentPr::where('book_id',$data['book'])
-						->where('paragraph',$data['para'])
-						->where('word_start',$data['begin'])
-						->where('word_end',$data['end'])
-						->where('content',$data['text'])
-						->where('channel_uid',$data['channel'])
-						->exists();
-        if($exists){
-            return $this->error("已经存在同样的修改建议",200,200);
+        #查询是否存在
+        #同样的内容只能提交一次
+        $exists = SentPr::where('book_id', $data['book'])
+            ->where('paragraph', $data['para'])
+            ->where('word_start', $data['begin'])
+            ->where('word_end', $data['end'])
+            ->where('content', $data['text'])
+            ->where('channel_uid', $data['channel'])
+            ->exists();
+        if ($exists) {
+            return $this->error("已经存在同样的修改建议", 200, 200);
         }
 
         #不存在,新建
@@ -143,33 +143,41 @@ class SentPrController extends Controller
         $new->channel_uid = $data['channel'];
         $new->editor_uid = $user_uid;
         $new->content = $data['text'];
-        $new->language = Channel::where('uid',$data['channel'])->value('lang');
-        $new->status = 1;//未处理状态
-        $new->strlen = mb_strlen($data['text'],"UTF-8");
-        $new->create_time = time()*1000;
-        $new->modify_time = time()*1000;
+        $new->language = Channel::where('uid', $data['channel'])->value('lang');
+        $new->status = 1; //未处理状态
+        $new->strlen = mb_strlen($data['text'], "UTF-8");
+        $new->create_time = time() * 1000;
+        $new->modify_time = time() * 1000;
         $new->save();
-        Mq::publish('suggestion',['data'=>new SentPrResource($new),
-                                  'token'=>AuthApi::getToken($request)]);
 
-		$robotMessageOk=true;
-		$webHookMessage="";
+        $suggestionData =  [
+            'data' => new SentPrResource($new),
+            'token' => AuthApi::getToken($request),
+            'notification' => $request->get('notification', true),
+            'webhook' => $request->get('webhook', true),
+        ];
+        Mq::publish(
+            'suggestion',
+            $suggestionData
+        );
 
-		#同时返回此句子pr数量
-		$info['book_id'] = $data['book'];
-		$info['paragraph'] = $data['para'];
-		$info['word_start'] = $data['begin'];
-		$info['word_end'] = $data['end'];
-		$info['channel_uid'] = $data['channel'];
-		$count = SentPr::where('book_id' , $data['book'])
-						->where('paragraph' , $data['para'])
-						->where('word_start' , $data['begin'])
-						->where('word_end' , $data['end'])
-						->where('channel_uid' , $data['channel'])
-						->count();
+        $robotMessageOk = true;
+        $webHookMessage = "";
 
-		return $this->ok(["new"=>$info,"count"=>$count,"webhook"=>["message"=>$webHookMessage,"ok"=>$robotMessageOk]]);
+        #同时返回此句子pr数量
+        $info['book_id'] = $data['book'];
+        $info['paragraph'] = $data['para'];
+        $info['word_start'] = $data['begin'];
+        $info['word_end'] = $data['end'];
+        $info['channel_uid'] = $data['channel'];
+        $count = SentPr::where('book_id', $data['book'])
+            ->where('paragraph', $data['para'])
+            ->where('word_start', $data['begin'])
+            ->where('word_end', $data['end'])
+            ->where('channel_uid', $data['channel'])
+            ->count();
 
+        return $this->ok(["new" => $info, "count" => $count, "webhook" => ["message" => $webHookMessage, "ok" => $robotMessageOk]]);
     }
 
     /**
@@ -178,20 +186,20 @@ class SentPrController extends Controller
      * @param  string  $uid
      * @return \Illuminate\Http\Response
      */
-    public function show(Request $request,string $uid)
+    public function show(Request $request, string $uid)
     {
         //
 
-        $pr = SentPr::where('uid',$uid)->first();
-        if(!$pr){
-            return $this->error('no data',404,404);
+        $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']);
+        if ($user) {
+            Notification::where('res_id', $uid)
+                ->where('to', $user['user_uid'])
+                ->update(['status' => 'read']);
         }
 
         return $this->ok(new SentPrResource($pr));
@@ -207,22 +215,21 @@ class SentPrController extends Controller
     public function update(Request $request, string $id)
     {
         $user = AuthApi::current($request);
-        if(!$user){
-            return $this->error(__('auth.failed'),401,401);
+        if (!$user) {
+            return $this->error(__('auth.failed'), 401, 401);
         }
 
-		$sentPr = SentPr::find($id);
-        if(!$sentPr){
+        $sentPr = SentPr::find($id);
+        if (!$sentPr) {
             return $this->error('no res');
         }
-		if($sentPr->editor_uid !== $user['user_uid']){
-            return $this->error('not power',403,403);
+        if ($sentPr->editor_uid !== $user['user_uid']) {
+            return $this->error('not power', 403, 403);
         }
         $sentPr->content = $request->get('text');
-        $sentPr->modify_time = time()*1000;
+        $sentPr->modify_time = time() * 1000;
         $sentPr->save();
         return $this->ok($sentPr);
-
     }
 
     /**
@@ -235,31 +242,31 @@ class SentPrController extends Controller
     {
         //
         $user = AuthApi::current($request);
-        if(!$user){
-            return $this->error(__('auth.failed'),401,401);
+        if (!$user) {
+            return $this->error(__('auth.failed'), 401, 401);
         }
-		$old = SentPr::where('id', $id)->first();
-        if(!$old){
+        $old = SentPr::where('id', $id)->first();
+        if (!$old) {
             return $this->error('no res');
         }
         //鉴权
-        if($old->editor_uid !== $user["user_uid"]){
-            return $this->error(__('auth.failed'),403,403);
+        if ($old->editor_uid !== $user["user_uid"]) {
+            return $this->error(__('auth.failed'), 403, 403);
+        }
+        $result = SentPr::where('id', $id)
+            ->where('editor_uid', $user["user_uid"])
+            ->delete();
+        if ($result > 0) {
+            #同时返回此句子pr数量
+            $count = SentPr::where('book_id', $old->book_id)
+                ->where('paragraph', $old->paragraph)
+                ->where('word_start', $old->word_start)
+                ->where('word_end', $old->word_end)
+                ->where('channel_uid', $old->channel_uid)
+                ->count();
+            return $this->ok($count);
+        } else {
+            return $this->error('not power', 403, 403);
         }
-		$result = SentPr::where('id', $id)
-						->where('editor_uid', $user["user_uid"])
-						->delete();
-		if($result>0){
-					#同时返回此句子pr数量
-		    $count = SentPr::where('book_id' , $old->book_id)
-						->where('paragraph' , $old->paragraph)
-						->where('word_start' , $old->word_start)
-						->where('word_end' , $old->word_end)
-						->where('channel_uid' , $old->channel_uid)
-						->count();
-			return $this->ok($count);
-		}else{
-			return $this->error('not power',403,403);
-		}
     }
 }