Skip to content

Commit

Permalink
refactor: migrate RenderRecentForumPostsComponent to blade (#1889)
Browse files Browse the repository at this point in the history
  • Loading branch information
wescopeland authored Oct 27, 2023
1 parent 3772d69 commit 3fbbbe1
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 77 deletions.
2 changes: 2 additions & 0 deletions app/Community/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Community\Commands\SyncUserRelations;
use App\Community\Commands\SyncVotes;
use App\Community\Components\DeveloperGameStatsTable;
use App\Community\Components\ForumRecentPosts;
use App\Community\Components\GlobalStatistics;
use App\Community\Components\MessageIcon;
use App\Community\Components\UserCard;
Expand Down Expand Up @@ -107,6 +108,7 @@ public function boot(): void

Blade::component('developer-game-stats-table', DeveloperGameStatsTable::class);
Blade::component('global-statistics', GlobalStatistics::class);
Blade::component('forum-recent-posts', ForumRecentPosts::class);
Blade::component('user-card', UserCard::class);
Blade::component('user-progression-status', UserProgressionStatus::class);

Expand Down
84 changes: 84 additions & 0 deletions app/Community/Components/ForumRecentPosts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace App\Community\Components;

use App\Site\Enums\Permissions;
use App\Site\Enums\UserPreference;
use App\Site\Models\User;
use App\Support\Shortcode\Shortcode;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\Component;

class ForumRecentPosts extends Component
{
private int $numToFetch = 4;
private ?User $user = null;

public function __construct(int $numToFetch = 4)
{
$this->user = Auth::user() ?? null;
$this->numToFetch = $numToFetch;
}

public function render(): ?View
{
$recentForumPosts = $this->prepareRecentForumPosts(
$this->numToFetch,
$this->user?->Permissions ?? Permissions::Unregistered,
$this->user?->websitePrefs ?? 0,
);

return view('community.components.forum.recent-posts', [
'recentForumPosts' => $recentForumPosts,
'userPreferences' => $this->user?->websitePrefs ?? 0,
]);
}

private function prepareRecentForumPosts(int $numToFetch = 4, int $userPermissions = Permissions::Unregistered, int $userPreferences = 0): array
{
$recentForumPosts = [];
$rawRecentPosts = getRecentForumPosts(0, $numToFetch, 100, $userPermissions);

if ($rawRecentPosts->isEmpty()) {
return $recentForumPosts;
}

$isShowAbsoluteDatesPreferenceSet = $userPreferences && BitSet($userPreferences, UserPreference::Forum_ShowAbsoluteDates);

foreach ($rawRecentPosts as $rawRecentPost) {
$recentForumPosts[] = [
'ShortMsg' => $this->buildShortMessage($rawRecentPost),
'Author' => $rawRecentPost['Author'],
'ForumTopicTitle' => $rawRecentPost['ForumTopicTitle'],
'HasDateTooltip' => !$isShowAbsoluteDatesPreferenceSet,

// "viewtopic.php?t=12345&c=112233#112233"
'URL' => '/viewtopic.php?t='
. $rawRecentPost['ForumTopicID'] . '&c=' . $rawRecentPost['CommentID']
. '#' . $rawRecentPost['CommentID'],

'PostedAt' => $isShowAbsoluteDatesPreferenceSet
? getNiceDate(strtotime($rawRecentPost['PostedAt']))
: Carbon::parse($rawRecentPost['PostedAt'])->diffForHumans(),

'TitleAttribute' => $isShowAbsoluteDatesPreferenceSet
? null
: Carbon::parse($rawRecentPost['PostedAt'])->format('F j Y, g:ia'),
];
}

return $recentForumPosts;
}

private function buildShortMessage(array $rawRecentPost): string
{
$shortMsg = trim($rawRecentPost['ShortMsg']);
$shortMsg = $rawRecentPost['IsTruncated'] ? $shortMsg . "..." : $shortMsg;

return Shortcode::stripAndClamp($shortMsg);
}
}
1 change: 0 additions & 1 deletion app/Helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
require_once __DIR__ . '/render/code-note.php';
require_once __DIR__ . '/render/comment.php';
require_once __DIR__ . '/render/content.php';
require_once __DIR__ . '/render/forum.php';
require_once __DIR__ . '/render/game.php';
require_once __DIR__ . '/render/layout.php';
require_once __DIR__ . '/render/leaderboard.php';
Expand Down
67 changes: 0 additions & 67 deletions app/Helpers/render/forum.php

This file was deleted.

5 changes: 2 additions & 3 deletions public/forum.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use App\Site\Enums\Permissions;
use Illuminate\Support\Facades\Blade;

$requestedCategoryID = requestInputSanitized('c', null, 'integer');

Expand Down Expand Up @@ -132,8 +133,6 @@
</article>
<?php view()->share('sidebar', true) ?>
<aside>
<?php
RenderRecentForumPostsComponent(8);
?>
<?= Blade::render('<x-forum-recent-posts :numToFetch="8" />') ?>
</aside>
<?php RenderContentEnd(); ?>
3 changes: 2 additions & 1 deletion public/viewforum.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Site\Enums\Permissions;
use App\Support\Shortcode\Shortcode;
use Illuminate\Support\Facades\Blade;

authenticateFromCookie($user, $permissions, $userDetails);

Expand Down Expand Up @@ -167,6 +168,6 @@
</article>
<?php view()->share('sidebar', true) ?>
<aside>
<?php RenderRecentForumPostsComponent(8); ?>
<?= Blade::render('<x-forum-recent-posts :numToFetch="8" />') ?>
</aside>
<?php RenderContentEnd(); ?>
39 changes: 39 additions & 0 deletions resources/views/community/components/forum/recent-posts.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@props([
'recentForumPosts' => [],
'userPreferences' => 0,
])

<div class="component">
<h3>Forum Activity</h3>

<div class="flex flex-col gap-y-1">
@foreach ($recentForumPosts as $recentForumPost)
<div class="embedded">
<div class="flex justify-between items-center">
<div>
{!! userAvatar($recentForumPost['Author'], iconSize: 16) !!}
<span
class="smalldate
{{ $recentForumPost['HasDateTooltip'] ? 'cursor-help' : '' }}"
@if ($recentForumPost['TitleAttribute']) title="{{ $recentForumPost['TitleAttribute'] }}" @endif
>
{{ $recentForumPost['PostedAt'] }}
</span>
</div>

<a class="btn btn-link" href="{{ $recentForumPost['URL'] }}">View</a>
</div>

<p>in <a href="{{ $recentForumPost['URL'] }}">{{ $recentForumPost['ForumTopicTitle'] }}</a></p>

<p class="comment text-overflow-wrap">
{{ $recentForumPost['ShortMsg'] }}
</p>
</div>
@endforeach
</div>

<div class="text-right">
<a class="btn btn-link" href="/forumposthistory.php">more...</a>
</div>
</div>
10 changes: 5 additions & 5 deletions resources/views/home.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
RenderActivePlayersComponent();
?>

<x-user.online-count-chart />
<div class="mb-8">
<x-user.online-count-chart />
</div>

<x-claims.new-claims count="5" />

<?php
RenderRecentForumPostsComponent();
?>
<x-forum-recent-posts />

@slot('sidebar')
@include('content.top-links')
Expand Down

0 comments on commit 3fbbbe1

Please sign in to comment.