Sort of working beta
This commit is contained in:
40
app/Notification/Notification.php
Normal file
40
app/Notification/Notification.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notification;
|
||||
|
||||
use App\Models\Job;
|
||||
use App\Notification\Stringifiable\StringifiableSimpleText;
|
||||
|
||||
abstract class Notification {
|
||||
|
||||
protected Job $job;
|
||||
private NotificationBody $body;
|
||||
|
||||
public bool $isError;
|
||||
|
||||
public function __construct(int $jobId, NotificationBody $body, bool $isError = false) {
|
||||
$this->job = Job::find($jobId);
|
||||
$this->body = $body;
|
||||
$this->isError = $isError;
|
||||
}
|
||||
|
||||
public function getTitle(): Stringifiable {
|
||||
return new StringifiableSimpleText($this->job->name);
|
||||
}
|
||||
|
||||
public function getBody(): Stringifiable {
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
abstract public function getLinkURL(): ?string;
|
||||
|
||||
public function getImageURL(): ?string {
|
||||
$imageProjectPath = $this->getImageProjectPath();
|
||||
if ($imageProjectPath === null) {
|
||||
return null;
|
||||
}
|
||||
return url($imageProjectPath);
|
||||
}
|
||||
|
||||
abstract public function getImageProjectPath(): ?string;
|
||||
}
|
8
app/Notification/NotificationBody.php
Normal file
8
app/Notification/NotificationBody.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notification;
|
||||
|
||||
use App\Notification\Stringifiable;
|
||||
|
||||
abstract class NotificationBody extends Stringifiable {
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notification\NotificationBody\Hellcase;
|
||||
|
||||
use App\Browser\Jobs\Hellcase\HellcaseLoginQrCode;
|
||||
use App\Notification\NotificationBody;
|
||||
|
||||
class HellcaseNotificationDailyFreeBody extends NotificationBody {
|
||||
|
||||
private string $content = "Vous avez remporté un cadeau gratuit sur Hellcase !";
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toMarkdownString(): string {
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toString(): string {
|
||||
return $this->content;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notification\NotificationBody\Hellcase;
|
||||
|
||||
use App\Browser\Jobs\Hellcase\HellcaseLoginQrCode;
|
||||
use App\Notification\NotificationBody;
|
||||
|
||||
class HellcaseNotificationLoginBody extends NotificationBody {
|
||||
|
||||
private string $content = "Veuillez utiliser steam guard pour vous connecter à Hellcase.\nLe QR code se rafrachira toutes les ". HellcaseLoginQrCode::QR_CODE_VALIDITY ." secondes.";
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toMarkdownString(): string {
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toString(): string {
|
||||
return $this->content;
|
||||
}
|
||||
}
|
44
app/Notification/NotificationBody/ListNotificationBody.php
Normal file
44
app/Notification/NotificationBody/ListNotificationBody.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notification\NotificationBody;
|
||||
|
||||
use App\Notification\NotificationBody;
|
||||
use App\Notification\Stringifiable;
|
||||
|
||||
class ListNotificationBody extends NotificationBody {
|
||||
|
||||
private array $content;
|
||||
|
||||
public function __construct(array $content) {
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toMarkdownString(): string {
|
||||
$string = "";
|
||||
foreach ($this->content as $item) {
|
||||
$string .= "- ". $this->getTextFromContent($item) . "\n";
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toString(): string {
|
||||
$string = "";
|
||||
foreach ($this->content as $item) {
|
||||
$string .= $this->getTextFromContent($item) . "\n";
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
public function getTextFromContent(string|Stringifiable $content): string {
|
||||
if ($content instanceof Stringifiable) {
|
||||
return $content->toString();
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
}
|
32
app/Notification/NotificationBody/SimpleNotificationBody.php
Normal file
32
app/Notification/NotificationBody/SimpleNotificationBody.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notification\NotificationBody;
|
||||
|
||||
use App\Notification\NotificationBody;
|
||||
use App\Notification\Stringifiable;
|
||||
|
||||
class SimpleNotificationBody extends NotificationBody {
|
||||
|
||||
private string $body;
|
||||
|
||||
public function __construct(string $body) {
|
||||
$this->body = $body;
|
||||
}
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toMarkdownString(): string {
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
public function toHTMLString(): string {
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toString(): string {
|
||||
return $this->body;
|
||||
}
|
||||
}
|
7
app/Notification/NotificationProvider.php
Normal file
7
app/Notification/NotificationProvider.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notification;
|
||||
|
||||
abstract class NotificationProvider {
|
||||
abstract public static function send(Notification $notification): void;
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notification\Notifications\Hellcase;
|
||||
|
||||
use App\Browser\Jobs\Hellcase\HellcaseDailyFreeScreenshot;
|
||||
use App\Browser\Jobs\Hellcase\HellcaseLoginQrCode;
|
||||
use App\Notification\Notification;
|
||||
use App\Notification\Notifications\NotificationLogin;
|
||||
use Laravel\Dusk\Browser;
|
||||
|
||||
class HellcaseNotificationDailyFree extends NotificationLogin {
|
||||
|
||||
public function __construct(int $jobId, \App\Notification\NotificationBody $body) {
|
||||
parent::__construct($jobId, $body);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getImageProjectPath(): string|null {
|
||||
return HellcaseDailyFreeScreenshot::getImgFileProjectPath();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getLinkURL(): string|null {
|
||||
return route('jobs.show', ['job' => $this->job->id]);
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notification\Notifications\Hellcase;
|
||||
|
||||
use App\Browser\Jobs\Hellcase\HellcaseLoginQrCode;
|
||||
use App\Notification\Notification;
|
||||
use App\Notification\Notifications\NotificationLogin;
|
||||
use Laravel\Dusk\Browser;
|
||||
|
||||
class HellcaseNotificationLogin extends NotificationLogin {
|
||||
|
||||
public function __construct(int $jobId, \App\Notification\NotificationBody $body) {
|
||||
parent::__construct($jobId, $body);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getImageProjectPath(): string|null {
|
||||
return HellcaseLoginQrCode::getImgFileProjectPath();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getLinkURL(): string|null {
|
||||
return route('jobs.show', ['job' => $this->job->id]);
|
||||
}
|
||||
}
|
11
app/Notification/Notifications/NotificationLogin.php
Normal file
11
app/Notification/Notifications/NotificationLogin.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notification\Notifications;
|
||||
|
||||
use App\Notification\Notification;
|
||||
|
||||
abstract class NotificationLogin extends Notification {
|
||||
public function __construct(int $jobId, \App\Notification\NotificationBody $body) {
|
||||
parent::__construct($jobId, $body, true);
|
||||
}
|
||||
}
|
36
app/Notification/Notifications/SimpleNotification.php
Normal file
36
app/Notification/Notifications/SimpleNotification.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notification\Notifications;
|
||||
|
||||
use App\Notification\Notification;
|
||||
use App\Notification\NotificationBody\SimpleNotificationBody;
|
||||
use App\Notification\Stringifiable;
|
||||
use App\Notification\Stringifiable\StringifiableSimpleText;
|
||||
|
||||
class SimpleNotification extends Notification {
|
||||
|
||||
private StringifiableSimpleText $title;
|
||||
|
||||
public function __construct(int $jobId, string $title, string $body, bool $isError = false) {
|
||||
$this->title = new StringifiableSimpleText($title);
|
||||
parent::__construct($jobId, new SimpleNotificationBody($body), $isError);
|
||||
}
|
||||
|
||||
public function getTitle(): Stringifiable {
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getImageProjectPath(): string|null {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getLinkURL(): string|null {
|
||||
return route('home');
|
||||
}
|
||||
}
|
23
app/Notification/Providers/AllNotification.php
Normal file
23
app/Notification/Providers/AllNotification.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notification\Providers;
|
||||
|
||||
use App\Notification\NotificationProvider;
|
||||
use App\Notification\INotificationProvider;
|
||||
use App\Models\JobInfo;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class AllNotification extends NotificationProvider {
|
||||
private const NOTIFICATIONS_PROVIDERS = [
|
||||
DiscordWebHookNotification::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function send(\App\Notification\Notification $notification): void {
|
||||
foreach (self::NOTIFICATIONS_PROVIDERS as $provider) {
|
||||
$provider::send($notification);
|
||||
}
|
||||
}
|
||||
}
|
110
app/Notification/Providers/DiscordWebHookNotification.php
Normal file
110
app/Notification/Providers/DiscordWebHookNotification.php
Normal file
@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notification\Providers;
|
||||
|
||||
use App\Notification\NotificationProvider;
|
||||
use App\Notification\INotificationProvider;
|
||||
use App\Models\JobInfo;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class DiscordWebHookNotification extends NotificationProvider {
|
||||
|
||||
private const EMBED_COLOR = ["521254", "16058119"];
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function send(\App\Notification\Notification $notification): void {
|
||||
/*
|
||||
Test Json for a complete embed :
|
||||
{
|
||||
"content": "",
|
||||
"tts": false,
|
||||
"embeds": [
|
||||
{
|
||||
"id": 652627557,
|
||||
"title": "Title",
|
||||
"description": "This is the markdown body\n",
|
||||
"color": 521254,
|
||||
"fields": [
|
||||
{
|
||||
"id": 984079152,
|
||||
"name": "Field 1",
|
||||
"value": "test"
|
||||
}
|
||||
],
|
||||
"url": "https://localhost:8000",
|
||||
"image": {
|
||||
"url": "https://www.thoughtco.com/thmb/jzJO77P0K9zIbqxQOVOaHWFCfj4=/1732x1732/filters:fill(auto,1)/GettyImages-186451154-58c3965a3df78c353cf8cc7b.jpg"
|
||||
}
|
||||
}
|
||||
],
|
||||
"components": [],
|
||||
"actions": {},
|
||||
"username": "Datboi",
|
||||
"avatar_url": "https://www.fairytailrp.com/t40344-here-come-dat-boi"
|
||||
}
|
||||
*/
|
||||
$webHookUrl = static::getDiscordWebHookUrl($notification->isError);
|
||||
$body = [
|
||||
"content"=> "",
|
||||
"tts"=> false,
|
||||
"embeds" => [
|
||||
[
|
||||
"id" => 652627557,
|
||||
"title" => $notification->getTitle()->toString(),
|
||||
"description" => $notification->getBody()->toMarkdownString(),
|
||||
"color" => self::EMBED_COLOR[(int)$notification->isError],
|
||||
"url" => $notification->getLinkURL(),
|
||||
],
|
||||
],
|
||||
"username" => "Datboi",
|
||||
"avatar_url" => "https://media1.giphy.com/media/yDTWAecZcB2Jq/200w.gif?cid=6c09b952f68kz3wnkqsmyha8e7xrpe8n2kx0nkf2b8cir6am&rid=200w.gif&ct=g",
|
||||
];
|
||||
|
||||
if ($notification->getImageURL() !== null) {
|
||||
$body["embeds"][0]["image"] = [
|
||||
"url" => "attachment://image.png"
|
||||
];
|
||||
}
|
||||
|
||||
$payloadJson = json_encode($body);
|
||||
|
||||
$formData = [
|
||||
'payload_json' => $payloadJson,
|
||||
];
|
||||
|
||||
if ($notification->getImageURL() !== null) {
|
||||
$formData['file'] = curl_file_create($notification->getImageProjectPath(), 'image/png', 'image.png');
|
||||
}
|
||||
|
||||
$ch = curl_init($webHookUrl);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $formData);
|
||||
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
$response = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
}
|
||||
|
||||
private static function getDiscordWebHookUrl(bool $isError): string {
|
||||
$generalWebHookUrlKey = 'discord_webhook_url';
|
||||
$generalWebHookUrl = Cache::rememberForever($generalWebHookUrlKey, function () use ($generalWebHookUrlKey) {
|
||||
return JobInfo::where('key', $generalWebHookUrlKey)->first()->value;
|
||||
});
|
||||
if ($generalWebHookUrl === null) {
|
||||
throw new \Exception("Le webhook discord n'a pas été configuré");
|
||||
}
|
||||
|
||||
if ($isError) {
|
||||
$errorWebHookUrlKey = 'discord_error_webhook_url';
|
||||
$errorWebHookUrl = Cache::rememberForever($errorWebHookUrlKey, function () use ($errorWebHookUrlKey) {
|
||||
return JobInfo::where('key', $errorWebHookUrlKey)->first()->value;
|
||||
});
|
||||
return $errorWebHookUrl ?? $generalWebHookUrl;
|
||||
}
|
||||
else {
|
||||
return $generalWebHookUrl;
|
||||
}
|
||||
}
|
||||
}
|
21
app/Notification/Stringifiable.php
Normal file
21
app/Notification/Stringifiable.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notification;
|
||||
|
||||
use Parsedown;
|
||||
|
||||
abstract class Stringifiable{
|
||||
private Parsedown $parsedown;
|
||||
|
||||
public function __construct() {
|
||||
$this->parsedown = new Parsedown();
|
||||
}
|
||||
|
||||
abstract public function toString(): string;
|
||||
|
||||
public function toHTMLString(): string {
|
||||
return $this->parsedown->text($this->toMarkdownString());
|
||||
}
|
||||
|
||||
abstract public function toMarkdownString(): string;
|
||||
}
|
34
app/Notification/Stringifiable/StringifiableSimpleText.php
Normal file
34
app/Notification/Stringifiable/StringifiableSimpleText.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notification\Stringifiable;
|
||||
|
||||
use App\Notification\Stringifiable;
|
||||
|
||||
class StringifiableSimpleText extends Stringifiable {
|
||||
private string $text;
|
||||
|
||||
public function __construct(string $text) {
|
||||
$this->text = $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toHTMLString(): string {
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toMarkdownString(): string {
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toString(): string {
|
||||
return $this->text;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user