From c13e7afda47d4781ceb145c9058dbfd04255b363 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 29 Aug 2024 18:11:53 +0200 Subject: [PATCH] wait #7016 @2 --- app/Console/Commands/MattermostPost.php | 4 +- .../FluidbookQuote/AssignOperation.php | 6 +- .../FluidbookQuote/CreateFromWebsite.php | 1 - .../FluidbookQuote/SpamOperation.php | 3 +- app/Jobs/WebflowPublish.php | 2 +- app/Slack/Mattermost.php | 45 +++-- composer.json | 6 +- composer.lock | 187 ++++++++++++------ 8 files changed, 177 insertions(+), 77 deletions(-) diff --git a/app/Console/Commands/MattermostPost.php b/app/Console/Commands/MattermostPost.php index 2123e6669..46c5c6ee5 100644 --- a/app/Console/Commands/MattermostPost.php +++ b/app/Console/Commands/MattermostPost.php @@ -13,7 +13,9 @@ class MattermostPost extends ToolboxCommand public function handle() { + $id = User::withoutGlobalScopes()->find(5)->mattermost; - Mattermost::send($id, 'Test subject', 'Test Text', ['test' => ['url' => 'https://example.com']]); + $postid = Mattermost::send($id, 'Test subject', 'Test Text', ['test' => ['url' => 'https://example.com']]); + Mattermost::addReaction($postid, 'white_check_mark'); } } diff --git a/app/Http/Controllers/Admin/Operations/FluidbookQuote/AssignOperation.php b/app/Http/Controllers/Admin/Operations/FluidbookQuote/AssignOperation.php index 52691b124..b841b73c0 100644 --- a/app/Http/Controllers/Admin/Operations/FluidbookQuote/AssignOperation.php +++ b/app/Http/Controllers/Admin/Operations/FluidbookQuote/AssignOperation.php @@ -6,11 +6,13 @@ use App\Mail\FluidbookQuoteAssign; use App\Mail\FluidbookQuoteReceived; use App\Models\FluidbookQuote; use App\Models\User; +use App\Slack\Mattermost; use App\Slack\Slack; use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\Route; use Prologue\Alerts\Facades\Alert; + // __('!! Demandes de devis fluidbook') trait AssignOperation { @@ -46,8 +48,8 @@ trait AssignOperation } Alert::add('success', __('La demande de devis a été attribuée'))->flash(); - if($quote->slack_ts){ - Slack::addReaction(Slack::fluidbookQuoteChannel,$quote->slack_ts,'white_check_mark'); + if ($quote->slack_ts) { + Mattermost::addReaction($quote->slack_ts, 'white_check_mark'); } return Redirect::to(session('_previous', ['url' => backpack_url('fluidbook-quote')])['url']); diff --git a/app/Http/Controllers/Admin/Operations/FluidbookQuote/CreateFromWebsite.php b/app/Http/Controllers/Admin/Operations/FluidbookQuote/CreateFromWebsite.php index e9a18977a..7e95d791a 100644 --- a/app/Http/Controllers/Admin/Operations/FluidbookQuote/CreateFromWebsite.php +++ b/app/Http/Controllers/Admin/Operations/FluidbookQuote/CreateFromWebsite.php @@ -104,7 +104,6 @@ trait CreateFromWebsite } $resp['message'] = __("Votre demande de devis a bien été envoyée. Merci pour l'intérêt que vous portez à Fluidbook.", [], $post['lang']); - if ($post && !$spam) { try { $quote->slack_ts = Mattermost::send(Mattermost::fluidbookQuoteChannel, 'Demande de devis', FluidbookQuoteReceived::getMarkdown($post), diff --git a/app/Http/Controllers/Admin/Operations/FluidbookQuote/SpamOperation.php b/app/Http/Controllers/Admin/Operations/FluidbookQuote/SpamOperation.php index 5b9779f87..7acc55406 100644 --- a/app/Http/Controllers/Admin/Operations/FluidbookQuote/SpamOperation.php +++ b/app/Http/Controllers/Admin/Operations/FluidbookQuote/SpamOperation.php @@ -6,6 +6,7 @@ use App\Mail\FluidbookQuoteAssign; use App\Mail\FluidbookQuoteReceived; use App\Models\FluidbookQuote; use App\Models\User; +use App\Slack\Mattermost; use App\Slack\Slack; use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Redirect; @@ -30,7 +31,7 @@ trait SpamOperation $quote->deleteOrFail(); if ($slack_ts) { - Slack::deleteMessage(Slack::fluidbookQuoteChannel, $slack_ts); + Mattermost::deleteMessage($slack_ts); } Alert::add('success', __('La demande de devis a été supprimée'))->flash(); diff --git a/app/Jobs/WebflowPublish.php b/app/Jobs/WebflowPublish.php index 39dadf9f6..58e49a1b4 100644 --- a/app/Jobs/WebflowPublish.php +++ b/app/Jobs/WebflowPublish.php @@ -68,7 +68,7 @@ class WebflowPublish extends Base } if ($notify && isset($text)) { if ($this->user === null) { - Mattermost::send($wf->mattermost, $subject, $text, $actions, false); + Mattermost::send($wf->slack, $subject, $text, $actions, false); } else { $this->user->notify(new ToolboxNotification($subject, $text, $actions, true)); } diff --git a/app/Slack/Mattermost.php b/app/Slack/Mattermost.php index e68308cb9..dc2c8eab1 100644 --- a/app/Slack/Mattermost.php +++ b/app/Slack/Mattermost.php @@ -2,6 +2,8 @@ namespace App\Slack; +use Cubist\Backpack\Facades\App; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Log; use Pnz\MattermostClient\ApiClient; use Pnz\MattermostClient\HttpClientConfigurator; @@ -15,20 +17,19 @@ class Mattermost const fluidbookQuoteChannel = 'tswc7buu1bf9ixdccfqmo36s5r'; const fluidbookPreviewAlertsChannel = 'y1z7rdtppbg1jr91o8f6prcbcc'; - const fluidbookFilesChannel='6sgtn7rrw3buz8fqftm936oqer'; + const fluidbookFilesChannel = '6sgtn7rrw3buz8fqftm936oqer'; + const endpoint = 'https://mm.cubedesigners.com/api/v4'; + const email = 'vincent+toolbox@cubedesigners.com'; + const password = 'HtMYveJ3%8HMnWooXsm9'; + const team = 'cubedesigners'; 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); + ->setEndpoint(self::endpoint) + ->setCredentials(self::email, self::password); self::$_client = ApiClient::configure($configurator); } return self::$_client; @@ -36,7 +37,6 @@ class Mattermost 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"; @@ -80,6 +80,7 @@ class Mattermost 'channel_id' => $channel, 'props' => $props, ]); + return $response->getId(); } catch (\Exception $e) { Log::critical($e); Log::critical($e->getMessage() . ' // ' . $from . ' // ' . $channel . ' // ' . $text . ' // ' . $markdown); @@ -92,8 +93,30 @@ class Mattermost return self::_client()->posts()->deletePost($postId)->getStatus(); } - public static function addReaction($channel, $ts, $reaction) + public static function addReaction($postId, $emoji, $userId = null) + { + if (null === $userId) { + $userId = self::getUserIdByEmail(self::email); + } + return self::_client()->posts()->addReaction(['post_id' => $postId, 'emoji_name' => $emoji, 'user_id' => $userId]); + } + + public static function getUserIdByEmail($email) { - return self::_client()->reactionsAdd(['channel' => $channel, 'timestamp' => $ts, 'name' => $reaction]); + return Cache::remember('mm_user_by_email_' . $email, 7200, function () use ($email) { + return self::_client()->users()->getUserByEmail($email)->getId(); + }); } + + public static function getChannelIdByName($channel, $teamName = null) + { + if ($teamName === null) { + $teamName = self::team; + } + return Cache::remember('mm_channel_id_by_name_' . $channel . '_' . $teamName, 7200, function () use ($channel, $teamName) { + return self::_client()->channels()->getChannelByNameAndTeamName($teamName, $channel)->getId(); + }); + } + + } diff --git a/composer.json b/composer.json index 0469e47d8..42e3b64c2 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,10 @@ { "type": "git", "url": "https://github.com/enhydrav/standalone-blade.git" + }, + { + "type": "git", + "url": "https://github.com/enhydrav/mattermostclient.git" } ], "license": "MIT", @@ -55,7 +59,7 @@ "php-ffmpeg/php-ffmpeg": "^v1.2.0", "php-http/curl-client": "^2.3", "php-http/message": "^1.16", - "pnz/mattermost-client": "2.x-dev", + "pnz/mattermost-client": "dev-master", "rickselby/laravel-gate-cache": "^3.7", "rodneyrehm/plist": "^2.0", "rustici-software/scormcloud-api-v2-client-php": "^2.1.0", diff --git a/composer.lock b/composer.lock index fdfa546e1..e9d2f6dfe 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "534e3e6f2014dd23ae4c7ad3f482c968", + "content-hash": "9aabb63950a197c861594703ffd8b56d", "packages": [ { "name": "archtechx/enums", @@ -7200,16 +7200,16 @@ }, { "name": "norkunas/youtube-dl-php", - "version": "v2.6.0", + "version": "v2.7.0", "source": { "type": "git", "url": "https://github.com/norkunas/youtube-dl-php.git", - "reference": "c02b9c45a76e0d1978136caa261fcc17d3d91b61" + "reference": "c0ef03ace6ab794ef94ea72e4b083d608d64b754" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/norkunas/youtube-dl-php/zipball/c02b9c45a76e0d1978136caa261fcc17d3d91b61", - "reference": "c02b9c45a76e0d1978136caa261fcc17d3d91b61", + "url": "https://api.github.com/repos/norkunas/youtube-dl-php/zipball/c0ef03ace6ab794ef94ea72e4b083d608d64b754", + "reference": "c0ef03ace6ab794ef94ea72e4b083d608d64b754", "shasum": "" }, "require": { @@ -7221,11 +7221,11 @@ }, "require-dev": { "mikey179/vfsstream": "^1.6.11", - "php-cs-fixer/shim": "^3.49", - "phpstan/phpstan": "^1.10.57", - "phpstan/phpstan-phpunit": "^1.3.15", - "phpstan/phpstan-strict-rules": "^1.5.2", - "symfony/phpunit-bridge": "^6.4.3" + "php-cs-fixer/shim": "^3.60", + "phpstan/phpstan": "^1.11.8", + "phpstan/phpstan-phpunit": "^1.4.0", + "phpstan/phpstan-strict-rules": "^1.6.0", + "symfony/phpunit-bridge": "^6.4.10" }, "type": "library", "extra": { @@ -7256,9 +7256,9 @@ ], "support": { "issues": "https://github.com/norkunas/youtube-dl-php/issues", - "source": "https://github.com/norkunas/youtube-dl-php/tree/v2.6.0" + "source": "https://github.com/norkunas/youtube-dl-php/tree/v2.7.0" }, - "time": "2024-02-28T05:14:01+00:00" + "time": "2024-08-28T08:22:20+00:00" }, { "name": "nothingworks/blade-svg", @@ -8482,48 +8482,121 @@ "time": "2024-07-20T21:41:07+00:00" }, { - "name": "pnz/mattermost-client", - "version": "2.x-dev", + "name": "pnz/json-exception", + "version": "v1.0.1", "source": { "type": "git", - "url": "https://github.com/thePanz/MattermostClient.git", - "reference": "74c14f78bce005131da44d293d45cbd04aaedb3d" + "url": "https://github.com/thePanz/json-exception.git", + "reference": "cce24ccdb59f6eecb6cb45dbdc8695808f6d64d7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/thePanz/MattermostClient/zipball/74c14f78bce005131da44d293d45cbd04aaedb3d", - "reference": "74c14f78bce005131da44d293d45cbd04aaedb3d", + "url": "https://api.github.com/repos/thePanz/json-exception/zipball/cce24ccdb59f6eecb6cb45dbdc8695808f6d64d7", + "reference": "cce24ccdb59f6eecb6cb45dbdc8695808f6d64d7", "shasum": "" }, "require": { "ext-json": "*", - "php": "^8.1", + "php": "^7.0 || ^8.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "psr-4": { + "Pnz\\JsonException\\": "src/" + }, + "classmap": [ + "src/stubs" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GPL-3.0" + ], + "description": "Json decode/encode library with exception throwing on error.", + "keywords": [ + "json" + ], + "support": { + "issues": "https://github.com/thePanz/json-exception/issues", + "source": "https://github.com/thePanz/json-exception/tree/v1.0.1" + }, + "time": "2021-08-18T13:32:12+00:00" + }, + { + "name": "pnz/mattermost-client", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/enhydrav/mattermostclient.git", + "reference": "2562057e91f255cace2d304af6ac4c61740d74bb" + }, + "require": { + "ext-json": "*", + "php": "^7.4 || ^8.0", "php-http/client-common": "^2.0", - "php-http/discovery": "^1.17", - "php-http/httplug": "^2.1", + "php-http/discovery": "^1.7", + "php-http/httplug": "^2.0", "php-http/multipart-stream-builder": "^1.1", - "psr/http-client-implementation": "*", - "psr/http-factory-implementation": "*", - "psr/http-message": "^1.0" + "pnz/json-exception": "^1.0" }, "require-dev": { - "nyholm/psr7": "^1.8", - "php-cs-fixer/shim": "^v3.34", - "php-http/curl-client": "^2.3", - "php-http/mock-client": "^1.6", - "phpstan/extension-installer": "^1.3", - "phpstan/phpstan": "^1.10", - "phpstan/phpstan-phpunit": "^1.3", - "phpunit/phpunit": "^10.5", - "rector/rector": "^0.18.12" + "nyholm/psr7": "^1.2.1", + "php-http/curl-client": "^2.0", + "php-http/message": "^1.8", + "php-http/message-factory": "^1.0", + "phpstan/extension-installer": "^1.0", + "phpstan/phpstan-phpunit": "^0.11.2", + "phpstan/phpstan-shim": "^0.11", + "phpunit/phpunit": "^7.5", + "psr/http-message": "^1.0.1" }, + "default-branch": true, "type": "library", + "extra": { + "branch-alias": { + "dev-master": "0.1.x-dev" + } + }, "autoload": { "psr-4": { "Pnz\\MattermostClient\\": "src/" } }, - "notification-url": "https://packagist.org/downloads/", + "autoload-dev": { + "psr-4": { + "Pnz\\MattermostClient\\Tests\\": "tests/" + } + }, + "scripts": { + "ci": [ + "@phpcs", + "@phpstan", + "@test-ci" + ], + "phpstan": [ + "vendor/bin/phpstan analyze --level=7 src/ tests/" + ], + "phpcs": [ + "vendor/bin/php-cs-fixer fix --dry-run --verbose --ansi" + ], + "phpcs-install": [ + "curl -L https://cs.symfony.com/download/php-cs-fixer-v2.phar -o vendor/bin/php-cs-fixer && chmod u+x vendor/bin/php-cs-fixer" + ], + "phpcs-fix": [ + "vendor/bin/php-cs-fixer fix --verbose --ansi" + ], + "test": [ + "vendor/bin/phpunit" + ], + "test-ci": [ + "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml" + ] + }, "license": [ "MIT" ], @@ -8536,14 +8609,10 @@ ], "description": "Mattermost APi Client for PHP", "keywords": [ - "Mattermost", - "api" + "api", + "mattermost" ], - "support": { - "issues": "https://github.com/thePanz/MattermostClient/issues", - "source": "https://github.com/thePanz/MattermostClient/tree/2.x" - }, - "time": "2023-12-20T14:23:35+00:00" + "time": "2023-09-07T09:16:48+00:00" }, { "name": "prologue/alerts", @@ -10153,16 +10222,16 @@ }, { "name": "spatie/laravel-package-tools", - "version": "1.16.4", + "version": "1.16.5", "source": { "type": "git", "url": "https://github.com/spatie/laravel-package-tools.git", - "reference": "ddf678e78d7f8b17e5cdd99c0c3413a4a6592e53" + "reference": "c7413972cf22ffdff97b68499c22baa04eddb6a2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/spatie/laravel-package-tools/zipball/ddf678e78d7f8b17e5cdd99c0c3413a4a6592e53", - "reference": "ddf678e78d7f8b17e5cdd99c0c3413a4a6592e53", + "url": "https://api.github.com/repos/spatie/laravel-package-tools/zipball/c7413972cf22ffdff97b68499c22baa04eddb6a2", + "reference": "c7413972cf22ffdff97b68499c22baa04eddb6a2", "shasum": "" }, "require": { @@ -10201,7 +10270,7 @@ ], "support": { "issues": "https://github.com/spatie/laravel-package-tools/issues", - "source": "https://github.com/spatie/laravel-package-tools/tree/1.16.4" + "source": "https://github.com/spatie/laravel-package-tools/tree/1.16.5" }, "funding": [ { @@ -10209,7 +10278,7 @@ "type": "github" } ], - "time": "2024-03-20T07:29:11+00:00" + "time": "2024-08-27T18:56:10+00:00" }, { "name": "spatie/laravel-permission", @@ -15030,16 +15099,16 @@ }, { "name": "composer/pcre", - "version": "3.3.0", + "version": "3.3.1", "source": { "type": "git", "url": "https://github.com/composer/pcre.git", - "reference": "1637e067347a0c40bbb1e3cd786b20dcab556a81" + "reference": "63aaeac21d7e775ff9bc9d45021e1745c97521c4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/composer/pcre/zipball/1637e067347a0c40bbb1e3cd786b20dcab556a81", - "reference": "1637e067347a0c40bbb1e3cd786b20dcab556a81", + "url": "https://api.github.com/repos/composer/pcre/zipball/63aaeac21d7e775ff9bc9d45021e1745c97521c4", + "reference": "63aaeac21d7e775ff9bc9d45021e1745c97521c4", "shasum": "" }, "require": { @@ -15089,7 +15158,7 @@ ], "support": { "issues": "https://github.com/composer/pcre/issues", - "source": "https://github.com/composer/pcre/tree/3.3.0" + "source": "https://github.com/composer/pcre/tree/3.3.1" }, "funding": [ { @@ -15105,7 +15174,7 @@ "type": "tidelift" } ], - "time": "2024-08-19T19:43:53+00:00" + "time": "2024-08-27T18:44:43+00:00" }, { "name": "doctrine/instantiator", @@ -15885,16 +15954,16 @@ }, { "name": "phpstan/phpdoc-parser", - "version": "1.29.1", + "version": "1.30.0", "source": { "type": "git", "url": "https://github.com/phpstan/phpdoc-parser.git", - "reference": "fcaefacf2d5c417e928405b71b400d4ce10daaf4" + "reference": "5ceb0e384997db59f38774bf79c2a6134252c08f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/fcaefacf2d5c417e928405b71b400d4ce10daaf4", - "reference": "fcaefacf2d5c417e928405b71b400d4ce10daaf4", + "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/5ceb0e384997db59f38774bf79c2a6134252c08f", + "reference": "5ceb0e384997db59f38774bf79c2a6134252c08f", "shasum": "" }, "require": { @@ -15926,9 +15995,9 @@ "description": "PHPDoc parser with support for nullable, intersection and generic types", "support": { "issues": "https://github.com/phpstan/phpdoc-parser/issues", - "source": "https://github.com/phpstan/phpdoc-parser/tree/1.29.1" + "source": "https://github.com/phpstan/phpdoc-parser/tree/1.30.0" }, - "time": "2024-05-31T08:52:43+00:00" + "time": "2024-08-29T09:54:52+00:00" }, { "name": "phpunit/php-code-coverage", -- 2.39.5