feat(readarr): add authors to Readarr and expose the command on the CLI

This commit is contained in:
Dark-Alex-17 committed 2026-08-26 17:17:41 -06:00
1 parent 9238c1b9d2
commit e514e9b6cc
8 files changed
+644 -9

No files matched your search

@@ -1,8 +1,9 @@
use anyhow::Result;
use log::info;
use serde_json::Value;
use crate::models::Route;
use crate::models::readarr_models::{AddAuthorSearchResult, Author};
use crate::models::readarr_models::{AddAuthorBody, AddAuthorSearchResult, Author};
use crate::models::servarr_data::readarr::readarr_data::ActiveReadarrBlock;
use crate::models::stateful_table::StatefulTable;
use crate::network::readarr_network::ReadarrEvent;
@@ -14,6 +15,34 @@ use urlencoding::encode;
mod readarr_authors_network_tests;
impl Network<'_, '_> {
pub(in crate::network::readarr_network) async fn add_author(
&mut self,
mut add_author_body: AddAuthorBody,
) -> Result<Value> {
info!("Adding Readarr author: {}", add_author_body.author_name);
if let Some(tag_input_str) = add_author_body.tag_input_string.as_ref() {
let tag_ids_vec = self
.extract_and_add_readarr_tag_ids_vec(tag_input_str)
.await;
add_author_body.tags = tag_ids_vec;
}
let event = ReadarrEvent::AddAuthor(AddAuthorBody::default());
let request_props = self
.request_props_from(
event,
RequestMethod::Post,
Some(add_author_body),
None,
None,
)
.await;
self
.handle_request::<AddAuthorBody, Value>(request_props, |_, _| ())
.await
}
pub(in crate::network::readarr_network) async fn get_author_details(
&mut self,
author_id: i64,
@@ -1,13 +1,17 @@
#[cfg(test)]
mod tests {
use crate::models::readarr_models::{AddAuthorSearchResult, Author, ReadarrSerdeable};
use crate::models::readarr_models::{
AddAuthorBody, AddAuthorSearchResult, Author, ReadarrSerdeable,
};
use crate::models::servarr_data::readarr::readarr_data::ActiveReadarrBlock;
use crate::models::stateful_table::StatefulTable;
use crate::network::network_tests::test_utils::{MockServarrApi, test_network};
use crate::network::readarr_network::ReadarrEvent;
use crate::network::readarr_network::readarr_network_test_utils::test_utils::{
ADD_AUTHOR_SEARCH_RESULT_JSON, AUTHOR_JSON, stale_add_author_search_result, stale_author,
ADD_AUTHOR_SEARCH_RESULT_JSON, AUTHOR_JSON, add_author_body, stale_add_author_search_result,
stale_author,
};
use bimap::BiMap;
use pretty_assertions::assert_eq;
use serde_json::{Value, json};
@@ -288,4 +292,99 @@ mod tests {
assert_some!(&app.data.readarr_data.add_searched_authors);
assert_is_empty!(app.data.readarr_data.add_searched_authors.as_ref().unwrap());
}
#[tokio::test]
async fn test_handle_add_author_event() {
let (mock, app, _server) = MockServarrApi::post()
.with_request_body(json!({
"foreignAuthorId": "test-foreign-id",
"authorName": "Test Author",
"monitored": true,
"rootFolderPath": "/nfs/books",
"qualityProfileId": 1,
"metadataProfileId": 1,
"tags": [1, 2],
"addOptions": {
"monitor": "all",
"monitorNewItems": "all",
"searchForMissingBooks": true
}
}))
.returns(json!({"id": 1}))
.build_for(ReadarrEvent::AddAuthor(AddAuthorBody::default()))
.await;
{
let mut app = app.lock().await;
app.server_tabs.set_index(3);
app.data.readarr_data.tags_map =
BiMap::from_iter([(1, "usenet".to_owned()), (2, "testing".to_owned())]);
}
let mut network = test_network(&app);
let result = network
.handle_readarr_event(ReadarrEvent::AddAuthor(add_author_body()))
.await;
mock.assert_async().await;
assert_ok!(result);
}
#[tokio::test]
async fn test_handle_add_author_event_does_not_overwrite_tags_vec_when_tag_input_string_is_none()
{
let add_author_body = AddAuthorBody {
tags: vec![1, 2],
tag_input_string: None,
..add_author_body()
};
let (mock, app, _server) = MockServarrApi::post()
.with_request_body(json!({
"foreignAuthorId": "test-foreign-id",
"authorName": "Test Author",
"monitored": true,
"rootFolderPath": "/nfs/books",
"qualityProfileId": 1,
"metadataProfileId": 1,
"tags": [1, 2],
"addOptions": {
"monitor": "all",
"monitorNewItems": "all",
"searchForMissingBooks": true
}
}))
.returns(json!({"id": 1}))
.build_for(ReadarrEvent::AddAuthor(add_author_body.clone()))
.await;
app.lock().await.server_tabs.set_index(3);
let mut network = test_network(&app);
let result = network
.handle_readarr_event(ReadarrEvent::AddAuthor(add_author_body))
.await;
mock.assert_async().await;
assert_ok!(result);
}
#[tokio::test]
async fn test_handle_add_author_event_failure() {
let add_author_body = AddAuthorBody {
tag_input_string: None,
..add_author_body()
};
let (mock, app, _server) = MockServarrApi::post()
.returns(json!({"id": 1}))
.status(400)
.build_for(ReadarrEvent::AddAuthor(add_author_body.clone()))
.await;
app.lock().await.server_tabs.set_index(3);
let mut network = test_network(&app);
let result = network
.handle_readarr_event(ReadarrEvent::AddAuthor(add_author_body))
.await;
mock.assert_async().await;
assert_err!(result);
}
}
+8 -2
View File
@@ -3,7 +3,9 @@ use log::info;
use serde_json::{Value, json};
use super::{NetworkEvent, NetworkResource};
use crate::models::readarr_models::{AddReadarrRootFolderBody, ReadarrSerdeable, ReadarrTaskName};
use crate::models::readarr_models::{
AddAuthorBody, AddReadarrRootFolderBody, ReadarrSerdeable, ReadarrTaskName,
};
use crate::models::servarr_models::{MetadataProfile, QualityProfile, Tag};
use crate::network::{Network, RequestMethod};
@@ -21,6 +23,7 @@ pub mod readarr_network_test_utils;
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum ReadarrEvent {
AddAuthor(AddAuthorBody),
AddRootFolder(AddReadarrRootFolderBody),
AddTag(String),
DeleteRootFolder(i64),
@@ -47,7 +50,9 @@ pub enum ReadarrEvent {
impl NetworkResource for ReadarrEvent {
fn resource(&self) -> &'static str {
match &self {
ReadarrEvent::GetAuthorDetails(_) | ReadarrEvent::ListAuthors => "/author",
ReadarrEvent::AddAuthor(_)
| ReadarrEvent::GetAuthorDetails(_)
| ReadarrEvent::ListAuthors => "/author",
ReadarrEvent::SearchNewAuthor(_) => "/author/lookup",
ReadarrEvent::GetQueuedEvents | ReadarrEvent::StartTask(_) => "/command",
ReadarrEvent::GetHostConfig | ReadarrEvent::GetSecurityConfig => "/config/host",
@@ -79,6 +84,7 @@ impl Network<'_, '_> {
readarr_event: ReadarrEvent,
) -> Result<ReadarrSerdeable> {
match readarr_event {
ReadarrEvent::AddAuthor(body) => self.add_author(body).await.map(ReadarrSerdeable::from),
ReadarrEvent::AddRootFolder(add_root_folder_body) => self
.add_readarr_root_folder(add_root_folder_body)
.await
@@ -2,7 +2,8 @@
#[allow(dead_code)]
pub mod test_utils {
use crate::models::readarr_models::{
AddAuthorSearchResult, Author, AuthorStatus, NewItemMonitorType,
AddAuthorBody, AddAuthorOptions, AddAuthorSearchResult, Author, AuthorStatus, MonitorType,
NewItemMonitorType,
};
use crate::models::servarr_models::RootFolder;
@@ -81,4 +82,22 @@ pub mod test_utils {
unmapped_folders: None,
}
}
pub fn add_author_body() -> AddAuthorBody {
AddAuthorBody {
foreign_author_id: "test-foreign-id".to_owned(),
author_name: "Test Author".to_owned(),
monitored: true,
root_folder_path: "/nfs/books".to_owned(),
quality_profile_id: 1,
metadata_profile_id: 1,
tags: Vec::default(),
tag_input_string: Some("usenet, testing".to_owned()),
add_options: AddAuthorOptions {
monitor: MonitorType::All,
monitor_new_items: NewItemMonitorType::All,
search_for_missing_books: true,
},
}
}
}
@@ -1,7 +1,7 @@
#[cfg(test)]
mod tests {
use crate::app::App;
use crate::models::readarr_models::{AddReadarrRootFolderBody, ReadarrSerdeable};
use crate::models::readarr_models::{AddAuthorBody, AddReadarrRootFolderBody, ReadarrSerdeable};
use crate::models::servarr_models::{MetadataProfile, QualityProfile, Tag};
use crate::network::network_tests::test_utils::{MockServarrApi, test_network};
use crate::network::{NetworkEvent, NetworkResource, readarr_network::ReadarrEvent};
@@ -13,6 +13,7 @@ mod tests {
use tokio::sync::Mutex;
#[rstest]
#[case(ReadarrEvent::AddAuthor(AddAuthorBody::default()), "/author")]
#[case(ReadarrEvent::GetAuthorDetails(1), "/author")]
#[case(ReadarrEvent::ListAuthors, "/author")]
#[case(ReadarrEvent::SearchNewAuthor(String::new()), "/author/lookup")]