podsync 0.1.12

A server to sync podcasts with, mirroring the gpodder API. Designed for use with AntennaPod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#![allow(unused_variables)]
#![allow(unused_imports)]

use std::collections::HashSet;
use std::fs::{self, File, OpenOptions};
use std::io::{BufRead, BufReader, ErrorKind, Write};
use std::path::{Path, PathBuf};

use log::{error, info, warn};

use crate::backend::FindError;

use crate::device::{DeviceAndSub, DeviceType, DeviceUpdate};
use crate::episode::{Episode, EpisodeRaw};
use crate::podsync::{QueryEpisodes, Url};
use crate::subscription::SubscriptionChangesFromClient;
use crate::user::User;
use crate::Timestamp;

mod kv;
use kv::KeyValues;

pub struct Backend {
    root: PathBuf,
}

impl Backend {
    pub async fn new(path: &Path) -> Self {
        Self {
            root: path.to_path_buf(),
        }
    }
}

macro_rules! path {
    ($root: expr, $($components: expr),*) => {
        {
            let mut p = $root.clone();
            path!(@internal, p, $($components),*);
            p
        }
    };
    (@internal, $p:expr, $next:expr, $($rest: expr),*) => {
        $p.push($next);
        path!(@internal, $p, $($rest),*);
    };
    (@internal, $p:expr, $next:expr) => {
        $p.push($next);
    };
}

impl Backend {
    fn read(&self, path: PathBuf, keys: &[&str]) -> Result<KeyValues, FindError> {
        let file = File::open(&path).map_err(|e| {
            if e.kind() == ErrorKind::NotFound {
                return FindError::NotFound;
            }
            error!("open \"{path:?}\": {e:?}");
            FindError::Internal
        })?;

        kv::read(file, keys)
    }

    fn write(&self, path: PathBuf, keyvalues: &KeyValues) -> Result<(), std::io::Error> {
        let file = OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(path)?;

        kv::write(file, keyvalues)
    }

    fn read_user(&self, username: &str) -> Result<KeyValues, FindError> {
        let path = path!(self.root, "users", username, "creds.txt");
        self.read(path, &["pwhash", "session_id"])
    }

    fn write_user(&self, username: &str, keyvalues: &KeyValues) -> Result<(), std::io::Error> {
        let path = path!(self.root, "users", username, "creds.txt");
        self.write(path, keyvalues)
    }
}

impl Backend {
    pub async fn find_user(&self, target_username: &str) -> Result<User, FindError> {
        let user = self.read_user(target_username)?;

        Ok(User {
            username: target_username.into(),
            pwhash: user.get("pwhash").ok_or(FindError::Internal)?.clone(),
            session_id: user.get("session_id").map(|x| x.into()),
        })
    }

    /// session_id: set to None to logout / make NULL
    pub async fn update_user(&self, username: &str, session_id: Option<&str>) -> bool {
        let mut user = match self.read_user(username) {
            Ok(u) => u,
            Err(e) => {
                error!("read \"{username}\": {e:?}");
                return false;
            }
        };

        match session_id {
            Some(id) => {
                user.insert("session_id".into(), id.into());
            }
            None => {
                user.remove("session_id");
            }
        }

        if let Err(e) = self.write_user(username, &user) {
            error!("write \"{username}\": {e:?}");
            false
        } else {
            true
        }
    }

    pub async fn users_with_session(&self, session_id: &str) -> Result<Vec<User>, ()> {
        let path = path!(self.root, "users");
        let mut users = vec![];

        let emap = |e: &dyn std::fmt::Debug| {
            error!("error looking up session: {e:?}");
        };

        for ent in fs::read_dir(path).map_err(|e| emap(&e))? {
            let ent = ent.map_err(|e| emap(&e))?;

            let fname = ent.file_name();
            let fname = match fname.into_string() {
                Ok(x) => x,
                Err(e) => {
                    warn!("couldn't convert path into string: {e:?}");
                    continue;
                }
            };

            let u = self.find_user(&fname).await.map_err(|e| emap(&e))?;
            if let Some(ref id) = u.session_id {
                if id == session_id {
                    users.push(u);
                }
            }
        }

        Ok(users)
    }
}

