删除云图时自动同步图鉴:移除无云图的类型、替换首个云图引用,补充测试覆盖

This commit is contained in:
2026-07-30 23:10:27 +08:00
parent e75833ee4b
commit de044ac4a1
2 changed files with 116 additions and 1 deletions
+54
View File
@@ -74,6 +74,54 @@ async def _get_cloud(db: DbSession, cloud_id: uuid.UUID) -> Cloud | None:
return result.scalar_one_or_none()
async def _sync_collections_after_cloud_removal(
db: DbSession,
user_id: uuid.UUID,
cloud_type_ids: set[int],
) -> None:
"""Keep collection entries aligned with the user's remaining typed clouds."""
if not cloud_type_ids:
return
result = await db.execute(
select(UserCollection).where(
UserCollection.user_id == user_id,
UserCollection.cloud_type_id.in_(cloud_type_ids),
)
)
collections_by_type = {
item.cloud_type_id: item for item in result.scalars().all()
}
removed_count = 0
for cloud_type_id in cloud_type_ids:
collection = collections_by_type.get(cloud_type_id)
if not collection:
continue
replacement_cloud_id = await db.scalar(
select(Cloud.id)
.where(
Cloud.user_id == user_id,
Cloud.cloud_type_id == cloud_type_id,
)
.order_by(Cloud.created_at.asc(), Cloud.id.asc())
.limit(1)
)
if replacement_cloud_id is None:
await db.delete(collection)
removed_count += 1
elif collection.first_cloud_id != replacement_cloud_id:
collection.first_cloud_id = replacement_cloud_id
if removed_count:
await db.execute(
update(User)
.where(User.id == user_id)
.values(collection_count=User.collection_count - removed_count)
)
@router.get("", response_model=PageOut[CloudOut])
async def list_gallery_clouds(
db: DbSession,
@@ -335,9 +383,12 @@ async def delete_cloud(cloud_id: uuid.UUID, db: DbSession, user: CurrentUser) ->
if cloud.user_id != user.id and user.role != "admin":
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="不能删除其他用户的图片")
paths = [cloud.image_path, cloud.thumbnail_path]
cloud_type_ids = {cloud.cloud_type_id} if cloud.cloud_type_id is not None else set()
was_public = cloud.status == "approved" and not cloud.is_hidden
await db.delete(cloud)
await db.flush()
await _sync_collections_after_cloud_removal(db, cloud.user_id, cloud_type_ids)
await db.execute(
update(User)
@@ -370,9 +421,12 @@ async def batch_delete_clouds(
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="部分图片不存在或不属于当前用户")
public_count = sum(1 for c in clouds if c.status == "approved" and not c.is_hidden)
cloud_type_ids = {c.cloud_type_id for c in clouds if c.cloud_type_id is not None}
paths = [path for cloud in clouds for path in (cloud.image_path, cloud.thumbnail_path)]
for cloud in clouds:
await db.delete(cloud)
await db.flush()
await _sync_collections_after_cloud_removal(db, user.id, cloud_type_ids)
await db.execute(
update(User)