feat: Support for toggling the monitoring of a given artist via the CLI and TUI

This commit is contained in:
2026-01-06 09:40:16 -07:00
parent 059fa48bd9
commit 5afee1998b
12 changed files with 583 additions and 12 deletions
@@ -3,6 +3,7 @@ mod tests {
use crate::models::lidarr_models::{Artist, DeleteArtistParams, LidarrSerdeable};
use crate::network::lidarr_network::LidarrEvent;
use crate::network::network_tests::test_utils::{MockServarrApi, test_network};
use mockito::Matcher;
use pretty_assertions::assert_eq;
use serde_json::json;
@@ -18,6 +19,7 @@ mod tests {
"qualityProfileId": 1,
"metadataProfileId": 1,
"monitored": true,
"monitorNewItems": "all",
"genres": [],
"tags": [],
"added": "2023-01-01T00:00:00Z"
@@ -66,4 +68,88 @@ mod tests {
async_server.assert_async().await;
}
#[tokio::test]
async fn test_handle_get_artist_details_event() {
let artist_json = json!({
"id": 1,
"mbId": "test-mb-id",
"artistName": "Test Artist",
"foreignArtistId": "test-foreign-id",
"status": "continuing",
"path": "/music/test-artist",
"qualityProfileId": 1,
"metadataProfileId": 1,
"monitored": true,
"monitorNewItems": "all",
"genres": [],
"tags": [],
"added": "2023-01-01T00:00:00Z"
});
let response: Artist = serde_json::from_value(artist_json.clone()).unwrap();
let (mock, app, _server) = MockServarrApi::get()
.returns(artist_json)
.path("/1")
.build_for(LidarrEvent::GetArtistDetails(1))
.await;
app.lock().await.server_tabs.set_index(2);
let mut network = test_network(&app);
let result = network
.handle_lidarr_event(LidarrEvent::GetArtistDetails(1))
.await;
mock.assert_async().await;
let LidarrSerdeable::Artist(artist) = result.unwrap() else {
panic!("Expected Artist");
};
assert_eq!(artist, response);
}
#[tokio::test]
async fn test_handle_toggle_artist_monitoring_event() {
let artist_json = json!({
"id": 1,
"mbId": "test-mb-id",
"artistName": "Test Artist",
"foreignArtistId": "test-foreign-id",
"status": "continuing",
"path": "/music/test-artist",
"qualityProfileId": 1,
"metadataProfileId": 1,
"monitored": true,
"monitorNewItems": "all",
"genres": [],
"tags": [],
"added": "2023-01-01T00:00:00Z"
});
let mut expected_body = artist_json.clone();
*expected_body.get_mut("monitored").unwrap() = json!(false);
let (get_mock, app, mut server) = MockServarrApi::get()
.returns(artist_json)
.path("/1")
.build_for(LidarrEvent::GetArtistDetails(1))
.await;
let put_mock = server
.mock("PUT", "/api/v1/artist/1")
.match_body(Matcher::Json(expected_body))
.match_header("X-Api-Key", "test1234")
.with_status(202)
.create_async()
.await;
app.lock().await.server_tabs.set_index(2);
let mut network = test_network(&app);
assert!(
network
.handle_lidarr_event(LidarrEvent::ToggleArtistMonitoring(1))
.await
.is_ok()
);
get_mock.assert_async().await;
put_mock.assert_async().await;
}
}
+87 -1
View File
@@ -1,5 +1,6 @@
use anyhow::Result;
use log::info;
use log::{debug, info, warn};
use serde_json::{Value, json};
use crate::models::Route;
use crate::models::lidarr_models::{Artist, DeleteArtistParams};
@@ -65,4 +66,89 @@ impl Network<'_, '_> {
})
.await
}
pub(in crate::network::lidarr_network) async fn get_artist_details(
&mut self,
artist_id: i64,
) -> Result<Artist> {
info!("Fetching details for Lidarr artist with ID: {artist_id}");
let event = LidarrEvent::GetArtistDetails(artist_id);
let request_props = self
.request_props_from(
event,
RequestMethod::Get,
None::<()>,
Some(format!("/{artist_id}")),
None,
)
.await;
self
.handle_request::<(), Artist>(request_props, |_, _| ())
.await
}
pub(in crate::network::lidarr_network) async fn toggle_artist_monitoring(
&mut self,
artist_id: i64,
) -> Result<()> {
let event = LidarrEvent::ToggleArtistMonitoring(artist_id);
let detail_event = LidarrEvent::GetArtistDetails(artist_id);
info!("Toggling artist monitoring for artist with ID: {artist_id}");
info!("Fetching artist details for artist with ID: {artist_id}");
let request_props = self
.request_props_from(
detail_event,
RequestMethod::Get,
None::<()>,
Some(format!("/{artist_id}")),
None,
)
.await;
let mut response = String::new();
self
.handle_request::<(), Value>(request_props, |detailed_artist_body, _| {
response = detailed_artist_body.to_string()
})
.await?;
info!("Constructing toggle artist monitoring body");
match serde_json::from_str::<Value>(&response) {
Ok(mut detailed_artist_body) => {
let monitored = detailed_artist_body
.get("monitored")
.unwrap()
.as_bool()
.unwrap();
*detailed_artist_body.get_mut("monitored").unwrap() = json!(!monitored);
debug!("Toggle artist monitoring body: {detailed_artist_body:?}");
let request_props = self
.request_props_from(
event,
RequestMethod::Put,
Some(detailed_artist_body),
Some(format!("/{artist_id}")),
None,
)
.await;
self
.handle_request::<Value, ()>(request_props, |_, _| ())
.await
}
Err(_) => {
warn!("Request for detailed artist body was interrupted");
Ok(())
}
}
}
}