Skip to main content

autopulse_service/settings/triggers/
mod.rs

1/// Autoscan - autoscan compatibility trigger
2///
3/// Provides a trigger with compatibility for applications that are built for autoscan
4///
5/// # Example
6///
7/// ```yml
8/// triggers:
9///   my_autoscan:
10///     type: autoscan
11/// ```
12///
13/// or
14///
15/// ```yml
16/// triggers:
17///   my_autoscan:
18///     type: autoscan
19///     rewrite:
20///       from: "/downloads/all"
21///       to: "/all"
22///     timer:
23///       wait: 30
24///     excludes: [ "ignored_target" ]
25/// ```
26///
27/// See [`Autoscan`] for all options
28pub mod autoscan;
29/// Lidarr - Lidarr trigger
30///
31/// This trigger is used to process a file from Lidarr
32///
33/// # Example
34///
35/// ```yml
36/// triggers:
37///   my_lidarr:
38///     type: lidarr
39/// ```
40///
41/// or
42///
43/// ```yml
44/// triggers:
45///   my_lidarr:
46///     type: lidarr
47///     rewrite:
48///       from: "/downloads/music"
49///       to: "/music"
50///     timer:
51///       wait: 30
52///     excludes: [ "ignored_target" ]
53/// ```
54///
55/// See [`Lidarr`] for all options
56pub mod lidarr;
57/// Manual - Manual trigger
58///
59/// This trigger is used to manually process a file. Often used when implementing a custom trigger
60///
61/// Note: A default route of `/triggers/manual` is provided
62///
63/// # Example
64///
65/// ```yml
66/// triggers:
67///   my_manual:
68///     type: manual
69/// ```
70///
71/// or
72///
73/// ```yml
74/// triggers:
75///   my_manual:
76///     type: manual
77///     rewrite:
78///       from: "/downloads/stuff"
79///       to: "/stuff"
80///     timer:
81///       wait: 30
82///     excludes: [ "ignored_target" ]
83/// ```
84///
85/// See [`Manual`] for all options
86/// and
87/// [`ManualQueryParams`](manual::ManualQueryParams) for query parameters
88pub mod manual;
89/// Notify - Notify trigger
90///
91/// Cross-platform monitoring for a directory to process based on file events
92///
93/// # Example
94///
95/// ```yml
96/// triggers:
97///   my_notify:
98///     type: notify
99///     paths:
100///       - "/path/to/monitor"
101/// ```
102///
103/// or
104///
105/// ```yml
106/// triggers:
107///   my_notify:
108///     type: notify
109///     paths:
110///       - "/downloads"
111///     recursive: false
112///     rewrite:
113///       from: "/downloads"
114///       to: "/media"
115///     timer:
116///       wait: 30
117///     excludes: [ "ignored_target" ]
118/// ```
119///
120/// See [`Notify`] for all options
121pub mod notify;
122/// Radarr - Radarr trigger
123///
124/// This trigger is used to process a file from Radarr
125///
126/// # Example
127///
128/// ```yml
129/// triggers:
130///   my_radarr:
131///     type: radarr
132/// ```
133///
134/// or
135///
136/// ```yml
137/// triggers:
138///   my_radarr:
139///     type: radarr
140///     rewrite:
141///       from: "/downloads/movies"
142///       to: "/movies"
143///     timer:
144///       wait: 30
145///     excludes: [ "ignored_target" ]
146/// ```
147///
148/// See [`Radarr`] for all options
149pub mod radarr;
150/// Readarr - Readarr trigger
151///
152/// This trigger is used to process a file from Readarr
153///
154/// # Example
155///
156/// ```yml
157/// triggers:
158///   my_readarr:
159///     type: readarr
160/// ```
161///
162/// or
163///
164/// ```yml
165/// triggers:
166///   my_readarr:
167///     type: readarr
168///     rewrite:
169///       from: "/downloads/books"
170///       to: "/books"
171///     timer:
172///       wait: 30
173///     excludes: [ "ignored_target" ]
174/// ```
175///
176/// See [`Readarr`] for all options
177pub mod readarr;
178/// Sonarr - Sonarr trigger
179///
180/// This trigger is used to process a file from Sonarr
181///
182/// # Example
183///
184/// ```yml
185/// triggers:
186///   my_sonarr:
187///     type: sonarr
188/// ```
189///
190/// or
191///
192/// ```yml
193/// triggers:
194///   my_sonarr:
195///     type: sonarr
196///     rewrite:
197///       from: "/downloads/shows"
198///       to: "/shows"
199///     timer:
200///       wait: 30
201///     excludes: [ "ignored_target" ]
202/// ```
203///
204/// See [`Sonarr`] for all options
205pub mod sonarr;
206
207use crate::settings::path_filter::PathFilter;
208use crate::settings::timer::EventTimers;
209use crate::settings::timer::Timer;
210use crate::settings::{rewrite::Rewrite, triggers::autoscan::Autoscan};
211use serde::{Deserialize, Serialize};
212use {
213    lidarr::{Lidarr, LidarrRequest},
214    manual::Manual,
215    notify::Notify,
216    radarr::{Radarr, RadarrRequest},
217    readarr::{Readarr, ReadarrRequest},
218    sonarr::{Sonarr, SonarrRequest},
219};
220
221pub trait TriggerRequest {
222    fn from_json(json: serde_json::Value) -> anyhow::Result<Self>
223    where
224        Self: Sized;
225
226    // where the bool represents whether to check found status
227    fn paths(&self) -> Vec<(String, bool)>;
228}
229
230pub trait TriggerConfig {
231    fn rewrite(&self) -> Option<&Rewrite>;
232    fn timer(&self) -> Option<&Timer>;
233    fn excludes(&self) -> &Vec<String>;
234    fn filter(&self) -> &PathFilter;
235    fn event_timers(&self) -> Option<&EventTimers> {
236        None
237    }
238}
239
240#[derive(Serialize, Deserialize)]
241#[serde(rename_all = "lowercase")]
242pub enum TriggerType {
243    Manual,
244    Autoscan,
245    Radarr,
246    Bazarr,
247    Sonarr,
248    Lidarr,
249    Readarr,
250    Notify,
251}
252
253#[derive(Serialize, Deserialize, Clone)]
254#[serde(tag = "type", rename_all = "lowercase")]
255pub enum Trigger {
256    Manual(Manual),
257    Autoscan(Autoscan),
258    Bazarr(Manual),
259    Radarr(Radarr),
260    Sonarr(Sonarr),
261    Lidarr(Lidarr),
262    Readarr(Readarr),
263    Notify(Notify),
264}
265
266impl Trigger {
267    fn as_config(&self) -> &dyn TriggerConfig {
268        match self {
269            Self::Manual(trigger) | Self::Bazarr(trigger) => trigger,
270            Self::Autoscan(trigger) => trigger,
271            Self::Radarr(trigger) => trigger,
272            Self::Sonarr(trigger) => trigger,
273            Self::Lidarr(trigger) => trigger,
274            Self::Readarr(trigger) => trigger,
275            Self::Notify(trigger) => trigger,
276        }
277    }
278
279    pub fn get_rewrite(&self) -> Option<&Rewrite> {
280        self.as_config().rewrite()
281    }
282
283    pub fn get_timer(&self, event_name: Option<String>) -> Timer {
284        let config = self.as_config();
285        let mut base_timer = config.timer().cloned().unwrap_or_default();
286
287        let event_specific_timer = event_name
288            .as_ref()
289            .and_then(|event| config.event_timers().and_then(|timers| timers.get(event)));
290
291        if let Some(event_timer) = event_specific_timer {
292            base_timer = base_timer.chain(event_timer);
293        }
294
295        base_timer
296    }
297
298    pub fn paths(&self, body: serde_json::Value) -> anyhow::Result<(String, Vec<(String, bool)>)> {
299        let event_name = body["eventType"].as_str().unwrap_or("unknown").to_string();
300
301        let paths = match &self {
302            Self::Sonarr(_) => Ok(SonarrRequest::from_json(body)?.paths()),
303            Self::Radarr(_) => Ok(RadarrRequest::from_json(body)?.paths()),
304            Self::Lidarr(_) => Ok(LidarrRequest::from_json(body)?.paths()),
305            Self::Readarr(_) => Ok(ReadarrRequest::from_json(body)?.paths()),
306            Self::Manual(_) | Self::Notify(_) | Self::Autoscan(_) | Self::Bazarr(_) => {
307                Err(anyhow::anyhow!("Manual trigger does not have paths"))
308            }
309        }?;
310
311        Ok((event_name, paths))
312    }
313
314    pub fn excludes(&self) -> &Vec<String> {
315        self.as_config().excludes()
316    }
317
318    pub fn should_process_path(&self, path: &str) -> bool {
319        self.as_config().filter().allows(path)
320    }
321}