Skip to content

Commit

Permalink
Voting states for forum replies
Browse files Browse the repository at this point in the history
  • Loading branch information
musa11971 committed Sep 1, 2019
1 parent d6ce9c7 commit 442aa89
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 14 deletions.
107 changes: 107 additions & 0 deletions app/Helpers/CollectionLikeChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace App\Helpers;

use Illuminate\Support\Facades\DB;

class CollectionLikeChecker {
protected $collectionType;
protected $collection;
protected $userID;
protected $queryData;

/**
* Instantiates a CollectionLikeChecker and fetches the data.
*
* @param $userID
* @param $collection
* @return CollectionLikeChecker
*/
static function retrieve($userID, $collection) {
// Get the type of collection
$collectionType = $collection->getQueueableClass();

// Create the instance
$instance = new CollectionLikeChecker();
$instance->collectionType = $collectionType;
$instance->collection = $collection;
$instance->userID = $userID;

// Fetch the data for the instance
$instance->fetchData();

// Return the instance
return $instance;
}

/**
* Returns an array of IDs of the items in the collection.
*
* @return array
*/
protected function getCollectionIDs() {
$IDs = [];

foreach($this->collection as $item)
$IDs[] = $item->id;

return $IDs;
}

/**
* Fetches the data by making a query and stores it in the instance.
*/
protected function fetchData() {
$this->queryData = DB::table('love_likes')
->where('user_id', $this->userID)
->where('likeable_type', $this->collectionType)
->whereIn('likeable_id', $this->getCollectionIDs())
->get();
}

/**
* Checks if an item has been liked.
*
* @param $item
* @return bool
*/
function hasLiked($item) {
foreach($this->queryData as $queryItem)
if($queryItem->likeable_id == $item->id && $queryItem->type_id == 'LIKE')
return true;

return false;
}

/**
* Checks if an item has been disliked.
*
* @param $item
* @return bool
*/
function hasDisliked($item) {
foreach($this->queryData as $queryItem)
if($queryItem->likeable_id == $item->id && $queryItem->type_id == 'DISLIKE')
return true;

return false;
}

/**
* Returns the current like action for an item.
* -1 = disliked
* 0 = neutral
* 1 = liked
*
* @param $item
* @return int
*/
function getCurrentLikeAction($item) {
if($this->hasLiked($item))
return 1;
else if($this->hasDisliked($item))
return -1;
else
return 0;
}
}
24 changes: 11 additions & 13 deletions app/Http/Controllers/ForumThreadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use App\ForumReply;
use App\ForumSectionBan;
use App\ForumThread;
use App\Helpers\CollectionLikeChecker;
use App\Helpers\JSONResult;
use App\Http\Resources\ForumReplyResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
Expand Down Expand Up @@ -152,30 +154,26 @@ public function replies(Request $request, ForumThread $thread) {
else if($givenOrder == 'top')
$replies = $replies->orderByLikesCount();

// Paginate the replies
$replies = $replies->paginate(ForumThread::REPLIES_PER_PAGE);

// Instantiate a CollectionLikeChecker to get the current liked status for the user
$likeChecker = CollectionLikeChecker::retrieve($request->user()->id, $replies);

// Format the replies
$displayReplies = [];
$formattedReplies = [];

foreach($replies as $reply) {
$displayReplies[] = [
'id' => $reply->id,
'posted_at' => $reply->created_at->format('Y-m-d H:i:s'),
'user' => [
'id' => $reply->user->id,
'username' => $reply->user->username,
'avatar' => $reply->user->getAvatarURL()
],
'score' => $reply->likesDiffDislikesCount,
'content' => $reply->content
];
$formattedReplies[] = array_merge(ForumReplyResource::make($reply)->toArray($request), [
'current_like_action' => $likeChecker->getCurrentLikeAction($reply)
]);
}

// Show successful response
return JSONResult::success([
'page' => $givenPage,
'reply_pages' => $thread->getPageCount(),
'replies' => $displayReplies
'replies' => $formattedReplies
]);
}

Expand Down
29 changes: 29 additions & 0 deletions app/Http/Resources/ForumReplyResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class ForumReplyResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'posted_at' => $this->created_at->format('Y-m-d H:i:s'),
'poster' => [
'id' => $this->user->id,
'username' => $this->user->username,
'avatar' => $this->user->getAvatarURL()
],
'score' => $this->likesDiffDislikesCount,
'content' => $this->content
];
}
}
5 changes: 5 additions & 0 deletions public/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,11 @@
},
"/forum-threads/{threadID}/replies": {
"get": {
"security": [
{
"kurozoraBearer": []
}
],
"tags": [
"forum-threads"
],
Expand Down
3 changes: 2 additions & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@
Route::post('/{thread}/vote', [ForumThreadController::class, 'vote'])
->middleware('kurozora.userauth');

Route::get('/{thread}/replies', [ForumThreadController::class, 'replies']);
Route::get('/{thread}/replies', [ForumThreadController::class, 'replies'])
->middleware('kurozora.userauth');

Route::post('/{thread}/replies', [ForumThreadController::class, 'postReply'])
->middleware('kurozora.userauth');
Expand Down

0 comments on commit 442aa89

Please sign in to comment.