From 8aa83cdc0247bf51d28c1ccdf2ec3e437ed3c1cb Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 28 Mar 2023 20:54:14 +0200 Subject: [PATCH] wip #5835 @0.5 --- app/Fluidbook/Packager/Download.php | 6 +++- app/Jobs/DownloadBase.php | 28 +++++++++--------- app/Jobs/FluidbookCollectionDownload.php | 2 +- app/Notifications/DownloadReady.php | 32 ++++++++++++++------- app/Notifications/FluidbookslackChannel.php | 3 +- 5 files changed, 44 insertions(+), 27 deletions(-) diff --git a/app/Fluidbook/Packager/Download.php b/app/Fluidbook/Packager/Download.php index 02a7c71d4..dd0cbca95 100644 --- a/app/Fluidbook/Packager/Download.php +++ b/app/Fluidbook/Packager/Download.php @@ -5,6 +5,7 @@ namespace App\Fluidbook\Packager; use App\Jobs\DownloadBase; use App\Models\FluidbookExternalInstallServer; use App\Services\ScormCloud; +use Cubist\Backpack\Facades\App; use Cubist\Util\CommandLine\Rsync; use Cubist\Util\Files\Files; use Cubist\Util\Text; @@ -75,12 +76,14 @@ class Download extends DownloadBase public function handle() { + App::setLocale($this->user->preferredLocale()); try { $url = $this->_compileandpackage(); $translateVariables = ['title' => $this->_title(), 'nb' => $this->_id()]; $subject = __($this->_subject, $translateVariables); $text = __($this->_text, $translateVariables); + $showTextIfNotEmail = false; $actions = []; if ($this->action === 'download' || $this->action === 'scormcloud') { $actions = ['Télécharger' => $url]; @@ -124,10 +127,11 @@ class Download extends DownloadBase $subject = __('Erreur lors de la compilation du :type :nb', ['nb' => $this->_id(), 'type' => $this->type]); $text = __('Détails de l\'erreur :message', ['message' => $e->getMessage() . ' at line ' . $e->getLine() . ' of ' . $e->getFile()]); $actions = []; + $showTextIfNotEmail = true; Log::error($e); } - $this->sendNotification($subject, $text, $actions); + $this->sendNotification($subject, $text, $actions, $showTextIfNotEmail); } protected static function _cleanInstallDir($path) diff --git a/app/Jobs/DownloadBase.php b/app/Jobs/DownloadBase.php index 51ba61038..28015d768 100644 --- a/app/Jobs/DownloadBase.php +++ b/app/Jobs/DownloadBase.php @@ -54,19 +54,17 @@ class DownloadBase extends Base public function sendNotification($subject, $text, $actions = []) { - if ($this->getUser()->id == 5) { - $this->getUser()->notify(new DownloadReady($subject, $text, $actions)); - return; - } - - if ($this->getUser()->slack) { - if ($this->sendSlack($subject, $text, $actions)) { - return; - } - } - - // Fallback email if - $this->sendEmail($subject, $text, $actions); + $this->getUser()->notify(new DownloadReady($subject, $text, $actions)); +// +// +// if ($this->getUser()->slack) { +// if ($this->sendSlack($subject, $text, $actions)) { +// return; +// } +// } +// +// // Fallback email if +// $this->sendEmail($subject, $text, $actions); } public function sendSlack($subject, $text, $actions = []) @@ -131,6 +129,7 @@ class DownloadBase extends Base $subject = __($this->_subject, $translateVariables); $text = __($this->_text, $translateVariables); $actions = [__('Télécharger') => $url]; + $showTextIfNotEmail = false; try { if ($this->action === 'scormcloud') { $scormURL = ScormCloud::send($url, 'toolbox_' . $this->type . '_' . $this->_id()); @@ -142,10 +141,11 @@ class DownloadBase extends Base } catch (\Exception $e) { $subject = __('Erreur lors de la compilation du :type :nb', ['nb' => $this->_id(), 'type' => $this->type]); $text = __('Détails de l\'erreur :message', ['message' => $e->getMessage() . ' at line ' . $e->getLine() . ' of ' . $e->getFile()]); + $showTextIfNotEmail = true; $actions = []; } - $this->sendNotification($subject, $text, $actions); + $this->sendNotification($subject, $text, $actions, $showTextIfNotEmail); } protected function _getTranslateVariables() diff --git a/app/Jobs/FluidbookCollectionDownload.php b/app/Jobs/FluidbookCollectionDownload.php index 7bdb873d2..7713aada9 100644 --- a/app/Jobs/FluidbookCollectionDownload.php +++ b/app/Jobs/FluidbookCollectionDownload.php @@ -30,7 +30,7 @@ class FluidbookCollectionDownload extends DownloadBase $this->sendNotification(__('Collection ":title" (#:nb) installée sur le serveur hosting', $translateVariables), '', $url); } else if ($this->action === 'export' && in_array($this->entry->version, ['win_inss_html', 'win_ins_html'])) { $url = $this->downloadList($this->entry->getPageData()); - $this->sendNotification(__($this->_subject, $translateVariables), __($this->_text,$translateVariables), $url); + $this->sendNotification(__($this->_subject, $translateVariables), __($this->_text, $translateVariables), $url); } else { parent::handle(); } diff --git a/app/Notifications/DownloadReady.php b/app/Notifications/DownloadReady.php index 34e8f4189..0ca9018c7 100644 --- a/app/Notifications/DownloadReady.php +++ b/app/Notifications/DownloadReady.php @@ -15,18 +15,19 @@ class DownloadReady extends Notification protected $text; protected $actions = []; protected $error = false; + protected $showTextIfNotEmail = false; /** * Create a new notification instance. * * @return void */ - public function __construct($subject = '', $text = '', $actions = [], $error = false) + public function __construct($subject = '', $text = '', $actions = [], $showTextIfNotEmail = false) { $this->subject = $subject; $this->text = $text; $this->actions = $actions; - $this->error = $error; + $this->showTextIfNotEmail = $showTextIfNotEmail; } /** @@ -37,12 +38,13 @@ class DownloadReady extends Notification */ public function via($notifiable) { - return ['database', 'mail']; if ($notifiable->slack) { + if ($notifiable->id == 5) { + return ['database', FluidbookslackChannel::class, 'mail']; + } return ['database', FluidbookslackChannel::class]; - } else { - return ['database', 'mail']; } + return ['database', 'mail']; } /** @@ -62,11 +64,6 @@ class DownloadReady extends Notification $m->line(new HtmlString($label . __(': ') . ' ' . $url . '')); } $m->salutation(new HtmlString(__('Cordialement,') . "
" . __('L\'équipe Fluidbook'))); - if ($this->error) { - $m->error(); - } else { - $m->success(); - } return $m; } @@ -79,10 +76,25 @@ class DownloadReady extends Notification */ public function toArray($notifiable) { + return [ 'subject' => $this->subject, 'text' => $this->text, 'actions' => $this->actions, ]; } + + public function toSlack($notifiable) + { + return [ + 'subject' => $this->subject, + 'text' => $this->showTextIfNotEmail ? $this->text : '', + 'actions' => $this->actions, + ]; + } + + public function toDatabase($notifiable) + { + return $this->toSlack($notifiable); + } } diff --git a/app/Notifications/FluidbookslackChannel.php b/app/Notifications/FluidbookslackChannel.php index 0fa714e06..34b94a842 100644 --- a/app/Notifications/FluidbookslackChannel.php +++ b/app/Notifications/FluidbookslackChannel.php @@ -4,6 +4,7 @@ namespace App\Notifications; use App\Slack\Slack; use Illuminate\Notifications\Notification; +use Illuminate\Support\Facades\Log; class FluidbookslackChannel { @@ -16,7 +17,7 @@ class FluidbookslackChannel */ public function send($notifiable, Notification $notification) { - $message = $notification->toArray($notifiable); + $message = $notification->toSlack($notifiable); Slack::send($notifiable->slack, $message['subject'], $message['text'], $message['actions']); } } -- 2.39.5