Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/lar-8 notification soumission darticle #146

Closed
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ composer.phar

# Laravel Exclude
#
/.phpunit.cache
.phpunit.result.cache
/public/build
/public/hot
Expand Down
1 change: 0 additions & 1 deletion .phpunit.cache/test-results

This file was deleted.

59 changes: 59 additions & 0 deletions app/Actions/Article/CreateArticleAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace App\Actions\Article;

use App\Data\Article\CreateArticleData;
use App\Gamify\Points\ArticleCreated;
use App\Models\Article;
use App\Notifications\PostArticleToTelegram;
use Carbon\Carbon;
use DateTimeInterface;
use Illuminate\Support\Facades\Auth;

final class CreateArticleAction
{
public function execute(CreateArticleData $articleData): Article
{
if ($articleData->published_at && ! ($articleData->published_at instanceof DateTimeInterface)) {
$articleData->published_at = new Carbon(
time: $articleData->published_at,
tz: config('app.timezone')
);
}

/** @var Article $article */
$article = Article::query()->create([
'title' => $articleData->title,
'slug' => $articleData->title,
'body' => $articleData->body,
'published_at' => $articleData->published_at,
'submitted_at' => $articleData->submitted_at,
'approved_at' => $articleData->approved_at,
'show_toc' => $articleData->show_toc,
'canonical_url' => $articleData->canonical_url,
'user_id' => Auth::id(),
]);

if (collect($article->associateTags)->isNotEmpty()) {
$article->syncTags(tags: $article->associateTags);
}

if ($article->file) {
$article->addMedia($article->file->getRealPath())->toMediaCollection('media');
}

if ($article->isAwaitingApproval()) {
// Envoi de la notification sur le channel Telegram pour la validation de l'article.
Auth::user()?->notify(new PostArticleToTelegram($article));
session()->flash('status', __('Merci d\'avoir soumis votre article. Vous aurez des nouvelles que lorsque nous accepterons votre article.'));
}

if (Auth::user()?->hasAnyRole(['admin', 'moderator'])) {
givePoint(new ArticleCreated($article));
}

return $article;
}
}
35 changes: 35 additions & 0 deletions app/Actions/Discussion/CreateDiscussionAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\Actions\Discussion;

use App\Data\Discussion\CreateDiscussionData;
use App\Gamify\Points\DiscussionCreated;
use App\Models\Discussion;
use App\Notifications\PostDiscussionToTelegram;
use Illuminate\Support\Facades\Auth;

final class CreateDiscussionAction
{
public function execute(CreateDiscussionData $discussionData): Discussion
{
/** @var Discussion $discussion */
$discussion = Discussion::query()->create([
'title' => $discussionData->title,
'slug' => $discussionData->title,
'body' => $discussionData->body,
'user_id' => Auth::id(),
]);

if (collect($discussionData->tags)->isNotEmpty()) {
$discussion->syncTags($discussionData->tags);
}

givePoint(new DiscussionCreated($discussion));

Auth::user()?->notify(new PostDiscussionToTelegram($discussion));

return $discussion;
}
}
23 changes: 23 additions & 0 deletions app/Data/Article/CreateArticleData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace App\Data\Article;

use Carbon\Carbon;
use Spatie\LaravelData\Data;

final class CreateArticleData extends Data
{
public function __construct(
public string $title,
public string $slug,
public string $body,
public Carbon $published_at,
public ?Carbon $submitted_at,
public ?Carbon $approved_at,
public string $show_toc,
public string $canonical_url,
public array $associateTags = [],
) {}
}
16 changes: 16 additions & 0 deletions app/Data/Discussion/CreateDiscussionData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\Data\Discussion;

use Spatie\LaravelData\Data;

final class CreateDiscussionData extends Data
{
public function __construct(
public string $title,
public string $body,
public array $tags = [],
) {}
}
25 changes: 25 additions & 0 deletions app/Gamify/Points/ArticleCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\Gamify\Points;

use App\Models\Article;
use App\Models\User;
use QCod\Gamify\PointType;

final class ArticleCreated extends PointType
{
public int $points = 50;

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

public function payee(): User
{
// @phpstan-ignore-next-line
return $this->getSubject()->user;
}
}
40 changes: 4 additions & 36 deletions app/Livewire/Articles/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@

namespace App\Livewire\Articles;

use App\Events\ArticleWasSubmittedForApproval;
use App\Gamify\Points\PostCreated;
use App\Models\Article;
use App\Actions\Article\CreateArticleAction;
use App\Data\Article\CreateArticleData;
use App\Models\Tag;
use App\Models\User;
use App\Traits\WithArticleAttributes;
use App\Traits\WithTagsAssociation;
use Carbon\Carbon;
use DateTimeInterface;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
Expand Down Expand Up @@ -52,15 +49,7 @@ public function store(): void
/** @var User $user */
$user = Auth::user();

