Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ TWITTER_ACCESS_SECRET=

FLARE_KEY=
MIX_FLARE_KEY="${FLARE_KEY}"

TELEGRAM_BOT_TOKEN=
TELEGRAM_CHANNEL=
18 changes: 18 additions & 0 deletions app/Events/ArticleWasSubmittedForApproval.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Events;

use App\Models\Article;
use Illuminate\Queue\SerializesModels;

class ArticleWasSubmittedForApproval
{
use SerializesModels;

public $article;

public function __construct(Article $article)
{
$this->article = $article;
}
}
5 changes: 5 additions & 0 deletions app/Jobs/CreateArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Jobs;

use App\Events\ArticleWasSubmittedForApproval;
use App\Http\Requests\ArticleRequest;
use App\Models\Article;
use App\Models\User;
Expand Down Expand Up @@ -56,6 +57,10 @@ public function handle(): Article
$article->authoredBy($this->author);
$article->syncTags($this->tags);

if ($article->isAwaitingApproval()) {
event(new ArticleWasSubmittedForApproval($article));
}

return $article;
}
}
9 changes: 8 additions & 1 deletion app/Jobs/UpdateArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Jobs;

use App\Events\ArticleWasSubmittedForApproval;
use App\Http\Requests\ArticleRequest;
use App\Models\Article;

Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions app/Listeners/SendNewArticleNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Listeners;

use App\Events\ArticleWasSubmittedForApproval;
use App\Notifications\ArticleSubmitted;
use Illuminate\Notifications\AnonymousNotifiable;

final class SendNewArticleNotification
{
public function __construct(
private AnonymousNotifiable $notifiable
) {
}

public function handle(ArticleWasSubmittedForApproval $event): void
{
$this->notifiable->notify(new ArticleSubmitted($event->article));
}
}
45 changes: 45 additions & 0 deletions app/Notifications/ArticleSubmitted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Notifications;

use App\Models\Article;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use NotificationChannels\Telegram\TelegramMessage;

class ArticleSubmitted extends Notification implements ShouldQueue
{
use Queueable;

private Article $article;

public function __construct(Article $article)
{
$this->article = $article;
}

public function via($notifiable)
{
return ['telegram'];
}

public function toTelegram($notifiable)
{
$url = route('articles.show', $this->article->slug());

return TelegramMessage::create()
->to(config('services.telegram-bot-api.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;
}
}
5 changes: 5 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace App\Providers;

use App\Events\ArticleWasApproved;
use App\Events\ArticleWasSubmittedForApproval;
use App\Events\ReplyWasCreated;
use App\Listeners\MarkLastActivity;
use App\Listeners\SendArticleApprovedNotification;
use App\Listeners\SendNewArticleNotification;
use App\Listeners\SendNewReplyNotification;
use App\Listeners\StoreTweetIdentifier;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
Expand All @@ -23,6 +25,9 @@ class EventServiceProvider extends ServiceProvider
MarkLastActivity::class,
SendNewReplyNotification::class,
],
ArticleWasSubmittedForApproval::class => [
SendNewArticleNotification::class,
],
ArticleWasApproved::class => [
SendArticleApprovedNotification::class,
],
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"guzzlehttp/guzzle": "^7.0.1",
"intervention/image": "^2.5",
"intervention/imagecache": "^2.5",
"laravel-notification-channels/telegram": "^0.9.0",
"laravel-notification-channels/twitter": "^5.0",
"laravel/framework": "8.66.0",
"laravel/horizon": "^5.2",
Expand Down
66 changes: 66 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@
'access_secret' => env('TWITTER_ACCESS_SECRET'),
],

'telegram-bot-api' => [
'token' => env('TELEGRAM_BOT_TOKEN'),
'channel' => env('TELEGRAM_CHANNEL'),
],

];
2 changes: 2 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SCOUT_DRIVER" value="null"/>
<env name="TELEGRAM_BOT_TOKEN" value="null"/>
<env name="TELEGRAM_CHANNEL" value="null"/>
</php>
</phpunit>
71 changes: 71 additions & 0 deletions tests/Feature/ArticleTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

use App\Events\ArticleWasSubmittedForApproval;
use App\Models\Article;
use App\Models\Tag;
use App\Notifications\ArticleApprovedNotification;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Notification;
use Tests\Feature\BrowserKitTestCase;

Expand Down Expand Up @@ -58,6 +60,19 @@
->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();

Expand All @@ -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();

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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([
Expand Down