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
21 changes: 9 additions & 12 deletions app/Http/Controllers/Articles/ArticlesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,27 @@ public function index(Request $request)
->latest('approved_at')
->{$filter}();

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

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

$moderators = Cache::remember('moderators', now()->addMinutes(30), function () {
return User::moderators()->get();
});

$canonical = canonical('articles', ['filter' => $filter, 'tag' => $activeTag?->slug()]);
$topAuthors = Cache::remember('topAuthors', now()->addMinutes(30), function () {
return User::mostSubmissionsInLastDays(365)->take(5)->get();
});
$topAuthors = Cache::remember(
'topAuthors',
now()->addMinutes(30),
fn () => User::mostSubmissionsInLastDays(365)->take(5)->get()
);

return view('articles.overview', [
'pinnedArticles' => $pinnedArticles,
'articles' => $articles->paginate(10),
'tags' => $tags,
'activeTag' => $activeTag,
'filter' => $filter,
'moderators' => $moderators,
'canonical' => $canonical,
'topAuthors' => $topAuthors,
]);
Expand Down Expand Up @@ -155,7 +152,7 @@ public function update(ArticleRequest $request, Article $article)

$article = $article->fresh();

if ($wasNotPreviouslySubmitted && $request->shouldBeSubmitted()) {
if ($wasNotPreviouslySubmitted && $request->shouldBeSubmitted() && $article->isNotApproved()) {
$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.');
} else {
$this->success('Article successfully updated!');
Expand Down
47 changes: 19 additions & 28 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,34 @@ class HomeController extends Controller
{
public function show()
{
$communityMembers = Cache::remember('communityMembers', now()->addMinutes(5), function () {
return User::withCounts()
->hasActivity()
->notBanned()
->inRandomOrder()
->take(100)
->get()
->chunk(20);
});
$communityMembers = Cache::remember(
'communityMembers',
now()->addMinutes(5),
fn () => User::notBanned()->inRandomOrder()->take(100)->get()->chunk(20)
);

$totalUsers = Cache::remember('totalUsers', now()->addDay(), function () {
return number_format(User::notBanned()->count());
});
$totalUsers = Cache::remember('totalUsers', now()->addDay(), fn () => number_format(User::notBanned()->count()));

$totalThreads = Cache::remember('totalThreads', now()->addDay(), function () {
return number_format(Thread::count());
});
$totalThreads = Cache::remember('totalThreads', now()->addDay(), fn () => number_format(Thread::count()));

$totalReplies = Cache::remember('totalReplies', now()->addDay(), function () {
return number_format(Reply::count());
});
$totalReplies = Cache::remember('totalReplies', now()->addDay(), fn () => number_format(Reply::count()));

$latestThreads = Cache::remember('latestThreads', now()->addHour(), function () {
return Thread::whereNull('solution_reply_id')
$latestThreads = Cache::remember(
'latestThreads',
now()->addHour(),
fn () => Thread::whereNull('solution_reply_id')
->whereBetween('threads.created_at', [now()->subMonth(), now()])
->unlocked()
->inRandomOrder()
->limit(3)
->get();
});
->get()
);

$latestArticles = Cache::remember('latestArticles', now()->addHour(), function () {
return Article::published()
->recent()
->limit(4)
->get();
});
$latestArticles = Cache::remember(
'latestArticles',
now()->addHour(),
fn () => Article::published()->recent()->limit(4)->get()
);

return view('home', [
'communityMembers' => $communityMembers,
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/CreateArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ public function handle(): void

private function canBeAutoApproved(): bool
{
return $this->shouldBeSubmitted && $this->author->canVerifiedAuthorPublishMoreArticleToday();
return $this->shouldBeSubmitted && $this->author->canVerifiedAuthorPublishMoreArticlesToday();
}
}
10 changes: 9 additions & 1 deletion app/Jobs/UpdateArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ public function handle(): void

if ($this->shouldUpdateSubmittedAt()) {
$this->article->submitted_at = now();
$this->article->approved_at = $this->canBeAutoApproved() ? now() : null;
$this->article->save();

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

$this->article->syncTags($this->tags);
Expand All @@ -69,4 +72,9 @@ private function shouldUpdateSubmittedAt(): bool
{
return $this->shouldBeSubmitted && $this->article->isNotSubmitted();
}

private function canBeAutoApproved(): bool
{
return $this->article->author()->canVerifiedAuthorPublishMoreArticlesToday();
}
}
2 changes: 1 addition & 1 deletion app/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function isShared(): bool

public function isAwaitingApproval(): bool
{
return $this->isSubmitted() && $this->isNotApproved() && $this->isNotDeclined() && ! $this->author()->canVerifiedAuthorPublishMoreArticleToday();
return $this->isSubmitted() && $this->isNotApproved() && $this->isNotDeclined();
}

public function isNotAwaitingApproval(): bool
Expand Down
40 changes: 13 additions & 27 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public function isVerifiedAuthor(): bool
return ! is_null($this->author_verified_at) || $this->isAdmin();
}

public function canVerifiedAuthorPublishMoreArticleToday(): bool
public function canVerifiedAuthorPublishMoreArticlesToday(): bool
{
if ($this->isAdmin()) {
return true;
Expand Down Expand Up @@ -364,13 +364,19 @@ public function scopeMostSolutions(Builder $query, ?int $inLastDays = null)

public function scopeMostSubmissions(Builder $query, ?int $inLastDays = null)
{
return $query->withCount(['articles as articles_count' => function ($query) use ($inLastDays) {
if ($inLastDays) {
$query->where('articles.approved_at', '>', now()->subDays($inLastDays));
}
return $query
->selectRaw('users.*, COUNT(DISTINCT articles.id) AS articles_count')
->join('articles', 'articles.author_id', '=', 'users.id')
->where(function ($query) use ($inLastDays) {
if ($inLastDays) {
$query->where('articles.approved_at', '>', now()->subDays($inLastDays));
}

return $query;
}])->orderBy('articles_count', 'desc');
return $query;
})
->groupBy('users.id')
->having('articles_count', '>', 0)
->orderBy('articles_count', 'desc');
}

public function scopeMostSolutionsInLastDays(Builder $query, int $days)
Expand All @@ -397,26 +403,6 @@ public function toSearchableArray(): array
];
}

public function scopeWithCounts(Builder $query)
{
return $query->withCount([
'threadsRelation as threads_count',
'replyAble as replies_count',
'replyAble as solutions_count' => function (Builder $query) {
return $query->join('threads', 'threads.solution_reply_id', '=', 'replies.id')
->where('replyable_type', 'threads');
},
]);
}

public function scopeHasActivity(Builder $query)
{
return $query->where(function ($query) {
$query->has('threadsRelation')
->orHas('replyAble');
});
}

public function scopeModerators(Builder $query)
{
return $query->whereIn('type', [
Expand Down
11 changes: 0 additions & 11 deletions resources/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,6 @@ svg .secondary {
@apply outline-hidden bg-gray-100;
}

.member:after {
bottom: -1rem;
content: ' ';
display: block;
left: 7rem;
position: absolute;
border-color: #f9fafb transparent transparent transparent;
border-style: solid;
border-width: 0.5rem;
}

/** Choices.js **/
.choices__input.choices__input--cloned {
@apply hidden;
Expand Down
9 changes: 1 addition & 8 deletions resources/views/components/community-member.blade.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
<div class="shrink-0 mr-8 my-2 cursor-pointer">
<x-stat-popout
x-cloak
x-show="active == '{{ $member->id }}'"
class="w-64 absolute -mt-40 -ml-20"
:user="$member"
/>

<x-avatar
:user="$member"
class="inset-0"
class="inset-0 hover:scale-120 transition-transform duration-300 ease-in-out"
x-on:mouseover="active = {{ $member->id }}"
x-on:mouseout="active = false"
size="xl"
Expand Down
26 changes: 0 additions & 26 deletions resources/views/components/stat-popout.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,6 @@
])

<div {{ $attributes->merge(['class' => 'member w-64 shadow-xl rounded-sm']) }}>
<div class="flex justify-between border-b border-gray-200 p-3 bg-white rounded-t">
<div class="flex flex-col items-center text-lio-500 text-center">
<span class="flex min-w-8 h-8 px-2 bg-lio-100 font-bold rounded-sm items-center justify-center mb-1.5">
{{ $user->solutions_count }}
</span>

<span class="text-sm">Solutions</span>
</div>

<div class="flex flex-col items-center text-lio-500 text-center">
<span class="flex min-w-8 h-8 px-2 bg-lio-100 font-bold rounded-sm items-center justify-center mb-1.5">
{{ $user->threads_count }}
</span>

<span class="text-sm">Threads</span>
</div>

<div class="flex flex-col items-center text-lio-500 text-center">
<span class="flex min-w-8 h-8 px-2 bg-lio-100 font-bold rounded-sm items-center justify-center mb-1.5">
{{ $user->replies_count }}
</span>

<span class="text-sm">Replies</span>
</div>
</div>

<div class="flex flex-col bg-gray-100 p-3 text-sm rounded-b">
<span>{{ $user->username() }}</span>

Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/ArticleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@
'submitted_at' => now()->addMinutes(1), // after verification
]);

expect($author->canVerifiedAuthorPublishMoreArticleToday())->toBeFalse();
expect($author->canVerifiedAuthorPublishMoreArticlesToday())->toBeFalse();
});

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