impl Backend {
    fn devices(&self, username: &str) -> Result<Vec<(String, DeviceType, String)>, ()> {
        let path = path!(self.root, "users", username, "devices.txt");
        let file = File::open(&path).map_err(|e| {
            error!("open \"{path:?}\": {e:?}");
        })?;

        let mut devices = vec![];

        for line in BufReader::new(file).lines() {
            let line = line.map_err(|e| {
                error!("read \"{path:?}\": {e:?}");
            })?;

            let [id, type_, caption] = *line.splitn(3, ' ').collect::<Vec<_>>() else {
                error!("invalid device line");
                return Err(());
            };

            devices.push((
                id.into(),
                type_.try_into().map_err(|()| {
                    error!("couldn't parse device type for \"{username}\"");
                })?,
                caption.into(),
            ));
        }

        Ok(devices)
    }

    pub async fn devices_for_user(&self, username: &str) -> Result<Vec<DeviceAndSub>, ()> {
        let subcount = self.subscriptions_anydev(username)?.len(); // inefficient

        self.devices(username)?
            .into_iter()
            .map(|(id, type_, caption)| {
                Ok(DeviceAndSub {
                    r#type: type_,
                    id,
                    caption,
                    subscriptions: subcount as _,
                })
            })
            .collect::<Result<Vec<_>, _>>()
    }

    pub async fn update_device(
        &self,
        username: &str,
        device_id: &str,
        update: DeviceUpdate,
    ) -> Result<(), ()> {
        let mut devices = self.devices(username)?;
        let mut found = false;

        for dev in &mut devices {
            if dev.0 == device_id {
                if let Some(ref t) = update.r#type {
                    dev.1 = t.clone();
                }
                if let Some(ref c) = update.caption {
                    dev.2 = c.clone();
                }
                found = true;
                break;
            }
        }

        if !found {
            devices.push((
                device_id.into(),
                update.r#type.unwrap_or_default(),
                update.caption.unwrap_or_else(|| "".into()),
            ));
        }

        let path = path!(self.root, "users", username, "devices.txt");
        let mut file = OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(path)
            .map_err(|e| {
                error!("couldn't open \"{username}\"'s devices: {e:?}");
            })?;

        for dev in devices {
            let (id, type_, caption) = dev;
            writeln!(file, "{id} {} {caption}", type_.as_str()).map_err(|e| {
                error!("writing \"{username}\" devices: {e:?}");
            })?;
        }

        Ok(())
    }
}

impl Backend {
    fn subscriptions_anydev(
        &self,
        username: &str,
    ) -> Result<Vec<(String, String, Timestamp, Option<Timestamp>)>, ()> {
        let path = path!(self.root, "users", username, "subs.txt");
        let file = File::open(&path).map_err(|e| {
            error!("open \"{path:?}\": {e:?}");
        })?;
        let mut subs = vec![];

        for line in BufReader::new(file).lines() {
            let line = line.map_err(|e| {
                error!("read \"{path:?}\": {e:?}");
            })?;

            let [device, created, deleted, url] = *line.splitn(4, ' ').collect::<Vec<_>>() else {
                error!("invalid sub line");
                return Err(());
            };

            let parse = |s: &str| {
                s.parse().map_err(|e| {
                    error!("couldn't parse \"{s}\" as a timestamp: {e:?}");
                })
            };

            subs.push((
                device.into(),
                url.into(),
                parse(created)?,
                match deleted {
                    "-" => None,
                    _ => Some(parse(deleted)?),
                },
            ));
        }

        Ok(subs)
    }

