mirror of
https://github.com/invoke-ai/InvokeAI.git
synced 2026-02-14 18:34:55 -05:00
When we delete images, boards, or do any other board mutation, we need to invalidate numerous query caches and related internal frontend state. This gets complicated very quickly. We can drastically reduce the complexity by having the backend return some more information when we make these mutations. For example, when deleting a list of images by name, we can return a list of deleted image name and affected boards. The frontend can use this information to determine which queries to invalidate with far less tedium. This will also enable the more efficient storage of images (e.g. in the gallery selection). Previously, we had to store the entire image DTO object, else we wouldn't be able to figure out which queries to invalidate. But now that the backend tells us exactly what images/boards have changed, we can just store image names in frontend state. This amounts to a substantial improvement in DX and reduction in frontend complexity.
66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
from typing import Optional
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
from invokeai.app.services.image_records.image_records_common import ImageRecord
|
|
from invokeai.app.util.model_exclude_null import BaseModelExcludeNull
|
|
|
|
|
|
class ImageUrlsDTO(BaseModelExcludeNull):
|
|
"""The URLs for an image and its thumbnail."""
|
|
|
|
image_name: str = Field(description="The unique name of the image.")
|
|
"""The unique name of the image."""
|
|
image_url: str = Field(description="The URL of the image.")
|
|
"""The URL of the image."""
|
|
thumbnail_url: str = Field(description="The URL of the image's thumbnail.")
|
|
"""The URL of the image's thumbnail."""
|
|
|
|
|
|
class ImageDTO(ImageRecord, ImageUrlsDTO):
|
|
"""Deserialized image record, enriched for the frontend."""
|
|
|
|
board_id: Optional[str] = Field(
|
|
default=None, description="The id of the board the image belongs to, if one exists."
|
|
)
|
|
"""The id of the board the image belongs to, if one exists."""
|
|
|
|
|
|
def image_record_to_dto(
|
|
image_record: ImageRecord,
|
|
image_url: str,
|
|
thumbnail_url: str,
|
|
board_id: Optional[str],
|
|
) -> ImageDTO:
|
|
"""Converts an image record to an image DTO."""
|
|
return ImageDTO(
|
|
**image_record.model_dump(),
|
|
image_url=image_url,
|
|
thumbnail_url=thumbnail_url,
|
|
board_id=board_id,
|
|
)
|
|
|
|
|
|
class ResultWithAffectedBoards(BaseModel):
|
|
affected_boards: list[str] = Field(description="The ids of boards affected by the delete operation")
|
|
|
|
|
|
class DeleteImagesResult(ResultWithAffectedBoards):
|
|
deleted_images: list[str] = Field(description="The names of the images that were deleted")
|
|
|
|
|
|
class StarredImagesResult(ResultWithAffectedBoards):
|
|
starred_images: list[str] = Field(description="The names of the images that were starred")
|
|
|
|
|
|
class UnstarredImagesResult(ResultWithAffectedBoards):
|
|
unstarred_images: list[str] = Field(description="The names of the images that were unstarred")
|
|
|
|
|
|
class AddImagesToBoardResult(ResultWithAffectedBoards):
|
|
added_images: list[str] = Field(description="The image names that were added to the board")
|
|
|
|
|
|
class RemoveImagesFromBoardResult(ResultWithAffectedBoards):
|
|
removed_images: list[str] = Field(description="The image names that were removed from their board")
|