From fdbc834be4e00813eba8c98180be0664a3e43e19 Mon Sep 17 00:00:00 2001 From: Chris Samory Date: Wed, 18 Dec 2024 22:41:36 +0100 Subject: [PATCH 1/2] =?UTF-8?q?fix:[lar-135]=20fix=20blank=20slideover=20w?= =?UTF-8?q?hen=20user=20are=20not=20authenticate=20and=20=E2=80=A6=20(#255?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit chore: [LAR-131] refactoring class feat: [LAR-131] remove locale global scope and add local selector to views --- .../ConvertDiscussionToThreadAction.php | 2 +- .../CreateDiscussionReplyAction.php} | 6 ++--- .../LikeReplyAction.php} | 4 +-- ...> NotifyUsersOfThreadConversionAction.php} | 2 +- app/Events/CommentWasAdded.php | 3 ++- .../Controllers/SubscriptionController.php | 2 +- app/Livewire/Components/ChangeLocale.php | 5 +--- .../Components/Discussion/Comment.php | 4 +-- .../Components/Discussion/Comments.php | 4 +-- .../Components/Slideovers/ArticleForm.php | 12 +++++++++ app/Livewire/Pages/Articles/Index.php | 10 +++++++- app/Livewire/Pages/Articles/SingleTag.php | 10 +++++++- app/Livewire/Pages/Discussions/Index.php | 10 +++++++- app/Livewire/Pages/Forum/Index.php | 20 +++++++++++++++ app/Models/Article.php | 9 +++---- app/Models/Channel.php | 2 +- app/Models/Discussion.php | 11 ++++---- app/Models/Enterprise.php | 2 +- app/Models/Reply.php | 4 +-- app/Models/Scopes/LocaleScope.php | 17 ------------- app/Models/Tag.php | 2 +- app/Models/Thread.php | 15 ++++++----- app/{ => Models}/Traits/HasAuthor.php | 2 +- app/Models/Traits/HasLocaleScope.php | 15 +++++++++++ app/{ => Models}/Traits/HasReplies.php | 2 +- app/{ => Models}/Traits/HasSlug.php | 12 +++------ app/Notifications/ArticleSubmitted.php | 2 +- app/Notifications/NewCommentNotification.php | 5 ++-- app/Notifications/PostArticleToTelegram.php | 2 +- app/Notifications/PostArticleToTwitter.php | 2 +- .../PostDiscussionToTelegram.php | 2 +- app/Spotlight/Article.php | 2 +- app/Spotlight/Discussion.php | 2 +- app/Spotlight/Sujet.php | 2 +- app/Traits/WithLocale.php | 15 +++++++++++ app/helpers.php | 2 +- .../components/articles/card-author.blade.php | 2 +- .../components/discussions/summary.blade.php | 2 +- .../components/feeds/created_reply.blade.php | 2 +- .../components/locale-selector.blade.php | 25 +++++++++++++++++++ resources/views/components/tag.blade.php | 2 +- resources/views/emails/new_reply.blade.php | 4 +-- .../slideovers/article-form.blade.php | 2 +- .../livewire/pages/articles/index.blade.php | 8 +++--- .../livewire/pages/articles/tag.blade.php | 18 +++++++------ .../pages/discussions/index.blade.php | 7 ++++-- .../livewire/pages/forum/index.blade.php | 8 ++++-- resources/views/user/dashboard.blade.php | 6 ++--- 48 files changed, 204 insertions(+), 105 deletions(-) rename app/Actions/{Replies/CreateReply.php => Discussion/CreateDiscussionReplyAction.php} (80%) rename app/Actions/{Replies/LikeReply.php => Discussion/LikeReplyAction.php} (85%) rename app/Actions/Discussion/{NotifyUsersOfThreadConversion.php => NotifyUsersOfThreadConversionAction.php} (93%) delete mode 100644 app/Models/Scopes/LocaleScope.php rename app/{ => Models}/Traits/HasAuthor.php (94%) create mode 100644 app/Models/Traits/HasLocaleScope.php rename app/{ => Models}/Traits/HasReplies.php (98%) rename app/{ => Models}/Traits/HasSlug.php (78%) create mode 100644 app/Traits/WithLocale.php create mode 100644 resources/views/components/locale-selector.blade.php diff --git a/app/Actions/Discussion/ConvertDiscussionToThreadAction.php b/app/Actions/Discussion/ConvertDiscussionToThreadAction.php index f001314b..a9bdb1d3 100644 --- a/app/Actions/Discussion/ConvertDiscussionToThreadAction.php +++ b/app/Actions/Discussion/ConvertDiscussionToThreadAction.php @@ -28,7 +28,7 @@ public function execute(Discussion $discussion): Thread $discussion->delete(); - app(NotifyUsersOfThreadConversion::class)->execute($thread); + app(NotifyUsersOfThreadConversionAction::class)->execute($thread); return $thread; }); diff --git a/app/Actions/Replies/CreateReply.php b/app/Actions/Discussion/CreateDiscussionReplyAction.php similarity index 80% rename from app/Actions/Replies/CreateReply.php rename to app/Actions/Discussion/CreateDiscussionReplyAction.php index ac3cd4c2..ed35f06b 100644 --- a/app/Actions/Replies/CreateReply.php +++ b/app/Actions/Discussion/CreateDiscussionReplyAction.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace App\Actions\Replies; +namespace App\Actions\Discussion; use App\Events\CommentWasAdded; use App\Gamify\Points\ReplyCreated; @@ -10,7 +10,7 @@ use App\Models\User; use Illuminate\Database\Eloquent\Model; -final class CreateReply +final class CreateDiscussionReplyAction { public function __invoke(string $body, User $user, Model $model): Reply { @@ -22,7 +22,7 @@ public function __invoke(string $body, User $user, Model $model): Reply $user->givePoint(new ReplyCreated($model, $user)); // On envoie un event pour une nouvelle réponse à tous les abonnés de la discussion - event(new CommentWasAdded($reply, $model)); // @phpstan-ignore-line + event(new CommentWasAdded($reply, $model)); // @phpstan-ignore-line return $reply; } diff --git a/app/Actions/Replies/LikeReply.php b/app/Actions/Discussion/LikeReplyAction.php similarity index 85% rename from app/Actions/Replies/LikeReply.php rename to app/Actions/Discussion/LikeReplyAction.php index 6fd48277..0099caa6 100644 --- a/app/Actions/Replies/LikeReply.php +++ b/app/Actions/Discussion/LikeReplyAction.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace App\Actions\Replies; +namespace App\Actions\Discussion; use App\Models\Reaction; use App\Models\Reply; use App\Models\User; -final class LikeReply +final class LikeReplyAction { public function __invoke(User $user, Reply $reply, string $reaction = 'love'): void { diff --git a/app/Actions/Discussion/NotifyUsersOfThreadConversion.php b/app/Actions/Discussion/NotifyUsersOfThreadConversionAction.php similarity index 93% rename from app/Actions/Discussion/NotifyUsersOfThreadConversion.php rename to app/Actions/Discussion/NotifyUsersOfThreadConversionAction.php index 2c9120ba..18f3e575 100644 --- a/app/Actions/Discussion/NotifyUsersOfThreadConversion.php +++ b/app/Actions/Discussion/NotifyUsersOfThreadConversionAction.php @@ -10,7 +10,7 @@ use App\Notifications\ThreadConvertedByCreator; use Illuminate\Support\Facades\Auth; -final class NotifyUsersOfThreadConversion +final class NotifyUsersOfThreadConversionAction { public function execute(Thread $thread): void { diff --git a/app/Events/CommentWasAdded.php b/app/Events/CommentWasAdded.php index bf162991..f3ac67b7 100644 --- a/app/Events/CommentWasAdded.php +++ b/app/Events/CommentWasAdded.php @@ -4,6 +4,7 @@ namespace App\Events; +use App\Contracts\ReplyInterface; use App\Models\Discussion; use App\Models\Reply; use Illuminate\Queue\SerializesModels; @@ -12,5 +13,5 @@ { use SerializesModels; - public function __construct(public Reply $reply, public Discussion $discussion) {} + public function __construct(public Reply $reply, public Discussion|ReplyInterface $discussion) {} } diff --git a/app/Http/Controllers/SubscriptionController.php b/app/Http/Controllers/SubscriptionController.php index 102a1c16..83df2f8d 100644 --- a/app/Http/Controllers/SubscriptionController.php +++ b/app/Http/Controllers/SubscriptionController.php @@ -18,7 +18,7 @@ public function unsubscribe(Subscribe $subscription): RedirectResponse session()->flash('status', __('Vous êtes maintenant désabonné de ce sujet.')); - return redirect()->route('forum.show', $thread->slug()); + return redirect()->route('forum.show', $thread->slug); } public function redirect(int $id, string $type): RedirectResponse diff --git a/app/Livewire/Components/ChangeLocale.php b/app/Livewire/Components/ChangeLocale.php index 4ce220d4..891d2638 100644 --- a/app/Livewire/Components/ChangeLocale.php +++ b/app/Livewire/Components/ChangeLocale.php @@ -6,7 +6,6 @@ use Illuminate\Contracts\View\View; use Illuminate\Support\Facades\Auth; -use Illuminate\Support\Pluralizer; use Livewire\Attributes\Computed; use Livewire\Component; @@ -31,9 +30,7 @@ public function changeLocale(): void app()->setLocale($locale); session()->put('locale', $locale); - Pluralizer::useLanguage($this->currentLocale === 'fr' ? 'french' : 'english'); - - $this->redirectRoute('home', navigate: true); + $this->redirect(url()->previous(), navigate: true); } #[Computed] diff --git a/app/Livewire/Components/Discussion/Comment.php b/app/Livewire/Components/Discussion/Comment.php index 02c2a012..862046e3 100644 --- a/app/Livewire/Components/Discussion/Comment.php +++ b/app/Livewire/Components/Discussion/Comment.php @@ -4,7 +4,7 @@ namespace App\Livewire\Components\Discussion; -use App\Actions\Replies\LikeReply; +use App\Actions\Discussion\LikeReplyAction; use App\Models\Reply; use Filament\Notifications\Notification; use Illuminate\Contracts\View\View; @@ -32,7 +32,7 @@ public function toggleLike(): void { $this->authorize('like', $this->comment); - app()->call(LikeReply::class, [ + app()->call(LikeReplyAction::class, [ 'user' => auth()->user(), 'reply' => $this->comment, ]); diff --git a/app/Livewire/Components/Discussion/Comments.php b/app/Livewire/Components/Discussion/Comments.php index 540bdc54..4c61074b 100644 --- a/app/Livewire/Components/Discussion/Comments.php +++ b/app/Livewire/Components/Discussion/Comments.php @@ -4,7 +4,7 @@ namespace App\Livewire\Components\Discussion; -use App\Actions\Replies\CreateReply; +use App\Actions\Discussion\CreateDiscussionReplyAction; use App\Models\Discussion; use App\Models\Reply; use Filament\Forms; @@ -52,7 +52,7 @@ public function save(): void { $this->validate(); - app()->call(CreateReply::class, [ + app()->call(CreateDiscussionReplyAction::class, [ 'body' => data_get($this->form->getState(), 'body'), 'user' => Auth::user(), 'model' => $this->discussion, diff --git a/app/Livewire/Components/Slideovers/ArticleForm.php b/app/Livewire/Components/Slideovers/ArticleForm.php index f7660a35..a094a947 100644 --- a/app/Livewire/Components/Slideovers/ArticleForm.php +++ b/app/Livewire/Components/Slideovers/ArticleForm.php @@ -119,6 +119,18 @@ public function form(Form $form): Form Forms\Components\Group::make() ->schema([ Forms\Components\MarkdownEditor::make('body') + ->toolbarButtons([ + 'attachFiles', + 'blockquote', + 'bold', + 'bulletList', + 'codeBlock', + 'italic', + 'link', + 'orderedList', + 'strike', + 'table', + ]) ->label(__('validation.attributes.content')) ->fileAttachmentsDisk('public') ->minLength(10) diff --git a/app/Livewire/Pages/Articles/Index.php b/app/Livewire/Pages/Articles/Index.php index 9a3d00e4..150d5ed7 100644 --- a/app/Livewire/Pages/Articles/Index.php +++ b/app/Livewire/Pages/Articles/Index.php @@ -7,22 +7,30 @@ use App\Models\Article; use App\Models\Tag; use App\Traits\WithInfiniteScroll; +use App\Traits\WithLocale; use Illuminate\Contracts\View\View; use Livewire\Component; final class Index extends Component { use WithInfiniteScroll; + use WithLocale; + + public function mount(): void + { + $this->locale = config('app.locale'); + } public function render(): View { return view('livewire.pages.articles.index', [ - 'articles' => Article::with(['tags', 'user', 'user.transactions']) + 'articles' => Article::with(['tags', 'user', 'user.transactions']) // @phpstan-ignore-line ->withCount(['views', 'reactions']) ->orderByDesc('sponsored_at') ->orderByDesc('published_at') ->published() ->notPinned() + ->forLocale($this->locale) ->paginate($this->perPage), 'tags' => Tag::query()->whereHas('articles', function ($query): void { $query->published(); diff --git a/app/Livewire/Pages/Articles/SingleTag.php b/app/Livewire/Pages/Articles/SingleTag.php index 8d205617..97d72f08 100644 --- a/app/Livewire/Pages/Articles/SingleTag.php +++ b/app/Livewire/Pages/Articles/SingleTag.php @@ -7,19 +7,26 @@ use App\Models\Article; use App\Models\Tag; use App\Traits\WithInfiniteScroll; +use App\Traits\WithLocale; use Illuminate\Contracts\View\View; use Livewire\Component; final class SingleTag extends Component { use WithInfiniteScroll; + use WithLocale; public Tag $tag; + public function mount(): void + { + $this->locale = config('app.locale'); + } + public function render(): View { return view('livewire.pages.articles.tag', [ - 'articles' => Article::with(['tags', 'user', 'user.transactions']) + 'articles' => Article::with(['tags', 'user', 'user.transactions']) // @phpstan-ignore-line ->whereHas('tags', function ($query): void { $query->where('id', $this->tag->id); }) @@ -28,6 +35,7 @@ public function render(): View ->orderByDesc('published_at') ->published() ->notPinned() + ->forLocale($this->locale) ->paginate($this->perPage), ])->title($this->tag->name); } diff --git a/app/Livewire/Pages/Discussions/Index.php b/app/Livewire/Pages/Discussions/Index.php index f0714781..9e845e02 100644 --- a/app/Livewire/Pages/Discussions/Index.php +++ b/app/Livewire/Pages/Discussions/Index.php @@ -7,6 +7,7 @@ use App\Models\Builders\DiscussionQueryBuilder; use App\Models\Discussion; use App\Models\Tag; +use App\Traits\WithLocale; use Illuminate\Contracts\View\View; use Livewire\Attributes\Url; use Livewire\Component; @@ -15,6 +16,7 @@ final class Index extends Component { + use WithLocale; use WithoutUrlPagination; use WithPagination; @@ -23,6 +25,11 @@ final class Index extends Component public string $sortBy = 'recent'; + public function mount(): void + { + $this->locale = config('app.locale'); + } + public function validSort(string $sort): bool { return in_array($sort, [ @@ -50,8 +57,9 @@ public function tagExists(string $tag): bool public function render(): View { /** @var DiscussionQueryBuilder $query */ - $query = Discussion::with('tags') + $query = Discussion::with('tags') // @phpstan-ignore-line ->withCount('replies') + ->forLocale($this->locale) ->notPinned(); $tags = Tag::query() diff --git a/app/Livewire/Pages/Forum/Index.php b/app/Livewire/Pages/Forum/Index.php index 1c1987ec..72e3f2a8 100644 --- a/app/Livewire/Pages/Forum/Index.php +++ b/app/Livewire/Pages/Forum/Index.php @@ -6,6 +6,7 @@ use App\Models\Channel; use App\Models\Thread; +use App\Traits\WithLocale; use Illuminate\Contracts\View\View; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Facades\Auth; @@ -19,6 +20,7 @@ #[Layout('layouts.forum')] final class Index extends Component { + use WithLocale; use WithoutUrlPagination; use WithPagination; @@ -48,6 +50,8 @@ public function mount(): void if ($this->channel) { $this->currentChannel = Channel::findBySlug($this->channel); } + + $this->locale = config('app.locale'); } #[On('channelUpdated')] @@ -63,6 +67,12 @@ public function reloadThreads(?int $channelId): void $this->dispatch('render'); } + #[On('redirectToLogin')] + public function redirectToLogin(): void + { + $this->redirectRoute('login', navigate: true); + } + protected function applySearch(Builder $query): Builder { if ($this->search) { @@ -87,6 +97,15 @@ protected function applySolved(Builder $query): Builder return $query; } + protected function applyLocale(Builder $query): Builder + { + if ($this->locale) { + $query->forLocale($this->locale); // @phpstan-ignore-line + } + + return $query; + } + protected function applyAuthor(Builder $query): Builder { if (Auth::check() && $this->user) { @@ -125,6 +144,7 @@ public function render(): View $query = $this->applyChannel($query); $query = $this->applySearch($query); $query = $this->applySolved($query); + $query = $this->applyLocale($query); $query = $this->applyAuthor($query); $query = $this->applySubscribe($query); $query = $this->applyUnAnswer($query); diff --git a/app/Models/Article.php b/app/Models/Article.php index 38e1135c..cecbbbcb 100644 --- a/app/Models/Article.php +++ b/app/Models/Article.php @@ -6,15 +6,14 @@ use App\Contracts\ReactableInterface; use App\Models\Builders\ArticleQueryBuilder; -use App\Models\Scopes\LocaleScope; -use App\Traits\HasAuthor; -use App\Traits\HasSlug; +use App\Models\Traits\HasAuthor; +use App\Models\Traits\HasLocaleScope; +use App\Models\Traits\HasSlug; use App\Traits\HasTags; use App\Traits\Reactable; use App\Traits\RecordsActivity; use CyrildeWit\EloquentViewable\Contracts\Viewable; use CyrildeWit\EloquentViewable\InteractsWithViews; -use Illuminate\Database\Eloquent\Attributes\ScopedBy; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; @@ -43,11 +42,11 @@ * @property \Illuminate\Support\Carbon $updated_at * @property \Illuminate\Database\Eloquent\Collection | Tag[] $tags */ -#[ScopedBy([LocaleScope::class])] final class Article extends Model implements HasMedia, ReactableInterface, Viewable { use HasAuthor; use HasFactory; + use HasLocaleScope; use HasSlug; use HasTags; use InteractsWithMedia; diff --git a/app/Models/Channel.php b/app/Models/Channel.php index a8d7853a..98e4cdf0 100644 --- a/app/Models/Channel.php +++ b/app/Models/Channel.php @@ -5,7 +5,7 @@ namespace App\Models; use App\Exceptions\CannotAddChannelToChild; -use App\Traits\HasSlug; +use App\Models\Traits\HasSlug; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; diff --git a/app/Models/Discussion.php b/app/Models/Discussion.php index e708d3a3..fe2390c3 100644 --- a/app/Models/Discussion.php +++ b/app/Models/Discussion.php @@ -9,10 +9,10 @@ use App\Contracts\SpamReportableContract; use App\Contracts\SubscribeInterface; use App\Models\Builders\DiscussionQueryBuilder; -use App\Models\Scopes\LocaleScope; -use App\Traits\HasAuthor; -use App\Traits\HasReplies; -use App\Traits\HasSlug; +use App\Models\Traits\HasAuthor; +use App\Models\Traits\HasLocaleScope; +use App\Models\Traits\HasReplies; +use App\Models\Traits\HasSlug; use App\Traits\HasSpamReports; use App\Traits\HasSubscribers; use App\Traits\HasTags; @@ -21,7 +21,6 @@ use Carbon\Carbon; use CyrildeWit\EloquentViewable\Contracts\Viewable; use CyrildeWit\EloquentViewable\InteractsWithViews; -use Illuminate\Database\Eloquent\Attributes\ScopedBy; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -42,11 +41,11 @@ * @property User $user * @property Collection | SpamReport[] $spamReports */ -#[ScopedBy([LocaleScope::class])] final class Discussion extends Model implements ReactableInterface, ReplyInterface, SpamReportableContract, SubscribeInterface, Viewable { use HasAuthor; use HasFactory; + use HasLocaleScope; use HasReplies; use HasSlug; use HasSpamReports; diff --git a/app/Models/Enterprise.php b/app/Models/Enterprise.php index 24091ab2..e2061df0 100644 --- a/app/Models/Enterprise.php +++ b/app/Models/Enterprise.php @@ -5,8 +5,8 @@ namespace App\Models; use App\Filters\Enterprise\EnterpriseFilters; +use App\Models\Traits\HasSlug; use App\Traits\HasSettings; -use App\Traits\HasSlug; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; diff --git a/app/Models/Reply.php b/app/Models/Reply.php index 4b9067fc..a01f91fe 100644 --- a/app/Models/Reply.php +++ b/app/Models/Reply.php @@ -7,8 +7,8 @@ use App\Contracts\ReactableInterface; use App\Contracts\ReplyInterface; use App\Contracts\SpamReportableContract; -use App\Traits\HasAuthor; -use App\Traits\HasReplies; +use App\Models\Traits\HasAuthor; +use App\Models\Traits\HasReplies; use App\Traits\HasSpamReports; use App\Traits\Reactable; use App\Traits\RecordsActivity; diff --git a/app/Models/Scopes/LocaleScope.php b/app/Models/Scopes/LocaleScope.php deleted file mode 100644 index 7a3851ff..00000000 --- a/app/Models/Scopes/LocaleScope.php +++ /dev/null @@ -1,17 +0,0 @@ -where('locale', app()->getLocale()); - } -} diff --git a/app/Models/Tag.php b/app/Models/Tag.php index 44ea1db3..f6053d58 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -4,7 +4,7 @@ namespace App\Models; -use App\Traits\HasSlug; +use App\Models\Traits\HasSlug; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphToMany; diff --git a/app/Models/Thread.php b/app/Models/Thread.php index cfa62aa3..156327be 100644 --- a/app/Models/Thread.php +++ b/app/Models/Thread.php @@ -10,10 +10,10 @@ use App\Contracts\SubscribeInterface; use App\Exceptions\CouldNotMarkReplyAsSolution; use App\Filters\Thread\ThreadFilters; -use App\Models\Scopes\LocaleScope; -use App\Traits\HasAuthor; -use App\Traits\HasReplies; -use App\Traits\HasSlug; +use App\Models\Traits\HasAuthor; +use App\Models\Traits\HasLocaleScope; +use App\Models\Traits\HasReplies; +use App\Models\Traits\HasSlug; use App\Traits\HasSpamReports; use App\Traits\HasSubscribers; use App\Traits\Reactable; @@ -22,7 +22,6 @@ use CyrildeWit\EloquentViewable\Contracts\Viewable; use CyrildeWit\EloquentViewable\InteractsWithViews; use Exception; -use Illuminate\Database\Eloquent\Attributes\ScopedBy; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -53,11 +52,11 @@ * @property Reply | null $solutionReply * @property \Illuminate\Database\Eloquent\Collection | Channel[] $channels */ -#[ScopedBy([LocaleScope::class])] final class Thread extends Model implements Feedable, ReactableInterface, ReplyInterface, SpamReportableContract, SubscribeInterface, Viewable { use HasAuthor; use HasFactory; + use HasLocaleScope; use HasReplies; use HasSlug; use HasSpamReports; @@ -100,7 +99,7 @@ public function replyAbleSubject(): string public function getPathUrl(): string { - return route('forum.show', $this->slug()); + return route('forum.show', $this->slug); } public function excerpt(int $limit = 200): string @@ -172,7 +171,7 @@ public function scopeChannel(Builder $query, Channel $channel): Builder if ($channel->hasItems()) { $query->whereIn('channels.id', array_merge([$channel->id], $channel->items->modelKeys())); } else { - $query->where('channels.slug', $channel->slug()); + $query->where('channels.slug', $channel->slug); } }); } diff --git a/app/Traits/HasAuthor.php b/app/Models/Traits/HasAuthor.php similarity index 94% rename from app/Traits/HasAuthor.php rename to app/Models/Traits/HasAuthor.php index a2e5ceb2..6d641272 100644 --- a/app/Traits/HasAuthor.php +++ b/app/Models/Traits/HasAuthor.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace App\Traits; +namespace App\Models\Traits; use App\Models\User; use Illuminate\Database\Eloquent\Relations\BelongsTo; diff --git a/app/Models/Traits/HasLocaleScope.php b/app/Models/Traits/HasLocaleScope.php new file mode 100644 index 00000000..4e350a87 --- /dev/null +++ b/app/Models/Traits/HasLocaleScope.php @@ -0,0 +1,15 @@ +where('locale', $locale); + } +} diff --git a/app/Traits/HasReplies.php b/app/Models/Traits/HasReplies.php similarity index 98% rename from app/Traits/HasReplies.php rename to app/Models/Traits/HasReplies.php index 68e692d3..ebfd1363 100644 --- a/app/Traits/HasReplies.php +++ b/app/Models/Traits/HasReplies.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace App\Traits; +namespace App\Models\Traits; use App\Models\Reply; use Illuminate\Database\Eloquent\Collection; diff --git a/app/Traits/HasSlug.php b/app/Models/Traits/HasSlug.php similarity index 78% rename from app/Traits/HasSlug.php rename to app/Models/Traits/HasSlug.php index 7211c9ad..2b2e2cba 100644 --- a/app/Traits/HasSlug.php +++ b/app/Models/Traits/HasSlug.php @@ -2,20 +2,16 @@ declare(strict_types=1); -namespace App\Traits; +namespace App\Models\Traits; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Support\Str; trait HasSlug { - public function slug(): string + protected function slug(): Attribute { - return $this->slug; - } - - public function setSlugAttribute(string $slug): void - { - $this->attributes['slug'] = $this->generateUniqueSlug($slug); + return Attribute::set(fn (string $value) => $this->generateUniqueSlug($value)); } public static function findBySlug(string $slug): static diff --git a/app/Notifications/ArticleSubmitted.php b/app/Notifications/ArticleSubmitted.php index 8ace5eb0..96782f59 100644 --- a/app/Notifications/ArticleSubmitted.php +++ b/app/Notifications/ArticleSubmitted.php @@ -31,7 +31,7 @@ public function via(mixed $notifiable): array public function toTelegram(): TelegramMessage { - $url = route('articles.show', $this->article->slug()); + $url = route('articles.show', $this->article->slug); return TelegramMessage::create() ->to(config('services.telegram-bot-api.channel')) diff --git a/app/Notifications/NewCommentNotification.php b/app/Notifications/NewCommentNotification.php index 5b1f0b43..1583ac5c 100644 --- a/app/Notifications/NewCommentNotification.php +++ b/app/Notifications/NewCommentNotification.php @@ -4,6 +4,7 @@ namespace App\Notifications; +use App\Contracts\ReplyInterface; use App\Models\Discussion; use App\Models\Reply; use App\Models\Subscribe; @@ -19,7 +20,7 @@ final class NewCommentNotification extends Notification implements ShouldQueue public function __construct( public readonly Reply $reply, public readonly Subscribe $subscription, - public readonly Discussion $discussion + public readonly Discussion|ReplyInterface $discussion ) {} /** @@ -34,7 +35,7 @@ public function toMail(): MailMessage { return (new MailMessage) ->subject("Re: {$this->discussion->subject()}") - ->line(__('@:name a répondu à ce sujet.', ['name' => $this->reply->user?->username])) + ->line(__('@:name a répondu à ce sujet.', ['name' => $this->reply->user->username])) ->line($this->reply->excerpt(150)) ->action(__('Voir la discussion'), route('discussions.show', $this->discussion)) ->line(__('Vous recevez ceci parce que vous êtes abonné à cette discussion.')); diff --git a/app/Notifications/PostArticleToTelegram.php b/app/Notifications/PostArticleToTelegram.php index 1903dfe7..b1d4ecb3 100644 --- a/app/Notifications/PostArticleToTelegram.php +++ b/app/Notifications/PostArticleToTelegram.php @@ -25,6 +25,6 @@ public function toTelegram(): TelegramMessage { return TelegramMessage::create() ->to('@laravelcm') - ->content("{$this->article->title} ".route('articles.show', $this->article->slug())); + ->content("{$this->article->title} ".route('articles.show', $this->article->slug)); } } diff --git a/app/Notifications/PostArticleToTwitter.php b/app/Notifications/PostArticleToTwitter.php index c2243ff7..7135a9c1 100644 --- a/app/Notifications/PostArticleToTwitter.php +++ b/app/Notifications/PostArticleToTwitter.php @@ -38,7 +38,7 @@ public function toTwitter(): TwitterStatusUpdate public function generateTweet(): string { $title = $this->article->title; - $url = route('articles.show', $this->article->slug()); + $url = route('articles.show', $this->article->slug); $author = $this->article->user; $author = $author->twitter() ? "@{$author->twitter()}" : $author->name; diff --git a/app/Notifications/PostDiscussionToTelegram.php b/app/Notifications/PostDiscussionToTelegram.php index 5d12b379..d254a26a 100644 --- a/app/Notifications/PostDiscussionToTelegram.php +++ b/app/Notifications/PostDiscussionToTelegram.php @@ -25,6 +25,6 @@ public function toTelegram(): TelegramMessage { return TelegramMessage::create() ->to('@laravelcm') - ->content("{$this->discussion->title} ".route('discussions.show', $this->discussion->slug())); + ->content("{$this->discussion->title} ".route('discussions.show', $this->discussion->slug)); } } diff --git a/app/Spotlight/Article.php b/app/Spotlight/Article.php index 8a3adbb6..4413660e 100644 --- a/app/Spotlight/Article.php +++ b/app/Spotlight/Article.php @@ -36,7 +36,7 @@ public function searchArticle(string $query): Collection ->where('title', 'like', "%{$query}%") ->get() ->map(fn (ArticleModel $article) => new SpotlightSearchResult( - $article->slug(), + $article->slug, $article->title, sprintf('par @%s', $article->user->username) )); diff --git a/app/Spotlight/Discussion.php b/app/Spotlight/Discussion.php index d5b11292..2a8605a4 100644 --- a/app/Spotlight/Discussion.php +++ b/app/Spotlight/Discussion.php @@ -36,7 +36,7 @@ public function searchDiscussion(string $query): Collection ->get() // @phpstan-ignore-next-line ->map(fn (DiscussionModel $discussion) => new SpotlightSearchResult( - $discussion->slug(), + $discussion->slug, $discussion->title, sprintf('par @%s', $discussion->user->username) )); diff --git a/app/Spotlight/Sujet.php b/app/Spotlight/Sujet.php index 9703a0f8..eaff8699 100644 --- a/app/Spotlight/Sujet.php +++ b/app/Spotlight/Sujet.php @@ -40,7 +40,7 @@ public function searchThread(string $query): Collection ->where('title', 'like', "%{$query}%") ->get() ->map(fn (Thread $thread) => new SpotlightSearchResult( - $thread->slug(), + $thread->slug, $thread->title, sprintf('par @%s', $thread->user->username) )); diff --git a/app/Traits/WithLocale.php b/app/Traits/WithLocale.php new file mode 100644 index 00000000..82367bf2 --- /dev/null +++ b/app/Traits/WithLocale.php @@ -0,0 +1,15 @@ +locale = $locale; + } +} diff --git a/app/helpers.php b/app/helpers.php index 980dfd97..8525f126 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -88,6 +88,6 @@ function route_to_reply_able(mixed $replyAble): string { $routeName = $replyAble instanceof \App\Models\Thread ? 'forum.show' : 'discussions.show'; - return route($routeName, $replyAble->slug()); + return route($routeName, $replyAble->slug); } } diff --git a/resources/views/components/articles/card-author.blade.php b/resources/views/components/articles/card-author.blade.php index ed884899..98ae8752 100644 --- a/resources/views/components/articles/card-author.blade.php +++ b/resources/views/components/articles/card-author.blade.php @@ -8,7 +8,7 @@ : asset('images/socialcard.png') @endphp -
+
{{ __('réponses') }}

{{ $activity->subject->replyAble->title }} diff --git a/resources/views/components/locale-selector.blade.php b/resources/views/components/locale-selector.blade.php new file mode 100644 index 00000000..f75569f8 --- /dev/null +++ b/resources/views/components/locale-selector.blade.php @@ -0,0 +1,25 @@ +@props([ + 'locale', +]) + +
twMerge(['class' => 'inline-flex items-center justify-start rounded-lg bg-gray-100/50 p-1 h-10 ring-1 ring-gray-200 dark:ring-white/10 dark:bg-gray-800 sm:w-auto']) }} +> + @foreach(config('lcm.supported_locales') as $supportedLocale) + + @endforeach +
diff --git a/resources/views/components/tag.blade.php b/resources/views/components/tag.blade.php index 11094530..5cdd1084 100644 --- a/resources/views/components/tag.blade.php +++ b/resources/views/components/tag.blade.php @@ -6,7 +6,7 @@ :href="route('articles.tag', $tag)" {{ $attributes->twMerge(['class' => 'inline-flex items-center text-sm gap-1.5 relative z-10 rounded-lg bg-gray-100 px-2 py-1 font-medium text-gray-600 hover:bg-gray-200/50 dark:bg-gray-800 dark:text-white']) }} > - + {{ $tag->name }} diff --git a/resources/views/emails/new_reply.blade.php b/resources/views/emails/new_reply.blade.php index b65935bf..932566e7 100644 --- a/resources/views/emails/new_reply.blade.php +++ b/resources/views/emails/new_reply.blade.php @@ -5,11 +5,11 @@ {{ $reply->excerpt(200) }} @endcomponent - @component('mail::button', ['url' => route('forum.show', $reply->replyAble->slug()), 'color' => 'green']) + @component('mail::button', ['url' => route('forum.show', $reply->replyAble->slug), 'color' => 'green']) Voir le sujet @endcomponent @component('mail::subcopy') Vous recevez ceci parce que vous êtes abonné à ce sujet. [Se désabonner]({{ route('subscriptions.unsubscribe', $subscription->uuid()->toString()) }}) de ce sujet. @endcomponent -@endcomponent \ No newline at end of file +@endcomponent diff --git a/resources/views/livewire/components/slideovers/article-form.blade.php b/resources/views/livewire/components/slideovers/article-form.blade.php index 65ab2845..78bfb71d 100644 --- a/resources/views/livewire/components/slideovers/article-form.blade.php +++ b/resources/views/livewire/components/slideovers/article-form.blade.php @@ -7,7 +7,7 @@

