Skip to content

Commit

Permalink
Add last updated information to forum posts
Browse files Browse the repository at this point in the history
  • Loading branch information
Obi-Wana committed Dec 29, 2024
1 parent 62521e8 commit e5f1341
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function index(Request $request): \Illuminate\Contracts\View\Factory|\Ill
'latest_posts:by-group:'.auth()->user()->group_id,
$expiresAt,
fn () => Post::query()
->with('user', 'user.group', 'topic:id,name')
->with('user', 'user.group', 'topic:id,name', 'updatedBy.group')
->withCount('likes', 'dislikes', 'authorPosts', 'authorTopics')
->withSum('tips', 'bon')
->withExists([
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ public function update(Request $request, int $id): \Illuminate\Http\RedirectResp
abort_unless($post->topic()->authorized(canReplyTopic: true)->exists(), 403);

$post->update([
'content' => $request->input('content'),
'content' => $request->input('content'),
'updated_by' => $user->id,
]);

return redirect()->to($postUrl)
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/User/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function index(User $user): \Illuminate\Contracts\View\Factory|\Illuminat
return view('user.post.index', [
'user' => $user,
'posts' => $user->posts()
->with('user', 'user.group', 'topic:id,name,state')
->with('user', 'user.group', 'topic:id,name,state', 'updatedBy.group')
->withCount('likes', 'dislikes', 'authorPosts', 'authorTopics')
->withSum('tips', 'bon')
->authorized(canReadTopic: true)
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Livewire/PostSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final public function updatingSearch(): void
final public function posts(): \Illuminate\Pagination\LengthAwarePaginator
{
return Post::query()
->with('user', 'user.group', 'topic:id,name,state')
->with('user', 'user.group', 'topic:id,name,state', 'updatedBy.group')
->withCount('likes', 'dislikes', 'authorPosts', 'authorTopics')
->withSum('tips', 'bon')
->withExists([
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Livewire/TopicPostSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final public function updatingSearch(): void
final public function posts(): \Illuminate\Pagination\LengthAwarePaginator
{
$posts = Post::query()
->with('user', 'user.group')
->with('user', 'user.group', 'updatedBy.group')
->withCount('likes', 'dislikes', 'authorPosts', 'authorTopics')
->withSum('tips', 'bon')
->where('topic_id', '=', $this->topic->id)
Expand Down
12 changes: 12 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @property string $content
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property int $updated_by
* @property int $user_id
* @property int $topic_id
*/
Expand All @@ -49,6 +50,7 @@ class Post extends Model
'content',
'topic_id',
'user_id',
'updated_by',
];

/**
Expand All @@ -74,6 +76,16 @@ public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
]);
}

/**
* Belongs To An Updated User.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<User, $this>
*/
public function updatedBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(User::class, 'updated_by', 'id')->withTrashed();
}

/**
* A Post Has Many Likes.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <[email protected]>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('posts', function (Blueprint $table): void {
$table->unsignedInteger('updated_by')->nullable()->after('updated_at');
$table->foreign('updated_by')->references('id')->on('users')->cascadeOnUpdate()->cascadeOnDelete();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('posts', function (Blueprint $table): void {
$table->dropForeign(['updated_by']);
$table->dropColumn('updated_by');
});
}
};
66 changes: 56 additions & 10 deletions resources/sass/components/forum/_post.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
grid-template-areas:
'aside header'
'aside content'
'aside timestamps'
'aside footer';
grid-template-columns: minmax(153px, min-content) minmax(0, 1fr);
grid-template-rows: auto 1fr auto;
Expand All @@ -38,8 +39,8 @@
.post__header {
grid-area: header;
display: grid;
grid-template-areas: 'datetime topic . tip-stats toolbar';
grid-template-columns: auto auto 1fr auto auto;
grid-template-areas: '. . topic . tip-stats toolbar';
grid-template-columns: auto auto auto 1fr auto auto;
align-items: center;
gap: 9px;
font-size: 13px;
Expand All @@ -48,10 +49,6 @@
padding-left: 9px;
}

.post__datetime {
grid-area: datetime;
}

.post__topic {
grid-area: topic;
}
Expand Down Expand Up @@ -158,6 +155,7 @@
background-color: transparent;
color: var(--post-like-fg);
}

.votes__dislike {
background-color: transparent;
color: var(--post-dislike-fg);
Expand Down Expand Up @@ -254,6 +252,40 @@
line-height: 1.5;
}

.post__timestamps {
display: grid;
grid-template-areas: '. datetime updated';
grid-template-columns: 1fr auto auto;
border-top: 2px solid var(--post-aside-bg);
padding: 4px;
}

.post__datetime {
grid-area: datetime;
}

.post__updated {
grid-area: updated;
cursor: pointer;
margin-left: 4px;
margin-right: 8px;
}

.post__updated-dropdown {
display: none;
background-color: var(--top-nav-dropdown-menu-item-hover-bg);
position: absolute;
padding: 5px;
box-shadow: var(--post-shadow);
border-radius: 5px;
border: var(--bbcode-input-border);
}

/* Show dropdown menu when the dropdown is focused */
.post__updated:hover .post__updated-dropdown {
display: block;
}

.post__footer {
grid-area: footer;
border-top: 2px solid var(--post-aside-bg);
Expand Down Expand Up @@ -282,15 +314,15 @@
cursor: pointer;
}

@media screen and (max-width: 576px) {
@media screen and (max-width: 1358px) {
.post {
grid-template-areas: 'aside' 'header' 'content' 'footer';
grid-template-areas: 'aside' 'header' 'content' 'timestamps' 'footer';
grid-template-columns: 100%;
}

.post__header {
grid-template-areas: 'datetime topic . tip-stats overflow' '. . . . toolbar';
grid-template-columns: auto auto 1fr auto auto;
grid-template-areas: '. . topic tip-stats overflow' '. . . . toolbar';
grid-template-columns: auto auto auto 1fr auto auto;
gap: 0 6px;
}

Expand Down Expand Up @@ -353,15 +385,27 @@
}
}

@media screen and (max-width: 867px) {
.post__timestamps {
display: grid;
grid-template-areas: '. datetime updated .';
grid-template-columns: 1fr auto auto 1fr;
border-top: 2px solid var(--post-aside-bg);
padding: 4px;
}
}

@-webkit-keyframes post__like-animation {
0% {
-webkit-transform: scale(1);
transform: scale(1);
}

50% {
-webkit-transform: scale(1.1);
transform: scale(1.1);
}

100% {
-webkit-transform: scale(1);
transform: scale(1);
Expand All @@ -373,10 +417,12 @@
-webkit-transform: scale(1);
transform: scale(1);
}

50% {
-webkit-transform: scale(1.1);
transform: scale(1.1);
}

100% {
-webkit-transform: scale(1);
transform: scale(1);
Expand Down
36 changes: 29 additions & 7 deletions resources/views/components/forum/post.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@

<article class="post" id="post-{{ $post->id }}" x-data>
<header class="post__header">
<time
class="post__datetime"
datetime="{{ $post->created_at }}"
title="{{ $post->created_at }}"
>
{{ $post->created_at?->diffForHumans() }}
</time>
@if (! Route::is('topics.show'))
<span class="post__topic">
{{ __('forum.in') }}
Expand Down Expand Up @@ -234,6 +227,35 @@ class="post__content bbcode-rendered"
>
@joypixels($post->getContentHtml())
</div>
<span class="post__timestamps">
<time
class="post__datetime"
datetime="{{ $post->created_at }}"
title="{{ $post->created_at }}"
>
{{ $post->created_at?->diffForHumans() }}
</time>
@if ($post->updated_at > $post->created_at)
<div class="post__updated">
&bull; edited
<i class="{{ config('other.font-awesome') }} fa-caret-down"></i>
<div class="post__updated-dropdown">
{{ $post->updated_at?->diffForHumans() }}
by
<a
class="user-tag__link {{ $post->updatedBy->group->icon ?? $post->user->group->icon }}"
href="{{ route('users.show', ['user' => $post->updatedBy ?? $post->user]) }}"
style="
color: {{ $post->updatedBy->group->color ?? $post->user->group->color }};
"
title="{{ $post->updatedBy->group->name ?? $post->user->group->name }}"
>
{{ $post->updatedBy?->username ?? $post->user->username }}
</a>
</div>
</div>
@endif
</span>
@if (! empty($post->user->signature))
<footer class="post__footer" x-init>
<p class="post__signature">
Expand Down

0 comments on commit e5f1341

Please sign in to comment.