From 2cc4a369579058b49c4d020435a1ebd0de30d7fd Mon Sep 17 00:00:00 2001 From: faissaloux Date: Mon, 22 Nov 2021 14:11:24 +0100 Subject: [PATCH 01/14] send telegram notification when new article submitted --- .env.example | 3 ++ app/Jobs/CreateArticle.php | 7 +++ app/Notifications/ArticleSubmitted.php | 44 +++++++++++++++++ composer.json | 1 + composer.lock | 66 ++++++++++++++++++++++++++ config/services.php | 4 ++ 6 files changed, 125 insertions(+) create mode 100644 app/Notifications/ArticleSubmitted.php diff --git a/.env.example b/.env.example index be3e47d42..5e2dc5bfa 100644 --- a/.env.example +++ b/.env.example @@ -33,3 +33,6 @@ TWITTER_ACCESS_SECRET= FLARE_KEY= MIX_FLARE_KEY="${FLARE_KEY}" + +TELEGRAM_BOT_TOKEN= +TELEGRAM_LARAVELIO_CHANNEL= diff --git a/app/Jobs/CreateArticle.php b/app/Jobs/CreateArticle.php index d05c030ee..c8f0a3f9e 100644 --- a/app/Jobs/CreateArticle.php +++ b/app/Jobs/CreateArticle.php @@ -5,6 +5,9 @@ use App\Http\Requests\ArticleRequest; use App\Models\Article; use App\Models\User; +use App\Notifications\ArticleSubmitted; +use Illuminate\Notifications\AnonymousNotifiable; +use Illuminate\Support\Facades\App; final class CreateArticle { @@ -56,6 +59,10 @@ public function handle(): Article $article->authoredBy($this->author); $article->syncTags($this->tags); + if (App::environment() !== 'testing'){ + (new AnonymousNotifiable())->notify(new ArticleSubmitted($article)); + } + return $article; } } diff --git a/app/Notifications/ArticleSubmitted.php b/app/Notifications/ArticleSubmitted.php new file mode 100644 index 000000000..15ff80ac6 --- /dev/null +++ b/app/Notifications/ArticleSubmitted.php @@ -0,0 +1,44 @@ +article = $article; + } + + public function via($notifiable) + { + return ['telegram']; + } + + public function toTelegram($notifiable) + { + $url = route('articles.show', $this->article->slug()); + return TelegramMessage::create() + ->to(env("TELEGRAM_LARAVELIO_CHANNEL")) + ->content($this->content()) + ->button('View article', $url); + } + + private function content(): string + { + $content = "*New article submitted!*\n\n"; + $content .= "Title: " . $this->article->title() . "\n"; + $content .= "By: [@" . $this->article->author()->username() . "](" . route('profile', $this->article->author()->username()) . ")"; + + return $content; + } +} diff --git a/composer.json b/composer.json index 0bd83e9c6..fef5a9e4a 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,7 @@ "guzzlehttp/guzzle": "^7.0.1", "intervention/image": "^2.5", "intervention/imagecache": "^2.5", + "laravel-notification-channels/telegram": "^0.8.0", "laravel-notification-channels/twitter": "^5.0", "laravel/framework": "8.66.0", "laravel/horizon": "^5.2", diff --git a/composer.lock b/composer.lock index 2a88c038e..3a743195d 100644 --- a/composer.lock +++ b/composer.lock @@ -2384,6 +2384,72 @@ }, "time": "2017-11-25T19:51:26+00:00" }, + { + "name": "laravel-notification-channels/telegram", + "version": "0.8.0", + "source": { + "type": "git", + "url": "https://github.com/laravel-notification-channels/telegram.git", + "reference": "dd7cee42b1da9a5528abd298a93c309e25a8306f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel-notification-channels/telegram/zipball/dd7cee42b1da9a5528abd298a93c309e25a8306f", + "reference": "dd7cee42b1da9a5528abd298a93c309e25a8306f", + "shasum": "" + }, + "require": { + "ext-json": "*", + "guzzlehttp/guzzle": "^6.2 || ^7.0", + "illuminate/contracts": "^5.5 || ^6.0 || ^7.0 || ^8.0", + "illuminate/notifications": "^5.5 || ^6.0 || ^7.0 || ^8.0", + "illuminate/support": "^5.5 || ^6.0 || ^7.0 || ^8.0", + "php": "^7.2 || ^8.0" + }, + "require-dev": { + "mockery/mockery": "^1.3", + "phpunit/phpunit": "^7.0 || ^8.5.21 || ^9.0" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "NotificationChannels\\Telegram\\TelegramServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "NotificationChannels\\Telegram\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Irfaq Syed", + "email": "syed@lukonet.com", + "homepage": "https://lukonet.com", + "role": "Developer" + } + ], + "description": "Telegram Notifications Channel for Laravel", + "homepage": "https://github.com/laravel-notification-channels/telegram", + "keywords": [ + "laravel", + "notification", + "telegram", + "telegram notification", + "telegram notifications channel" + ], + "support": { + "issues": "https://github.com/laravel-notification-channels/telegram/issues", + "source": "https://github.com/laravel-notification-channels/telegram/tree/0.8.0" + }, + "time": "2021-11-14T01:06:40+00:00" + }, { "name": "laravel-notification-channels/twitter", "version": "v5.1.0", diff --git a/config/services.php b/config/services.php index a59a428cb..d70ff62e1 100644 --- a/config/services.php +++ b/config/services.php @@ -51,4 +51,8 @@ 'access_secret' => env('TWITTER_ACCESS_SECRET'), ], + 'telegram-bot-api' => [ + 'token' => env('TELEGRAM_BOT_TOKEN'), + ], + ]; From 0bcaf84b1dffcd920f80927946f39a4d3703f77b Mon Sep 17 00:00:00 2001 From: faissaloux Date: Mon, 22 Nov 2021 14:17:07 +0100 Subject: [PATCH 02/14] apply StyleCI fixes --- app/Jobs/CreateArticle.php | 2 +- app/Notifications/ArticleSubmitted.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Jobs/CreateArticle.php b/app/Jobs/CreateArticle.php index c8f0a3f9e..a3dea5b01 100644 --- a/app/Jobs/CreateArticle.php +++ b/app/Jobs/CreateArticle.php @@ -59,7 +59,7 @@ public function handle(): Article $article->authoredBy($this->author); $article->syncTags($this->tags); - if (App::environment() !== 'testing'){ + if (App::environment() !== 'testing') { (new AnonymousNotifiable())->notify(new ArticleSubmitted($article)); } diff --git a/app/Notifications/ArticleSubmitted.php b/app/Notifications/ArticleSubmitted.php index 15ff80ac6..aa413cf0b 100644 --- a/app/Notifications/ArticleSubmitted.php +++ b/app/Notifications/ArticleSubmitted.php @@ -16,7 +16,7 @@ class ArticleSubmitted extends Notification implements ShouldQueue public function __construct(Article $article) { - $this->article = $article; + $this->article = $article; } public function via($notifiable) @@ -36,8 +36,8 @@ public function toTelegram($notifiable) private function content(): string { $content = "*New article submitted!*\n\n"; - $content .= "Title: " . $this->article->title() . "\n"; - $content .= "By: [@" . $this->article->author()->username() . "](" . route('profile', $this->article->author()->username()) . ")"; + $content .= 'Title: '.$this->article->title()."\n"; + $content .= 'By: [@'.$this->article->author()->username().']('.route('profile', $this->article->author()->username()).')'; return $content; } From c4f78c10f45ec6ecf699e1d34cd83fede39e2d71 Mon Sep 17 00:00:00 2001 From: faissaloux Date: Mon, 22 Nov 2021 14:18:04 +0100 Subject: [PATCH 03/14] apply StyleCI fixes --- app/Notifications/ArticleSubmitted.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Notifications/ArticleSubmitted.php b/app/Notifications/ArticleSubmitted.php index aa413cf0b..2a8c57523 100644 --- a/app/Notifications/ArticleSubmitted.php +++ b/app/Notifications/ArticleSubmitted.php @@ -28,7 +28,7 @@ public function toTelegram($notifiable) { $url = route('articles.show', $this->article->slug()); return TelegramMessage::create() - ->to(env("TELEGRAM_LARAVELIO_CHANNEL")) + ->to(env('TELEGRAM_LARAVELIO_CHANNEL')) ->content($this->content()) ->button('View article', $url); } From f31f6d52ceea35150a65858e081546bad966da30 Mon Sep 17 00:00:00 2001 From: faissaloux Date: Mon, 22 Nov 2021 14:18:55 +0100 Subject: [PATCH 04/14] apply StyleCI fixes --- app/Notifications/ArticleSubmitted.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Notifications/ArticleSubmitted.php b/app/Notifications/ArticleSubmitted.php index 2a8c57523..fa8a5d167 100644 --- a/app/Notifications/ArticleSubmitted.php +++ b/app/Notifications/ArticleSubmitted.php @@ -27,6 +27,7 @@ public function via($notifiable) public function toTelegram($notifiable) { $url = route('articles.show', $this->article->slug()); + return TelegramMessage::create() ->to(env('TELEGRAM_LARAVELIO_CHANNEL')) ->content($this->content()) From 4c0ba75eb8d39c21d0e9ad937de978807c4b3c34 Mon Sep 17 00:00:00 2001 From: faissaloux Date: Mon, 22 Nov 2021 16:15:17 +0100 Subject: [PATCH 05/14] move telegram credentials to services.php --- .env.example | 2 +- app/Notifications/ArticleSubmitted.php | 2 +- config/services.php | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 5e2dc5bfa..e07d8a7b9 100644 --- a/.env.example +++ b/.env.example @@ -35,4 +35,4 @@ FLARE_KEY= MIX_FLARE_KEY="${FLARE_KEY}" TELEGRAM_BOT_TOKEN= -TELEGRAM_LARAVELIO_CHANNEL= +TELEGRAM_CHANNEL= diff --git a/app/Notifications/ArticleSubmitted.php b/app/Notifications/ArticleSubmitted.php index fa8a5d167..368c70a3a 100644 --- a/app/Notifications/ArticleSubmitted.php +++ b/app/Notifications/ArticleSubmitted.php @@ -29,7 +29,7 @@ public function toTelegram($notifiable) $url = route('articles.show', $this->article->slug()); return TelegramMessage::create() - ->to(env('TELEGRAM_LARAVELIO_CHANNEL')) + ->to(config('services.telegram-bot-api.channel')) ->content($this->content()) ->button('View article', $url); } diff --git a/config/services.php b/config/services.php index d70ff62e1..586e80d8b 100644 --- a/config/services.php +++ b/config/services.php @@ -53,6 +53,7 @@ 'telegram-bot-api' => [ 'token' => env('TELEGRAM_BOT_TOKEN'), + 'channel' => env('TELEGRAM_CHANNEL'), ], ]; From 6076c245af125c69dd0c2e4f208ef28442744b9f Mon Sep 17 00:00:00 2001 From: faissaloux Date: Mon, 22 Nov 2021 16:29:19 +0100 Subject: [PATCH 06/14] ArticleWasCreated event --- app/Events/ArticleWasCreated.php | 18 ++++++++++++++++++ app/Jobs/CreateArticle.php | 5 ++--- .../SendTelegramNewArticleNotification.php | 15 +++++++++++++++ app/Providers/EventServiceProvider.php | 5 +++++ 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 app/Events/ArticleWasCreated.php create mode 100644 app/Listeners/SendTelegramNewArticleNotification.php diff --git a/app/Events/ArticleWasCreated.php b/app/Events/ArticleWasCreated.php new file mode 100644 index 000000000..10332aecd --- /dev/null +++ b/app/Events/ArticleWasCreated.php @@ -0,0 +1,18 @@ +article = $article; + } +} diff --git a/app/Jobs/CreateArticle.php b/app/Jobs/CreateArticle.php index a3dea5b01..cbab507f2 100644 --- a/app/Jobs/CreateArticle.php +++ b/app/Jobs/CreateArticle.php @@ -2,11 +2,10 @@ namespace App\Jobs; +use App\Events\ArticleWasCreated; use App\Http\Requests\ArticleRequest; use App\Models\Article; use App\Models\User; -use App\Notifications\ArticleSubmitted; -use Illuminate\Notifications\AnonymousNotifiable; use Illuminate\Support\Facades\App; final class CreateArticle @@ -60,7 +59,7 @@ public function handle(): Article $article->syncTags($this->tags); if (App::environment() !== 'testing') { - (new AnonymousNotifiable())->notify(new ArticleSubmitted($article)); + event(new ArticleWasCreated($article)); } return $article; diff --git a/app/Listeners/SendTelegramNewArticleNotification.php b/app/Listeners/SendTelegramNewArticleNotification.php new file mode 100644 index 000000000..291fe1af3 --- /dev/null +++ b/app/Listeners/SendTelegramNewArticleNotification.php @@ -0,0 +1,15 @@ +notify(new ArticleSubmitted($event->article)); + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index a0a24aaff..45e0e39cc 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -3,10 +3,12 @@ namespace App\Providers; use App\Events\ArticleWasApproved; +use App\Events\ArticleWasCreated; use App\Events\ReplyWasCreated; use App\Listeners\MarkLastActivity; use App\Listeners\SendArticleApprovedNotification; use App\Listeners\SendNewReplyNotification; +use App\Listeners\SendTelegramNewArticleNotification; use App\Listeners\StoreTweetIdentifier; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Illuminate\Notifications\Events\NotificationSent; @@ -23,6 +25,9 @@ class EventServiceProvider extends ServiceProvider MarkLastActivity::class, SendNewReplyNotification::class, ], + ArticleWasCreated::class => [ + SendTelegramNewArticleNotification::class, + ], ArticleWasApproved::class => [ SendArticleApprovedNotification::class, ], From 03bb256ab68f74ec1b0dd461ce08ff762ee24b70 Mon Sep 17 00:00:00 2001 From: faissaloux Date: Wed, 24 Nov 2021 22:37:56 +0100 Subject: [PATCH 07/14] pass AnonymousNotifiable in construct --- app/Listeners/SendTelegramNewArticleNotification.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/Listeners/SendTelegramNewArticleNotification.php b/app/Listeners/SendTelegramNewArticleNotification.php index 291fe1af3..856118a6f 100644 --- a/app/Listeners/SendTelegramNewArticleNotification.php +++ b/app/Listeners/SendTelegramNewArticleNotification.php @@ -8,8 +8,12 @@ final class SendTelegramNewArticleNotification { + public function __construct( + private AnonymousNotifiable $notifiable + ) {} + public function handle(ArticleWasCreated $event): void { - (new AnonymousNotifiable())->notify(new ArticleSubmitted($event->article)); + $this->notifiable->notify(new ArticleSubmitted($event->article)); } } From e070560c11b5762070e12cf15124ee991314e103 Mon Sep 17 00:00:00 2001 From: faissaloux Date: Wed, 24 Nov 2021 22:43:03 +0100 Subject: [PATCH 08/14] apply StyleCI fixes --- app/Listeners/SendTelegramNewArticleNotification.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Listeners/SendTelegramNewArticleNotification.php b/app/Listeners/SendTelegramNewArticleNotification.php index 856118a6f..1da63b055 100644 --- a/app/Listeners/SendTelegramNewArticleNotification.php +++ b/app/Listeners/SendTelegramNewArticleNotification.php @@ -10,7 +10,8 @@ final class SendTelegramNewArticleNotification { public function __construct( private AnonymousNotifiable $notifiable - ) {} + ) { + } public function handle(ArticleWasCreated $event): void { From 14c469dfbf77a598a6c24d71a6ad0cfbbe65c63b Mon Sep 17 00:00:00 2001 From: faissaloux Date: Wed, 24 Nov 2021 22:56:05 +0100 Subject: [PATCH 09/14] remove check env before event fire --- app/Jobs/CreateArticle.php | 5 +---- phpunit.xml | 2 ++ 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/Jobs/CreateArticle.php b/app/Jobs/CreateArticle.php index cbab507f2..efc1e8762 100644 --- a/app/Jobs/CreateArticle.php +++ b/app/Jobs/CreateArticle.php @@ -6,7 +6,6 @@ use App\Http\Requests\ArticleRequest; use App\Models\Article; use App\Models\User; -use Illuminate\Support\Facades\App; final class CreateArticle { @@ -58,9 +57,7 @@ public function handle(): Article $article->authoredBy($this->author); $article->syncTags($this->tags); - if (App::environment() !== 'testing') { - event(new ArticleWasCreated($article)); - } + event(new ArticleWasCreated($article)); return $article; } diff --git a/phpunit.xml b/phpunit.xml index 476245d85..daabbf7bd 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -25,5 +25,7 @@ + + From ce37a2837c6acf97b4d895dc16d161c73ea8dd34 Mon Sep 17 00:00:00 2001 From: faissaloux Date: Wed, 24 Nov 2021 22:58:52 +0100 Subject: [PATCH 10/14] Bump laravel-notification-channels/telegram to 0.9.0 --- composer.json | 2 +- composer.lock | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index fef5a9e4a..012a7b270 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "guzzlehttp/guzzle": "^7.0.1", "intervention/image": "^2.5", "intervention/imagecache": "^2.5", - "laravel-notification-channels/telegram": "^0.8.0", + "laravel-notification-channels/telegram": "^0.9.0", "laravel-notification-channels/twitter": "^5.0", "laravel/framework": "8.66.0", "laravel/horizon": "^5.2", diff --git a/composer.lock b/composer.lock index 3a743195d..497ea03ac 100644 --- a/composer.lock +++ b/composer.lock @@ -2386,16 +2386,16 @@ }, { "name": "laravel-notification-channels/telegram", - "version": "0.8.0", + "version": "0.9.0", "source": { "type": "git", "url": "https://github.com/laravel-notification-channels/telegram.git", - "reference": "dd7cee42b1da9a5528abd298a93c309e25a8306f" + "reference": "a941130a555908dc16b119b4f8c99b5f3605bf25" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel-notification-channels/telegram/zipball/dd7cee42b1da9a5528abd298a93c309e25a8306f", - "reference": "dd7cee42b1da9a5528abd298a93c309e25a8306f", + "url": "https://api.github.com/repos/laravel-notification-channels/telegram/zipball/a941130a555908dc16b119b4f8c99b5f3605bf25", + "reference": "a941130a555908dc16b119b4f8c99b5f3605bf25", "shasum": "" }, "require": { @@ -2446,9 +2446,9 @@ ], "support": { "issues": "https://github.com/laravel-notification-channels/telegram/issues", - "source": "https://github.com/laravel-notification-channels/telegram/tree/0.8.0" + "source": "https://github.com/laravel-notification-channels/telegram/tree/0.9.0" }, - "time": "2021-11-14T01:06:40+00:00" + "time": "2021-11-24T12:19:23+00:00" }, { "name": "laravel-notification-channels/twitter", From e23bd18a001b98f9d07c14057ed87e841027ba4c Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Tue, 30 Nov 2021 21:22:41 +0000 Subject: [PATCH 11/14] Send notification when article submitted --- ...php => ArticleWasSubmittedForApproval.php} | 2 +- app/Jobs/CreateArticle.php | 5 +- app/Jobs/UpdateArticle.php | 9 ++- .../SendTelegramNewArticleNotification.php | 4 +- app/Notifications/ArticleSubmitted.php | 4 +- app/Providers/EventServiceProvider.php | 3 +- tests/Feature/ArticleTest.php | 71 +++++++++++++++++++ 7 files changed, 90 insertions(+), 8 deletions(-) rename app/Events/{ArticleWasCreated.php => ArticleWasSubmittedForApproval.php} (86%) diff --git a/app/Events/ArticleWasCreated.php b/app/Events/ArticleWasSubmittedForApproval.php similarity index 86% rename from app/Events/ArticleWasCreated.php rename to app/Events/ArticleWasSubmittedForApproval.php index 10332aecd..901096650 100644 --- a/app/Events/ArticleWasCreated.php +++ b/app/Events/ArticleWasSubmittedForApproval.php @@ -5,7 +5,7 @@ use App\Models\Article; use Illuminate\Queue\SerializesModels; -class ArticleWasCreated +class ArticleWasSubmittedForApproval { use SerializesModels; diff --git a/app/Jobs/CreateArticle.php b/app/Jobs/CreateArticle.php index efc1e8762..c67df3551 100644 --- a/app/Jobs/CreateArticle.php +++ b/app/Jobs/CreateArticle.php @@ -3,6 +3,7 @@ namespace App\Jobs; use App\Events\ArticleWasCreated; +use App\Events\ArticleWasSubmittedForApproval; use App\Http\Requests\ArticleRequest; use App\Models\Article; use App\Models\User; @@ -57,7 +58,9 @@ public function handle(): Article $article->authoredBy($this->author); $article->syncTags($this->tags); - event(new ArticleWasCreated($article)); + if($article->isAwaitingApproval()) { + event(new ArticleWasSubmittedForApproval($article)); + } return $article; } diff --git a/app/Jobs/UpdateArticle.php b/app/Jobs/UpdateArticle.php index d1612df39..462e26cea 100644 --- a/app/Jobs/UpdateArticle.php +++ b/app/Jobs/UpdateArticle.php @@ -2,6 +2,7 @@ namespace App\Jobs; +use App\Events\ArticleWasSubmittedForApproval; use App\Http\Requests\ArticleRequest; use App\Models\Article; @@ -50,9 +51,15 @@ public function handle(): Article 'body' => $this->body, 'original_url' => $this->originalUrl, 'slug' => $this->title, - 'submitted_at' => $this->shouldUpdateSubmittedAt() ? now() : $this->article->submittedAt(), ]); + if ($this->shouldUpdateSubmittedAt()) { + $this->article->submitted_at = now(); + $this->article->save(); + + event(new ArticleWasSubmittedForApproval($this->article)); + } + $this->article->syncTags($this->tags); return $this->article; diff --git a/app/Listeners/SendTelegramNewArticleNotification.php b/app/Listeners/SendTelegramNewArticleNotification.php index 1da63b055..38239dcb5 100644 --- a/app/Listeners/SendTelegramNewArticleNotification.php +++ b/app/Listeners/SendTelegramNewArticleNotification.php @@ -2,7 +2,7 @@ namespace App\Listeners; -use App\Events\ArticleWasCreated; +use App\Events\ArticleWasSubmittedForApproval; use App\Notifications\ArticleSubmitted; use Illuminate\Notifications\AnonymousNotifiable; @@ -13,7 +13,7 @@ public function __construct( ) { } - public function handle(ArticleWasCreated $event): void + public function handle(ArticleWasSubmittedForApproval $event): void { $this->notifiable->notify(new ArticleSubmitted($event->article)); } diff --git a/app/Notifications/ArticleSubmitted.php b/app/Notifications/ArticleSubmitted.php index 368c70a3a..89b4235fa 100644 --- a/app/Notifications/ArticleSubmitted.php +++ b/app/Notifications/ArticleSubmitted.php @@ -31,12 +31,12 @@ public function toTelegram($notifiable) return TelegramMessage::create() ->to(config('services.telegram-bot-api.channel')) ->content($this->content()) - ->button('View article', $url); + ->button('View Article', $url); } private function content(): string { - $content = "*New article submitted!*\n\n"; + $content = "*New Article Submitted!*\n\n"; $content .= 'Title: '.$this->article->title()."\n"; $content .= 'By: [@'.$this->article->author()->username().']('.route('profile', $this->article->author()->username()).')'; diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 45e0e39cc..f77f4780d 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -4,6 +4,7 @@ use App\Events\ArticleWasApproved; use App\Events\ArticleWasCreated; +use App\Events\ArticleWasSubmittedForApproval; use App\Events\ReplyWasCreated; use App\Listeners\MarkLastActivity; use App\Listeners\SendArticleApprovedNotification; @@ -25,7 +26,7 @@ class EventServiceProvider extends ServiceProvider MarkLastActivity::class, SendNewReplyNotification::class, ], - ArticleWasCreated::class => [ + ArticleWasSubmittedForApproval::class => [ SendTelegramNewArticleNotification::class, ], ArticleWasApproved::class => [ diff --git a/tests/Feature/ArticleTest.php b/tests/Feature/ArticleTest.php index e61dee483..c85b06bdd 100644 --- a/tests/Feature/ArticleTest.php +++ b/tests/Feature/ArticleTest.php @@ -1,9 +1,11 @@ see('Awaiting approval'); }); +test('articles submitted for approval send telegram notification', function () { + Event::fake(); + $this->login(); + + $this->post('/articles', [ + 'title' => 'Using database migrations', + 'body' => 'This article will go into depth on working with database migrations.', + 'submitted' => '1', + ]); + + Event::assertDispatched(ArticleWasSubmittedForApproval::class); +}); + test('users can create a draft article', function () { $this->login(); @@ -71,6 +86,19 @@ ->see('Draft'); }); +test('draft articles do not send telegram notification', function () { + Event::fake(); + $this->login(); + + $this->post('/articles', [ + 'title' => 'Using database migrations', + 'body' => 'This article will go into depth on working with database migrations.', + 'submitted' => '0', + ]); + + Event::assertNotDispatched(ArticleWasSubmittedForApproval::class); +}); + test('users cannot create an article with a title that is too long', function () { $this->login(); @@ -132,6 +160,28 @@ ->assertSessionHas('success', 'Article successfully updated!'); }); +test('editing a draft article does not send telegram notification', function () { + Event::fake(); + $user = $this->createUser(); + $tag = Tag::factory()->create(['name' => 'Test Tag']); + + Article::factory()->create([ + 'author_id' => $user->id(), + 'slug' => 'my-first-article', + ]); + + $this->loginAs($user); + + $this->put('/articles/my-first-article', [ + 'title' => 'Using database migrations', + 'body' => 'This article will go into depth on working with database migrations.', + 'tags' => [$tag->id()], + 'submitted' => '0', + ]); + + Event::assertNotDispatched(ArticleWasSubmittedForApproval::class); +}); + test('user gets submitted message when submitting existing article for approval', function () { $user = $this->createUser(); @@ -173,6 +223,27 @@ ->dontSee('Draft'); }); +test('notification is sent to telegram when existing article is submitted for approval', function () { + Event::fake(); + $user = $this->createUser(); + + Article::factory()->create([ + 'author_id' => $user->id(), + 'slug' => 'my-first-article', + ]); + + $this->loginAs($user); + + $this->put('/articles/my-first-article', [ + 'title' => 'Using database migrations', + 'body' => 'This article will go into depth on working with database migrations.', + 'tags' => [], + 'submitted' => '1', + ]); + + Event::assertDispatched(ArticleWasSubmittedForApproval::class); +}); + test('users cannot edit an article with a title that is too long', function () { $user = $this->createUser(); Article::factory()->create([ From f0f678a00c64b7afc12760b1d66531d2aa37deda Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Tue, 30 Nov 2021 21:25:58 +0000 Subject: [PATCH 12/14] Update formatting --- app/Jobs/CreateArticle.php | 3 +-- app/Providers/EventServiceProvider.php | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/app/Jobs/CreateArticle.php b/app/Jobs/CreateArticle.php index c67df3551..235169ee8 100644 --- a/app/Jobs/CreateArticle.php +++ b/app/Jobs/CreateArticle.php @@ -2,7 +2,6 @@ namespace App\Jobs; -use App\Events\ArticleWasCreated; use App\Events\ArticleWasSubmittedForApproval; use App\Http\Requests\ArticleRequest; use App\Models\Article; @@ -58,7 +57,7 @@ public function handle(): Article $article->authoredBy($this->author); $article->syncTags($this->tags); - if($article->isAwaitingApproval()) { + if ($article->isAwaitingApproval()) { event(new ArticleWasSubmittedForApproval($article)); } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index f77f4780d..b8f9edc05 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -3,7 +3,6 @@ namespace App\Providers; use App\Events\ArticleWasApproved; -use App\Events\ArticleWasCreated; use App\Events\ArticleWasSubmittedForApproval; use App\Events\ReplyWasCreated; use App\Listeners\MarkLastActivity; From 6a8e6270a0f8be2bf2c8e08e6bcab553ca0e13cf Mon Sep 17 00:00:00 2001 From: faissaloux Date: Wed, 1 Dec 2021 00:07:37 +0100 Subject: [PATCH 13/14] rename listener --- ...ArticleNotification.php => SendNewArticleNotification.php} | 2 +- app/Providers/EventServiceProvider.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename app/Listeners/{SendTelegramNewArticleNotification.php => SendNewArticleNotification.php} (89%) diff --git a/app/Listeners/SendTelegramNewArticleNotification.php b/app/Listeners/SendNewArticleNotification.php similarity index 89% rename from app/Listeners/SendTelegramNewArticleNotification.php rename to app/Listeners/SendNewArticleNotification.php index 1da63b055..4e9fde882 100644 --- a/app/Listeners/SendTelegramNewArticleNotification.php +++ b/app/Listeners/SendNewArticleNotification.php @@ -6,7 +6,7 @@ use App\Notifications\ArticleSubmitted; use Illuminate\Notifications\AnonymousNotifiable; -final class SendTelegramNewArticleNotification +final class SendNewArticleNotification { public function __construct( private AnonymousNotifiable $notifiable diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 45e0e39cc..3c5576636 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -8,7 +8,7 @@ use App\Listeners\MarkLastActivity; use App\Listeners\SendArticleApprovedNotification; use App\Listeners\SendNewReplyNotification; -use App\Listeners\SendTelegramNewArticleNotification; +use App\Listeners\SendNewArticleNotification; use App\Listeners\StoreTweetIdentifier; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Illuminate\Notifications\Events\NotificationSent; @@ -26,7 +26,7 @@ class EventServiceProvider extends ServiceProvider SendNewReplyNotification::class, ], ArticleWasCreated::class => [ - SendTelegramNewArticleNotification::class, + SendNewArticleNotification::class, ], ArticleWasApproved::class => [ SendArticleApprovedNotification::class, From b287101c908457c6e1907f9c1f4e1fa0da9188f9 Mon Sep 17 00:00:00 2001 From: faissaloux Date: Wed, 1 Dec 2021 00:27:12 +0100 Subject: [PATCH 14/14] apply StyleCI fixes --- app/Providers/EventServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index cd1661aef..28d79a29c 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -7,8 +7,8 @@ use App\Events\ReplyWasCreated; use App\Listeners\MarkLastActivity; use App\Listeners\SendArticleApprovedNotification; -use App\Listeners\SendNewReplyNotification; use App\Listeners\SendNewArticleNotification; +use App\Listeners\SendNewReplyNotification; use App\Listeners\StoreTweetIdentifier; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Illuminate\Notifications\Events\NotificationSent;