From c59a6b62faaa24c5dc16fec6ecb77479ddf1f788 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 21 Dec 2023 11:26:56 +0100 Subject: [PATCH] Patch issue with service keys --- src/api_core/endpoints/adding_tags.rs | 27 ++++++------------------- src/wrapper/builders/tagging_builder.rs | 19 +++++++++++++++-- src/wrapper/hydrus_file.rs | 9 +++++---- tests/client/test_adding_tags.rs | 4 ++-- tests/wrapper/test_files.rs | 4 ++-- 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/api_core/endpoints/adding_tags.rs b/src/api_core/endpoints/adding_tags.rs index e7c7507..833cd1d 100644 --- a/src/api_core/endpoints/adding_tags.rs +++ b/src/api_core/endpoints/adding_tags.rs @@ -21,9 +21,7 @@ impl Endpoint for CleanTags { #[derive(Debug, Clone, Serialize)] pub struct AddTagsRequest { pub hashes: Vec, - pub service_names_to_tags: HashMap>, pub service_keys_to_tags: HashMap>, - pub service_names_to_actions_to_tags: HashMap>>, pub service_keys_to_actions_to_tags: HashMap>>, } @@ -41,9 +39,7 @@ impl Endpoint for AddTags { #[derive(Default)] pub struct AddTagsRequestBuilder { hashes: Vec, - service_names_to_tags: HashMap>, service_keys_to_tags: HashMap>, - service_names_to_actions_to_tags: HashMap>>, service_keys_to_actions_to_tags: HashMap>>, } @@ -100,11 +96,8 @@ impl AddTagsRequestBuilder { } /// Adds a single tag for a given service - pub fn add_tag>(mut self, service_id: ServiceIdentifier, tag: S) -> Self { - let (service, relevant_mappings) = match service_id { - ServiceIdentifier::Name(name) => (name, &mut self.service_names_to_tags), - ServiceIdentifier::Key(key) => (key, &mut self.service_keys_to_tags), - }; + pub fn add_tag>(mut self, service_key: String, tag: S) -> Self { + let (service, relevant_mappings) = (service_key, &mut self.service_keys_to_tags); if let Some(mappings) = relevant_mappings.get_mut(&service) { mappings.push(tag.as_ref().into()) } else { @@ -115,11 +108,8 @@ impl AddTagsRequestBuilder { } /// Adds multiple tags for a given service - pub fn add_tags(mut self, service_id: ServiceIdentifier, mut tags: Vec) -> Self { - let (service, relevant_mappings) = match service_id { - ServiceIdentifier::Name(name) => (name, &mut self.service_names_to_tags), - ServiceIdentifier::Key(key) => (key, &mut self.service_keys_to_tags), - }; + pub fn add_tags(mut self, service_key: String, mut tags: Vec) -> Self { + let (service, relevant_mappings) = (service_key, &mut self.service_keys_to_tags); if let Some(mappings) = relevant_mappings.get_mut(&service) { mappings.append(&mut tags); } else { @@ -132,14 +122,11 @@ impl AddTagsRequestBuilder { /// Adds one tag for a given service with a defined action pub fn add_tag_with_action>( mut self, - service_id: ServiceIdentifier, + service_key: String, tag: S, action: TagAction, ) -> Self { - let (service, relevant_mappings) = match service_id { - ServiceIdentifier::Name(name) => (name, &mut self.service_names_to_actions_to_tags), - ServiceIdentifier::Key(key) => (key, &mut self.service_keys_to_actions_to_tags), - }; + let (service, relevant_mappings) = (service_key, &mut self.service_keys_to_actions_to_tags); let action_id = action.into_id(); if let Some(actions) = relevant_mappings.get_mut(&service) { if let Some(tags) = actions.get_mut(&action_id.to_string()) { @@ -159,9 +146,7 @@ impl AddTagsRequestBuilder { pub fn build(self) -> AddTagsRequest { AddTagsRequest { hashes: self.hashes, - service_names_to_tags: self.service_names_to_tags, service_keys_to_tags: self.service_keys_to_tags, - service_names_to_actions_to_tags: self.service_names_to_actions_to_tags, service_keys_to_actions_to_tags: self.service_keys_to_actions_to_tags, } } diff --git a/src/wrapper/builders/tagging_builder.rs b/src/wrapper/builders/tagging_builder.rs index ad0c748..f6ce0b7 100644 --- a/src/wrapper/builders/tagging_builder.rs +++ b/src/wrapper/builders/tagging_builder.rs @@ -1,6 +1,6 @@ use crate::api_core::common::ServiceIdentifier; use crate::api_core::endpoints::adding_tags::{AddTagsRequestBuilder, TagAction}; -use crate::error::Result; +use crate::error::{Error, Result}; use crate::wrapper::tag::Tag; use crate::Client; use std::collections::HashMap; @@ -59,10 +59,25 @@ impl TaggingBuilder { pub async fn run(self) -> Result<()> { let mut request = AddTagsRequestBuilder::default().add_hashes(self.hashes); for (service, action_tag_mappings) in self.tag_mappings { + let service_key = match service { + ServiceIdentifier::Name(n) => self + .client + .get_services() + .await? + .other + .values() + .flatten() + .filter(|v| *v.name == n) + .next() + .ok_or_else(|| Error::Hydrus(String::from("Service not found")))? + .service_key + .clone(), + ServiceIdentifier::Key(k) => k, + }; for (action, tags) in action_tag_mappings { for tag in tags { request = request.add_tag_with_action( - service.clone().into(), + service_key.clone(), tag.to_string(), action.clone(), ); diff --git a/src/wrapper/hydrus_file.rs b/src/wrapper/hydrus_file.rs index 5222c70..399df0a 100644 --- a/src/wrapper/hydrus_file.rs +++ b/src/wrapper/hydrus_file.rs @@ -337,11 +337,11 @@ impl HydrusFile { } /// Adds tags for a specific service to the file - pub async fn add_tags(&mut self, service: ServiceIdentifier, tags: Vec) -> Result<()> { + pub async fn add_tags(&mut self, service_key: String, tags: Vec) -> Result<()> { let hash = self.hash().await?; let request = AddTagsRequestBuilder::default() .add_hash(hash) - .add_tags(service, tag_list_to_string_list(tags)) + .add_tags(service_key, tag_list_to_string_list(tags)) .build(); self.client.add_tags(request).await @@ -350,7 +350,7 @@ impl HydrusFile { /// Allows modification of tags by using the defined tag actions pub async fn modify_tags( &mut self, - service: ServiceIdentifier, + service_key: String, action: TagAction, tags: Vec, ) -> Result<()> { @@ -358,7 +358,8 @@ impl HydrusFile { let mut reqwest = AddTagsRequestBuilder::default().add_hash(hash); for tag in tags { - reqwest = reqwest.add_tag_with_action(service.clone(), tag.to_string(), action.clone()); + reqwest = + reqwest.add_tag_with_action(service_key.clone(), tag.to_string(), action.clone()); } self.client.add_tags(reqwest.build()).await diff --git a/tests/client/test_adding_tags.rs b/tests/client/test_adding_tags.rs index 409df96..e696dd9 100644 --- a/tests/client/test_adding_tags.rs +++ b/tests/client/test_adding_tags.rs @@ -27,11 +27,11 @@ async fn it_adds_tags() { let request = AddTagsRequestBuilder::default() .add_hash(EMPTY_HASH) // valid hash, I hope no files are affected .add_tags( - ServiceIdentifier::name("my tags"), + "6c6f63616c2074616773".into(), vec!["beach".into(), "summer".into()], ) .add_tag_with_action( - ServiceIdentifier::name("my tags"), + "6c6f63616c2074616773".into(), "rain", TagAction::DeleteFromLocalService, ) diff --git a/tests/wrapper/test_files.rs b/tests/wrapper/test_files.rs index a34a354..ea766cc 100644 --- a/tests/wrapper/test_files.rs +++ b/tests/wrapper/test_files.rs @@ -58,7 +58,7 @@ async fn it_has_tags() { async fn it_adds_tags() { let mut file = get_file().await; file.add_tags( - ServiceName::my_tags().into(), + "6c6f63616c2074616773".into(), vec!["character:megumin".into(), "ark mage".into()], ) .await @@ -69,7 +69,7 @@ async fn it_adds_tags() { async fn it_modifies_tags() { let mut file = get_file().await; file.modify_tags( - ServiceName::my_tags().into(), + "6c6f63616c2074616773".into(), TagAction::DeleteFromLocalService, vec!["ark mage".into()], )