From 6be5d6a5d14cd4141d46f1ffcc4cb272c5a87621 Mon Sep 17 00:00:00 2001 From: namse Date: Sat, 21 Sep 2024 09:38:30 +0000 Subject: [PATCH] Put tags in asset, remove sprite & audio doc --- .../new-server/database/database/src/lib.rs | 9 ++++ .../database/schema/0/src/asset/audio.rs | 8 ---- .../database/schema/0/src/asset/mod.rs | 15 +++++-- .../database/schema/0/src/asset/sprite/mod.rs | 45 ------------------- .../database/schema/0/src/asset/tag.rs | 31 +++++++++++++ luda-editor/new-server/rpc/src/lib.rs | 16 ++++++- luda-editor/new-server/rpc/src/types/mod.rs | 8 +--- .../new-server/server/src/api/asset/mod.rs | 1 + .../asset/reserve_team_asset_upload/mod.rs | 7 +++ .../asset/update_asset_tags_for_asset/mod.rs | 33 ++++++++++++++ 10 files changed, 108 insertions(+), 65 deletions(-) delete mode 100644 luda-editor/new-server/database/schema/0/src/asset/audio.rs delete mode 100644 luda-editor/new-server/database/schema/0/src/asset/sprite/mod.rs create mode 100644 luda-editor/new-server/database/schema/0/src/asset/tag.rs create mode 100644 luda-editor/new-server/server/src/api/asset/update_asset_tags_for_asset/mod.rs diff --git a/luda-editor/new-server/database/database/src/lib.rs b/luda-editor/new-server/database/database/src/lib.rs index 4a706b2fc..7a86d0d69 100644 --- a/luda-editor/new-server/database/database/src/lib.rs +++ b/luda-editor/new-server/database/database/src/lib.rs @@ -107,3 +107,12 @@ impl MaybeAborted { } } } + +impl MaybeAborted<()> { + pub fn unwrap(self) { + match self { + MaybeAborted::Aborted { .. } => unreachable!("You should make AbortReason generic"), + MaybeAborted::No => (), + } + } +} diff --git a/luda-editor/new-server/database/schema/0/src/asset/audio.rs b/luda-editor/new-server/database/schema/0/src/asset/audio.rs deleted file mode 100644 index 662a6b62f..000000000 --- a/luda-editor/new-server/database/schema/0/src/asset/audio.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::*; - -#[document] -struct AudioDoc { - #[pk] - id: String, - name: String, -} diff --git a/luda-editor/new-server/database/schema/0/src/asset/mod.rs b/luda-editor/new-server/database/schema/0/src/asset/mod.rs index 5f0865dde..34f40e2bc 100644 --- a/luda-editor/new-server/database/schema/0/src/asset/mod.rs +++ b/luda-editor/new-server/database/schema/0/src/asset/mod.rs @@ -6,12 +6,11 @@ //! Any member of the team can delete the assets uploaded by the team. //! Trash can function. What the team deleted is temporarily stored for 1 week by default, and can be forcibly deleted -mod audio; -mod sprite; +mod tag; use crate::*; -pub use audio::*; -pub use sprite::*; +use std::collections::HashSet; +pub use tag::*; #[document] struct AssetDoc { @@ -21,6 +20,7 @@ struct AssetDoc { shared: bool, asset_kind: AssetKind, byte_size: u64, + tags: HashSet, } #[doc_part] @@ -38,6 +38,13 @@ struct TeamAssetDoc { asset_id: String, } +#[document] +struct AssetTeamDoc { + #[pk] + asset_id: String, + team_id: String, +} + #[document] struct TeamAssetTotalBytesDoc { #[pk] diff --git a/luda-editor/new-server/database/schema/0/src/asset/sprite/mod.rs b/luda-editor/new-server/database/schema/0/src/asset/sprite/mod.rs deleted file mode 100644 index 5672d8f91..000000000 --- a/luda-editor/new-server/database/schema/0/src/asset/sprite/mod.rs +++ /dev/null @@ -1,45 +0,0 @@ -use crate::*; - -#[document] -struct SpriteDoc { - #[pk] - id: String, - sprite: Sprite, - tags: Vec, -} - -#[doc_part] -enum SpriteTag { - System { tag: SystemTag }, - Custom { id: String }, -} - -#[doc_part] -#[derive(Copy, PartialEq, Eq, Hash)] -#[archive_attr(derive(PartialEq, Eq, Hash))] -#[repr(u8)] -enum SystemTag { - Character, - Object, - Background, -} - -#[document] -struct SpriteTagDoc { - #[pk] - id: String, - names: Translations, -} - -#[doc_part] -struct Sprite { - id: String, - name: String, - kind: SpriteKind, -} - -#[doc_part] -enum SpriteKind { - Parts, - SingleImage, -} diff --git a/luda-editor/new-server/database/schema/0/src/asset/tag.rs b/luda-editor/new-server/database/schema/0/src/asset/tag.rs new file mode 100644 index 000000000..abde66215 --- /dev/null +++ b/luda-editor/new-server/database/schema/0/src/asset/tag.rs @@ -0,0 +1,31 @@ +use crate::*; + +#[doc_part] +#[derive(Copy, PartialEq, Eq, Hash)] +#[archive_attr(derive(PartialEq, Eq, Hash))] +#[repr(u8)] +enum AssetSystemTag { + // Sprite 0 ~ 39 + SpriteCharacter = 0, + SpriteObject = 1, + SpriteBackground = 2, + // Audio 40 ~ 79 + AudioCharacter = 40, + AudioProp = 41, + AudioBackground = 42, +} + +#[document] +struct AssetCustomTagDoc { + #[pk] + id: String, + names: Translations, +} + +#[doc_part] +#[derive(PartialEq, Eq, Hash)] +#[archive_attr(derive(PartialEq, Eq, Hash))] +enum AssetTag { + System { tag: AssetSystemTag }, + Custom { id: String }, +} diff --git a/luda-editor/new-server/rpc/src/lib.rs b/luda-editor/new-server/rpc/src/lib.rs index b35ecbbc5..2e6948937 100644 --- a/luda-editor/new-server/rpc/src/lib.rs +++ b/luda-editor/new-server/rpc/src/lib.rs @@ -266,7 +266,8 @@ rpc_macro::define_rpc! { team_id: String, asset_name: String, byte_size: u64, - asset_kind: migration::schema::AssetKind, + asset_kind: AssetKind, + tags: Vec, } struct Response { asset_id: String, @@ -291,5 +292,18 @@ rpc_macro::define_rpc! { PermissionDenied, } }, + update_asset_tags_for_asset: { + struct Request { + asset_id: String, + tags: Vec, + } + struct Response { + } + enum Error { + NeedLogin, + AssetNotExist, + PermissionDenied, + } + }, }, } diff --git a/luda-editor/new-server/rpc/src/types/mod.rs b/luda-editor/new-server/rpc/src/types/mod.rs index 30d1e0942..01ea99ed2 100644 --- a/luda-editor/new-server/rpc/src/types/mod.rs +++ b/luda-editor/new-server/rpc/src/types/mod.rs @@ -52,10 +52,4 @@ pub enum EpisodeEditAction { }, } -pub type SceneSprite = migration::schema::SceneSprite; -pub type SpriteDoc = migration::schema::SpriteDoc; -pub type Sprite = migration::schema::Sprite; -pub type SystemTag = migration::schema::SystemTag; -pub type SpriteTag = migration::schema::SpriteTag; -pub type AssetKind = migration::schema::AssetKind; -pub type AssetDoc = migration::schema::AssetDoc; +pub use migration::schema::{AssetDoc, AssetKind, AssetSystemTag, AssetTag, SceneSprite}; diff --git a/luda-editor/new-server/server/src/api/asset/mod.rs b/luda-editor/new-server/server/src/api/asset/mod.rs index c2f403129..1fe6feefc 100644 --- a/luda-editor/new-server/server/src/api/asset/mod.rs +++ b/luda-editor/new-server/server/src/api/asset/mod.rs @@ -3,6 +3,7 @@ mod common; pub mod get_team_asset_docs; pub mod reserve_team_asset_upload; +pub mod update_asset_tags_for_asset; #[allow(unused_imports)] pub use common::*; diff --git a/luda-editor/new-server/server/src/api/asset/reserve_team_asset_upload/mod.rs b/luda-editor/new-server/server/src/api/asset/reserve_team_asset_upload/mod.rs index ca273913a..7983d4423 100644 --- a/luda-editor/new-server/server/src/api/asset/reserve_team_asset_upload/mod.rs +++ b/luda-editor/new-server/server/src/api/asset/reserve_team_asset_upload/mod.rs @@ -11,6 +11,7 @@ pub async fn reserve_team_asset_upload( asset_name, byte_size, asset_kind, + tags, }: &ArchivedRequest, db: &Database, session: Session, @@ -46,6 +47,11 @@ pub async fn reserve_team_asset_upload( asset_id: &asset_id, ttl: None, }, + AssetTeamDocPut { + asset_id: &asset_id, + team_id, + ttl: None, + }, AssetDocPut { id: &asset_id, name: asset_name, @@ -53,6 +59,7 @@ pub async fn reserve_team_asset_upload( asset_kind: &asset_kind.deserialize(), byte_size: *byte_size, ttl: None, + tags: &tags.iter().map(|x| x.deserialize()).collect(), }, )) .await? diff --git a/luda-editor/new-server/server/src/api/asset/update_asset_tags_for_asset/mod.rs b/luda-editor/new-server/server/src/api/asset/update_asset_tags_for_asset/mod.rs new file mode 100644 index 000000000..b5eede1f5 --- /dev/null +++ b/luda-editor/new-server/server/src/api/asset/update_asset_tags_for_asset/mod.rs @@ -0,0 +1,33 @@ +use crate::*; +use api::team::is_team_member; +use database::{schema::*, DeserializeInfallible, WantUpdate}; +use luda_rpc::asset::update_asset_tags_for_asset::*; + +pub async fn update_asset_tags_for_asset( + ArchivedRequest { asset_id, tags }: &ArchivedRequest, + db: &Database, + session: Session, +) -> Result { + let user_id = session.user_id().await.ok_or(Error::NeedLogin)?; + + let asset_team_doc = db + .get(AssetTeamDocGet { asset_id }) + .await? + .ok_or(Error::AssetNotExist)?; + + if !is_team_member(db, &asset_team_doc.team_id, &user_id).await? { + bail!(Error::PermissionDenied) + } + + db.transact::<()>(AssetDocUpdate { + id: asset_id, + want_update: |_| WantUpdate::Yes, + update: |doc| { + doc.tags = tags.iter().map(|x| x.deserialize()).collect(); + }, + }) + .await? + .unwrap(); + + Ok(Response {}) +}