{{ __('pages/article.blog_summary') }}

+
+
@@ -77,7 +79,7 @@ class="hide-scroll -mb-px flex items-center space-x-2 overflow-x-auto scroll-smo :href="route('articles.tag', $tag)" class="inline-flex items-center rounded-lg py-1.5 px-3 text-nowrap gap-2 text-gray-500 transition-colors duration-200 ease-in-out hover:bg-gray-50 hover:text-gray-700 dark:hover:bg-gray-900/70 dark:text-gray-400 dark:hover:text-white" > -
+
-
-
@foreach ($articles as $article) - + @endforeach
diff --git a/resources/views/livewire/pages/discussions/index.blade.php b/resources/views/livewire/pages/discussions/index.blade.php index 98a1bd5d..76846ba9 100644 --- a/resources/views/livewire/pages/discussions/index.blade.php +++ b/resources/views/livewire/pages/discussions/index.blade.php @@ -1,9 +1,9 @@
-
+
@@ -27,6 +27,7 @@ class="w-2/3 bg-white rounded-xl ring-1 ring-gray-200/60 dark:bg-gray-800 dark:r
+
@@ -48,6 +49,8 @@ class="w-2/3 bg-white rounded-xl ring-1 ring-gray-200/60 dark:bg-gray-800 dark:r + + @can('create', \App\Models\Discussion::class) {{ __('pages/forum.new_thread') }} @@ -13,7 +13,11 @@ class="gap-2 w-full justify-center py-2.5"
- +
+ + + +
diff --git a/resources/views/user/dashboard.blade.php b/resources/views/user/dashboard.blade.php index 08147b1a..58cff4dc 100644 --- a/resources/views/user/dashboard.blade.php +++ b/resources/views/user/dashboard.blade.php @@ -57,7 +57,7 @@ class="inline-flex items-center rounded-full bg-yellow-100 px-2.5 py-0.5 text-xs @endforeach
- +

