visuddhinanda před 2 roky
rodič
revize
7d253b4606

+ 158 - 0
app/Http/Controllers/WebHookController.php

@@ -0,0 +1,158 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Models\WebHook;
+use Illuminate\Http\Request;
+use App\Http\Resources\WebHookResource;
+use App\Http\Api\AuthApi;
+use Illuminate\Support\Str;
+
+class WebHookController extends Controller
+{
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index(Request $request)
+    {
+		switch ($request->get('view')) {
+            case 'channel':
+                $table = WebHook::where('res_type','channel')->where('res_id',$request->get('id'));
+                break;
+            default:
+                return $this->error("no view");
+                break;
+        }
+        if(!empty($search)){
+            $table->where('url', 'like', $search."%");
+        }
+        $table->orderBy($request->get('order','updated_at'),$request->get('dir','desc'));
+        $count = $table->count();
+        $table->skip($request->get("offset",0))
+              ->take($request->get('limit',1000));
+
+        $result = $table->get();
+        return $this->ok(["rows"=>WebHookResource::collection($result),"count"=>$count]);
+    }
+
+    /**
+     * Store a newly created resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return \Illuminate\Http\Response
+     */
+    public function store(Request $request)
+    {
+        //
+        $user = AuthApi::current($request);
+        if(!$user){
+            return $this->error(__('auth.failed'),[],401);
+        }
+
+        $validated = $request->validate([
+            'res_type' => 'required',
+            'res_id' => 'required',
+            'url' => 'required',
+            'receiver' => 'required',
+        ]);
+        //TODO 判断权限
+        $new = new WebHook;
+        $new->id = Str::uuid();
+        $new->res_type = $validated['res_type'];
+        $new->res_id = $validated['res_id'];
+        $new->url = $validated['url'];
+        $new->receiver = $validated['receiver'];
+        if($request->has('event')){
+            $new->event = json_encode($request->get('event'),JSON_UNESCAPED_UNICODE);
+        }else{
+            $new->event = null;
+        }
+        $new->status = $request->get('status');
+        $new->editor_uid = $user['user_uid'];
+        $new->save();
+        return $this->ok(new WebHookResource($new));
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  \App\Models\WebHook  $webHook
+     * @return \Illuminate\Http\Response
+     */
+    public function show(string $id)
+    {
+        //
+        $webHook = WebHook::find($id);
+        if(!$webHook){
+            return $this->error('no id');
+        }
+        return $this->ok(new WebHookResource($webHook));
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \App\Models\WebHook  $webHook
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, string $id)
+    {
+        //
+        $user = AuthApi::current($request);
+        if(!$user){
+            return $this->error(__('auth.failed'),[],401);
+        }
+
+        $validated = $request->validate([
+            'res_type' => 'required',
+            'res_id' => 'required',
+            'url' => 'required',
+            'receiver' => 'required',
+        ]);
+        //TODO 判断权限
+        $webHook = WebHook::find($id);
+        if(!$webHook){
+            return $this->error('no id');
+        }
+        $webHook->res_type = $validated['res_type'];
+        $webHook->res_id = $validated['res_id'];
+        $webHook->url = $validated['url'];
+        $webHook->receiver = $validated['receiver'];
+        if($request->has('event')){
+            $webHook->event = json_encode($request->get('event'),JSON_UNESCAPED_UNICODE);
+        }else{
+            $webHook->event = null;
+        }
+        $webHook->status = $request->get('status');
+        $webHook->editor_uid = $user['user_uid'];
+        $webHook->save();
+        return $this->ok(new WebHookResource($webHook));
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  \App\Models\WebHook  $webHook
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy(string $id)
+    {
+        //
+        $user = AuthApi::current($request);
+        if(!$user){
+            return $this->error(__('auth.failed'));
+        }
+        $webHook = WebHook::find($id);
+        if(!$webHook){
+            return $this->error('no id');
+        }
+        //TODO 判断当前用户是否有权限
+        $delete = 0;
+        $delete = $webHook->delete();
+
+        return $this->ok($delete);
+    }
+}

+ 32 - 0
app/Http/Resources/WebHookResource.php

@@ -0,0 +1,32 @@
+<?php
+
+namespace App\Http\Resources;
+
+use Illuminate\Http\Resources\Json\JsonResource;
+
+class WebHookResource extends JsonResource
+{
+    /**
+     * Transform the resource into an array.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
+     */
+    public function toArray($request)
+    {
+        $data = [
+            "id"=>$this->id,
+            "res_type"=> $this->res_type,
+            "res_id"=> $this->res_id,
+            "url"=> $this->url,
+            "receiver"=> $this->receiver,
+            "event"=> json_decode($this->event),
+            "status"=> $this->status,
+            "fail"=> $this->fail,
+            "success"=> $this->success,
+            "created_at"=> $this->created_at,
+            "updated_at"=> $this->updated_at,
+        ];
+        return $data;
+    }
+}

+ 17 - 0
app/Models/WebHook.php

@@ -0,0 +1,17 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class WebHook extends Model
+{
+    use HasFactory;
+    protected $primaryKey = 'id';
+    protected $casts = [
+		'id' => 'string'
+	];
+    public  $incrementing = true;
+
+}

+ 34 - 0
database/migrations/2023_08_06_125849_add_config_in_channels.php

@@ -0,0 +1,34 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddConfigInChannels extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('channels', function (Blueprint $table) {
+            //
+            $table->json('config')->nullable();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('channels', function (Blueprint $table) {
+            //
+            $table->dropColumn('config');
+        });
+    }
+}

+ 40 - 0
database/migrations/2023_08_07_113207_create_web_hooks_table.php

@@ -0,0 +1,40 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateWebHooksTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('web_hooks', function (Blueprint $table) {
+            $table->uuid("id")->primary();
+            $table->string('res_type',32)->index();
+            $table->uuid('res_id')->index();
+            $table->string('url',512)->index();
+            $table->string('receiver',32)->index();
+            $table->json('event')->nullable();
+            $table->integer('fail')->default(0);
+            $table->integer('success')->default(0);
+            $table->string('status',16)->index()->default('active');
+            $table->uuid("editor_uid")->index();
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('web_hooks');
+    }
+}

+ 22 - 0
tests/Feature/WebhookTest.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace Tests\Feature;
+
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Foundation\Testing\WithFaker;
+use Tests\TestCase;
+
+class WebhookTest extends TestCase
+{
+    /**
+     * A basic feature test example.
+     *
+     * @return void
+     */
+    public function test_index()
+    {
+        $response = $this->get('/api/v2/webhook?view=channel&id=1e4b926d-54d7-4932-b8a6-7cdc65abd992');
+
+        $response->assertStatus(200);
+    }
+}