-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc5d375
commit 3ed8dbc
Showing
8 changed files
with
149 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\Article; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Notifications\AnonymousNotifiable; | ||
use App\Notifications\PendingArticlesNotification; | ||
|
||
final class NotifyPendingArticles extends Command | ||
{ | ||
protected $signature = 'lcm:notify-pending-articles'; | ||
|
||
protected $description = 'Send a Telegram notification for articles that are submitted but neither approved nor declined'; | ||
|
||
public function handle(AnonymousNotifiable $notifiable): void | ||
{ | ||
$pendingArticles = Article::awaitingApproval()->get(); | ||
|
||
if ($pendingArticles->isNotEmpty()) { | ||
$notifiable->notify(new PendingArticlesNotification($pendingArticles)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Notifications; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Notifications\Notification; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
use NotificationChannels\Telegram\TelegramChannel; | ||
use NotificationChannels\Telegram\TelegramMessage; | ||
|
||
final class PendingArticlesNotification extends Notification | ||
{ | ||
use Queueable; | ||
|
||
public function __construct(public Collection $pendingArticles) {} | ||
|
||
public function via(mixed $notifiable): array | ||
{ | ||
return [TelegramChannel::class]; | ||
} | ||
|
||
public function toTelegram(): TelegramMessage | ||
{ | ||
$message = $this->content(); | ||
|
||
return TelegramMessage::create() | ||
->to(config('services.telegram-bot-api.channel')) | ||
->content($message); | ||
} | ||
|
||
private function content(): string | ||
{ | ||
$message = __("Pending approval articles:\n\n"); | ||
foreach ($this->pendingArticles as $article) { | ||
$message .= __( | ||
"[@:username](:profile_url) submitted the article [:title](:url) on: :date\n\n", [ | ||
'username' => $article->user?->username, | ||
'profile_url' => route('profile', $article->user?->username), | ||
'title' => $article->title, | ||
'url' => route('articles.show', $article->slug), | ||
'date' => $article->submitted_at->translatedFormat('d/m/Y') | ||
] | ||
); | ||
} | ||
|
||
return $message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"The :attribute must be at least :length characters.": "The :attribute must be at least :length characters.", | ||
"The given :attribute has appeared in a data leak. Please choose a different :attribute.": "The given :attribute has appeared in a data leak. Please choose a different :attribute.", | ||
"Verify Email Address": "Verify Email Address", | ||
"Please click the button below to verify your email address.": "Please click the button below to verify your email address.", | ||
"If you did not create an account, no further action is required.": "If you did not create an account, no further action is required.", | ||
"Pending approval articles:" : "Pending approval articles:", | ||
"[@:username](:profile_url) submitted the article [:title](:url) on: :date" : "[@:username](:profile_url) submitted the article [:title](:url) on: :date" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"The :attribute must be at least :length characters.": "Le champ :attribute doit contenir au moins :length caractères.", | ||
"The given :attribute has appeared in a data leak. Please choose a different :attribute.": "Le champ :attribute donné est apparu dans une fuite de données. Veuillez choisir un autre :attribute.", | ||
"Verify Email Address": "Vérifier l'adresse e-mail", | ||
"Please click the button below to verify your email address.": "Veuillez cliquer sur le bouton ci-dessous pour vérifier votre adresse email.", | ||
"If you did not create an account, no further action is required.": "Si vous n'avez pas créé de compte, aucune autre action n'est requise.", | ||
"Pending approval articles:" : "Articles soumis en attente d'approbation:", | ||
"[@:username](:profile_url) submitted the article [:title](:url) on: :date" : "[@:username](:profile_url) a soumit l'article [:title](:url) le: :date" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -187,4 +187,4 @@ function createActiveThread(): Thread | |
$reply->save(); | ||
|
||
return $thread; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use App\Console\Commands\NotifyPendingArticles; | ||
use App\Models\Article; | ||
use Illuminate\Support\Facades\Notification; | ||
|
||
beforeEach(fn() => Notification::fake()); | ||
|
||
it('will send a notification when there are pending articles', function (): void { | ||
Article::factory()->createMany([ | ||
[ | ||
'submitted_at' => now(), | ||
], | ||
[ | ||
'submitted_at' => now()->subDay(), | ||
'approved_at' => now(), | ||
], | ||
[ | ||
'submitted_at' => now()->subDay(), | ||
'declined_at' => now(), | ||
], | ||
]); | ||
|
||
$this->assertDatabaseCount('articles', 3); | ||
$this->artisan(NotifyPendingArticles::class)->assertExitCode(0); | ||
|
||
Notification::assertCount(1); | ||
}); | ||
|
||
it('will not send a notification when there are no pending articles', function (): void { | ||
Article::factory()->createMany([ | ||
[ | ||
'submitted_at' => now()->subDay(), | ||
'approved_at' => now(), | ||
], | ||
[ | ||
'submitted_at' => now()->subDay(), | ||
'declined_at' => now(), | ||
], | ||
]); | ||
|
||
$this->assertDatabaseCount('articles', 2); | ||
$this->artisan(NotifyPendingArticles::class)->assertExitCode(0); | ||
|
||
Notification::assertNothingSent(); | ||
Notification::assertCount(0); | ||
}); |