Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0ffe415166 | |||
| dcf8c28f1a | |||
| f4f7373272 |
41
app/Helpers/Slack.php
Normal file
41
app/Helpers/Slack.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Helpers\Slack;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Quote;
|
||||
use Illuminate\Http\Request;
|
||||
@ -9,11 +10,11 @@ use Illuminate\Support\Facades\Http;
|
||||
|
||||
class WebHookController extends Controller
|
||||
{
|
||||
public function webHookSend(string $payload)
|
||||
{
|
||||
// TODO: move this to a helper class so we can reuse code between API and FE
|
||||
$data = ['text' => $payload];
|
||||
$response = Http::post(config('bot.webhook'), $data);
|
||||
|
||||
private Slack $slack;
|
||||
|
||||
public function __construct() {
|
||||
$this->slack = new Slack();
|
||||
}
|
||||
|
||||
public function sendQuote(Request $request)
|
||||
@ -24,28 +25,13 @@ class WebHookController extends Controller
|
||||
return;
|
||||
}
|
||||
|
||||
activity()
|
||||
->event('send')
|
||||
->log("Manually sent quote: $quote");
|
||||
|
||||
$this->webHookSend($quote);
|
||||
$this->slack->sendText($quote);
|
||||
}
|
||||
|
||||
public function sendRandomQuote()
|
||||
{
|
||||
$quote = Quote::inRandomOrder()->first();
|
||||
|
||||
activity()
|
||||
->performedOn($quote)
|
||||
->event('send')
|
||||
->log("Requested quote: $quote->quote");
|
||||
|
||||
$this->webHookSend($quote->quote);
|
||||
$this->slack->sendQuote($quote);
|
||||
}
|
||||
|
||||
public function test()
|
||||
{
|
||||
var_dump('secrets');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Helpers\Slack;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Spatie\Activitylog\LogOptions;
|
||||
@ -22,13 +23,9 @@ class Quote extends Model
|
||||
|
||||
public function request(): void
|
||||
{
|
||||
// Send the quote
|
||||
// If success, add it to the transactions
|
||||
$slack = new Slack();
|
||||
|
||||
activity()
|
||||
->performedOn($this)
|
||||
->event('send')
|
||||
->log("Requested quote: $this->quote");
|
||||
$slack->sendQuote($this);
|
||||
}
|
||||
|
||||
public function tapActivity(Activity $activity, string $eventName)
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Helpers\Slack;
|
||||
use App\Models\Log;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Livewire\Attributes\Layout;
|
||||
@ -15,12 +16,9 @@ new #[Layout('layouts.app')] class extends Component
|
||||
{
|
||||
$validated = $this->validate();
|
||||
|
||||
// TODO: move this to a helper class so we can reuse code between API and FE
|
||||
$response = Http::post(config('bot.webhook'), ['text' => $validated['quote']]);
|
||||
$slack = new Slack();
|
||||
|
||||
activity()
|
||||
->event('send')
|
||||
->log("Manually sent quote: {$validated['quote']}");
|
||||
$slack->sendText($validated['quote']);
|
||||
|
||||
$this->quote = '';
|
||||
}
|
||||
|
||||
@ -20,7 +20,6 @@ new #[Layout('layouts.guest')] class extends Component
|
||||
$this->authorize('request', $quote);
|
||||
|
||||
$quote->request();
|
||||
// TODO: add code to send a notification
|
||||
}
|
||||
|
||||
public function delete(Quote $quote): void
|
||||
@ -64,9 +63,12 @@ new #[Layout('layouts.guest')] class extends Component
|
||||
@if(auth()->user())
|
||||
<td class="relative w-8 pl-4 pr-3 text-sm sm:pl-6 border-t">
|
||||
<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 }})">
|
||||
<x-button wire:loading.attr="disabled" wire:loading.class="opacity-50" class="bg-nexi-purple w-24">{{ __('Request') }}</x-button>
|
||||
</form>
|
||||
@endif
|
||||
@if(auth()->user()->is_admin)
|
||||
<x-button
|
||||
x-data=""
|
||||
|
||||
@ -16,7 +16,6 @@ use Illuminate\Support\Facades\Route;
|
||||
*/
|
||||
|
||||
Route::middleware(['header.auth', 'throttle:api'])->group(function () {
|
||||
Route::get('/test', [WebHookController::class, 'test']);
|
||||
Route::post('/sendQuote', [WebHookController::class, 'sendQuote']);
|
||||
Route::post('/randomQuote', [WebHookController::class, 'sendRandomQuote']);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user