Skip to content

Commit 8a15fce

Browse files
river0525claude
andcommitted
fix: address remaining CodeRabbit/Copilot findings on PR #3316
- Skip getMyVote API call when user is not logged in to avoid unnecessary 401s - Use resolve() for /votes/[slug] href in dropdown to satisfy no-navigation-without-resolve - Guard getMedianVote endpoint: require auth and verify user has voted before returning stats Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 61d6892 commit 8a15fce

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/features/votes/components/VotableGrade.svelte

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<script lang="ts">
22
import { tick } from 'svelte';
33
import { enhance } from '$app/forms';
4+
import { resolve } from '$app/paths';
45
56
import { Dropdown, DropdownItem, DropdownDivider } from 'flowbite-svelte';
67
import Check from '@lucide/svelte/icons/check';
78
89
import { TaskGrade, getTaskGrade, type TaskResult } from '$lib/types/task';
910
import { getTaskGradeLabel } from '$lib/utils/task';
1011
import { nonPendingGrades } from '$features/votes/utils/grade_options';
11-
import { SIGNUP_PAGE, LOGIN_PAGE, VOTES_PAGE } from '$lib/constants/navbar-links';
12+
import { SIGNUP_PAGE, LOGIN_PAGE } from '$lib/constants/navbar-links';
1213
import { errorMessageStore } from '$lib/stores/error_message';
1314
1415
import GradeLabel from '$lib/components/GradeLabel.svelte';
@@ -42,7 +43,7 @@
4243
let votedGrade = $state<TaskGrade | null>(null);
4344
4445
async function onTriggerClick() {
45-
if (isOpening) return;
46+
if (!isLoggedIn || isOpening) return;
4647
isOpening = true;
4748
try {
4849
const res = await fetch(
@@ -169,7 +170,9 @@
169170
</DropdownItem>
170171
{/each}
171172
<DropdownDivider />
172-
<DropdownItem href="{VOTES_PAGE}/{taskResult.task_id}" class="rounded-md">詳細</DropdownItem>
173+
<DropdownItem href={resolve('/votes/[slug]', { slug: taskResult.task_id })} class="rounded-md"
174+
>詳細</DropdownItem
175+
>
173176
</Dropdown>
174177
{:else}
175178
<Dropdown

src/routes/problems/getMedianVote/+server.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import { json, type RequestHandler } from '@sveltejs/kit';
22
import { getVoteStatsByTaskId } from '$features/votes/services/vote_statistics';
3+
import { getVoteGrade } from '$features/votes/services/vote_grade';
34

4-
export const GET: RequestHandler = async ({ url }) => {
5+
export const GET: RequestHandler = async ({ url, locals }) => {
56
const taskId = url.searchParams.get('taskId');
67
if (!taskId) return json({ error: 'taskId required' }, { status: 400 });
78

9+
const session = await locals.auth.validate();
10+
if (!session || !session.user || !session.user.userId) {
11+
return json({ error: 'unauthorized' }, { status: 401 });
12+
}
13+
814
try {
15+
// Only return vote statistics to users who have already cast a vote for this task.
16+
const voteRecord = await getVoteGrade(session.user.userId, taskId);
17+
if (!voteRecord.voted) return json({ grade: null });
18+
919
const stats = await getVoteStatsByTaskId(taskId);
1020
if (stats && stats.grade) return json({ grade: stats.grade });
1121
return json({ grade: null });

0 commit comments

Comments
 (0)