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 Oct 31, 2024
1 parent ee168a4 commit 607de57
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,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');
});
}
};
35 changes: 31 additions & 4 deletions resources/sass/components/forum/_post.scss
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,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: 'datetime edited topic . tip-stats toolbar';
grid-template-columns: auto auto auto 1fr auto auto;
align-items: center;
gap: 9px;
font-size: 13px;
Expand All @@ -52,6 +52,28 @@
grid-area: datetime;
}

.post__edited {
grid-area: edited;
cursor: pointer;
// Overwrite the default column-gap to have same margin on both sides of the dash
margin-left: -6px;
}

.post__edited-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__edited:hover .post__edited-dropdown {
display: block;
}

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

.votes__dislike {
background-color: transparent;
color: var(--post-dislike-fg);
Expand Down Expand Up @@ -289,8 +312,8 @@
}

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

Expand Down Expand Up @@ -358,10 +381,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 All @@ -373,10 +398,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
21 changes: 21 additions & 0 deletions resources/views/components/forum/post.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ class="post__datetime"
>
{{ $post->created_at?->diffForHumans() }}
</time>

@if ($post->updated_at > $post->created_at)
<span class="post__edited">
<i class="{{ config('other.font-awesome') }} fa-minus fa-xs"></i>
edited
<i class="{{ config('other.font-awesome') }} fa-caret-down"></i>
<div class="post__edited-dropdown">
{{ $post->updated_at?->diffForHumans() }}
by
<a
class="user-tag__link {{ $post->updatedBy->group->icon }}"
href="{{ route('users.show', ['user' => $post->updatedBy]) }}"
style="color: {{ $post->updatedBy->group->color }}"
title="{{ $post->updatedBy->group->name }}"
>
{{ $post->updatedBy->username }}
</a>
</div>
</span>
@endif

@if (! Route::is('topics.show'))
<span class="post__topic">
{{ __('forum.in') }}
Expand Down

0 comments on commit 607de57

Please sign in to comment.