diff --git a/app/Http/Controllers/Comments/CommentController.php b/app/Http/Controllers/Comments/CommentController.php index 9ed612ab28..714428bb91 100644 --- a/app/Http/Controllers/Comments/CommentController.php +++ b/app/Http/Controllers/Comments/CommentController.php @@ -41,14 +41,7 @@ public function __construct() { public function store(Request $request, $model, $id) { $model = urldecode(base64_decode($model)); - $accepted_models = config('lorekeeper.allowed_comment_models'); - if (!count($accepted_models)) { - flash('Invalid Models')->error(); - - return redirect()->back(); - } - - if (!in_array($model, $accepted_models)) { + if (!count(config('lorekeeper.allowed_comment_models')) || !in_array($model, config('lorekeeper.allowed_comment_models'))) { abort(404); } @@ -303,4 +296,44 @@ public function getLikedComments(Request $request) { 'user' => Auth::user(), ]); } + + /** + * Sorts comments based on the user's preference. + * + * @param Request $request + */ + public function getSortedComments(Request $request, $model, $id) { + $sort = $request->input('sort'); + $perPage = $request->input('perPage'); + + $approved = $request->input('approved'); + $type = $request->input('type'); + + $model = urldecode(base64_decode($model)); + if (!count(config('lorekeeper.allowed_comment_models')) || !in_array($model, config('lorekeeper.allowed_comment_models'))) { + abort(404); + } + $model = $model::findOrFail($id); + + if (isset($approved) && $approved) { + if (isset($type)) { + $comments = $model->approvedComments->where('type', $type); + } else { + $comments = $model->approvedComments->where('type', 'User-User'); + } + } else { + if (isset($type)) { + $comments = $model->commentz->where('type', $type); + } else { + $comments = $model->commentz->where('type', 'User-User'); + } + } + + return view('comments._comments', [ + 'comments' => $comments, + 'sort' => $sort, + 'perPage' => $perPage, + 'allow_dislikes' => $request->input('allow_dislikes'), + ]); + } } diff --git a/app/Http/Controllers/PermalinkController.php b/app/Http/Controllers/PermalinkController.php index 6c44c67c5d..dbe8a98969 100644 --- a/app/Http/Controllers/PermalinkController.php +++ b/app/Http/Controllers/PermalinkController.php @@ -85,7 +85,7 @@ public function getComment($id) { $comment->location = $comment->commentable->url; } - return view('comments._perma_layout', [ + return view('comments.permalink_comment', [ 'comment' => $comment, ]); } diff --git a/app/Models/Comment/Comment.php b/app/Models/Comment/Comment.php index a275354f9a..ce37820825 100644 --- a/app/Models/Comment/Comment.php +++ b/app/Models/Comment/Comment.php @@ -131,4 +131,17 @@ public function getTopCommentAttribute() { return $this->parent->topComment; } } + + /** + * Gets the end of a comment's thread. + * + * @return Comment + */ + public function getEndOfThreadAttribute() { + if ($this->children->count() > 0) { + return $this->children->sortByDesc('created_at')->first()->endOfThread; + } else { + return $this; + } + } } diff --git a/resources/views/comments/_comments.blade.php b/resources/views/comments/_comments.blade.php new file mode 100644 index 0000000000..a2acecba0c --- /dev/null +++ b/resources/views/comments/_comments.blade.php @@ -0,0 +1,48 @@ +
+
+ @php + $comments = (isset($sort) && $sort == 'oldest') ? $comments->sortBy('created_at') : $comments->sortByDesc('created_at'); + + if (isset($perPage)) { + $page = request()->query('page', 1) - 1; + + $parentComments = $comments->where('child_id', null); + $slicedParentComments = $parentComments->slice($page * $perPage, $perPage); + + $m = config('comments.model'); // This has to be done like this, otherwise it will complain. + $modelKeyName = (new $m())->getKeyName(); // This defaults to 'id' if not changed. + + $slicedParentCommentsIds = $slicedParentComments->pluck($modelKeyName)->toArray(); + + // Remove parent Comments from comments. + $comments = $comments->where('child_id', '!=', null); + $grouped_comments = new \Illuminate\Pagination\LengthAwarePaginator($slicedParentComments->merge($comments)->groupBy('child_id'), $parentComments->count(), $perPage); + $grouped_comments->withPath(request()->url()); + } else { + $grouped_comments = $comments->groupBy('child_id'); + } + @endphp + @foreach ($grouped_comments as $comment_id => $comments) + {{-- Process parent nodes --}} + @if ($comment_id == '') + @foreach ($comments as $comment) + @include('comments::_comment', [ + 'comment' => $comment, + 'grouped_comments' => $grouped_comments, + 'limit' => 0, + 'compact' => $comment->type == 'Staff-Staff' ? true : false, + 'allow_dislikes' => isset($allow_dislikes) ? $allow_dislikes : false, + ]) + @endforeach + @endif + @endforeach +
+ + @if ($comments->count() < 1) +
There are no comments yet.
+ @endif + + @isset($perPage) +
{{ $grouped_comments->links() }}
+ @endisset +
\ No newline at end of file diff --git a/resources/views/comments/comments.blade.php b/resources/views/comments/comments.blade.php index 463e0533c8..350a635122 100644 --- a/resources/views/comments/comments.blade.php +++ b/resources/views/comments/comments.blade.php @@ -1,12 +1,12 @@ @php - if (isset($approved) and $approved == true) { - if (isset($type) && $type != null) { + if (isset($approved) && $approved) { + if (isset($type)) { $comments = $model->approvedComments->where('type', $type); } else { $comments = $model->approvedComments->where('type', 'User-User'); } } else { - if (isset($type) && $type != null) { + if (isset($type)) { $comments = $model->commentz->where('type', $type); } else { $comments = $model->commentz->where('type', 'User-User'); @@ -15,57 +15,50 @@ @endphp @if (!isset($type) || $type == 'User-User') -

Comments

-@endif -
- @php - $comments = $comments->sortByDesc('created_at'); - - if (isset($perPage)) { - $page = request()->query('page', 1) - 1; - - $parentComments = $comments->where('child_id', ''); - - $slicedParentComments = $parentComments->slice($page * $perPage, $perPage); - - $m = config('comments.model'); // This has to be done like this, otherwise it will complain. - $modelKeyName = (new $m())->getKeyName(); // This defaults to 'id' if not changed. - - $slicedParentCommentsIds = $slicedParentComments->pluck($modelKeyName)->toArray(); - - // Remove parent Comments from comments. - $comments = $comments->where('child_id', '!=', ''); - - $grouped_comments = new \Illuminate\Pagination\LengthAwarePaginator($slicedParentComments->merge($comments)->groupBy('child_id'), $parentComments->count(), $perPage); - - $grouped_comments->withPath(request()->url()); - } else { - $grouped_comments = $comments->groupBy('child_id'); - } - @endphp - @foreach ($grouped_comments as $comment_id => $comments) - {{-- Process parent nodes --}} - @if ($comment_id == '') - @foreach ($comments as $comment) - @include('comments::_comment', [ - 'comment' => $comment, - 'grouped_comments' => $grouped_comments, - 'limit' => 0, - 'compact' => $comment->type == 'Staff-Staff' ? true : false, - 'allow_dislikes' => isset($allow_dislikes) ? $allow_dislikes : false, - ]) - @endforeach - @endif - @endforeach -
+
+
+ Comments +
-@if ($comments->count() < 1) -
There are no comments yet.
+
+
+
+ {!! Form::select( + 'sort', + [ + 'newest' => 'Newest First', + 'oldest' => 'Oldest First', + ], + Request::get('sort') ?: 'newest', + ['class' => 'form-control', 'id' => 'sort'], + ) !!} +
+
+ {!! Form::select( + 'perPage', + [ + 5 => '5 Per Page', + 10 => '10 Per Page', + 25 => '25 Per Page', + 50 => '50 Per Page', + 100 => '100 Per Page', + ], + Request::get('perPage') ?: 5, + ['class' => 'form-control', 'id' => 'perPage'], + ) !!} +
+
+
+
@endif - -@isset($perPage) -
{{ $grouped_comments->links() }}
-@endisset +
+ @include('comments._comments', [ + 'comments' => $comments, + 'allow_dislikes' => isset($allow_dislikes) ? $allow_dislikes : false, + 'approved' => isset($approved) ? $approved : false, + 'type' => isset($type) ? $type : null, + ]) +
@auth @include('comments._form') @@ -101,6 +94,42 @@ spoiler_caption: 'Toggle Spoiler', target_list: false }); + + function sortComments(fade = true) { + if (fade) $('#comments').fadeOut(); + $.ajax({ + url: "{{ url('sort-comments/'.base64_encode(urlencode(get_class($model))) . '/' . $model->getKey()) }}", + type: 'GET', + data: { + allow_dislikes: '{{ isset($allow_dislikes) ? $allow_dislikes : false }}', + approved: '{{ isset($approved) ? $approved : false }}', + type: '{{ isset($type) ? $type : null }}', + sort: $('#sort').val(), + perPage: $('#perPage').val(), + }, + success: function(data) { + $('#comments').html(data); + // update current url to reflect sort change + var url = new URL(window.location.href); + url.searchParams.set('sort', $('#sort').val()); + url.searchParams.set('perPage', $('#perPage').val()); + + window.history.pushState({}, '', url); + } + }); + if (fade) $('#comments').fadeIn(); + } + if (window.location.search.includes('sort') || window.location.search.includes('perPage')) { + sortComments(false); // initial sort + } + + $('#sort').change(function() { + sortComments(); + }); + + $('#perPage').change(function() { + sortComments(); + }); }); @endsection diff --git a/resources/views/comments/_perma_layout.blade.php b/resources/views/comments/permalink_comment.blade.php similarity index 89% rename from resources/views/comments/_perma_layout.blade.php rename to resources/views/comments/permalink_comment.blade.php index 41d8188a35..5719e171e3 100644 --- a/resources/views/comments/_perma_layout.blade.php +++ b/resources/views/comments/permalink_comment.blade.php @@ -11,14 +11,16 @@ @section('content')

Comments on {!! $comment->commentable_type == 'App\Models\User\UserProfile' ? $comment->commentable->user->displayName : $comment->commentable->displayName !!}

+ @if (count($comment->children)) + Go To End Of Thread + @endif @if (isset($comment->child_id)) See Parent Go To Top Comment @endif
-
-
+
@include('comments._perma_comments', ['comment' => $comment, 'limit' => 0, 'depth' => 0])
diff --git a/routes/lorekeeper/browse.php b/routes/lorekeeper/browse.php index 98f55ed164..0721183b6f 100644 --- a/routes/lorekeeper/browse.php +++ b/routes/lorekeeper/browse.php @@ -158,6 +158,7 @@ Comments **************************************************************************************************/ Route::get('comment/{id}', 'PermalinkController@getComment'); +Route::get('sort-comments/{model}/{id}', 'Comments\CommentController@getSortedComments'); /************************************************************************************************** Galleries diff --git a/routes/lorekeeper/members.php b/routes/lorekeeper/members.php index cf22f2a4ad..c24969d76f 100644 --- a/routes/lorekeeper/members.php +++ b/routes/lorekeeper/members.php @@ -224,10 +224,10 @@ **************************************************************************************************/ Route::group(['prefix' => 'comments', 'namespace' => 'Comments'], function () { Route::post('make/{model}/{id}', 'CommentController@store'); - Route::delete('/{comment}', 'CommentController@destroy')->name('comments.destroy'); + Route::delete('{comment}', 'CommentController@destroy')->name('comments.destroy')->where('comment', '[0-9]+'); Route::post('edit/{comment}', 'CommentController@update')->name('comments.update'); - Route::post('/{comment}', 'CommentController@reply')->name('comments.reply'); - Route::post('/{id}/feature', 'CommentController@feature')->name('comments.feature'); - Route::post('/{id}/like/{action}', 'CommentController@like')->name('comments.like'); - Route::get('/liked', 'CommentController@getLikedComments'); + Route::post('{comment}', 'CommentController@reply')->name('comments.reply'); + Route::post('{id}/feature', 'CommentController@feature')->name('comments.feature'); + Route::post('{id}/like/{action}', 'CommentController@like')->name('comments.like'); + Route::get('liked', 'CommentController@getLikedComments'); });