Skip to content

Commit

Permalink
feat: add comment sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
ScuffedNewt committed Aug 16, 2024
1 parent 9f1f09e commit 5c94ddb
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 68 deletions.
49 changes: 41 additions & 8 deletions app/Http/Controllers/Comments/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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'),
]);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/PermalinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
Expand Down
13 changes: 13 additions & 0 deletions app/Models/Comment/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
48 changes: 48 additions & 0 deletions resources/views/comments/_comments.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<div id="comments">
<div class="d-flex mw-100 row mx-0" style="overflow:hidden;">
@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
</div>

@if ($comments->count() < 1)
<div class="alert alert-warning">There are no comments yet.</div>
@endif

@isset($perPage)
<div class="ml-auto mt-2">{{ $grouped_comments->links() }}</div>
@endisset
</div>
133 changes: 81 additions & 52 deletions resources/views/comments/comments.blade.php
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -15,57 +15,50 @@
@endphp

@if (!isset($type) || $type == 'User-User')
<h2>Comments</h2>
@endif
<div class="d-flex mw-100 row mx-0" style="overflow:hidden;">
@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
</div>
<div class="row">
<div class="{{ !isset($type) || $type == 'User-User' ? 'h2' : 'hide' }}">
Comments
</div>

@if ($comments->count() < 1)
<div class="alert alert-warning">There are no comments yet.</div>
<div class="ml-auto">
<div class="form-inline justify-content-end">
<div class="form-group ml-3 mb-3">
{!! Form::select(
'sort',
[
'newest' => 'Newest First',
'oldest' => 'Oldest First',
],
Request::get('sort') ?: 'newest',
['class' => 'form-control', 'id' => 'sort'],
) !!}
</div>
<div class="form-group ml-3 mb-3">
{!! 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'],
) !!}
</div>
</div>
</div>
</div>
@endif

@isset($perPage)
<div class="ml-auto mt-2">{{ $grouped_comments->links() }}</div>
@endisset
<div id="comments">
@include('comments._comments', [
'comments' => $comments,
'allow_dislikes' => isset($allow_dislikes) ? $allow_dislikes : false,
'approved' => isset($approved) ? $approved : false,
'type' => isset($type) ? $type : null,
])
</div>

@auth
@include('comments._form')
Expand Down Expand Up @@ -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();
});
});
</script>
@endsection
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
@section('content')
<h1>Comments on {!! $comment->commentable_type == 'App\Models\User\UserProfile' ? $comment->commentable->user->displayName : $comment->commentable->displayName !!}</h1>
<h5>
@if (count($comment->children))
<a href="{{ url('comment/') . '/' . $comment->endOfThread->id }}" class="btn btn-secondary btn-sm mr-2">Go To End Of Thread</a>
@endif
@if (isset($comment->child_id))
<a href="{{ url('comment/') . '/' . $comment->child_id }}" class="btn btn-secondary btn-sm mr-2">See Parent</a>
<a href="{{ url('comment/') . '/' . $comment->topComment->id }}" class="btn btn-secondary btn-sm mr-2">Go To Top Comment</a>
@endif
</h5>

<hr>
<br>
<hr class="mb-3">
<div class="d-flex mw-100 row mx-0" style="overflow:hidden;">
@include('comments._perma_comments', ['comment' => $comment, 'limit' => 0, 'depth' => 0])
</div>
Expand Down
1 change: 1 addition & 0 deletions routes/lorekeeper/browse.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
Comments
**************************************************************************************************/
Route::get('comment/{id}', 'PermalinkController@getComment');
Route::get('sort-comments/{model}/{id}', 'Comments\CommentController@getSortedComments');

/**************************************************************************************************
Galleries
Expand Down
10 changes: 5 additions & 5 deletions routes/lorekeeper/members.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

0 comments on commit 5c94ddb

Please sign in to comment.