Build 2502262321: fix auto-search quality and per-user toggle
This commit is contained in:
@@ -120,6 +120,27 @@ def _normalize_username(value: Any) -> Optional[str]:
|
||||
return normalized if normalized else None
|
||||
|
||||
|
||||
def _user_can_use_search_auto(user: Dict[str, Any]) -> bool:
|
||||
if user.get("role") == "admin":
|
||||
return True
|
||||
return bool(user.get("auto_search_enabled", True))
|
||||
|
||||
|
||||
def _filter_snapshot_actions_for_user(snapshot: Snapshot, user: Dict[str, Any]) -> Snapshot:
|
||||
if _user_can_use_search_auto(user):
|
||||
return snapshot
|
||||
snapshot.actions = [action for action in snapshot.actions if action.id != "search_auto"]
|
||||
return snapshot
|
||||
|
||||
|
||||
def _quality_profile_id(value: Any) -> Optional[int]:
|
||||
if isinstance(value, int):
|
||||
return value
|
||||
if isinstance(value, str) and value.strip().isdigit():
|
||||
return int(value.strip())
|
||||
return None
|
||||
|
||||
|
||||
def _request_matches_user(request_data: Any, username: str) -> bool:
|
||||
requested_by = None
|
||||
if isinstance(request_data, dict):
|
||||
@@ -1476,7 +1497,8 @@ async def get_snapshot(request_id: str, user: Dict[str, str] = Depends(get_curre
|
||||
client = JellyseerrClient(runtime.jellyseerr_base_url, runtime.jellyseerr_api_key)
|
||||
if client.configured():
|
||||
await _ensure_request_access(client, int(request_id), user)
|
||||
return await build_snapshot(request_id)
|
||||
snapshot = await build_snapshot(request_id)
|
||||
return _filter_snapshot_actions_for_user(snapshot, user)
|
||||
|
||||
|
||||
@router.get("/recent")
|
||||
@@ -1747,7 +1769,7 @@ async def ai_triage(request_id: str, user: Dict[str, str] = Depends(get_current_
|
||||
client = JellyseerrClient(runtime.jellyseerr_base_url, runtime.jellyseerr_api_key)
|
||||
if client.configured():
|
||||
await _ensure_request_access(client, int(request_id), user)
|
||||
snapshot = await build_snapshot(request_id)
|
||||
snapshot = _filter_snapshot_actions_for_user(await build_snapshot(request_id), user)
|
||||
return triage_snapshot(snapshot)
|
||||
|
||||
|
||||
@@ -1784,6 +1806,8 @@ async def action_search(request_id: str, user: Dict[str, str] = Depends(get_curr
|
||||
|
||||
@router.post("/{request_id}/actions/search_auto")
|
||||
async def action_search_auto(request_id: str, user: Dict[str, str] = Depends(get_current_user)) -> dict:
|
||||
if not _user_can_use_search_auto(user):
|
||||
raise HTTPException(status_code=403, detail="Auto search and download is disabled for this user")
|
||||
runtime = get_runtime_settings()
|
||||
client = JellyseerrClient(runtime.jellyseerr_base_url, runtime.jellyseerr_api_key)
|
||||
if client.configured():
|
||||
@@ -1797,10 +1821,23 @@ async def action_search_auto(request_id: str, user: Dict[str, str] = Depends(get
|
||||
client = SonarrClient(runtime.sonarr_base_url, runtime.sonarr_api_key)
|
||||
if not client.configured():
|
||||
raise HTTPException(status_code=400, detail="Sonarr not configured")
|
||||
target_profile_id = _quality_profile_id(runtime.sonarr_quality_profile_id)
|
||||
current_profile_id = _quality_profile_id(arr_item.get("qualityProfileId"))
|
||||
profile_message = None
|
||||
series_id = _quality_profile_id(arr_item.get("id"))
|
||||
if target_profile_id and series_id and current_profile_id != target_profile_id:
|
||||
series = await client.get_series(series_id)
|
||||
if not isinstance(series, dict):
|
||||
raise HTTPException(status_code=502, detail="Could not load Sonarr series before search")
|
||||
series["qualityProfileId"] = target_profile_id
|
||||
await client.update_series(series)
|
||||
profile_message = f"Sonarr quality profile updated to {target_profile_id} before search."
|
||||
episodes = await client.get_episodes(int(arr_item["id"]))
|
||||
missing_by_season = _missing_episode_ids_by_season(episodes)
|
||||
if not missing_by_season:
|
||||
message = "No missing monitored episodes found."
|
||||
if profile_message:
|
||||
message = f"{profile_message} {message}"
|
||||
await asyncio.to_thread(
|
||||
save_action, request_id, "search_auto", "Search and auto-download", "ok", message
|
||||
)
|
||||
@@ -1814,6 +1851,8 @@ async def action_search_auto(request_id: str, user: Dict[str, str] = Depends(get
|
||||
{"season": season_number, "episodeCount": len(episode_ids), "response": response}
|
||||
)
|
||||
message = "Search sent to Sonarr."
|
||||
if profile_message:
|
||||
message = f"{profile_message} {message}"
|
||||
await asyncio.to_thread(
|
||||
save_action, request_id, "search_auto", "Search and auto-download", "ok", message
|
||||
)
|
||||
@@ -1822,8 +1861,21 @@ async def action_search_auto(request_id: str, user: Dict[str, str] = Depends(get
|
||||
client = RadarrClient(runtime.radarr_base_url, runtime.radarr_api_key)
|
||||
if not client.configured():
|
||||
raise HTTPException(status_code=400, detail="Radarr not configured")
|
||||
target_profile_id = _quality_profile_id(runtime.radarr_quality_profile_id)
|
||||
current_profile_id = _quality_profile_id(arr_item.get("qualityProfileId"))
|
||||
profile_message = None
|
||||
movie_id = _quality_profile_id(arr_item.get("id"))
|
||||
if target_profile_id and movie_id and current_profile_id != target_profile_id:
|
||||
movie = await client.get_movie(movie_id)
|
||||
if not isinstance(movie, dict):
|
||||
raise HTTPException(status_code=502, detail="Could not load Radarr movie before search")
|
||||
movie["qualityProfileId"] = target_profile_id
|
||||
await client.update_movie(movie)
|
||||
profile_message = f"Radarr quality profile updated to {target_profile_id} before search."
|
||||
response = await client.search(int(arr_item["id"]))
|
||||
message = "Search sent to Radarr."
|
||||
if profile_message:
|
||||
message = f"{profile_message} {message}"
|
||||
await asyncio.to_thread(
|
||||
save_action, request_id, "search_auto", "Search and auto-download", "ok", message
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user