Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions app/Http/Controllers/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public function store(StoreCommentRequest $request)
$comment->content = $validatedData['content'];
$comment->user_id = Auth::id();

$commentableType = $this->modelResolver->getModelClass($validatedData['commentable_type']);
$commentableType = $this->modelResolver->getModelClass($validatedData['commentable_key']);
$commentableId = $validatedData['commentable_id'];

// Ensure that the model exists
$model = $this->modelResolver->resolve($validatedData['commentable_type'], $commentableId);
$model = $this->modelResolver->resolve($validatedData['commentable_key'], $commentableId);
if (!$model) {
return response()->json(['message' => 'Model not found'], 404);
}
Expand Down Expand Up @@ -140,7 +140,7 @@ public function store(StoreCommentRequest $request)
/**
* Display the specified comment, with pagination.
*/
public function show(Request $request, string $commentableType, int $commentableId, int $index, int $paginationLimit = -1)
public function show(Request $request, string $commentableKey, int $commentableId, int $index, int $paginationLimit = -1)
{
if ($paginationLimit == -1)
{
Expand All @@ -149,8 +149,8 @@ public function show(Request $request, string $commentableType, int $commentable

$sortBy = $request->query('sort_by', 'top');

Log::debug("Request is, commentable_type: " . $commentableType . ". id: " . $commentableId . ". index: " . $index . ". Sorting: " . $sortBy);
$paginatedResults = $this->commentService->getPaginatedComments($commentableType, $commentableId, $index, $paginationLimit, $sortBy);
Log::debug("Request is, commentable_type: " . $commentableKey . ". id: " . $commentableId . ". index: " . $index . ". Sorting: " . $sortBy);
$paginatedResults = $this->commentService->getPaginatedComments($commentableKey, $commentableId, $index, $paginationLimit, $sortBy);

return $paginatedResults;
}
Expand Down
18 changes: 9 additions & 9 deletions app/Http/Controllers/UpvoteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ function __construct(ModelResolverService $modelResolver)
}

/**
* Upvote a Model (type, id)
* Upvote a Model
*/
public function upvote($type, $id)
public function upvote($typeKey, $id)
{
validator(
[
'type' => $type,
'type_key' => $typeKey,
],
[
'type' => ['required', Rule::in(config('upvotes.upvotable_types'))]
'type_key' => ['required', Rule::in(config('upvotes.upvotable_keys'))]
]
)->validate();

$model = $this->modelResolver->resolve($type, $id);
$model = $this->modelResolver->resolve($typeKey, $id);

if (!$model) {
return response()->json(['message' => 'Model not found'], 404);
Expand All @@ -50,18 +50,18 @@ public function upvote($type, $id)
/**
* Downvote a Model (type, id)
*/
public function downvote($type, $id)
public function downvote($typeKey, $id)
{
validator(
[
'type' => $type,
'type_key' => $typeKey,
],
[
'type' => ['required', Rule::in(config('upvotes.upvotable_types'))]
'type_key' => ['required', Rule::in(config('upvotes.upvotable_keys'))]
]
)->validate();

$model = $this->modelResolver->resolve($type, $id);
$model = $this->modelResolver->resolve($typeKey, $id);

if (!$model) {
return response()->json(['message' => 'Model not found'], 404);
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Requests/Comment/StoreCommentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public function rules(): array
{
return [
"commentable_id" => ['required', 'integer'],
"commentable_type" => [
"commentable_key" => [
'required',
'string',
Rule::in(config('comment.commentable_types_shorthand')),
Rule::in(config('comment.commentable_keys')),
],
"content" => ["required", "string", "max:4000"],
"parent_comment_id" => ["nullable", "exists:App\Models\Comment,id"]
Expand Down
8 changes: 4 additions & 4 deletions app/Services/CommentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function __construct(ModelResolverService $resolver)
* @return array
*/
// TODO: Refactor commentable types, and commentable types short for all other objects
public function getPaginatedComments(string $commentableTypeShorthand, int $commentableId, int $index, int $paginationLimit = -1, string $sortBy = 'top'): array
public function getPaginatedComments(string $commentableKey, int $commentableId, int $index, int $paginationLimit = -1, string $sortBy = 'top'): array
{
if ($paginationLimit == -1)
{
Expand All @@ -37,15 +37,15 @@ public function getPaginatedComments(string $commentableTypeShorthand, int $comm

Validator::make([
'index' => $index,
'commentable_type_short' => $commentableTypeShorthand,
'commentable_key' => $commentableKey,
'pagination_limit' => $paginationLimit,
], [
'index' => ['required', 'integer', 'min:0'],
'commentable_type_short' => ['required', Rule::in(config('comment.commentable_types_shorthand'))],
'commentable_key' => ['required', Rule::in(config('comment.commentable_keys'))],
'pagination_limit' => ['required', 'integer', 'max:' . config('comment.pagination_limit')],
])->validate();

$commentableType = $this->modelResolver->getModelClass($commentableTypeShorthand);
$commentableType = $this->modelResolver->getModelClass($commentableKey);
Log::debug("Request is, commentable_type: {$commentableType}. id: {$commentableId}. index: {$index}");

// Get the root comments:
Expand Down
2 changes: 1 addition & 1 deletion config/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
'max_depth' => 7, // 1-indexed, 1 is the start
'pagination_limit' => 150,
'default_pagination_limit' => 5,
'commentable_types_shorthand' => ['review', 'comment', 'edit', 'resource'],
'commentable_keys' => ['review', 'comment', 'edit', 'resource'],
'sortable_options' => ['latest', 'top', 'bottom', 'controversial', 'mine']
];
2 changes: 1 addition & 1 deletion config/upvotes.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

return [
'upvotable_types' => ['review', 'comment', 'edit', 'resource']
'upvotable_keys' => ['review', 'comment', 'edit', 'resource']
];
4 changes: 2 additions & 2 deletions database/factories/CommentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class CommentFactory extends Factory
public function definition(): array
{
// Pick a random commentable type from config.
$commentableName = $this->faker->randomElement(['comment', 'resource']);
$commentableKey = $this->faker->randomElement(['comment', 'resource']);
$modelResolver = app(ModelResolverService::class);
$modelClass = $modelResolver->getModelClass($commentableName);
$modelClass = $modelResolver->getModelClass($commentableKey);

// Use an existing user or create one.
$user = User::inRandomOrder()->first() ?? User::factory()->create();
Expand Down
4 changes: 2 additions & 2 deletions resources/js/Components/Comments/CommentActionsForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const props = defineProps({

const isOpen = ref(false);

const commentableType = inject("commentableType");
const commentableKey = inject("commentableKey");
const commentableId = inject("commentableId");
const createdNewCommentCallback = inject("createdNewCommentCallback");

Expand All @@ -29,7 +29,7 @@ const toggleOpen = () => {
const form = reactive({
content: "",
commentable_id: commentableId,
commentable_type: commentableType,
commentable_key: commentableKey,
parent_comment_id: props.parentCommentId ?? null,
errors: {},
processing: false,
Expand Down
8 changes: 4 additions & 4 deletions resources/js/Components/Comments/Commentable.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup>
import { ref, provide, readonly, nextTick, onMounted, reactive } from "vue";
import { ref, provide, readonly, nextTick, onMounted } from "vue";
import axios from "axios";
import CommentActionsForm from "@/Components/Comments/CommentActionsForm.vue";
import SortByDropdown from "@/Components/Comments/SortUpvotesByDropdown.vue";
Expand All @@ -10,7 +10,7 @@ const props = defineProps({
type: Number,
required: true,
},
commentableType: {
commentableKey: {
type: String,
required: true,
},
Expand Down Expand Up @@ -69,7 +69,7 @@ const createdNewCommentCallback = (newComment, userData) => {
};

provide("commentableId", props.commentableId);
provide("commentableType", props.commentableType);
provide("commentableKey", props.commentableKey);
provide("users", readonly(usersMap));
provide("createdNewCommentCallback", createdNewCommentCallback);

Expand Down Expand Up @@ -139,7 +139,7 @@ async function loadComments() {
const response = await axios.get(
route("comments.show", {
id: props.commentableId,
type: props.commentableType,
type: props.commentableKey,
index: currentIndex.value,
paginationLimit: props.paginationLimit,
sort_by: sortBy.value,
Expand Down
2 changes: 1 addition & 1 deletion resources/js/Components/Comments/SingleComment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const formattedDate = computed(() =>
<div class="p-4 border-b border-gray-200" :key="comment.id" :id="'comment_'+comment.id">

<Upvotable
:upvotable-type="'comment'"
:upvotable-key="'comment'"
:upvotable-id="comment.id"
:initial-votes="comment.vote_score"
:user-vote="comment.user_vote"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const props = defineProps({
>
<div class="flex flex-col bg-slate-100">
<Upvotable
:upvotable-type="'edit'"
:upvotable-key="'edit'"
:upvotable-id="edit.id"
:initial-votes="edit.vote_score"
:user-vote="edit.user_vote"
Expand Down
2 changes: 1 addition & 1 deletion resources/js/Components/Resources/ResourceItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const emit = defineEmits(["upvote", "downvote"]);
<td class="align-top pr-6">
<Upvotable
:upvotable-id="resource.id"
:upvotable-type="'resource'"
:upvotable-key="'resource'"
:initial-votes="resource.vote_score"
:user-vote="resource.user_vote"
></Upvotable>
Expand Down
4 changes: 2 additions & 2 deletions resources/js/Components/Resources/Reviews/ResourceReview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const ratingFeatures = Object.entries(ratingLabels).map(([key, label]) => ({

<template>
<Upvotable
:upvotable-type="'review'"
:upvotable-key="'review'"
:upvotable-id="props.review.id"
:initial-votes="props.review.vote_score"
:user-vote="props.review.user_vote"
Expand Down Expand Up @@ -75,7 +75,7 @@ const ratingFeatures = Object.entries(ratingLabels).map(([key, label]) => ({

<Commentable
:commentable-id="review.id"
:commentable-type="'review'"
:commentable-key="'review'"
:comments-count="review.comments_count"
/>
</template>
6 changes: 3 additions & 3 deletions resources/js/Components/Upvote/Upvotable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Icon } from "@iconify/vue";
import { router } from '@inertiajs/vue3'

const props = defineProps({
upvotableType: {
upvotableKey: {
type: String,
required: true,
},
Expand Down Expand Up @@ -52,7 +52,7 @@ async function handleUpvote() {
const response = await axios.post(
route("upvote", {
id: props.upvotableId,
type: props.upvotableType,
type: props.upvotableKey,
})
);
userVote.value = response.data.userVote;
Expand All @@ -77,7 +77,7 @@ async function handleDownvote() {
const response = await axios.post(
route("downvote", {
id: props.upvotableId,
type: props.upvotableType,
type: props.upvotableKey,
})
);
userVote.value = response.data.userVote;
Expand Down
4 changes: 2 additions & 2 deletions resources/js/Pages/ResourceEdits/Show.vue
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ function mergeEdits(id) {
<!-- TODO: Add partial reload to the refresh -->
<Upvotable
:flexRow="true"
:upvotable-type="'edit'"
:upvotable-key="'edit'"
:upvotable-id="resourceId"
:initial-votes="editedResource.vote_score"
:user-vote="editedResource.user_vote"
Expand Down Expand Up @@ -565,7 +565,7 @@ function mergeEdits(id) {

<Commentable
:commentable-id="props.editedResource.id"
:commentable-type="'edit'"
:commentable-key="'edit'"
:comments-count="props.editedResource.comments_count"
></Commentable>
</div>
Expand Down
4 changes: 2 additions & 2 deletions resources/js/Pages/Resources/Show.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const sortingType = urlParams.get('sort_by') || 'top';
>
<UpvoteResource
:upvotable-id="props.resource.id"
:upvotable-type="'resource'"
:upvotable-key="'resource'"
:initial-votes="
props.resource.vote_score
"
Expand Down Expand Up @@ -246,7 +246,7 @@ const sortingType = urlParams.get('sort_by') || 'top';
:sort-by-initial-value="props.discussionSortByValue"
:has-sort-by-dropdown="false"
:commentable-id="props.resource.id"
:commentable-type="'resource'"
:commentable-key="'resource'"
:comments-count="
props.resource.comments_count
"
Expand Down
Loading
Loading