From 0d3fffe9806bafb58e0252e0836668977d73a047 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 6 Nov 2021 12:58:33 +0100 Subject: [PATCH] Implement api to create tag and change file tags Signed-off-by: trivernis --- mediarepo-daemon/Cargo.lock | 4 +- mediarepo-daemon/mediarepo-model/src/file.rs | 13 +++++ mediarepo-daemon/mediarepo-socket/Cargo.lock | 4 +- mediarepo-daemon/mediarepo-socket/Cargo.toml | 2 +- .../mediarepo-socket/src/namespaces/tags.rs | 50 ++++++++++++++++++- 5 files changed, 66 insertions(+), 7 deletions(-) diff --git a/mediarepo-daemon/Cargo.lock b/mediarepo-daemon/Cargo.lock index d4117de..23adadc 100644 --- a/mediarepo-daemon/Cargo.lock +++ b/mediarepo-daemon/Cargo.lock @@ -828,8 +828,8 @@ checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" [[package]] name = "mediarepo-api" -version = "0.2.0" -source = "git+https://github.com/Trivernis/mediarepo-api.git?rev=ea72553cd5284e785abea815d6d6b9ad262d880a#ea72553cd5284e785abea815d6d6b9ad262d880a" +version = "0.3.0" +source = "git+https://github.com/Trivernis/mediarepo-api.git?rev=6cd483a4a7d0a101f391b755b73fa253e70b4a29#6cd483a4a7d0a101f391b755b73fa253e70b4a29" dependencies = [ "chrono", "serde", diff --git a/mediarepo-daemon/mediarepo-model/src/file.rs b/mediarepo-daemon/mediarepo-model/src/file.rs index 71d9659..6aae077 100644 --- a/mediarepo-daemon/mediarepo-model/src/file.rs +++ b/mediarepo-daemon/mediarepo-model/src/file.rs @@ -308,6 +308,19 @@ impl File { Ok(()) } + /// Removes multiple tags from the file + #[tracing::instrument(level = "debug", skip(self))] + pub async fn remove_tags(&self, tag_ids: Vec) -> RepoResult<()> { + let hash_id = self.hash.id; + hash_tag::Entity::delete_many() + .filter(hash_tag::Column::HashId.eq(hash_id)) + .filter(hash_tag::Column::TagId.is_in(tag_ids)) + .exec(&self.db) + .await?; + + Ok(()) + } + /// Returns the reader for the file #[tracing::instrument(level = "debug", skip(self))] pub async fn get_reader(&self) -> RepoResult> { diff --git a/mediarepo-daemon/mediarepo-socket/Cargo.lock b/mediarepo-daemon/mediarepo-socket/Cargo.lock index a655bca..243e0e7 100644 --- a/mediarepo-daemon/mediarepo-socket/Cargo.lock +++ b/mediarepo-daemon/mediarepo-socket/Cargo.lock @@ -769,8 +769,8 @@ checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" [[package]] name = "mediarepo-api" -version = "0.2.0" -source = "git+https://github.com/Trivernis/mediarepo-api.git?rev=ea72553cd5284e785abea815d6d6b9ad262d880a#ea72553cd5284e785abea815d6d6b9ad262d880a" +version = "0.3.0" +source = "git+https://github.com/Trivernis/mediarepo-api.git?rev=6cd483a4a7d0a101f391b755b73fa253e70b4a29#6cd483a4a7d0a101f391b755b73fa253e70b4a29" dependencies = [ "chrono", "serde", diff --git a/mediarepo-daemon/mediarepo-socket/Cargo.toml b/mediarepo-daemon/mediarepo-socket/Cargo.toml index 75c62c8..10fc4ec 100644 --- a/mediarepo-daemon/mediarepo-socket/Cargo.toml +++ b/mediarepo-daemon/mediarepo-socket/Cargo.toml @@ -33,4 +33,4 @@ features = ["tokio-executor"] [dependencies.mediarepo-api] git = "https://github.com/Trivernis/mediarepo-api.git" -rev = "ea72553cd5284e785abea815d6d6b9ad262d880a" \ No newline at end of file +rev = "6cd483a4a7d0a101f391b755b73fa253e70b4a29" \ No newline at end of file diff --git a/mediarepo-daemon/mediarepo-socket/src/namespaces/tags.rs b/mediarepo-daemon/mediarepo-socket/src/namespaces/tags.rs index c12d181..7609339 100644 --- a/mediarepo-daemon/mediarepo-socket/src/namespaces/tags.rs +++ b/mediarepo-daemon/mediarepo-socket/src/namespaces/tags.rs @@ -1,7 +1,7 @@ use crate::from_model::FromModel; use crate::utils::{file_by_identifier, get_repo_from_context}; use mediarepo_api::types::files::{GetFileTagsRequest, GetFilesTagsRequest}; -use mediarepo_api::types::tags::TagResponse; +use mediarepo_api::types::tags::{ChangeFileTagsRequest, TagResponse}; use mediarepo_core::rmp_ipc::prelude::*; pub struct TagsNamespace; @@ -15,7 +15,9 @@ impl NamespaceProvider for TagsNamespace { events!(handler, "all_tags" => Self::all_tags, "tags_for_file" => Self::tags_for_file, - "tags_for_files" => Self::tags_for_files + "tags_for_files" => Self::tags_for_files, + "create_tags" => Self::create_tags, + "change_file_tags" => Self::change_file_tags ); } } @@ -71,4 +73,48 @@ impl TagsNamespace { Ok(()) } + + /// Creates all tags given as input or returns the existing tag + #[tracing::instrument(skip_all)] + async fn create_tags(ctx: &Context, event: Event) -> IPCResult<()> { + let repo = get_repo_from_context(ctx).await; + let tags = event.data::>()?; + let mut created_tags = Vec::new(); + + for tag in tags { + let created_tag = repo.add_or_find_tag(tag).await?; + created_tags.push(created_tag); + } + let responses: Vec = created_tags + .into_iter() + .map(TagResponse::from_model) + .collect(); + ctx.emitter + .emit_response_to(event.id(), Self::name(), "create_tags", responses) + .await?; + + Ok(()) + } + + /// Changes tags of a file + /// it removes the tags from the removed list and adds the one from the add list + #[tracing::instrument(skip_all)] + async fn change_file_tags(ctx: &Context, event: Event) -> IPCResult<()> { + let repo = get_repo_from_context(ctx).await; + let request = event.data::()?; + let file = file_by_identifier(request.file_id, &repo).await?; + file.add_tags(request.added_tags).await?; + file.remove_tags(request.removed_tags).await?; + let responses: Vec = file + .tags() + .await? + .into_iter() + .map(TagResponse::from_model) + .collect(); + ctx.emitter + .emit_response_to(event.id(), Self::name(), "change_file_tags", responses) + .await?; + + Ok(()) + } }