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::timer::EventTimers;
208use crate::settings::timer::Timer;
209use crate::settings::{rewrite::Rewrite, triggers::autoscan::Autoscan};
210use serde::{Deserialize, Serialize};
211use {
212 lidarr::{Lidarr, LidarrRequest},
213 manual::Manual,
214 notify::Notify,
215 radarr::{Radarr, RadarrRequest},
216 readarr::{Readarr, ReadarrRequest},
217 sonarr::{Sonarr, SonarrRequest},
218};
219
220pub trait TriggerRequest {
221 fn from_json(json: serde_json::Value) -> anyhow::Result<Self>
222 where
223 Self: Sized;
224
225 // where the bool represents whether to check found status
226 fn paths(&self) -> Vec<(String, bool)>;
227}
228
229pub trait TriggerConfig {
230 fn rewrite(&self) -> Option<&Rewrite>;
231 fn timer(&self) -> Option<&Timer>;
232 fn excludes(&self) -> &Vec<String>;
233 fn event_timers(&self) -> Option<&EventTimers> {
234 None
235 }
236}
237
238#[derive(Serialize, Deserialize)]
239#[serde(rename_all = "lowercase")]
240pub enum TriggerType {
241 Manual,
242 Autoscan,
243 Radarr,
244 Bazarr,
245 Sonarr,
246 Lidarr,
247 Readarr,
248 Notify,
249}
250
251#[derive(Serialize, Deserialize, Clone)]
252#[serde(tag = "type", rename_all = "lowercase")]
253pub enum Trigger {
254 Manual(Manual),
255 Autoscan(Autoscan),
256 Bazarr(Manual),
257 Radarr(Radarr),
258 Sonarr(Sonarr),
259 Lidarr(Lidarr),
260 Readarr(Readarr),
261 Notify(Notify),
262}
263
264impl Trigger {
265 fn as_config(&self) -> &dyn TriggerConfig {
266 match self {
267 Self::Manual(trigger) | Self::Bazarr(trigger) => trigger,
268 Self::Autoscan(trigger) => trigger,
269 Self::Radarr(trigger) => trigger,
270 Self::Sonarr(trigger) => trigger,
271 Self::Lidarr(trigger) => trigger,
272 Self::Readarr(trigger) => trigger,
273 Self::Notify(trigger) => trigger,
274 }
275 }
276
277 pub fn get_rewrite(&self) -> Option<&Rewrite> {
278 self.as_config().rewrite()
279 }
280
281 pub fn get_timer(&self, event_name: Option<String>) -> Timer {
282 let config = self.as_config();
283 let mut base_timer = config.timer().cloned().unwrap_or_default();
284
285 let event_specific_timer = event_name
286 .as_ref()
287 .and_then(|event| config.event_timers().and_then(|timers| timers.get(event)));
288
289 if let Some(event_timer) = event_specific_timer {
290 base_timer = base_timer.chain(event_timer);
291 }
292
293 base_timer
294 }
295
296 pub fn paths(&self, body: serde_json::Value) -> anyhow::Result<(String, Vec<(String, bool)>)> {
297 let event_name = body["eventType"].as_str().unwrap_or("unknown").to_string();
298
299 let paths = match &self {
300 Self::Sonarr(_) => Ok(SonarrRequest::from_json(body)?.paths()),
301 Self::Radarr(_) => Ok(RadarrRequest::from_json(body)?.paths()),
302 Self::Lidarr(_) => Ok(LidarrRequest::from_json(body)?.paths()),
303 Self::Readarr(_) => Ok(ReadarrRequest::from_json(body)?.paths()),
304 Self::Manual(_) | Self::Notify(_) | Self::Autoscan(_) | Self::Bazarr(_) => {
305 Err(anyhow::anyhow!("Manual trigger does not have paths"))
306 }
307 }?;
308
309 Ok((event_name, paths))
310 }
311
312 pub fn excludes(&self) -> &Vec<String> {
313 self.as_config().excludes()
314 }
315}