Skip to content

Commit c4c3aa1

Browse files
committed
Fix articles code
1 parent 469614a commit c4c3aa1

File tree

6 files changed

+42
-23
lines changed

6 files changed

+42
-23
lines changed

app/Http/Controllers/Articles/ArticlesController.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,27 @@ public function index(Request $request)
4545
->latest('approved_at')
4646
->{$filter}();
4747

48-
$tags = Tag::whereHas('articles', function ($query) {
49-
$query->published();
50-
})->orderBy('name')->get();
48+
$tags = Tag::whereHas('articles', fn ($query) => $query->published())
49+
->orderBy('name')
50+
->get();
5151

5252
if ($activeTag = Tag::where('slug', $request->tag)->first()) {
5353
$articles->forTag($activeTag->slug());
5454
}
5555

56-
$moderators = Cache::remember('moderators', now()->addMinutes(30), function () {
57-
return User::moderators()->get();
58-
});
59-
6056
$canonical = canonical('articles', ['filter' => $filter, 'tag' => $activeTag?->slug()]);
61-
$topAuthors = Cache::remember('topAuthors', now()->addMinutes(30), function () {
62-
return User::mostSubmissionsInLastDays(365)->take(5)->get();
63-
});
57+
$topAuthors = Cache::remember(
58+
'topAuthors',
59+
now()->addMinutes(30),
60+
fn () => User::mostSubmissionsInLastDays(365)->take(5)->get()
61+
);
6462

6563
return view('articles.overview', [
6664
'pinnedArticles' => $pinnedArticles,
6765
'articles' => $articles->paginate(10),
6866
'tags' => $tags,
6967
'activeTag' => $activeTag,
7068
'filter' => $filter,
71-
'moderators' => $moderators,
7269
'canonical' => $canonical,
7370
'topAuthors' => $topAuthors,
7471
]);
@@ -155,7 +152,7 @@ public function update(ArticleRequest $request, Article $article)
155152

156153
$article = $article->fresh();
157154

158-
if ($wasNotPreviouslySubmitted && $request->shouldBeSubmitted()) {
155+
if ($wasNotPreviouslySubmitted && $request->shouldBeSubmitted() && $article->isNotApproved()) {
159156
$this->success('Thank you for submitting, unfortunately we can\'t accept every submission. You\'ll only hear back from us when we accept your article.');
160157
} else {
161158
$this->success('Article successfully updated!');

app/Jobs/CreateArticle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,6 @@ public function handle(): void
6969

7070
private function canBeAutoApproved(): bool
7171
{
72-
return $this->shouldBeSubmitted && $this->author->canVerifiedAuthorPublishMoreArticleToday();
72+
return $this->shouldBeSubmitted && $this->author->canVerifiedAuthorPublishMoreArticlesToday();
7373
}
7474
}

app/Jobs/UpdateArticle.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,12 @@ public function handle(): void
5353

5454
if ($this->shouldUpdateSubmittedAt()) {
5555
$this->article->submitted_at = now();
56+
$this->article->approved_at = $this->canBeAutoApproved() ? now() : null;
5657
$this->article->save();
5758

58-
event(new ArticleWasSubmittedForApproval($this->article));
59+
if ($this->article->isAwaitingApproval()) {
60+
event(new ArticleWasSubmittedForApproval($this->article));
61+
}
5962
}
6063

6164
$this->article->syncTags($this->tags);
@@ -69,4 +72,9 @@ private function shouldUpdateSubmittedAt(): bool
6972
{
7073
return $this->shouldBeSubmitted && $this->article->isNotSubmitted();
7174
}
75+
76+
private function canBeAutoApproved(): bool
77+
{
78+
return $this->article->author()->canVerifiedAuthorPublishMoreArticlesToday();
79+
}
7280
}

app/Models/Article.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public function isShared(): bool
196196

197197
public function isAwaitingApproval(): bool
198198
{
199-
return $this->isSubmitted() && $this->isNotApproved() && $this->isNotDeclined() && ! $this->author()->canVerifiedAuthorPublishMoreArticleToday();
199+
return $this->isSubmitted() && $this->isNotApproved() && $this->isNotDeclined();
200200
}
201201

202202
public function isNotAwaitingApproval(): bool

app/Models/User.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ public function isVerifiedAuthor(): bool
317317
return ! is_null($this->author_verified_at) || $this->isAdmin();
318318
}
319319

320-
public function canVerifiedAuthorPublishMoreArticleToday(): bool
320+
public function canVerifiedAuthorPublishMoreArticlesToday(): bool
321321
{
322322
if ($this->isAdmin()) {
323323
return true;
@@ -364,13 +364,27 @@ public function scopeMostSolutions(Builder $query, ?int $inLastDays = null)
364364

365365
public function scopeMostSubmissions(Builder $query, ?int $inLastDays = null)
366366
{
367-
return $query->withCount(['articles as articles_count' => function ($query) use ($inLastDays) {
368-
if ($inLastDays) {
369-
$query->where('articles.approved_at', '>', now()->subDays($inLastDays));
370-
}
367+
return $query
368+
->selectRaw('users.*, COUNT(DISTINCT articles.id) AS articles_count')
369+
->join('articles', 'articles.author_id', '=', 'users.id')
370+
->where(function ($query) use ($inLastDays) {
371+
if ($inLastDays) {
372+
$query->where('articles.approved_at', '>', now()->subDays($inLastDays));
373+
}
374+
375+
return $query;
376+
})
377+
->groupBy('users.id')
378+
->having('articles_count', '>', 0)
379+
->orderBy('articles_count', 'desc');
380+
381+
// return $query->withCount(['articles as articles_count' => function ($query) use ($inLastDays) {
382+
// if ($inLastDays) {
383+
// $query->where('articles.approved_at', '>', now()->subDays($inLastDays));
384+
// }
371385

372-
return $query;
373-
}])->orderBy('articles_count', 'desc');
386+
// return $query;
387+
// }])->orderBy('articles_count', 'desc');
374388
}
375389

376390
public function scopeMostSolutionsInLastDays(Builder $query, int $days)

tests/Feature/ArticleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@
594594
'submitted_at' => now()->addMinutes(1), // after verification
595595
]);
596596

597-
expect($author->canVerifiedAuthorPublishMoreArticleToday())->toBeFalse();
597+
expect($author->canVerifiedAuthorPublishMoreArticlesToday())->toBeFalse();
598598
});
599599

600600
test('verified authors skip the approval message when submitting new article', function () {

0 commit comments

Comments
 (0)