    pub async fn subscriptions(
        &self,
        username: &str,
        device_id: &str,
        since: Timestamp,
    ) -> Result<Vec<Url>, ()> {
        Ok(self
            .subscriptions_anydev(username)?
            .into_iter()
            .filter(|(dev, _url, created, deleted)| {
                if dev != device_id {
                    return false;
                }
                if *created >= since {
                    return true;
                }
                match deleted {
                    Some(deleted) => *deleted >= since,
                    None => false,
                }
            })
            .map(|(dev, url, created, deleted)| Url {
                url,
                created,
                deleted,
            })
            .collect())
    }

    pub async fn update_subscriptions(
        &self,
        username: &str,
        device_id: &str,
        changes: &SubscriptionChangesFromClient,
        now: Timestamp,
    ) -> Result<(), ()> {
        let existing = self.subscriptions_anydev(username)?;
        let urls_to_del = changes.remove.iter().collect::<HashSet<_>>();
        let now = Timestamp::now().map_err(|e| {
            error!("couldn't create timestamp: {e:?}");
        })?;
        let new_subs = changes
            .add
            .iter()
            .map(|url| (device_id, url.as_str(), &now, None));

        let to_write = existing
            .iter()
            .filter(|sub| sub.0 != device_id || !urls_to_del.contains(&sub.1))
            .map(|(dev, url, created, deleted)| {
                (dev.as_str(), url.as_str(), created, deleted.as_ref())
            })
            .chain(new_subs);

        let path = path!(self.root, "users", username, "subs.txt");
        let mut file = OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(path)
            .map_err(|e| {
                error!("couldn't open \"{username}\"'s subs: {e:?}");
            })?;

        for sub in to_write {
            let (device, url, created, deleted) = sub;

            let r = match deleted {
                Some(deleted) => writeln!(file, "{} {} {} {}", device, created, deleted, url),
                None => writeln!(file, "{} {} - {}", device, created, url),
            };

            r.map_err(|e| {
                error!("writing \"{username}\" subs: {e:?}");
            })?;
        }

        Ok(())
    }
}

impl Backend {
    pub async fn episodes(
        &self,
        username: &str,
        query: &QueryEpisodes,
    ) -> Result<Vec<EpisodeRaw>, ()> {
        let path = path!(self.root, "users", username, "episodes.txt");
        let file = File::open(&path).map_err(|e| {
            error!("open \"{path:?}\": {e:?}");
        })?;

        let mut eps = vec![];

        for line in BufReader::new(file).lines() {
            let line = line.map_err(|e| {
                error!("read \"{path:?}\": {e:?}");
            })?;

            let ep = serde_json::from_str(&line).map_err(|e| {
                error!("couldn't parse episode line for {username}");
            })?;
            eps.push(ep);
        }

        Ok(eps)
    }

    pub async fn update_episodes(
        &self,
        username: &str,
        now: Timestamp,
        changes: Vec<Episode>,
    ) -> Result<(), ()> {
        let mut eps = self.episodes(username, &QueryEpisodes::default()).await?;

        for change in changes {
            // insert `change`, if conflict then replace
            // supplement with username, device, podcast

            let change: EpisodeRaw = change.into();
            let change_id = change.id();
            let found = eps.iter_mut().find(|ep| ep.id() == change_id);

            match found {
                Some(ep) => {
                    *ep = change;
                }
                None => {
                    eps.push(change);
                }
            }
        }

        let path = path!(self.root, "users", username, "episodes.txt");
        let mut file = OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(path)
            .map_err(|e| {
                error!("couldn't open \"{username}\"'s episodes: {e:?}");
            })?;

        for ep in eps {
            let json = serde_json::to_string(&ep).map_err(|e| {
                error!("couldn't convert episode to json: {e:?}");
            })?;

            writeln!(file, "{}", json).map_err(|e| {
                error!("writing \"{username}\" episode: {e:?}");
            })?;
        }

        Ok(())
    }
}

#[cfg(test)]
pub mod test {}