feat(readarr): fetch Readarr author history and expose it on the CLI
Add the GetAuthorHistory event, backed by GET /history/author?authorId=N, which returns a flat array of history records for a single author. The fetcher mirrors the artist-history equivalent, sorting by id and carrying the sort-prompt route guard so a refresh landing while the sort prompt is open cannot clobber an in-progress sort selection. Expose it as `readarr list author-history --author-id N`.
This commit is contained in:
7 files changed
+402
-6
No files matched your search
@@ -5,6 +5,7 @@ use serde_json::{Value, json};
|
||||
use crate::models::Route;
|
||||
use crate::models::readarr_models::{
|
||||
AddAuthorBody, AddAuthorSearchResult, Author, DeleteParams, EditAuthorParams, ReadarrCommandBody,
|
||||
ReadarrHistoryItem,
|
||||
};
|
||||
use crate::models::servarr_data::readarr::readarr_data::ActiveReadarrBlock;
|
||||
use crate::models::stateful_table::StatefulTable;
|
||||
@@ -216,6 +217,41 @@ impl Network<'_, '_> {
|
||||
.await
|
||||
}
|
||||
|
||||
pub(in crate::network::readarr_network) async fn get_readarr_author_history(
|
||||
&mut self,
|
||||
author_id: i64,
|
||||
) -> Result<Vec<ReadarrHistoryItem>> {
|
||||
info!("Fetching Readarr author history for author with ID: {author_id}");
|
||||
let event = ReadarrEvent::GetAuthorHistory(author_id);
|
||||
|
||||
let request_props = self
|
||||
.request_props_from(
|
||||
event,
|
||||
RequestMethod::Get,
|
||||
None::<()>,
|
||||
None,
|
||||
Some(format!("authorId={author_id}")),
|
||||
)
|
||||
.await;
|
||||
|
||||
self
|
||||
.handle_request::<(), Vec<ReadarrHistoryItem>>(request_props, |mut history_vec, mut app| {
|
||||
let is_sorting = matches!(
|
||||
app.get_current_route(),
|
||||
Route::Readarr(ActiveReadarrBlock::AuthorHistorySortPrompt, _)
|
||||
);
|
||||
|
||||
let author_history = &mut app.data.readarr_data.author_history;
|
||||
|
||||
if !is_sorting {
|
||||
history_vec.sort_by_key(|a| a.id);
|
||||
author_history.set_items(history_vec);
|
||||
author_history.apply_sorting_toggle(false);
|
||||
}
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub(in crate::network::readarr_network) async fn list_authors(&mut self) -> Result<Vec<Author>> {
|
||||
info!("Fetching Readarr authors");
|
||||
let event = ReadarrEvent::ListAuthors;
|
||||
|
||||
@@ -2,16 +2,16 @@
|
||||
mod tests {
|
||||
use crate::models::readarr_models::{
|
||||
AddAuthorBody, AddAuthorSearchResult, Author, DeleteParams, EditAuthorParams,
|
||||
NewItemMonitorType, ReadarrSerdeable,
|
||||
NewItemMonitorType, ReadarrHistoryItem, ReadarrSerdeable,
|
||||
};
|
||||
use crate::models::servarr_data::readarr::readarr_data::ActiveReadarrBlock;
|
||||
use crate::models::stateful_table::StatefulTable;
|
||||
use crate::models::stateful_table::{SortOption, StatefulTable};
|
||||
use crate::network::NetworkResource;
|
||||
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, add_author_body, stale_add_author_search_result,
|
||||
stale_author,
|
||||
ADD_AUTHOR_SEARCH_RESULT_JSON, AUTHOR_JSON, add_author_body, readarr_history_item,
|
||||
stale_add_author_search_result, stale_author, stale_readarr_history_item,
|
||||
};
|
||||
use bimap::BiMap;
|
||||
use mockito::Matcher;
|
||||
@@ -76,6 +76,244 @@ mod tests {
|
||||
assert_err!(result);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_handle_get_readarr_author_history_event(
|
||||
#[values(true, false)] use_custom_sorting: bool,
|
||||
) {
|
||||
let history_json = json!([{
|
||||
"id": 456,
|
||||
"authorId": 2001,
|
||||
"bookId": 2001,
|
||||
"sourceTitle": "An Anthology",
|
||||
"quality": { "quality": { "name": "AZW3" } },
|
||||
"date": "2023-01-01T00:00:00Z",
|
||||
"eventType": "grabbed",
|
||||
"data": {
|
||||
"droppedPath": "/nfs/nzbget/completed/books/Something/cool.azw3",
|
||||
"importedPath": "/nfs/books/Test Author/Book 1/Cool.azw3"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 123,
|
||||
"authorId": 1007,
|
||||
"bookId": 1007,
|
||||
"sourceTitle": "z book",
|
||||
"quality": { "quality": { "name": "AZW3" } },
|
||||
"date": "2023-01-01T00:00:00Z",
|
||||
"eventType": "grabbed",
|
||||
"data": {
|
||||
"droppedPath": "/nfs/nzbget/completed/books/Something/cool.azw3",
|
||||
"importedPath": "/nfs/books/Test Author/Book 1/Cool.azw3"
|
||||
}
|
||||
}]);
|
||||
let response: Vec<ReadarrHistoryItem> = serde_json::from_value(history_json.clone()).unwrap();
|
||||
let mut expected_history_items = vec![
|
||||
ReadarrHistoryItem {
|
||||
id: 123,
|
||||
author_id: 1007,
|
||||
book_id: 1007,
|
||||
source_title: "z book".into(),
|
||||
..readarr_history_item()
|
||||
},
|
||||
ReadarrHistoryItem {
|
||||
id: 456,
|
||||
author_id: 2001,
|
||||
book_id: 2001,
|
||||
source_title: "An Anthology".into(),
|
||||
..readarr_history_item()
|
||||
},
|
||||
];
|
||||
let (mock, app, _server) = MockServarrApi::get()
|
||||
.returns(history_json)
|
||||
.query("authorId=1")
|
||||
.build_for(ReadarrEvent::GetAuthorHistory(1))
|
||||
.await;
|
||||
if use_custom_sorting {
|
||||
let cmp_fn = |a: &ReadarrHistoryItem, b: &ReadarrHistoryItem| {
|
||||
a.source_title
|
||||
.text
|
||||
.to_lowercase()
|
||||
.cmp(&b.source_title.text.to_lowercase())
|
||||
};
|
||||
expected_history_items.sort_by(cmp_fn);
|
||||
|
||||
let history_sort_option = SortOption {
|
||||
name: "Source Title",
|
||||
cmp_fn: Some(cmp_fn),
|
||||
};
|
||||
app
|
||||
.lock()
|
||||
.await
|
||||
.data
|
||||
.readarr_data
|
||||
.author_history
|
||||
.sorting(vec![history_sort_option]);
|
||||
}
|
||||
{
|
||||
let mut app = app.lock().await;
|
||||
app.server_tabs.set_index(3);
|
||||
app.data.readarr_data.author_history.sort_asc = true;
|
||||
}
|
||||
let mut network = test_network(&app);
|
||||
|
||||
let result = network
|
||||
.handle_readarr_event(ReadarrEvent::GetAuthorHistory(1))
|
||||
.await;
|
||||
|
||||
mock.assert_async().await;
|
||||
|
||||
let ReadarrSerdeable::ReadarrHistoryItems(history_items) = result.unwrap() else {
|
||||
panic!("Expected ReadarrHistoryItems")
|
||||
};
|
||||
|
||||
assert_eq!(history_items, response);
|
||||
let app = app.lock().await;
|
||||
assert_eq!(
|
||||
app.data.readarr_data.author_history.items,
|
||||
expected_history_items
|
||||
);
|
||||
assert!(app.data.readarr_data.author_history.sort_asc);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_handle_get_readarr_author_history_event_no_op_when_user_is_selecting_sort_options()
|
||||
{
|
||||
let history_json = json!([{
|
||||
"id": 456,
|
||||
"authorId": 2001,
|
||||
"bookId": 2001,
|
||||
"sourceTitle": "An Anthology",
|
||||
"quality": { "quality": { "name": "AZW3" } },
|
||||
"date": "2023-01-01T00:00:00Z",
|
||||
"eventType": "grabbed",
|
||||
"data": {
|
||||
"droppedPath": "/nfs/nzbget/completed/books/Something/cool.azw3",
|
||||
"importedPath": "/nfs/books/Test Author/Book 1/Cool.azw3"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 123,
|
||||
"authorId": 1007,
|
||||
"bookId": 1007,
|
||||
"sourceTitle": "z book",
|
||||
"quality": { "quality": { "name": "AZW3" } },
|
||||
"date": "2023-01-01T00:00:00Z",
|
||||
"eventType": "grabbed",
|
||||
"data": {
|
||||
"droppedPath": "/nfs/nzbget/completed/books/Something/cool.azw3",
|
||||
"importedPath": "/nfs/books/Test Author/Book 1/Cool.azw3"
|
||||
}
|
||||
}]);
|
||||
let response: Vec<ReadarrHistoryItem> = serde_json::from_value(history_json.clone()).unwrap();
|
||||
let (mock, app, _server) = MockServarrApi::get()
|
||||
.returns(history_json)
|
||||
.query("authorId=1")
|
||||
.build_for(ReadarrEvent::GetAuthorHistory(1))
|
||||
.await;
|
||||
{
|
||||
let mut app = app.lock().await;
|
||||
app.server_tabs.set_index(3);
|
||||
app.data.readarr_data.author_history.sort_asc = true;
|
||||
app.push_navigation_stack(ActiveReadarrBlock::AuthorHistorySortPrompt.into());
|
||||
}
|
||||
let mut network = test_network(&app);
|
||||
|
||||
let result = network
|
||||
.handle_readarr_event(ReadarrEvent::GetAuthorHistory(1))
|
||||
.await;
|
||||
|
||||
mock.assert_async().await;
|
||||
|
||||
let ReadarrSerdeable::ReadarrHistoryItems(history_items) = result.unwrap() else {
|
||||
panic!("Expected ReadarrHistoryItems")
|
||||
};
|
||||
|
||||
assert_eq!(history_items, response);
|
||||
let app = app.lock().await;
|
||||
assert_is_empty!(app.data.readarr_data.author_history);
|
||||
assert!(app.data.readarr_data.author_history.sort_asc);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_handle_get_readarr_author_history_event_empty_response() {
|
||||
let stale_history_items = vec![stale_readarr_history_item()];
|
||||
let (mock, app, _server) = MockServarrApi::get()
|
||||
.returns(json!([]))
|
||||
.query("authorId=1")
|
||||
.build_for(ReadarrEvent::GetAuthorHistory(1))
|
||||
.await;
|
||||
{
|
||||
let mut app = app.lock().await;
|
||||
app.server_tabs.set_index(3);
|
||||
app
|
||||
.data
|
||||
.readarr_data
|
||||
.author_history
|
||||
.set_items(stale_history_items.clone());
|
||||
}
|
||||
let mut network = test_network(&app);
|
||||
|
||||
let result = network
|
||||
.handle_readarr_event(ReadarrEvent::GetAuthorHistory(1))
|
||||
.await;
|
||||
|
||||
mock.assert_async().await;
|
||||
|
||||
let ReadarrSerdeable::ReadarrHistoryItems(history_items) = result.unwrap() else {
|
||||
panic!("Expected ReadarrHistoryItems")
|
||||
};
|
||||
|
||||
assert_is_empty!(history_items);
|
||||
assert_is_empty!(app.lock().await.data.readarr_data.author_history);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_handle_get_readarr_author_history_event_failure() {
|
||||
let history_json = json!([{
|
||||
"id": 123,
|
||||
"authorId": 1007,
|
||||
"bookId": 1007,
|
||||
"sourceTitle": "z book",
|
||||
"quality": { "quality": { "name": "AZW3" } },
|
||||
"date": "2023-01-01T00:00:00Z",
|
||||
"eventType": "grabbed",
|
||||
"data": {
|
||||
"droppedPath": "/nfs/nzbget/completed/books/Something/cool.azw3",
|
||||
"importedPath": "/nfs/books/Test Author/Book 1/Cool.azw3"
|
||||
}
|
||||
}]);
|
||||
let stale_history_items = vec![stale_readarr_history_item()];
|
||||
let (mock, app, _server) = MockServarrApi::get()
|
||||
.returns(history_json)
|
||||
.status(500)
|
||||
.query("authorId=1")
|
||||
.build_for(ReadarrEvent::GetAuthorHistory(1))
|
||||
.await;
|
||||
{
|
||||
let mut app = app.lock().await;
|
||||
app.server_tabs.set_index(3);
|
||||
app
|
||||
.data
|
||||
.readarr_data
|
||||
.author_history
|
||||
.set_items(stale_history_items.clone());
|
||||
}
|
||||
let mut network = test_network(&app);
|
||||
|
||||
let result = network
|
||||
.handle_readarr_event(ReadarrEvent::GetAuthorHistory(1))
|
||||
.await;
|
||||
|
||||
mock.assert_async().await;
|
||||
|
||||
assert_err!(result);
|
||||
assert_eq!(
|
||||
app.lock().await.data.readarr_data.author_history.items,
|
||||
stale_history_items
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_handle_list_authors_event() {
|
||||
let authors_json = json!([
|
||||
|
||||
@@ -32,6 +32,7 @@ pub enum ReadarrEvent {
|
||||
DeleteTag(i64),
|
||||
EditAuthor(EditAuthorParams),
|
||||
GetAuthorDetails(i64),
|
||||
GetAuthorHistory(i64),
|
||||
GetDiskSpace,
|
||||
GetHostConfig,
|
||||
GetLogs(u64),
|
||||
@@ -72,6 +73,7 @@ impl NetworkResource for ReadarrEvent {
|
||||
ReadarrEvent::GetHostConfig | ReadarrEvent::GetSecurityConfig => "/config/host",
|
||||
ReadarrEvent::GetDiskSpace => "/diskspace",
|
||||
ReadarrEvent::HealthCheck => "/health",
|
||||
ReadarrEvent::GetAuthorHistory(_) => "/history/author",
|
||||
ReadarrEvent::GetLogs(_) => "/log",
|
||||
ReadarrEvent::GetMetadataProfiles => "/metadataprofile",
|
||||
ReadarrEvent::GetQualityProfiles => "/qualityprofile",
|
||||
@@ -124,6 +126,10 @@ impl Network<'_, '_> {
|
||||
.get_author_details(author_id)
|
||||
.await
|
||||
.map(ReadarrSerdeable::from),
|
||||
ReadarrEvent::GetAuthorHistory(author_id) => self
|
||||
.get_readarr_author_history(author_id)
|
||||
.await
|
||||
.map(ReadarrSerdeable::from),
|
||||
ReadarrEvent::GetDiskSpace => self
|
||||
.get_readarr_diskspace()
|
||||
.await
|
||||
|
||||
@@ -3,9 +3,10 @@
|
||||
pub mod test_utils {
|
||||
use crate::models::readarr_models::{
|
||||
AddAuthorBody, AddAuthorOptions, AddAuthorSearchResult, Author, AuthorStatus, MonitorType,
|
||||
NewItemMonitorType,
|
||||
NewItemMonitorType, ReadarrHistoryData, ReadarrHistoryEventType, ReadarrHistoryItem,
|
||||
};
|
||||
use crate::models::servarr_models::RootFolder;
|
||||
use crate::models::servarr_models::{Quality, QualityWrapper, RootFolder};
|
||||
use chrono::DateTime;
|
||||
|
||||
pub const AUTHOR_JSON: &str = r#"{
|
||||
"id": 1,
|
||||
@@ -83,6 +84,45 @@ pub mod test_utils {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stale_readarr_history_item() -> ReadarrHistoryItem {
|
||||
ReadarrHistoryItem {
|
||||
id: 99,
|
||||
source_title: "Stale source title".into(),
|
||||
..ReadarrHistoryItem::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn quality_wrapper() -> QualityWrapper {
|
||||
QualityWrapper { quality: quality() }
|
||||
}
|
||||
|
||||
pub fn quality() -> Quality {
|
||||
Quality {
|
||||
name: "AZW3".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn readarr_history_item() -> ReadarrHistoryItem {
|
||||
ReadarrHistoryItem {
|
||||
id: 1,
|
||||
author_id: 1,
|
||||
book_id: 1,
|
||||
source_title: "Test source title".into(),
|
||||
quality: quality_wrapper(),
|
||||
date: DateTime::from(DateTime::parse_from_rfc3339("2023-01-01T00:00:00Z").unwrap()),
|
||||
event_type: ReadarrHistoryEventType::Grabbed,
|
||||
data: readarr_history_data(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn readarr_history_data() -> ReadarrHistoryData {
|
||||
ReadarrHistoryData {
|
||||
dropped_path: Some("/nfs/nzbget/completed/books/Something/cool.azw3".to_owned()),
|
||||
imported_path: Some("/nfs/books/Test Author/Book 1/Cool.azw3".to_owned()),
|
||||
..ReadarrHistoryData::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_author_body() -> AddAuthorBody {
|
||||
AddAuthorBody {
|
||||
foreign_author_id: "test-foreign-id".to_owned(),
|
||||
|
||||
@@ -31,6 +31,7 @@ mod tests {
|
||||
#[case(ReadarrEvent::GetSecurityConfig, "/config/host")]
|
||||
#[case(ReadarrEvent::GetDiskSpace, "/diskspace")]
|
||||
#[case(ReadarrEvent::HealthCheck, "/health")]
|
||||
#[case(ReadarrEvent::GetAuthorHistory(1), "/history/author")]
|
||||
#[case(ReadarrEvent::GetLogs(500), "/log")]
|
||||
#[case(ReadarrEvent::GetMetadataProfiles, "/metadataprofile")]
|
||||
#[case(ReadarrEvent::GetQualityProfiles, "/qualityprofile")]
|
||||
|
||||
Reference in new issue
Block a user