Compare commits

...

4 Commits
v0.4 ... master

Author SHA1 Message Date
0ffe415166 Merge pull request 'feat: add helper, clean up code, remove test route' (#29) from feat/request-quotes-from-fe into master
All checks were successful
deploy / production (push) Successful in 9m58s
Reviewed-on: #29
2023-10-28 10:43:13 -04:00
dcf8c28f1a fix: return type and typing on send 2023-10-28 22:47:32 +11:00
f4f7373272 feat: add helper, clean up code, remove test route 2023-10-26 20:14:05 +11:00
ben
c963982bfb Biiig feex
All checks were successful
deploy / production (push) Successful in 8m42s
2023-10-17 02:15:21 -04:00
7 changed files with 59 additions and 36 deletions

41
app/Helpers/Slack.php Normal file
View File

@ -0,0 +1,41 @@
<?php
namespace App\Helpers;
use App\Models\Quote;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
class Slack
{
private string $url;
public function __construct()
{
$this->url = config('bot.webhook');
}
private function send(string $text): Response
{
return Http::post($this->url, ['text' => $text]);
}
public function sendText(string $text): Response
{
activity()
->event('send')
->log("Manually sent quote: $text");
return $this->send($text);
}
public function sendQuote(Quote $quote): Response
{
activity()
->performedOn($quote)
->event('send')
->log("Requested quote: $quote");
return $this->send($quote->quote);
}
}

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use App\Helpers\Slack;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Quote; use App\Models\Quote;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -9,11 +10,11 @@ use Illuminate\Support\Facades\Http;
class WebHookController extends Controller class WebHookController extends Controller
{ {
public function webHookSend(string $payload)
{ private Slack $slack;
// TODO: move this to a helper class so we can reuse code between API and FE
$data = ['text' => $payload]; public function __construct() {
$response = Http::post(config('bot.webhook'), $data); $this->slack = new Slack();
} }
public function sendQuote(Request $request) public function sendQuote(Request $request)
@ -24,28 +25,13 @@ class WebHookController extends Controller
return; return;
} }
activity() $this->slack->sendText($quote);
->event('send')
->log("Manually sent quote: $quote");
$this->webHookSend($quote);
} }
public function sendRandomQuote() public function sendRandomQuote()
{ {
$quote = Quote::inRandomOrder()->first(); $quote = Quote::inRandomOrder()->first();
activity() $this->slack->sendQuote($quote);
->performedOn($quote)
->event('send')
->log("Requested quote: $quote->quote");
$this->webHookSend($quote->quote);
} }
public function test()
{
var_dump('secrets');
}
} }

View File

@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use App\Helpers\Slack;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\LogOptions; use Spatie\Activitylog\LogOptions;
@ -22,13 +23,9 @@ class Quote extends Model
public function request(): void public function request(): void
{ {
// Send the quote $slack = new Slack();
// If success, add it to the transactions
activity() $slack->sendQuote($this);
->performedOn($this)
->event('send')
->log("Requested quote: $this->quote");
} }
public function tapActivity(Activity $activity, string $eventName) public function tapActivity(Activity $activity, string $eventName)

View File

@ -37,7 +37,7 @@ run("cd {{release_path}} && {{bin/npm}} run {$command}");
}); });
task('perm:fix', function () { task('perm:fix', function () {
run("chown -R nginx:nginx /opt/PriceyBotPanel"); run("chown -R nginx:nginx /opt/PriceyBotPanel/{current,release,shared}");
}); });

View File

@ -1,5 +1,6 @@
<?php <?php
use App\Helpers\Slack;
use App\Models\Log; use App\Models\Log;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Livewire\Attributes\Layout; use Livewire\Attributes\Layout;
@ -15,12 +16,9 @@ new #[Layout('layouts.app')] class extends Component
{ {
$validated = $this->validate(); $validated = $this->validate();
// TODO: move this to a helper class so we can reuse code between API and FE $slack = new Slack();
$response = Http::post(config('bot.webhook'), ['text' => $validated['quote']]);
activity() $slack->sendText($validated['quote']);
->event('send')
->log("Manually sent quote: {$validated['quote']}");
$this->quote = ''; $this->quote = '';
} }

View File

@ -20,7 +20,6 @@ new #[Layout('layouts.guest')] class extends Component
$this->authorize('request', $quote); $this->authorize('request', $quote);
$quote->request(); $quote->request();
// TODO: add code to send a notification
} }
public function delete(Quote $quote): void public function delete(Quote $quote): void
@ -64,9 +63,12 @@ new #[Layout('layouts.guest')] class extends Component
@if(auth()->user()) @if(auth()->user())
<td class="relative w-8 pl-4 pr-3 text-sm sm:pl-6 border-t"> <td class="relative w-8 pl-4 pr-3 text-sm sm:pl-6 border-t">
<div class="flex flex-row space-x-2"> <div class="flex flex-row space-x-2">
@if(auth()->user()->is_admin)
{{-- For now this is admin only, when we add credits we can make it avaliable to users --}}
<form wire:submit="request({{ $quote }})"> <form wire:submit="request({{ $quote }})">
<x-button wire:loading.attr="disabled" wire:loading.class="opacity-50" class="bg-nexi-purple w-24">{{ __('Request') }}</x-button> <x-button wire:loading.attr="disabled" wire:loading.class="opacity-50" class="bg-nexi-purple w-24">{{ __('Request') }}</x-button>
</form> </form>
@endif
@if(auth()->user()->is_admin) @if(auth()->user()->is_admin)
<x-button <x-button
x-data="" x-data=""

View File

@ -16,7 +16,6 @@ use Illuminate\Support\Facades\Route;
*/ */
Route::middleware(['header.auth', 'throttle:api'])->group(function () { Route::middleware(['header.auth', 'throttle:api'])->group(function () {
Route::get('/test', [WebHookController::class, 'test']);
Route::post('/sendQuote', [WebHookController::class, 'sendQuote']); Route::post('/sendQuote', [WebHookController::class, 'sendQuote']);
Route::post('/randomQuote', [WebHookController::class, 'sendRandomQuote']); Route::post('/randomQuote', [WebHookController::class, 'sendRandomQuote']);