Patch issue with service keys

develop
trivernis 11 months ago
parent 2dec625c07
commit c59a6b62fa
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG Key ID: 7E6D18B61C8D2F4B

@ -21,9 +21,7 @@ impl Endpoint for CleanTags {
#[derive(Debug, Clone, Serialize)]
pub struct AddTagsRequest {
pub hashes: Vec<String>,
pub service_names_to_tags: HashMap<String, Vec<String>>,
pub service_keys_to_tags: HashMap<String, Vec<String>>,
pub service_names_to_actions_to_tags: HashMap<String, HashMap<String, Vec<String>>>,
pub service_keys_to_actions_to_tags: HashMap<String, HashMap<String, Vec<String>>>,
}
@ -41,9 +39,7 @@ impl Endpoint for AddTags {
#[derive(Default)]
pub struct AddTagsRequestBuilder {
hashes: Vec<String>,
service_names_to_tags: HashMap<String, Vec<String>>,
service_keys_to_tags: HashMap<String, Vec<String>>,
service_names_to_actions_to_tags: HashMap<String, HashMap<String, Vec<String>>>,
service_keys_to_actions_to_tags: HashMap<String, HashMap<String, Vec<String>>>,
}
@ -100,11 +96,8 @@ impl AddTagsRequestBuilder {
}
/// Adds a single tag for a given service
pub fn add_tag<S: AsRef<str>>(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<S: AsRef<str>>(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<String>) -> 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<String>) -> 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<S: AsRef<str>>(
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,
}
}

@ -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(),
);

@ -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<Tag>) -> Result<()> {
pub async fn add_tags(&mut self, service_key: String, tags: Vec<Tag>) -> 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<Tag>,
) -> 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

@ -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,
)

@ -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()],
)

Loading…
Cancel
Save