mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 02:22:44 +01:00
A filename and a timestamp cannot tell apart a 100% run from a speedrun
seed, so saves and states carry two owner-only annotations: a favorite
heart that sorts them ahead of their band, and a set of free-text labels.
Backend adds `is_favorite` and a JSON `labels` to both tables, with
`PUT /{saves,states}/{id}/{favorite,labels}` mirroring the existing
visibility toggle. Labels are trimmed, de-duplicated case-insensitively
and capped. Both routes write with `touch=False`, so annotating a save
leaves `updated_at` alone: it is not a write to the bytes, and device
sync reads that column to decide staleness.
In v2 the Save data tab grows a heart and a labels button per row, the
labels dialog suggests the labels already used on the ROM, and favorites
lead their slot or core without ever folding the newest version away.
The heart glyph shows only on the launch screens, where the toggle that
would otherwise say it is not there. Phones give the row four bands:
name, labels, facts, buttons.
Closes #942
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016bZ7R3xC73MZu1vTNuatPK
260 lines
10 KiB
Python
260 lines
10 KiB
Python
from __future__ import annotations
|
|
|
|
from functools import cached_property
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import BigInteger, ForeignKey, Index, String
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship, validates
|
|
|
|
from models.base import (
|
|
FILE_EXTENSION_MAX_LENGTH,
|
|
FILE_NAME_MAX_LENGTH,
|
|
FILE_PATH_MAX_LENGTH,
|
|
BaseModel,
|
|
compute_file_name_parts,
|
|
)
|
|
from utils.database import CustomJSON
|
|
|
|
if TYPE_CHECKING:
|
|
from models.device_save_sync import DeviceSaveSync
|
|
from models.platform import Platform
|
|
from models.rom import Rom
|
|
from models.user import User
|
|
|
|
|
|
SAVE_SLOT_MAX_LENGTH = 255
|
|
ASSET_LABEL_MAX_LENGTH = 255
|
|
ASSET_LABELS_MAX = 20
|
|
|
|
|
|
class BaseAsset(BaseModel):
|
|
__abstract__ = True
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
file_name: Mapped[str] = mapped_column(String(length=FILE_NAME_MAX_LENGTH))
|
|
file_name_no_tags: Mapped[str] = mapped_column(String(length=FILE_NAME_MAX_LENGTH))
|
|
file_name_no_ext: Mapped[str] = mapped_column(String(length=FILE_NAME_MAX_LENGTH))
|
|
file_extension: Mapped[str] = mapped_column(
|
|
String(length=FILE_EXTENSION_MAX_LENGTH)
|
|
)
|
|
file_path: Mapped[str] = mapped_column(String(length=FILE_PATH_MAX_LENGTH))
|
|
file_size_bytes: Mapped[int] = mapped_column(BigInteger(), default=0)
|
|
|
|
missing_from_fs: Mapped[bool] = mapped_column(default=False, nullable=False)
|
|
|
|
@validates("file_name")
|
|
def _sync_file_name_parts(self, _key: str, file_name: str) -> str:
|
|
"""Derive the stored `file_name_no_tags` / `file_name_no_ext` /
|
|
`file_extension` columns whenever `file_name` is assigned.
|
|
|
|
Defined on the abstract base so every asset subclass inherits it.
|
|
"""
|
|
parts = compute_file_name_parts(file_name)
|
|
self.file_name_no_tags = parts.no_tags
|
|
self.file_name_no_ext = parts.no_ext
|
|
self.file_extension = parts.extension
|
|
return file_name
|
|
|
|
@cached_property
|
|
def full_path(self) -> str:
|
|
return f"{self.file_path}/{self.file_name}"
|
|
|
|
@cached_property
|
|
def download_path(self) -> str:
|
|
# Served by the per-type `/{id}/content` route
|
|
return (
|
|
f"/api/{self.__tablename__}/{self.id}/content?timestamp={self.updated_at}"
|
|
)
|
|
|
|
|
|
class RomAsset(BaseAsset):
|
|
__abstract__ = True
|
|
|
|
rom_id: Mapped[int] = mapped_column(ForeignKey("roms.id", ondelete="CASCADE"))
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
|
|
|
|
|
|
class Screenshot(RomAsset):
|
|
__tablename__ = "screenshots"
|
|
__table_args__ = (
|
|
# `Save.screenshot` / `State.screenshot` hit this once per rendered card.
|
|
Index("ix_screenshots_rom_user", "rom_id", "user_id"),
|
|
Index("idx_screenshots_public", "is_public"),
|
|
{"extend_existing": True},
|
|
)
|
|
|
|
# `is_gallery` distinguishes intentionally-uploaded gallery screenshots from
|
|
# the auto-captured save/state thumbnails that also live in this table.
|
|
# `is_public` mirrors RomNote — lets other users browse a user's public
|
|
# screenshots (community). Both default false; save/state thumbnails keep the
|
|
# defaults, only the gallery upload endpoint sets `is_gallery=True`.
|
|
is_gallery: Mapped[bool] = mapped_column(default=False)
|
|
is_public: Mapped[bool] = mapped_column(default=False)
|
|
|
|
rom: Mapped[Rom] = relationship(lazy="joined", back_populates="screenshots")
|
|
user: Mapped[User] = relationship(lazy="joined", back_populates="screenshots")
|
|
|
|
|
|
class Save(RomAsset):
|
|
__tablename__ = "saves"
|
|
__table_args__ = (
|
|
Index("ix_saves_rom_user_hash", "rom_id", "user_id", "content_hash"),
|
|
Index("idx_saves_public", "is_public"),
|
|
{"extend_existing": True},
|
|
)
|
|
|
|
emulator: Mapped[str | None] = mapped_column(String(length=50))
|
|
slot: Mapped[str | None] = mapped_column(
|
|
String(length=SAVE_SLOT_MAX_LENGTH), index=True
|
|
)
|
|
content_hash: Mapped[str | None] = mapped_column(String(length=32))
|
|
origin_device_id: Mapped[str | None] = mapped_column(
|
|
String(length=255),
|
|
ForeignKey("devices.id", ondelete="SET NULL"),
|
|
default=None,
|
|
index=True,
|
|
)
|
|
# `is_public` mirrors Screenshot/RomNote — lets other users browse and
|
|
# download a user's public saves (community). Defaults false (private).
|
|
is_public: Mapped[bool] = mapped_column(default=False)
|
|
# Owner-only annotations: favorites sort ahead of the rest, labels tell
|
|
# apart runs a filename and a timestamp cannot (a route, a seed, a run).
|
|
is_favorite: Mapped[bool] = mapped_column(default=False)
|
|
labels: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
|
|
|
|
rom: Mapped[Rom] = relationship(lazy="joined", back_populates="saves")
|
|
user: Mapped[User] = relationship(lazy="joined", back_populates="saves")
|
|
device_syncs: Mapped[list[DeviceSaveSync]] = relationship(
|
|
back_populates="save",
|
|
cascade="all, delete-orphan",
|
|
lazy="raise",
|
|
)
|
|
|
|
@cached_property
|
|
def screenshot(self) -> Screenshot | None:
|
|
from handler.database import db_screenshot_handler
|
|
|
|
return db_screenshot_handler.get_screenshot(
|
|
rom_id=self.rom_id,
|
|
user_id=self.user_id,
|
|
file_name=self.file_name, # Match state filename against screenshot filename stem
|
|
file_name_no_ext=self.file_name_no_ext,
|
|
)
|
|
|
|
|
|
class State(RomAsset):
|
|
__tablename__ = "states"
|
|
__table_args__ = (
|
|
Index("ix_states_rom_user", "rom_id", "user_id"),
|
|
Index("idx_states_public", "is_public"),
|
|
{"extend_existing": True},
|
|
)
|
|
|
|
emulator: Mapped[str | None] = mapped_column(String(length=50))
|
|
# `is_public` mirrors Screenshot/RomNote — lets other users browse and
|
|
# download a user's public states (community). Defaults false (private).
|
|
is_public: Mapped[bool] = mapped_column(default=False)
|
|
# Owner-only annotations: favorites sort ahead of the rest, labels tell
|
|
# apart runs a filename and a timestamp cannot (a route, a seed, a run).
|
|
is_favorite: Mapped[bool] = mapped_column(default=False)
|
|
labels: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
|
|
# The disc mounted when this state was captured, so a resume can put the
|
|
# same one back. SET NULL rather than CASCADE: losing the file row must
|
|
# not take the player's save with it.
|
|
disc_file_id: Mapped[int | None] = mapped_column(
|
|
ForeignKey("rom_files.id", ondelete="SET NULL"),
|
|
nullable=True,
|
|
default=None,
|
|
index=True,
|
|
)
|
|
|
|
rom: Mapped[Rom] = relationship(lazy="joined", back_populates="states")
|
|
user: Mapped[User] = relationship(lazy="joined", back_populates="states")
|
|
|
|
@cached_property
|
|
def screenshot(self) -> Screenshot | None:
|
|
from handler.database import db_screenshot_handler
|
|
|
|
return db_screenshot_handler.get_screenshot(
|
|
rom_id=self.rom_id,
|
|
user_id=self.user_id,
|
|
file_name=self.file_name, # Match state filename against screenshot filename stem
|
|
file_name_no_ext=self.file_name_no_ext,
|
|
)
|
|
|
|
|
|
class MemoryCard(BaseModel):
|
|
"""A per-user, per-emulator memory card that follows the user across
|
|
streaming sessions. Unlike Save/State it is not tied to a single ROM: for
|
|
formats like the PCSX2 folder card one card holds every game's saves. The
|
|
card is an identity (name, owner); its actual data lives in `versions`,
|
|
a snapshot history mirroring how EmulatorJS keeps multiple Save rows.
|
|
"""
|
|
|
|
__tablename__ = "memory_cards"
|
|
__table_args__ = (
|
|
Index("ix_memory_cards_user_emulator", "user_id", "emulator"),
|
|
Index("ix_memory_cards_public", "is_public"),
|
|
{"extend_existing": True},
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
|
|
# `emulator` is the hard scoping key: a card is looked up by (user, emulator)
|
|
# at session claim, so one Dolphin card serves both GameCube and Wii roms.
|
|
emulator: Mapped[str] = mapped_column(String(length=50))
|
|
# `platform_id` is a loose, nullable hint (which platform the card was
|
|
# created under) for display/filtering only. It never scopes the lookup, so
|
|
# a card stays visible across every platform its emulator drives.
|
|
platform_id: Mapped[int | None] = mapped_column(
|
|
ForeignKey("platforms.id", ondelete="SET NULL"),
|
|
default=None,
|
|
)
|
|
name: Mapped[str] = mapped_column(String(length=255))
|
|
# Only slot 1 is used today; kept so a future multi-slot layout needs no
|
|
# schema change.
|
|
slot: Mapped[int] = mapped_column(default=1)
|
|
# `is_public` mirrors Save/State, letting another user browse this card and
|
|
# hydrate a snapshot of it. Sharing is one-way: the recipient's writes go
|
|
# to their own new card, never back to this one.
|
|
is_public: Mapped[bool] = mapped_column(default=False)
|
|
|
|
user: Mapped[User] = relationship(lazy="joined", back_populates="memory_cards")
|
|
# One-directional: the loose platform hint, for display only.
|
|
platform: Mapped[Platform | None] = relationship(lazy="joined")
|
|
versions: Mapped[list[MemoryCardVersion]] = relationship(
|
|
back_populates="memory_card",
|
|
cascade="all, delete-orphan",
|
|
lazy="raise",
|
|
order_by="MemoryCardVersion.created_at.desc()",
|
|
)
|
|
|
|
|
|
class MemoryCardVersion(BaseAsset):
|
|
"""A single snapshot of a `MemoryCard`'s data (the whole card image, e.g.
|
|
the zipped PCSX2 folder card). Multiple versions per card form its history.
|
|
"""
|
|
|
|
__tablename__ = "memory_card_versions"
|
|
__table_args__ = (
|
|
Index("ix_memory_card_versions_card_hash", "memory_card_id", "content_hash"),
|
|
{"extend_existing": True},
|
|
)
|
|
|
|
memory_card_id: Mapped[int] = mapped_column(
|
|
ForeignKey("memory_cards.id", ondelete="CASCADE")
|
|
)
|
|
content_hash: Mapped[str | None] = mapped_column(String(length=32))
|
|
|
|
memory_card: Mapped[MemoryCard] = relationship(
|
|
lazy="joined", back_populates="versions"
|
|
)
|
|
|
|
@cached_property
|
|
def download_path(self) -> str:
|
|
# Served under the memory-cards router rather than the default
|
|
# `/api/{tablename}/...`, keeping every card route in one namespace.
|
|
return (
|
|
f"/api/memory-cards/versions/{self.id}/content?timestamp={self.updated_at}"
|
|
)
|