From ea6e1aaa55c1582308bc4abbaa976565ed952841 Mon Sep 17 00:00:00 2001 From: zurdi Date: Thu, 24 Sep 2026 15:55:12 +0200 Subject: [PATCH] review: code-review xhigh fixes - A save's or state's download URL carries its file name, since a rename keeps updated_at and would otherwise hand a cached download back under the old name. - A name with nothing before the extension is refused, instead of moving the thumbnail to a bare ".png" that no longer binds. - The memory card editor only closes on the card it saved, and its Save stays off until the name or the visibility changes. - Comment lines this PR rewrote lose their em-dashes, new headers fit two lines, and the settings test drops its `as unknown as` fixtures. Co-Authored-By: Claude Opus 5.5 --- backend/handler/asset_store.py | 8 +++++++- backend/models/assets.py | 7 +++++-- backend/tests/endpoints/test_states.py | 10 ++++++++++ .../components/Collections/NewCollectionRow.vue | 4 ++-- .../Gallery/CollectionSettingsTab.test.ts | 4 ++-- .../v2/components/GameDetails/ScreenshotsTab.vue | 6 +++--- .../components/Player/MemoryCardDialog.test.ts | 8 ++++++++ .../v2/components/Player/MemoryCardDialog.vue | 16 +++++++++++----- .../v2/components/Player/MemoryCardManager.vue | 2 +- .../src/v2/components/shared/PublicBadge.vue | 5 ++--- .../v2/components/shared/VisibilitySwitch.vue | 5 ++--- frontend/src/v2/lib/forms/RSwitch/RSwitch.vue | 4 ++-- 12 files changed, 55 insertions(+), 24 deletions(-) diff --git a/backend/handler/asset_store.py b/backend/handler/asset_store.py index fd9a677ec..52e1b7262 100644 --- a/backend/handler/asset_store.py +++ b/backend/handler/asset_store.py @@ -204,6 +204,13 @@ async def rename_asset( ) from exc if new_name == asset.file_name: return {} + # The thumbnail follows the stem, so a bare extension would strand it. + new_stem = compute_file_name_no_ext(new_name) + if not new_stem: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Invalid filename: it needs a name before the extension", + ) folded = new_name.casefold() if any( @@ -214,7 +221,6 @@ async def rename_asset( # Thumbnails bind by stem, so taking a stem another screenshot holds would # show that one, and delete it along with the asset. - new_stem = compute_file_name_no_ext(new_name) thumbnail = asset.screenshot bound = db_screenshot_handler.get_screenshot( rom_id=asset.rom_id, diff --git a/backend/models/assets.py b/backend/models/assets.py index 53a5d4e79..081bb1f47 100644 --- a/backend/models/assets.py +++ b/backend/models/assets.py @@ -2,6 +2,7 @@ from __future__ import annotations from functools import cached_property from typing import TYPE_CHECKING +from urllib.parse import quote from sqlalchemy import BigInteger, ForeignKey, Index, String from sqlalchemy.orm import Mapped, mapped_column, relationship, validates @@ -61,9 +62,11 @@ class BaseAsset(BaseModel): @cached_property def download_path(self) -> str: - # Served by the per-type `/{id}/content` route + # Served by the per-type `/{id}/content` route. A rename keeps + # `updated_at`, so the name joins the cache key. return ( - f"/api/{self.__tablename__}/{self.id}/content?timestamp={self.updated_at}" + f"/api/{self.__tablename__}/{self.id}/content" + f"?timestamp={self.updated_at}&name={quote(self.file_name)}" ) diff --git a/backend/tests/endpoints/test_states.py b/backend/tests/endpoints/test_states.py index ce007dfeb..cffa0010a 100644 --- a/backend/tests/endpoints/test_states.py +++ b/backend/tests/endpoints/test_states.py @@ -829,6 +829,16 @@ class TestStateRename: refreshed = db_state_handler.get_state_by_id(state.id) assert refreshed is not None and refreshed.file_name == "test_state.state" + def test_name_with_nothing_before_the_extension_is_rejected( + self, client, access_token: str, state: State, state_file, thumbnail + ): + response = self._rename(client, access_token, state.id, ".state") + + assert response.status_code == status.HTTP_400_BAD_REQUEST + assert state_file.exists() + kept = db_screenshot_handler.get_screenshot_by_id(thumbnail.id) + assert kept is not None and kept.file_name == "test_state.png" + def test_blank_name_is_rejected( self, client, access_token: str, state: State, state_file ): diff --git a/frontend/src/v2/components/Collections/NewCollectionRow.vue b/frontend/src/v2/components/Collections/NewCollectionRow.vue index 165f3e833..4d58fbfb8 100644 --- a/frontend/src/v2/components/Collections/NewCollectionRow.vue +++ b/frontend/src/v2/components/Collections/NewCollectionRow.vue @@ -4,8 +4,8 @@ // Expanded: inline name input and visibility switch with Create / Cancel // actions. // -// Stateless — the parent owns `expanded`, `name`, `isPublic` and `creating`. The -// outer row + tile are persistent across both states so the background +// Stateless: the parent owns `expanded`, `name`, `isPublic` and `creating`. +// The outer row + tile are persistent across both states so the background // tint and tile-active swap transition smoothly instead of remounting; // only the middle (label vs input) and the trailing actions toggle. import { RBtn, RIcon } from "@v2/lib"; diff --git a/frontend/src/v2/components/Gallery/CollectionSettingsTab.test.ts b/frontend/src/v2/components/Gallery/CollectionSettingsTab.test.ts index f573b8d4a..4085b4db8 100644 --- a/frontend/src/v2/components/Gallery/CollectionSettingsTab.test.ts +++ b/frontend/src/v2/components/Gallery/CollectionSettingsTab.test.ts @@ -29,7 +29,7 @@ const stored = { user_id: 1, is_public: false, rom_ids: [1, 2], -} as unknown as Collection; +} as Collection; // Stands in for the switch: a click asks to flip it. const VisibilitySwitch = { @@ -65,7 +65,7 @@ describe("CollectionSettingsTab visibility", () => { storeAuth().setCurrentUser({ id: 1, oauth_scopes: ["collections.write"], - } as unknown as User); + } as User); }); it("saves as soon as it is switched, keeping a name draft out", async () => { diff --git a/frontend/src/v2/components/GameDetails/ScreenshotsTab.vue b/frontend/src/v2/components/GameDetails/ScreenshotsTab.vue index 5d3d199ad..6d4dd7570 100644 --- a/frontend/src/v2/components/GameDetails/ScreenshotsTab.vue +++ b/frontend/src/v2/components/GameDetails/ScreenshotsTab.vue @@ -5,9 +5,9 @@ // // Per-item affordances are driven by the item fields + props (the parent, // MediaTab, performs the actions): -// * edit — when `editable` and owned: opens its visibility (top-right) -// * delete — when `deletable` and the item is owned (top-right, hover) -// * username — community items (others' public shots) show an owner chip +// * edit: when `editable` and owned, opens its visibility (top-right) +// * delete: when `deletable` and the item is owned (top-right, hover) +// * username: community items (others' public shots) show an owner chip import { RAvatar, RBtn, RCarousel } from "@v2/lib"; import { computed, ref } from "vue"; import { useI18n } from "vue-i18n"; diff --git a/frontend/src/v2/components/Player/MemoryCardDialog.test.ts b/frontend/src/v2/components/Player/MemoryCardDialog.test.ts index 5b39deea4..04775f626 100644 --- a/frontend/src/v2/components/Player/MemoryCardDialog.test.ts +++ b/frontend/src/v2/components/Player/MemoryCardDialog.test.ts @@ -54,6 +54,14 @@ describe("MemoryCardDialog", () => { ); }); + it("keeps Save off until the name or the visibility changes", async () => { + const wrapper = mountDialog({ initialName: "Main" }); + expect(wrapper.get(".confirm").attributes("disabled")).toBeDefined(); + + await wrapper.get(".public").setValue(true); + expect(wrapper.get(".confirm").attributes("disabled")).toBeUndefined(); + }); + it("submits the trimmed name with the visibility", async () => { const wrapper = mountDialog(); diff --git a/frontend/src/v2/components/Player/MemoryCardDialog.vue b/frontend/src/v2/components/Player/MemoryCardDialog.vue index 73bcc0713..cefe466a4 100644 --- a/frontend/src/v2/components/Player/MemoryCardDialog.vue +++ b/frontend/src/v2/components/Player/MemoryCardDialog.vue @@ -4,7 +4,7 @@ // same fields, the same validation and the same footer, so they are one // dialog with a different title and confirm label. import { RBtn, RDialog, RForm, RTextField } from "@v2/lib"; -import { ref, watch } from "vue"; +import { computed, ref, watch } from "vue"; import { useI18n } from "vue-i18n"; import VisibilitySwitch from "@/v2/components/shared/VisibilitySwitch.vue"; import { required } from "@/v2/utils/validation"; @@ -46,6 +46,13 @@ const name = ref(props.initialName); const isPublic = ref(props.initialPublic); const valid = ref(true); const rules = [required(t("common.required"))]; +const canSubmit = computed( + () => + !!name.value.trim() && + (name.value.trim() !== props.initialName || + isPublic.value !== props.initialPublic) && + !props.busy, +); // Reopening is what resets the fields, so a cancelled edit does not carry // into the next one. @@ -64,9 +71,8 @@ function close(): void { } function submit(): void { - const trimmed = name.value.trim(); - if (!trimmed || props.busy) return; - emit("submit", { name: trimmed, isPublic: isPublic.value }); + if (!canSubmit.value) return; + emit("submit", { name: name.value.trim(), isPublic: isPublic.value }); } @@ -115,7 +121,7 @@ function submit(): void { variant="flat" color="primary" :prepend-icon="confirmIcon" - :disabled="!name.trim() || busy" + :disabled="!canSubmit" :loading="busy" @click="submit" > diff --git a/frontend/src/v2/components/Player/MemoryCardManager.vue b/frontend/src/v2/components/Player/MemoryCardManager.vue index 45d6f7007..31b52af3e 100644 --- a/frontend/src/v2/components/Player/MemoryCardManager.vue +++ b/frontend/src/v2/components/Player/MemoryCardManager.vue @@ -115,7 +115,7 @@ async function submitEdit({ name, isPublic }: MemoryCardFields): Promise { })); } cards.value = cards.value.map((c) => (c.id === updated.id ? updated : c)); - editTarget.value = null; + if (editTarget.value === card) editTarget.value = null; emit("changed"); snackbar.success(t("play.memory-card-edited"), { icon: "mdi-check-bold" }); } catch (err) { diff --git a/frontend/src/v2/components/shared/PublicBadge.vue b/frontend/src/v2/components/shared/PublicBadge.vue index 301e4f581..73d39e893 100644 --- a/frontend/src/v2/components/shared/PublicBadge.vue +++ b/frontend/src/v2/components/shared/PublicBadge.vue @@ -1,7 +1,6 @@