if ($this->published_at && ! ($this->published_at instanceof DateTimeInterface)) {
$this->published_at = new Carbon(
time: $this->published_at,
tz: config('app.timezone')
);
}

/** @var Article $article */
$article = Article::create([
$article = app(CreateArticleAction::class)->execute(CreateArticleData::from([
'title' => $this->title,
'slug' => $this->slug,
'body' => $this->body,
Expand All @@ -70,28 +59,7 @@ public function store(): void
'show_toc' => $this->show_toc,
'canonical_url' => $this->canonical_url,
'user_id' => $user->id,
]);

if (collect($this->associateTags)->isNotEmpty()) {
$article->syncTags(tags: $this->associateTags);
}

if ($this->file) {
$article->addMedia($this->file->getRealPath())->toMediaCollection('media');
}

if ($article->isAwaitingApproval()) {
if (app()->environment('production')) {
// Envoi de la notification sur le channel Telegram pour la validation de l'article.
event(new ArticleWasSubmittedForApproval($article));
}

session()->flash('status', __('Merci d\'avoir soumis votre article. Vous aurez des nouvelles que lorsque nous accepterons votre article.'));
}

if ($user->hasAnyRole(['admin', 'moderator'])) {
givePoint(new PostCreated($article));
}
]));

$user->hasRole('user') ?
$this->redirectRoute('dashboard') :
Expand Down
44 changes: 11 additions & 33 deletions app/Livewire/Discussions/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,26 @@

namespace App\Livewire\Discussions;

use App\Gamify\Points\DiscussionCreated;
use App\Models\Discussion;
use App\Actions\Discussion\CreateDiscussionAction;
use App\Data\Discussion\CreateDiscussionData;
use App\Models\Tag;
use App\Notifications\PostDiscussionToTelegram;
use App\Traits\WithTagsAssociation;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\On;
use Livewire\Attributes\Validate;
use Livewire\Component;

final class Create extends Component
{
use WithTagsAssociation;

#[Validate('required')]
public string $title = '';

#[Validate('required')]
public string $body = '';

/**
* @var string[]
*/
protected $listeners = ['markdown-x:update' => 'onMarkdownUpdate'];

/**
* @var array<string, string[]|string>
*/
protected $rules = [
'title' => ['required', 'max:150'],
'body' => ['required'],
'tags_selected' => 'nullable|array',
];

#[On('markdown-x:update')]
public function onMarkdownUpdate(string $content): void
{
$this->body = $content;
Expand All @@ -44,24 +33,13 @@ public function store(): void
{
$this->validate();

$discussion = Discussion::create([
$discussion = app(CreateDiscussionAction::class)->execute(CreateDiscussionData::from([
'title' => $this->title,
'slug' => $this->title,
'body' => $this->body,
'user_id' => Auth::id(),
]);

if (collect($this->associateTags)->isNotEmpty()) {
$discussion->syncTags($this->associateTags);
}

givePoint(new DiscussionCreated($discussion));

if (app()->environment('production')) {
Auth::user()->notify(new PostDiscussionToTelegram($discussion)); // @phpstan-ignore-line
}
'tags' => $this->associateTags,
]));

$this->redirectRoute('discussions.show', $discussion);
$this->redirectRoute('discussions.show', $discussion, navigate: true);
}

public function render(): View
Expand Down
10 changes: 9 additions & 1 deletion app/Models/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@
use Illuminate\Support\Str;

/**
* @mixin IdeHelperDiscussion
* @property int $id
* @property int $user_id
* @property string $title
* @property string $slug
* @property string $body
* @property bool $is_pinned
* @property bool $locked
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
*/
final class Discussion extends Model implements ReactableInterface, ReplyInterface, SubscribeInterface, Viewable
{
Expand Down
3 changes: 3 additions & 0 deletions app/Traits/WithTagsAssociation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

namespace App\Traits;

use Livewire\Attributes\Validate;

trait WithTagsAssociation
{
/**
* @var array<string, string>
*/
#[Validate('nullable|array')]
public array $tags_selected = [];

/**
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"ramsey/uuid": "^4.7.4",
"sentry/sentry-laravel": "^3.7",
"socialiteproviders/twitter": "^4.1.2",
"spatie/laravel-data": "^4.10",
"spatie/laravel-feed": "^4.2.1",
"spatie/laravel-google-fonts": "^1.2.3",
"spatie/laravel-medialibrary": "^10.10.0",
Expand Down
Loading