Skip to content

Commit

Permalink
chore: rebase main
Browse files Browse the repository at this point in the history
  • Loading branch information
mckenziearts committed Oct 11, 2024
1 parent bc5d375 commit 3ed8dbc
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 3 deletions.
26 changes: 26 additions & 0 deletions app/Console/Commands/NotifyPendingArticles.php
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));
}
}
}
3 changes: 2 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ protected function schedule(Schedule $schedule): void
$schedule->command('lcm:post-article-to-telegram')->everyFourHours();
$schedule->command('lcm:send-unverified-mails')->weeklyOn(1, '8:00');
$schedule->command('sitemap:generate')->daily();
$schedule->command('lcm:notify-pending-articles')->days(2);
}
}

protected function commands(): void
{
$this->load(__DIR__.'/Commands');
}
}
}
52 changes: 52 additions & 0 deletions app/Notifications/PendingArticlesNotification.php
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;
}
}
9 changes: 9 additions & 0 deletions lang/en.json
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"
}
9 changes: 9 additions & 0 deletions lang/fr.json
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"
}
2 changes: 1 addition & 1 deletion tests/Feature/Forum/DiscussionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@

// When providing a slug with invalid url characters, a random 5 character string is returned.
expect($discussion->slug())->toMatch('/\w{5}/');
});
});
2 changes: 1 addition & 1 deletion tests/Feature/Forum/ThreadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,4 @@ function createActiveThread(): Thread
$reply->save();

return $thread;
}
}
49 changes: 49 additions & 0 deletions tests/Feature/NotifyPendingArticlesTest.php
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);
});

0 comments on commit 3ed8dbc

Please sign in to comment.