]> _ Git - fluidbook-toolbox.git/commitdiff
wip #7016 @3
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 29 Aug 2024 12:38:59 +0000 (14:38 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 29 Aug 2024 12:38:59 +0000 (14:38 +0200)
13 files changed:
app/Console/Commands/FluidbookQuote.php
app/Console/Commands/MattermostPost.php [new file with mode: 0644]
app/Fluidbook/Link/Custom/IntexLink.php
app/Fluidbook/Link/Custom/PierronLink.php
app/Http/Controllers/Admin/Operations/Files/UploadOperation.php
app/Http/Controllers/Admin/Operations/FluidbookQuote/CreateFromWebsite.php
app/Jobs/DownloadBase.php
app/Jobs/WebflowPublish.php
app/Models/FluidbookCollection.php
app/Models/FluidbookPublication.php
app/Notifications/FluidbookmattermostChannel.php [new file with mode: 0644]
app/Notifications/ToolboxNotification.php
app/Slack/Mattermost.php [new file with mode: 0644]

index 4b91008d7b4e3a1011a35d2e77ec9b5034b24ae6..2f7cffcde8d1f9505c992f088c98a3ba49bd2522 100644 (file)
@@ -3,6 +3,7 @@
 namespace App\Console\Commands;
 
 use App\Console\Commands\Base\ToolboxCommand;
+use App\Slack\Mattermost;
 use App\Slack\Slack;
 use Cubist\Backpack\Facades\App;
 
@@ -23,7 +24,7 @@ class FluidbookQuote extends ToolboxCommand
         App::setLocale('fr');
         $nottreated = \App\Models\FluidbookQuote::where('status', '0')->count();
         if ($nottreated > 0) {
-            Slack::send(Slack::fluidbookQuoteChannel,
+            Mattermost::send(Mattermost::fluidbookQuoteChannel,
                 ':alarm_clock: ' . __('Des demandes de devis n\'ont pas été traitées'),
                 __(':awaiting demandes de devis doivent être confiées à un revendeur ou un chef de projet', ['awaiting' => $nottreated]),
                 [__('Toutes les demandes') => ['url' => backpack_url('fluidbook-quote?status=0')]],
diff --git a/app/Console/Commands/MattermostPost.php b/app/Console/Commands/MattermostPost.php
new file mode 100644 (file)
index 0000000..2123e66
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Console\Commands\Base\ToolboxCommand;
+use App\Models\Quiz;
+use App\Models\User;
+use App\Slack\Mattermost;
+
+class MattermostPost extends ToolboxCommand
+{
+    protected $signature = 'mattermost:post';
+
+    public function handle()
+    {
+        $id = User::withoutGlobalScopes()->find(5)->mattermost;
+        Mattermost::send($id, 'Test subject', 'Test Text', ['test' => ['url' => 'https://example.com']]);
+    }
+}
index 8010e2e449a50e179226ada3a0c8b23c1caad739..fc6e113bf2e737b241d83eab0cc591482723e01c 100644 (file)
@@ -8,15 +8,15 @@ class IntexLink extends CustomLink
 {
     public function ignore()
     {
-        if (!static::_getURL($this->to,'intex', false)) {
+        if (!static::_getURL($this->to, 'intex', false)) {
             return true;
         }
         return parent::ignore();
     }
 
-    public static function _getURL($to)
+    public static function _getURL($to, $type = '', $default = null)
     {
-        return static::_getURL( $to,'intex');
+        return static::_getURL($to, 'intex');
     }
 
     public function getDefaultTooltip()
index 70f18ae14186088a47a4922ab83bdd93d8638fb8..d50241a2af33c5360040786e7698cae47c35c7cc 100644 (file)
@@ -6,7 +6,7 @@ use Fluidbook\Tools\Links\CustomLink;
 
 class PierronLink extends CustomLink
 {
-    public static function _getURL($to)
+    public static function _getURL($to,$type='',$default=null)
     {
         return 'https://workshop.fluidbook.com/services/pierronRef?ref=' . $to;
     }
index d6cc9e4c2042fdb8b4bbeb2e5cf61d29f571542c..db1dff4e26b8fc58ff66a758b01be80415f1561c 100644 (file)
@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin\Operations\Files;
 
 use App\Models\File;
 use App\Models\User;
+use App\Slack\Mattermost;
 use App\Slack\Slack;
 use Cubist\Util\Files\Files;
 use Illuminate\Http\UploadedFile;
@@ -58,17 +59,17 @@ trait UploadOperation
                 /** @var User $destUser */
                 $destUser = User::withoutGlobalScopes()->find($user);
                 $subject = __(':nb fichiers chargés pour ' . $destUser->nameWithCompany, ['nb' => $j]);
-                $destChannel = backpack_user()->slack;
+                $destChannel = backpack_user()->mattermost;
             } else {
                 $subject = __(':nb fichiers chargés par ' . backpack_user()->nameWithCompany, ['nb' => $j]);
-                $destChannel = Slack::fluidbookFilesChannel;
+                $destChannel = Mattermost::fluidbookFilesChannel;
             }
             $actions = [];
             foreach ($paths as $path) {
                 $spl = new \SplFileInfo($path);
                 $actions[$spl->getFilename()] = backpack_url('/file/' . File::hash($path, $spl->getSize()) . '/' . $spl->getFilename());
             }
-            Slack::send($destChannel, $subject, '', $actions);
+            Mattermost::send($destChannel, $subject, '', $actions);
             Alert::success(__(':nb fichiers chargés', ['nb' => '<b>' . $j . '</b>']))->flash();
         }
         return redirect($this->crud->route);
index dd63eacdc0f69aad83fd59e29ce13012187e132e..e9a18977aeb04b2ebc53999b52dfad3492ca4c87 100644 (file)
@@ -10,6 +10,7 @@ use App\Mail\FluidbookQuoteReceived;
 use App\Models\Company;
 use App\Models\FluidbookQuote;
 use App\Models\AuthUser;
+use App\Slack\Mattermost;
 use App\Slack\Slack;
 use Cubist\Backpack\Middleware\CORSMiddleware;
 use Cubist\Util\ArrayUtil;
@@ -106,7 +107,7 @@ trait CreateFromWebsite
 
             if ($post && !$spam) {
                 try {
-                    $quote->slack_ts = Slack::send(Slack::fluidbookQuoteChannel, 'Demande de devis', FluidbookQuoteReceived::getMarkdown($post),
+                    $quote->slack_ts = Mattermost::send(Mattermost::fluidbookQuoteChannel, 'Demande de devis', FluidbookQuoteReceived::getMarkdown($post),
                         ['Je vais voir' => backpack_url('fluidbook-quote', ['status' => 0]),
                             'Je m\'en charge' => ['url' => backpack_url('fluidbook-quote/' . $quote->id . '/assign/me'), 'style' => 'primary'],
                             'C\'est du SPAM' => ['url' => backpack_url('fluidbook-quote/' . $quote->id . '/spam'), 'style' => 'danger'],
@@ -136,7 +137,7 @@ trait CreateFromWebsite
             $data = unserialize($d64);
         }
         if (!$data) {
-            Slack::send(Slack::fluidbookQuoteChannel, __('Erreur envoi demande de devis'), $request->get('data') . "\n\n" . utf8_decode($d64));
+            Mattermost::send(Mattermost::fluidbookQuoteChannel, __('Erreur envoi demande de devis'), $request->get('data') . "\n\n" . utf8_decode($d64));
             return false;
         }
 
index f88a010f9e570e54de825fc50b85f11ce4610b20..060604d4210c3ac1c1341fd6b941f5e2f7d7fdc7 100644 (file)
@@ -6,6 +6,7 @@ use App\Mail\DeferredDownload;
 use App\Models\User;
 use App\Notifications\DownloadReady;
 use App\Services\ScormCloud;
+use App\Slack\Mattermost;
 use App\Slack\Slack;
 use Cubist\Backpack\Facades\App;
 use Cubist\Backpack\Magic\Models\CubistMagicAbstractModel;
@@ -51,7 +52,7 @@ class DownloadBase extends Base
 
     public function sendSlack($subject, $text, $actions = [])
     {
-        return Slack::send($this->getUser()->slack, $subject, $text, $actions);
+        return Mattermost::send($this->getUser()->mattermost, $subject, $text, $actions);
     }
 
     /**
index b754472eaf37180496a4a3cb4d9d0adbf2a12c90..39dadf9f606ec20dc5ceda3b3a3daefc59ce8ec6 100644 (file)
@@ -5,6 +5,7 @@ namespace App\Jobs;
 use App\Models\ToolWebflow;
 use App\Models\User;
 use App\Notifications\ToolboxNotification;
+use App\Slack\Mattermost;
 use App\Slack\Slack;
 
 class WebflowPublish extends Base
@@ -67,7 +68,7 @@ class WebflowPublish extends Base
         }
         if ($notify && isset($text)) {
             if ($this->user === null) {
-                Slack::send($wf->slack, $subject, $text, $actions, false);
+                Mattermost::send($wf->mattermost, $subject, $text, $actions, false);
             } else {
                 $this->user->notify(new ToolboxNotification($subject, $text, $actions, true));
             }
index 170d01bc5c1ca0b5faa02af30b2813cc29657a5c..a3fe25dd1623ca7db57626cdfa0aa8e83909f120 100644 (file)
@@ -13,6 +13,7 @@ use App\Models\Base\ToolboxDownloadable;
 use App\Jobs\RefreshUsersTree;
 use App\Models\Base\ToolboxStatusModel;
 use App\Models\Traits\CheckHash;
+use App\Slack\Mattermost;
 use App\Slack\Slack;
 use App\SubForms\CollectionPublication;
 use Cubist\Backpack\Magic\Fields\BunchOfFieldsMultiple;
@@ -130,7 +131,7 @@ class FluidbookCollection extends ToolboxStatusModel
                 $owner = \App\Models\User::withoutGlobalScopes()->find($this->owner);
                 $actions = [];
 
-                Slack::send(Slack::fluidbookPreviewAlertsChannel,
+                Mattermost::send(Mattermost::fluidbookPreviewAlertsChannel,
                     'Collection #' . $this->id . ' : ' . $this->visits_counter . ' visites',
                     'Le lien de preview de _' . $this->title . '_ a été utilisé à *' . $this->visits_counter . '* reprises par des visiteurs non authentifiés sur la toolbox. La collection se trouve sur le compte de ' . $owner->getNameWithCompanyAttribute() . '.',
                     $actions,
index f65be64476339f35f9ab85818236f318842403ff..9f4b3e1f7eb7114e61ad724909a2a3bf4938967b 100644 (file)
@@ -953,7 +953,7 @@ class FluidbookPublication extends ToolboxStatusModel
                     $actions['Voir les stats'] = backpack_url('/fluidbook-publication/stats/' . $this->id . '_' . $this->hash . '/');
                 }
 
-                Slack::send(Slack::fluidbookPreviewAlertsChannel,
+                Mattermost::send(Mattermost::fluidbookPreviewAlertsChannel,
                     'Fluidbook #' . $this->id . ' : ' . $this->visits_counter . ' visites',
                     'Le lien de preview de _' . $this->title . '_ a été utilisé à *' . $this->visits_counter . '* reprises par des visiteurs non authentifiés sur la toolbox. Le fluidbook se trouve sur le compte de ' . $owner->getNameWithCompanyAttribute() . '.',
                     $actions,
diff --git a/app/Notifications/FluidbookmattermostChannel.php b/app/Notifications/FluidbookmattermostChannel.php
new file mode 100644 (file)
index 0000000..43d58d4
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+
+namespace App\Notifications;
+
+use App\Slack\Mattermost;
+use Illuminate\Notifications\Notification;
+
+class FluidbookmattermostChannel
+{
+    /**
+     * Send the given notification.
+     *
+     * @param mixed $notifiable
+     * @param \Illuminate\Notifications\Notification $notification
+     * @return void
+     */
+    public function send($notifiable, Notification $notification)
+    {
+        $message = $notification->toSlack($notifiable);
+        Mattermost::send($notifiable->mattermost, $message['subject'], $message['text'], $message['actions']);
+    }
+}
index 11f90ef1de60665c84ff3c16378b7a3b265ce26c..77916686ecddb02f97ff96e02e9dc8eed559dde6 100644 (file)
@@ -40,13 +40,22 @@ class ToolboxNotification extends Notification
      */
     public function via($notifiable)
     {
+        $res = ['database'];
+        $slackOrMM=false;
         if ($notifiable->slack) {
-            if ($notifiable->id == 5) {
-                return ['database', FluidbookslackChannel::class, 'mail', WebPushChannel::class];
-            }
-            return ['database', FluidbookslackChannel::class];
+            $res[] = FluidbookslackChannel::class;
+            $slackOrMM=true;
         }
-        return ['database', 'mail', WebPushChannel::class];
+        if ($notifiable->mattermost) {
+            $res[] = FluidbookmattermostChannel::class;
+            $slackOrMM=true;
+        }
+
+        if(!$slackOrMM || $notifiable->id === 5){
+            $res[] = 'mail';
+        }
+
+        return array_unique($res);
     }
 
     /**
diff --git a/app/Slack/Mattermost.php b/app/Slack/Mattermost.php
new file mode 100644 (file)
index 0000000..e68308c
--- /dev/null
@@ -0,0 +1,99 @@
+<?php
+
+namespace App\Slack;
+
+use Illuminate\Support\Facades\Log;
+use Pnz\MattermostClient\ApiClient;
+use Pnz\MattermostClient\HttpClientConfigurator;
+
+class Mattermost
+{
+    /**
+     * @var null|ApiClient
+     */
+    protected static $_client = null;
+
+    const fluidbookQuoteChannel = 'tswc7buu1bf9ixdccfqmo36s5r';
+    const fluidbookPreviewAlertsChannel = 'y1z7rdtppbg1jr91o8f6prcbcc';
+    const fluidbookFilesChannel='6sgtn7rrw3buz8fqftm936oqer';
+
+
+    protected static function _client(): ApiClient
+    {
+        if (null === self::$_client) {
+
+            $endpoint = 'https://mm.cubedesigners.com/api/v4';
+            $username = 'vincent+toolbox@cubedesigners.com';
+            $password = 'HtMYveJ3%8HMnWooXsm9';
+
+            $configurator = (new HttpClientConfigurator())
+                ->setEndpoint($endpoint)
+                ->setCredentials($username, $password);
+            self::$_client = ApiClient::configure($configurator);
+        }
+        return self::$_client;
+    }
+
+    public static function send($channel, $subject, $text, $actions = [], $repeatActions = true, $from = 'Fluidbook Toolbox')
+    {
+        Log::debug(json_encode(func_get_args()));
+        $markdown = '';
+        if ($text) {
+            $markdown .= $text . "\n";
+        }
+
+        $attachment = ['title' => $subject, 'color' => '#9fc142'];
+        $a = [];
+        $actionsTexts = [];
+        $actionsCount = count($actions);
+        $repeatActions = true;
+        if ($actionsCount > 0) {
+            $i = 0;
+            foreach ($actions as $label => $data) {
+                if (!$data) {
+                    $data = ['url' => ''];
+                }
+                if (is_string($data)) {
+                    $data = ['url' => $data];
+                }
+                if (!$data['url']) {
+                    $data['url'] = backpack_url('404');
+                }
+//                if ($i < 6) {
+//                    $aa = ['id' => 'a_' . $i, 'name' => $label, 'type' => 'link', 'style' => 'error', 'integration' => ['url' => $data['url']]];
+//                    $a[] = $aa;
+//                }
+                if ($repeatActions) {
+                    $markdown .= '* ' . $label . ' : ' . $data['url'] . "\n";
+                }
+                $i++;
+            }
+            $attachment['actions'] = $a;
+
+        }
+        $attachment['text'] = $markdown;
+        $props['attachments'] = [$attachment];
+
+        try {
+
+            $response = self::_client()->posts()->createPost([
+                'channel_id' => $channel,
+                'props' => $props,
+            ]);
+        } catch (\Exception $e) {
+            Log::critical($e);
+            Log::critical($e->getMessage() . ' // ' . $from . ' // ' . $channel . ' // ' . $text . ' // ' . $markdown);
+        }
+        return false;
+    }
+
+    public static function deleteMessage($postId)
+    {
+        return self::_client()->posts()->deletePost($postId)->getStatus();
+    }
+
+    public static function addReaction($channel, $ts, $reaction)
+    {
+        return self::_client()->reactionsAdd(['channel' => $channel, 'timestamp' => $ts, 'name' => $reaction]);
+    }
+}