@if ($article->isPublished()) Voir @@ -77,7 +77,7 @@ class="hover:text-gray-500 dark:text-gray-400 hover:underline" @endif Éditer From 33879d1e5f7acea9eb20ac59ca4b74a3e3279070 Mon Sep 17 00:00:00 2001 From: Arthur Monney Date: Thu, 19 Dec 2024 01:13:58 +0100 Subject: [PATCH 2/2] chore: [LAR-131] fix phpstan and tests --- app/Events/CommentWasAdded.php | 3 +-- phpstan.neon | 2 +- tests/Feature/Forum/DiscussionTest.php | 6 +++--- tests/Feature/Forum/ThreadTest.php | 6 +++--- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/app/Events/CommentWasAdded.php b/app/Events/CommentWasAdded.php index f3ac67b7..bf162991 100644 --- a/app/Events/CommentWasAdded.php +++ b/app/Events/CommentWasAdded.php @@ -4,7 +4,6 @@ namespace App\Events; -use App\Contracts\ReplyInterface; use App\Models\Discussion; use App\Models\Reply; use Illuminate\Queue\SerializesModels; @@ -13,5 +12,5 @@ { use SerializesModels; - public function __construct(public Reply $reply, public Discussion|ReplyInterface $discussion) {} + public function __construct(public Reply $reply, public Discussion $discussion) {} } diff --git a/phpstan.neon b/phpstan.neon index 9b95bb97..81c90bf5 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -12,7 +12,7 @@ parameters: # Remove this config after migrate everything to livewire - app/Http/Controllers/* - app/Markdown/* - - app/Traits/HasSlug.php + - app/Models/Traits/HasSlug.php ignoreErrors: - "#^Cannot access property \\$transaction on array\\|object\\.$#" - identifier: missingType.iterableValue diff --git a/tests/Feature/Forum/DiscussionTest.php b/tests/Feature/Forum/DiscussionTest.php index 95ce0ef3..9ed787d1 100644 --- a/tests/Feature/Forum/DiscussionTest.php +++ b/tests/Feature/Forum/DiscussionTest.php @@ -47,19 +47,19 @@ it('generates a slug when valid url characters provided', function (): void { $discussion = Discussion::factory()->make(['slug' => 'Help with eloquent']); - expect($discussion->slug())->toEqual('help-with-eloquent'); + expect($discussion->slug)->toEqual('help-with-eloquent'); }); it('generates a unique slug when valid url characters provided', function (): void { $discussionOne = Discussion::factory()->create(['slug' => 'Help with eloquent']); $discussionTwo = Discussion::factory()->create(['slug' => 'Help with eloquent']); - expect($discussionTwo->slug())->toEqual('help-with-eloquent-1'); + expect($discussionTwo->slug)->toEqual('help-with-eloquent-1'); }); it('generates a slug when invalid url characters provided', function (): void { $discussion = Discussion::factory()->make(['slug' => '한글 테스트']); // When providing a slug with invalid url characters, a random 5 character string is returned. - expect($discussion->slug())->toMatch('/\w{5}/'); + expect($discussion->slug)->toMatch('/\w{5}/'); }); diff --git a/tests/Feature/Forum/ThreadTest.php b/tests/Feature/Forum/ThreadTest.php index 08225e5c..6970b388 100644 --- a/tests/Feature/Forum/ThreadTest.php +++ b/tests/Feature/Forum/ThreadTest.php @@ -145,19 +145,19 @@ it('generates a slug when valid url characters provided', function (): void { $thread = Thread::factory()->make(['slug' => 'Help with eloquent']); - expect($thread->slug())->toEqual('help-with-eloquent'); + expect($thread->slug)->toEqual('help-with-eloquent'); }); it('generates a unique slug when valid url characters provided', function (): void { Thread::factory()->create(['slug' => 'Help with eloquent']); $thread = Thread::factory()->create(['slug' => 'Help with eloquent']); - expect($thread->slug())->toEqual('help-with-eloquent-1'); + expect($thread->slug)->toEqual('help-with-eloquent-1'); }); it('generates a slug when invalid url characters provided', function (): void { $thread = Thread::factory()->make(['slug' => '한글 테스트']); // When providing a slug with invalid url characters, a random 5 character string is returned. - expect($thread->slug())->toMatch('/\w{5}/'); + expect($thread->slug)->toMatch('/\w{5}/'); });