0000: 75 73 65 20 73 74 64 3a 3a 62 6f 72 72 6f 77 3a use std::borrow:
0010: 3a 43 6f 77 3b 0a 0a 75 73 65 20 61 6e 79 68 6f :Cow;..use anyho
0020: 77 3a 3a 7b 0a 09 52 65 73 75 6c 74 2c 0a 09 62 w::{..Result,..b
0030: 61 69 6c 2c 0a 7d 3b 0a 75 73 65 20 63 68 72 6f ail,.};.use chro
0040: 6e 6f 3a 3a 7b 0a 09 44 61 74 65 54 69 6d 65 2c no::{..DateTime,
0050: 0a 09 46 69 78 65 64 4f 66 66 73 65 74 2c 0a 09 ..FixedOffset,..
0060: 4c 6f 63 61 6c 2c 0a 7d 3b 0a 75 73 65 20 73 71 Local,.};.use sq
0070: 6c 78 3a 3a 7b 0a 09 50 6f 6f 6c 2c 0a 09 50 6f lx::{..Pool,..Po
0080: 73 74 67 72 65 73 2c 0a 09 52 6f 77 2c 0a 09 70 stgres,..Row,..p
0090: 6f 73 74 67 72 65 73 3a 3a 50 67 50 6f 6f 6c 4f ostgres::PgPoolO
00a0: 70 74 69 6f 6e 73 2c 0a 09 70 6f 6f 6c 3a 3a 50 ptions,..pool::P
00b0: 6f 6f 6c 43 6f 6e 6e 65 63 74 69 6f 6e 2c 0a 7d oolConnection,.}
00c0: 3b 0a 0a 23 5b 64 65 72 69 76 65 28 73 71 6c 78 ;..#[derive(sqlx
00d0: 3a 3a 46 72 6f 6d 52 6f 77 2c 20 44 65 62 75 67 ::FromRow, Debug
00e0: 29 5d 0a 70 75 62 20 73 74 72 75 63 74 20 4c 69 )].pub struct Li
00f0: 73 74 20 7b 0a 09 70 75 62 20 73 6f 75 72 63 65 st {..pub source
0100: 5f 69 64 3a 20 69 36 34 2c 0a 09 70 75 62 20 63 _id: i64,..pub c
0110: 68 61 6e 6e 65 6c 3a 20 53 74 72 69 6e 67 2c 0a hannel: String,.
0120: 09 70 75 62 20 65 6e 61 62 6c 65 64 3a 20 62 6f .pub enabled: bo
0130: 6f 6c 2c 0a 09 70 75 62 20 75 72 6c 3a 20 53 74 ol,..pub url: St
0140: 72 69 6e 67 2c 0a 09 70 75 62 20 69 76 5f 68 61 ring,..pub iv_ha
0150: 73 68 3a 20 4f 70 74 69 6f 6e 3c 53 74 72 69 6e sh: Option<Strin
0160: 67 3e 2c 0a 09 70 75 62 20 75 72 6c 5f 72 65 3a g>,..pub url_re:
0170: 20 4f 70 74 69 6f 6e 3c 53 74 72 69 6e 67 3e 2c Option<String>,
0180: 0a 7d 0a 0a 23 5b 64 65 72 69 76 65 28 73 71 6c .}..#[derive(sql
0190: 78 3a 3a 46 72 6f 6d 52 6f 77 2c 20 44 65 62 75 x::FromRow, Debu
01a0: 67 29 5d 0a 70 75 62 20 73 74 72 75 63 74 20 53 g)].pub struct S
01b0: 6f 75 72 63 65 20 7b 0a 09 70 75 62 20 63 68 61 ource {..pub cha
01c0: 6e 6e 65 6c 5f 69 64 3a 20 69 36 34 2c 0a 09 70 nnel_id: i64,..p
01d0: 75 62 20 75 72 6c 3a 20 53 74 72 69 6e 67 2c 0a ub url: String,.
01e0: 09 70 75 62 20 69 76 5f 68 61 73 68 3a 20 4f 70 .pub iv_hash: Op
01f0: 74 69 6f 6e 3c 53 74 72 69 6e 67 3e 2c 0a 09 70 tion<String>,..p
0200: 75 62 20 6f 77 6e 65 72 3a 20 69 36 34 2c 0a 09 ub owner: i64,..
0210: 70 75 62 20 75 72 6c 5f 72 65 3a 20 4f 70 74 69 pub url_re: Opti
0220: 6f 6e 3c 53 74 72 69 6e 67 3e 2c 0a 7d 0a 0a 23 on<String>,.}..#
0230: 5b 64 65 72 69 76 65 28 73 71 6c 78 3a 3a 46 72 [derive(sqlx::Fr
0240: 6f 6d 52 6f 77 29 5d 0a 70 75 62 20 73 74 72 75 omRow)].pub stru
0250: 63 74 20 51 75 65 75 65 20 7b 0a 09 70 75 62 20 ct Queue {..pub
0260: 73 6f 75 72 63 65 5f 69 64 3a 20 4f 70 74 69 6f source_id: Optio
0270: 6e 3c 69 33 32 3e 2c 0a 09 70 75 62 20 6e 65 78 n<i32>,..pub nex
0280: 74 5f 66 65 74 63 68 3a 20 4f 70 74 69 6f 6e 3c t_fetch: Option<
0290: 44 61 74 65 54 69 6d 65 3c 4c 6f 63 61 6c 3e 3e DateTime<Local>>
02a0: 2c 0a 09 70 75 62 20 6f 77 6e 65 72 3a 20 4f 70 ,..pub owner: Op
02b0: 74 69 6f 6e 3c 69 36 34 3e 2c 0a 7d 0a 0a 23 5b tion<i64>,.}..#[
02c0: 64 65 72 69 76 65 28 43 6c 6f 6e 65 29 5d 0a 70 derive(Clone)].p
02d0: 75 62 20 73 74 72 75 63 74 20 44 62 20 7b 0a 09 ub struct Db {..
02e0: 70 6f 6f 6c 3a 20 73 71 6c 78 3a 3a 50 6f 6f 6c pool: sqlx::Pool
02f0: 3c 73 71 6c 78 3a 3a 50 6f 73 74 67 72 65 73 3e <sqlx::Postgres>
0300: 2c 0a 7d 0a 0a 70 75 62 20 73 74 72 75 63 74 20 ,.}..pub struct
0310: 43 6f 6e 6e 7b 0a 09 63 6f 6e 6e 3a 20 50 6f 6f Conn{..conn: Poo
0320: 6c 43 6f 6e 6e 65 63 74 69 6f 6e 3c 50 6f 73 74 lConnection<Post
0330: 67 72 65 73 3e 2c 0a 7d 0a 0a 69 6d 70 6c 20 44 gres>,.}..impl D
0340: 62 20 7b 0a 09 70 75 62 20 66 6e 20 6e 65 77 20 b {..pub fn new
0350: 28 70 67 75 72 69 3a 20 26 73 74 72 29 20 2d 3e (pguri: &str) ->
0360: 20 52 65 73 75 6c 74 3c 44 62 3e 20 7b 0a 09 09 Result<Db> {...
0370: 4f 6b 28 44 62 7b 0a 09 09 09 70 6f 6f 6c 3a 20 Ok(Db{....pool:
0380: 50 67 50 6f 6f 6c 4f 70 74 69 6f 6e 73 3a 3a 6e PgPoolOptions::n
0390: 65 77 28 29 0a 09 09 09 09 2e 6d 61 78 5f 63 6f ew()......max_co
03a0: 6e 6e 65 63 74 69 6f 6e 73 28 35 29 0a 09 09 09 nnections(5)....
03b0: 09 2e 61 63 71 75 69 72 65 5f 74 69 6d 65 6f 75 ..acquire_timeou
03c0: 74 28 73 74 64 3a 3a 74 69 6d 65 3a 3a 44 75 72 t(std::time::Dur
03d0: 61 74 69 6f 6e 3a 3a 6e 65 77 28 33 30 30 2c 20 ation::new(300,
03e0: 30 29 29 0a 09 09 09 09 2e 69 64 6c 65 5f 74 69 0))......idle_ti
03f0: 6d 65 6f 75 74 28 73 74 64 3a 3a 74 69 6d 65 3a meout(std::time:
0400: 3a 44 75 72 61 74 69 6f 6e 3a 3a 6e 65 77 28 36 :Duration::new(6
0410: 30 2c 20 30 29 29 0a 09 09 09 09 2e 63 6f 6e 6e 0, 0))......conn
0420: 65 63 74 5f 6c 61 7a 79 28 70 67 75 72 69 29 3f ect_lazy(pguri)?
0430: 2c 0a 09 09 7d 29 0a 09 7d 0a 0a 09 70 75 62 20 ,...})..}...pub
0440: 61 73 79 6e 63 20 66 6e 20 62 65 67 69 6e 28 26 async fn begin(&
0450: 6d 75 74 20 73 65 6c 66 29 20 2d 3e 20 52 65 73 mut self) -> Res
0460: 75 6c 74 3c 43 6f 6e 6e 3e 20 7b 0a 09 09 43 6f ult<Conn> {...Co
0470: 6e 6e 3a 3a 6e 65 77 28 26 6d 75 74 20 73 65 6c nn::new(&mut sel
0480: 66 2e 70 6f 6f 6c 29 2e 61 77 61 69 74 0a 09 7d f.pool).await..}
0490: 0a 7d 0a 0a 69 6d 70 6c 20 43 6f 6e 6e 20 7b 0a .}..impl Conn {.
04a0: 09 70 75 62 20 61 73 79 6e 63 20 66 6e 20 6e 65 .pub async fn ne
04b0: 77 20 28 70 6f 6f 6c 3a 20 26 6d 75 74 20 50 6f w (pool: &mut Po
04c0: 6f 6c 3c 50 6f 73 74 67 72 65 73 3e 29 20 2d 3e ol<Postgres>) ->
04d0: 20 52 65 73 75 6c 74 3c 43 6f 6e 6e 3e 20 7b 0a Result<Conn> {.
04e0: 09 09 6c 65 74 20 63 6f 6e 6e 20 3d 20 70 6f 6f ..let conn = poo
04f0: 6c 2e 61 63 71 75 69 72 65 28 29 2e 61 77 61 69 l.acquire().awai
0500: 74 3f 3b 0a 09 09 4f 6b 28 43 6f 6e 6e 7b 0a 09 t?;...Ok(Conn{..
0510: 09 09 63 6f 6e 6e 2c 0a 09 09 7d 29 0a 09 7d 0a ..conn,...})..}.
0520: 0a 09 70 75 62 20 61 73 79 6e 63 20 66 6e 20 61 ..pub async fn a
0530: 64 64 5f 70 6f 73 74 20 28 26 6d 75 74 20 73 65 dd_post (&mut se
0540: 6c 66 2c 20 69 64 3a 20 69 33 32 2c 20 64 61 74 lf, id: i32, dat
0550: 65 3a 20 26 44 61 74 65 54 69 6d 65 3c 46 69 78 e: &DateTime<Fix
0560: 65 64 4f 66 66 73 65 74 3e 2c 20 70 6f 73 74 5f edOffset>, post_
0570: 75 72 6c 3a 20 26 73 74 72 29 20 2d 3e 20 52 65 url: &str) -> Re
0580: 73 75 6c 74 3c 28 29 3e 20 7b 0a 09 09 73 71 6c sult<()> {...sql
0590: 78 3a 3a 71 75 65 72 79 28 22 69 6e 73 65 72 74 x::query("insert
05a0: 20 69 6e 74 6f 20 72 73 73 74 67 5f 70 6f 73 74 into rsstg_post
05b0: 20 28 73 6f 75 72 63 65 5f 69 64 2c 20 70 6f 73 (source_id, pos
05c0: 74 65 64 2c 20 75 72 6c 29 20 76 61 6c 75 65 73 ted, url) values
05d0: 20 28 24 31 2c 20 24 32 2c 20 24 33 29 3b 22 29 ($1, $2, $3);")
05e0: 0a 09 09 09 2e 62 69 6e 64 28 69 64 29 0a 09 09 .....bind(id)...
05f0: 09 2e 62 69 6e 64 28 64 61 74 65 29 0a 09 09 09 ..bind(date)....
0600: 2e 62 69 6e 64 28 70 6f 73 74 5f 75 72 6c 29 0a .bind(post_url).
0610: 09 09 09 2e 65 78 65 63 75 74 65 28 26 6d 75 74 ....execute(&mut
0620: 20 2a 73 65 6c 66 2e 63 6f 6e 6e 29 2e 61 77 61 *self.conn).awa
0630: 69 74 3f 3b 0a 09 09 4f 6b 28 28 29 29 0a 09 7d it?;...Ok(())..}
0640: 0a 0a 09 70 75 62 20 61 73 79 6e 63 20 66 6e 20 ...pub async fn
0650: 63 6c 65 61 6e 20 28 26 6d 75 74 20 73 65 6c 66 clean (&mut self
0660: 2c 20 73 6f 75 72 63 65 5f 69 64 3a 20 69 33 32 , source_id: i32
0670: 2c 20 6f 77 6e 65 72 3a 20 69 36 34 29 20 2d 3e , owner: i64) ->
0680: 20 52 65 73 75 6c 74 3c 43 6f 77 3c 27 5f 2c 20 Result<Cow<'_,
0690: 73 74 72 3e 3e 20 7b 0a 09 09 6d 61 74 63 68 20 str>> {...match
06a0: 73 71 6c 78 3a 3a 71 75 65 72 79 28 22 64 65 6c sqlx::query("del
06b0: 65 74 65 20 66 72 6f 6d 20 72 73 73 74 67 5f 70 ete from rsstg_p
06c0: 6f 73 74 20 70 20 75 73 69 6e 67 20 72 73 73 74 ost p using rsst
06d0: 67 5f 73 6f 75 72 63 65 20 73 20 77 68 65 72 65 g_source s where
06e0: 20 70 2e 73 6f 75 72 63 65 5f 69 64 20 3d 20 24 p.source_id = $
06f0: 31 20 61 6e 64 20 6f 77 6e 65 72 20 3d 20 24 32 1 and owner = $2
0700: 20 61 6e 64 20 70 2e 73 6f 75 72 63 65 5f 69 64 and p.source_id
0710: 20 3d 20 73 2e 73 6f 75 72 63 65 5f 69 64 3b 22 = s.source_id;"
0720: 29 0a 09 09 09 2e 62 69 6e 64 28 73 6f 75 72 63 ).....bind(sourc
0730: 65 5f 69 64 29 0a 09 09 09 2e 62 69 6e 64 28 6f e_id).....bind(o
0740: 77 6e 65 72 29 0a 09 09 09 2e 65 78 65 63 75 74 wner).....execut
0750: 65 28 26 6d 75 74 20 2a 73 65 6c 66 2e 63 6f 6e e(&mut *self.con
0760: 6e 29 2e 61 77 61 69 74 3f 2e 72 6f 77 73 5f 61 n).await?.rows_a
0770: 66 66 65 63 74 65 64 28 29 20 7b 0a 09 09 09 30 ffected() {....0
0780: 20 3d 3e 20 7b 20 4f 6b 28 22 4e 6f 20 64 61 74 => { Ok("No dat
0790: 61 20 66 6f 75 6e 64 20 66 6f 75 6e 64 2e 22 2e a found found.".
07a0: 69 6e 74 6f 28 29 29 20 7d 2c 0a 09 09 09 78 20 into()) },....x
07b0: 3d 3e 20 7b 20 4f 6b 28 66 6f 72 6d 61 74 21 28 => { Ok(format!(
07c0: 22 7b 78 7d 20 70 6f 73 74 73 20 70 75 72 67 65 "{x} posts purge
07d0: 64 2e 22 29 2e 69 6e 74 6f 28 29 29 20 7d 2c 0a d.").into()) },.
07e0: 09 09 7d 0a 09 7d 0a 0a 09 70 75 62 20 61 73 79 ..}..}...pub asy
07f0: 6e 63 20 66 6e 20 64 65 6c 65 74 65 20 28 26 6d nc fn delete (&m
0800: 75 74 20 73 65 6c 66 2c 20 73 6f 75 72 63 65 5f ut self, source_
0810: 69 64 3a 20 69 33 32 2c 20 6f 77 6e 65 72 3a 20 id: i32, owner:
0820: 69 36 34 29 20 2d 3e 20 52 65 73 75 6c 74 3c 43 i64) -> Result<C
0830: 6f 77 3c 27 5f 2c 20 73 74 72 3e 3e 20 7b 0a 09 ow<'_, str>> {..
0840: 09 6d 61 74 63 68 20 73 71 6c 78 3a 3a 71 75 65 .match sqlx::que
0850: 72 79 28 22 64 65 6c 65 74 65 20 66 72 6f 6d 20 ry("delete from
0860: 72 73 73 74 67 5f 73 6f 75 72 63 65 20 77 68 65 rsstg_source whe
0870: 72 65 20 73 6f 75 72 63 65 5f 69 64 20 3d 20 24 re source_id = $
0880: 31 20 61 6e 64 20 6f 77 6e 65 72 20 3d 20 24 32 1 and owner = $2
0890: 3b 22 29 0a 09 09 09 2e 62 69 6e 64 28 73 6f 75 ;").....bind(sou
08a0: 72 63 65 5f 69 64 29 0a 09 09 09 2e 62 69 6e 64 rce_id).....bind
08b0: 28 6f 77 6e 65 72 29 0a 09 09 09 2e 65 78 65 63 (owner).....exec
08c0: 75 74 65 28 26 6d 75 74 20 2a 73 65 6c 66 2e 63 ute(&mut *self.c
08d0: 6f 6e 6e 29 2e 61 77 61 69 74 3f 2e 72 6f 77 73 onn).await?.rows
08e0: 5f 61 66 66 65 63 74 65 64 28 29 20 7b 0a 09 09 _affected() {...
08f0: 09 30 20 3d 3e 20 7b 20 4f 6b 28 22 4e 6f 20 64 .0 => { Ok("No d
0900: 61 74 61 20 66 6f 75 6e 64 20 66 6f 75 6e 64 2e ata found found.
0910: 22 2e 69 6e 74 6f 28 29 29 20 7d 2c 0a 09 09 09 ".into()) },....
0920: 78 20 3d 3e 20 7b 20 4f 6b 28 66 6f 72 6d 61 74 x => { Ok(format
0930: 21 28 22 7b 7d 20 73 6f 75 72 63 65 73 20 72 65 !("{} sources re
0940: 6d 6f 76 65 64 2e 22 2c 20 78 29 2e 69 6e 74 6f moved.", x).into
0950: 28 29 29 20 7d 2c 0a 09 09 7d 0a 09 7d 0a 0a 09 ()) },...}..}...
0960: 70 75 62 20 61 73 79 6e 63 20 66 6e 20 64 69 73 pub async fn dis
0970: 61 62 6c 65 20 28 26 6d 75 74 20 73 65 6c 66 2c able (&mut self,
0980: 20 73 6f 75 72 63 65 5f 69 64 3a 20 69 33 32 2c source_id: i32,
0990: 20 6f 77 6e 65 72 3a 20 69 36 34 29 20 2d 3e 20 owner: i64) ->
09a0: 52 65 73 75 6c 74 3c 26 73 74 72 3e 20 7b 0a 09 Result<&str> {..
09b0: 09 6d 61 74 63 68 20 73 71 6c 78 3a 3a 71 75 65 .match sqlx::que
09c0: 72 79 28 22 75 70 64 61 74 65 20 72 73 73 74 67 ry("update rsstg
09d0: 5f 73 6f 75 72 63 65 20 73 65 74 20 65 6e 61 62 _source set enab
09e0: 6c 65 64 20 3d 20 66 61 6c 73 65 20 77 68 65 72 led = false wher
09f0: 65 20 73 6f 75 72 63 65 5f 69 64 20 3d 20 24 31 e source_id = $1
0a00: 20 61 6e 64 20 6f 77 6e 65 72 20 3d 20 24 32 22 and owner = $2"
0a10: 29 0a 09 09 09 2e 62 69 6e 64 28 73 6f 75 72 63 ).....bind(sourc
0a20: 65 5f 69 64 29 0a 09 09 09 2e 62 69 6e 64 28 6f e_id).....bind(o
0a30: 77 6e 65 72 29 0a 09 09 09 2e 65 78 65 63 75 74 wner).....execut
0a40: 65 28 26 6d 75 74 20 2a 73 65 6c 66 2e 63 6f 6e e(&mut *self.con
0a50: 6e 29 2e 61 77 61 69 74 3f 2e 72 6f 77 73 5f 61 n).await?.rows_a
0a60: 66 66 65 63 74 65 64 28 29 20 7b 0a 09 09 09 31 ffected() {....1
0a70: 20 3d 3e 20 7b 20 4f 6b 28 22 53 6f 75 72 63 65 => { Ok("Source
0a80: 20 64 69 73 61 62 6c 65 64 2e 22 29 20 7d 2c 0a disabled.") },.
0a90: 09 09 09 30 20 3d 3e 20 7b 20 4f 6b 28 22 53 6f ...0 => { Ok("So
0aa0: 75 72 63 65 20 6e 6f 74 20 66 6f 75 6e 64 2e 22 urce not found."
0ab0: 29 20 7d 2c 0a 09 09 09 5f 20 3d 3e 20 7b 20 62 ) },...._ => { b
0ac0: 61 69 6c 21 28 22 44 61 74 61 62 61 73 65 20 65 ail!("Database e
0ad0: 72 72 6f 72 2e 22 29 20 7d 2c 0a 09 09 7d 0a 09 rror.") },...}..
0ae0: 7d 0a 0a 09 70 75 62 20 61 73 79 6e 63 20 66 6e }...pub async fn
0af0: 20 65 6e 61 62 6c 65 20 28 26 6d 75 74 20 73 65 enable (&mut se
0b00: 6c 66 2c 20 73 6f 75 72 63 65 5f 69 64 3a 20 69 lf, source_id: i
0b10: 33 32 2c 20 6f 77 6e 65 72 3a 20 69 36 34 29 20 32, owner: i64)
0b20: 2d 3e 20 52 65 73 75 6c 74 3c 26 73 74 72 3e 20 -> Result<&str>
0b30: 7b 0a 09 09 6d 61 74 63 68 20 73 71 6c 78 3a 3a {...match sqlx::
0b40: 71 75 65 72 79 28 22 75 70 64 61 74 65 20 72 73 query("update rs
0b50: 73 74 67 5f 73 6f 75 72 63 65 20 73 65 74 20 65 stg_source set e
0b60: 6e 61 62 6c 65 64 20 3d 20 74 72 75 65 20 77 68 nabled = true wh
0b70: 65 72 65 20 73 6f 75 72 63 65 5f 69 64 20 3d 20 ere source_id =
0b80: 24 31 20 61 6e 64 20 6f 77 6e 65 72 20 3d 20 24 $1 and owner = $
0b90: 32 22 29 0a 09 09 09 2e 62 69 6e 64 28 73 6f 75 2").....bind(sou
0ba0: 72 63 65 5f 69 64 29 0a 09 09 09 2e 62 69 6e 64 rce_id).....bind
0bb0: 28 6f 77 6e 65 72 29 0a 09 09 09 2e 65 78 65 63 (owner).....exec
0bc0: 75 74 65 28 26 6d 75 74 20 2a 73 65 6c 66 2e 63 ute(&mut *self.c
0bd0: 6f 6e 6e 29 2e 61 77 61 69 74 3f 2e 72 6f 77 73 onn).await?.rows
0be0: 5f 61 66 66 65 63 74 65 64 28 29 20 7b 0a 09 09 _affected() {...
0bf0: 09 31 20 3d 3e 20 7b 20 4f 6b 28 22 53 6f 75 72 .1 => { Ok("Sour
0c00: 63 65 20 65 6e 61 62 6c 65 64 2e 22 29 20 7d 2c ce enabled.") },
0c10: 0a 09 09 09 30 20 3d 3e 20 7b 20 4f 6b 28 22 53 ....0 => { Ok("S
0c20: 6f 75 72 63 65 20 6e 6f 74 20 66 6f 75 6e 64 2e ource not found.
0c30: 22 29 20 7d 2c 0a 09 09 09 5f 20 3d 3e 20 7b 20 ") },...._ => {
0c40: 62 61 69 6c 21 28 22 44 61 74 61 62 61 73 65 20 bail!("Database
0c50: 65 72 72 6f 72 2e 22 29 20 7d 2c 0a 09 09 7d 0a error.") },...}.
0c60: 09 7d 0a 0a 09 70 75 62 20 61 73 79 6e 63 20 66 .}...pub async f
0c70: 6e 20 65 78 69 73 74 73 20 28 26 6d 75 74 20 73 n exists (&mut s
0c80: 65 6c 66 2c 20 70 6f 73 74 5f 75 72 6c 3a 20 26 elf, post_url: &
0c90: 73 74 72 2c 20 69 64 3a 20 69 33 32 29 20 2d 3e str, id: i32) ->
0ca0: 20 52 65 73 75 6c 74 3c 4f 70 74 69 6f 6e 3c 62 Result<Option<b
0cb0: 6f 6f 6c 3e 3e 20 7b 0a 09 09 6c 65 74 20 72 6f ool>> {...let ro
0cc0: 77 20 3d 20 73 71 6c 78 3a 3a 71 75 65 72 79 28 w = sqlx::query(
0cd0: 22 73 65 6c 65 63 74 20 65 78 69 73 74 73 28 73 "select exists(s
0ce0: 65 6c 65 63 74 20 74 72 75 65 20 66 72 6f 6d 20 elect true from
0cf0: 72 73 73 74 67 5f 70 6f 73 74 20 77 68 65 72 65 rsstg_post where
0d00: 20 75 72 6c 20 3d 20 24 31 20 61 6e 64 20 73 6f url = $1 and so
0d10: 75 72 63 65 5f 69 64 20 3d 20 24 32 29 20 61 73 urce_id = $2) as
0d20: 20 65 78 69 73 74 73 3b 22 29 0a 09 09 09 2e 62 exists;").....b
0d30: 69 6e 64 28 70 6f 73 74 5f 75 72 6c 29 0a 09 09 ind(post_url)...
0d40: 09 2e 62 69 6e 64 28 69 64 29 0a 09 09 09 2e 66 ..bind(id).....f
0d50: 65 74 63 68 5f 6f 6e 65 28 26 6d 75 74 20 2a 73 etch_one(&mut *s
0d60: 65 6c 66 2e 63 6f 6e 6e 29 2e 61 77 61 69 74 3f elf.conn).await?
0d70: 3b 0a 09 09 6c 65 74 20 65 78 69 73 74 73 3a 20 ;...let exists:
0d80: 4f 70 74 69 6f 6e 3c 62 6f 6f 6c 3e 20 3d 20 72 Option<bool> = r
0d90: 6f 77 2e 74 72 79 5f 67 65 74 28 22 65 78 69 73 ow.try_get("exis
0da0: 74 73 22 29 3f 3b 0a 09 09 4f 6b 28 65 78 69 73 ts")?;...Ok(exis
0db0: 74 73 29 0a 09 7d 0a 0a 09 70 75 62 20 61 73 79 ts)..}...pub asy
0dc0: 6e 63 20 66 6e 20 67 65 74 5f 71 75 65 75 65 20 nc fn get_queue
0dd0: 28 26 6d 75 74 20 73 65 6c 66 29 20 2d 3e 20 52 (&mut self) -> R
0de0: 65 73 75 6c 74 3c 56 65 63 3c 51 75 65 75 65 3e esult<Vec<Queue>
0df0: 3e 20 7b 0a 09 09 6c 65 74 20 62 6c 6f 63 6b 3a > {...let block:
0e00: 20 56 65 63 3c 51 75 65 75 65 3e 20 3d 20 73 71 Vec<Queue> = sq
0e10: 6c 78 3a 3a 71 75 65 72 79 5f 61 73 28 22 73 65 lx::query_as("se
0e20: 6c 65 63 74 20 73 6f 75 72 63 65 5f 69 64 2c 20 lect source_id,
0e30: 6e 65 78 74 5f 66 65 74 63 68 2c 20 6f 77 6e 65 next_fetch, owne
0e40: 72 20 66 72 6f 6d 20 72 73 73 74 67 5f 6f 72 64 r from rsstg_ord
0e50: 65 72 20 6e 61 74 75 72 61 6c 20 6c 65 66 74 20 er natural left
0e60: 6a 6f 69 6e 20 72 73 73 74 67 5f 73 6f 75 72 63 join rsstg_sourc
0e70: 65 20 77 68 65 72 65 20 6e 65 78 74 5f 66 65 74 e where next_fet
0e80: 63 68 20 3c 20 6e 6f 77 28 29 20 2b 20 69 6e 74 ch < now() + int
0e90: 65 72 76 61 6c 20 27 31 20 6d 69 6e 75 74 65 27 erval '1 minute'
0ea0: 3b 22 29 0a 09 09 09 2e 66 65 74 63 68 5f 61 6c ;").....fetch_al
0eb0: 6c 28 26 6d 75 74 20 2a 73 65 6c 66 2e 63 6f 6e l(&mut *self.con
0ec0: 6e 29 2e 61 77 61 69 74 3f 3b 0a 09 09 4f 6b 28 n).await?;...Ok(
0ed0: 62 6c 6f 63 6b 29 0a 09 7d 0a 0a 09 70 75 62 20 block)..}...pub
0ee0: 61 73 79 6e 63 20 66 6e 20 67 65 74 5f 6c 69 73 async fn get_lis
0ef0: 74 20 28 26 6d 75 74 20 73 65 6c 66 2c 20 6f 77 t (&mut self, ow
0f00: 6e 65 72 3a 20 69 36 34 29 20 2d 3e 20 52 65 73 ner: i64) -> Res
0f10: 75 6c 74 3c 56 65 63 3c 4c 69 73 74 3e 3e 20 7b ult<Vec<List>> {
0f20: 0a 09 09 6c 65 74 20 73 6f 75 72 63 65 3a 20 56 ...let source: V
0f30: 65 63 3c 4c 69 73 74 3e 20 3d 20 73 71 6c 78 3a ec<List> = sqlx:
0f40: 3a 71 75 65 72 79 5f 61 73 28 22 73 65 6c 65 63 :query_as("selec
0f50: 74 20 73 6f 75 72 63 65 5f 69 64 2c 20 63 68 61 t source_id, cha
0f60: 6e 6e 65 6c 2c 20 65 6e 61 62 6c 65 64 2c 20 75 nnel, enabled, u
0f70: 72 6c 2c 20 69 76 5f 68 61 73 68 2c 20 75 72 6c rl, iv_hash, url
0f80: 5f 72 65 20 66 72 6f 6d 20 72 73 73 74 67 5f 73 _re from rsstg_s
0f90: 6f 75 72 63 65 20 77 68 65 72 65 20 6f 77 6e 65 ource where owne
0fa0: 72 20 3d 20 24 31 20 6f 72 64 65 72 20 62 79 20 r = $1 order by
0fb0: 73 6f 75 72 63 65 5f 69 64 22 29 0a 09 09 09 2e source_id").....
0fc0: 62 69 6e 64 28 6f 77 6e 65 72 29 0a 09 09 09 2e bind(owner).....
0fd0: 66 65 74 63 68 5f 61 6c 6c 28 26 6d 75 74 20 2a fetch_all(&mut *
0fe0: 73 65 6c 66 2e 63 6f 6e 6e 29 2e 61 77 61 69 74 self.conn).await
0ff0: 3f 3b 0a 09 09 4f 6b 28 73 6f 75 72 63 65 29 0a ?;...Ok(source).
1000: 09 7d 0a 0a 09 70 75 62 20 61 73 79 6e 63 20 66 .}...pub async f
1010: 6e 20 67 65 74 5f 73 6f 75 72 63 65 20 28 26 6d n get_source (&m
1020: 75 74 20 73 65 6c 66 2c 20 69 64 3a 20 69 33 32 ut self, id: i32
1030: 2c 20 6f 77 6e 65 72 3a 20 69 36 34 29 20 2d 3e , owner: i64) ->
1040: 20 52 65 73 75 6c 74 3c 53 6f 75 72 63 65 3e 20 Result<Source>
1050: 7b 0a 09 09 6c 65 74 20 73 6f 75 72 63 65 3a 20 {...let source:
1060: 53 6f 75 72 63 65 20 3d 20 73 71 6c 78 3a 3a 71 Source = sqlx::q
1070: 75 65 72 79 5f 61 73 28 22 73 65 6c 65 63 74 20 uery_as("select
1080: 63 68 61 6e 6e 65 6c 5f 69 64 2c 20 75 72 6c 2c channel_id, url,
1090: 20 69 76 5f 68 61 73 68 2c 20 6f 77 6e 65 72 2c iv_hash, owner,
10a0: 20 75 72 6c 5f 72 65 20 66 72 6f 6d 20 72 73 73 url_re from rss
10b0: 74 67 5f 73 6f 75 72 63 65 20 77 68 65 72 65 20 tg_source where
10c0: 73 6f 75 72 63 65 5f 69 64 20 3d 20 24 31 20 61 source_id = $1 a
10d0: 6e 64 20 6f 77 6e 65 72 20 3d 20 24 32 22 29 0a nd owner = $2").
10e0: 09 09 09 2e 62 69 6e 64 28 69 64 29 0a 09 09 09 ....bind(id)....
10f0: 2e 62 69 6e 64 28 6f 77 6e 65 72 29 0a 09 09 09 .bind(owner)....
1100: 2e 66 65 74 63 68 5f 6f 6e 65 28 26 6d 75 74 20 .fetch_one(&mut
1110: 2a 73 65 6c 66 2e 63 6f 6e 6e 29 2e 61 77 61 69 *self.conn).awai
1120: 74 3f 3b 0a 09 09 4f 6b 28 73 6f 75 72 63 65 29 t?;...Ok(source)
1130: 0a 09 7d 0a 0a 09 70 75 62 20 61 73 79 6e 63 20 ..}...pub async
1140: 66 6e 20 73 65 74 5f 73 63 72 61 70 65 20 28 26 fn set_scrape (&
1150: 6d 75 74 20 73 65 6c 66 2c 20 69 64 3a 20 69 33 mut self, id: i3
1160: 32 29 20 2d 3e 20 52 65 73 75 6c 74 3c 28 29 3e 2) -> Result<()>
1170: 20 7b 0a 09 09 73 71 6c 78 3a 3a 71 75 65 72 79 {...sqlx::query
1180: 28 22 75 70 64 61 74 65 20 72 73 73 74 67 5f 73 ("update rsstg_s
1190: 6f 75 72 63 65 20 73 65 74 20 6c 61 73 74 5f 73 ource set last_s
11a0: 63 72 61 70 65 20 3d 20 6e 6f 77 28 29 20 77 68 crape = now() wh
11b0: 65 72 65 20 73 6f 75 72 63 65 5f 69 64 20 3d 20 ere source_id =
11c0: 24 31 3b 22 29 0a 09 09 09 2e 62 69 6e 64 28 69 $1;").....bind(i
11d0: 64 29 0a 09 09 09 2e 65 78 65 63 75 74 65 28 26 d).....execute(&
11e0: 6d 75 74 20 2a 73 65 6c 66 2e 63 6f 6e 6e 29 2e mut *self.conn).
11f0: 61 77 61 69 74 3f 3b 0a 09 09 4f 6b 28 28 29 29 await?;...Ok(())
1200: 0a 09 7d 0a 0a 09 70 75 62 20 61 73 79 6e 63 20 ..}...pub async
1210: 66 6e 20 75 70 64 61 74 65 20 28 26 6d 75 74 20 fn update (&mut
1220: 73 65 6c 66 2c 20 75 70 64 61 74 65 3a 20 4f 70 self, update: Op
1230: 74 69 6f 6e 3c 69 33 32 3e 2c 20 63 68 61 6e 6e tion<i32>, chann
1240: 65 6c 3a 20 26 73 74 72 2c 20 63 68 61 6e 6e 65 el: &str, channe
1250: 6c 5f 69 64 3a 20 69 36 34 2c 20 75 72 6c 3a 20 l_id: i64, url:
1260: 26 73 74 72 2c 20 69 76 5f 68 61 73 68 3a 20 4f &str, iv_hash: O
1270: 70 74 69 6f 6e 3c 26 73 74 72 3e 2c 20 75 72 6c ption<&str>, url
1280: 5f 72 65 3a 20 4f 70 74 69 6f 6e 3c 26 73 74 72 _re: Option<&str
1290: 3e 2c 20 6f 77 6e 65 72 3a 20 69 36 34 29 20 2d >, owner: i64) -
12a0: 3e 20 52 65 73 75 6c 74 3c 26 73 74 72 3e 20 7b > Result<&str> {
12b0: 0a 09 09 6d 61 74 63 68 20 6d 61 74 63 68 20 75 ...match match u
12c0: 70 64 61 74 65 20 7b 0a 09 09 09 09 53 6f 6d 65 pdate {.....Some
12d0: 28 69 64 29 20 3d 3e 20 7b 0a 09 09 09 09 09 73 (id) => {......s
12e0: 71 6c 78 3a 3a 71 75 65 72 79 28 22 75 70 64 61 qlx::query("upda
12f0: 74 65 20 72 73 73 74 67 5f 73 6f 75 72 63 65 20 te rsstg_source
1300: 73 65 74 20 63 68 61 6e 6e 65 6c 5f 69 64 20 3d set channel_id =
1310: 20 24 32 2c 20 75 72 6c 20 3d 20 24 33 2c 20 69 $2, url = $3, i
1320: 76 5f 68 61 73 68 20 3d 20 24 34 2c 20 6f 77 6e v_hash = $4, own
1330: 65 72 20 3d 20 24 35 2c 20 63 68 61 6e 6e 65 6c er = $5, channel
1340: 20 3d 20 24 36 2c 20 75 72 6c 5f 72 65 20 3d 20 = $6, url_re =
1350: 24 37 20 77 68 65 72 65 20 73 6f 75 72 63 65 5f $7 where source_
1360: 69 64 20 3d 20 24 31 22 29 0a 09 09 09 09 09 09 id = $1").......
1370: 2e 62 69 6e 64 28 69 64 29 0a 09 09 09 09 7d 2c .bind(id).....},
1380: 0a 09 09 09 09 4e 6f 6e 65 20 3d 3e 20 7b 0a 09 .....None => {..
1390: 09 09 09 09 73 71 6c 78 3a 3a 71 75 65 72 79 28 ....sqlx::query(
13a0: 22 69 6e 73 65 72 74 20 69 6e 74 6f 20 72 73 73 "insert into rss
13b0: 74 67 5f 73 6f 75 72 63 65 20 28 63 68 61 6e 6e tg_source (chann
13c0: 65 6c 5f 69 64 2c 20 75 72 6c 2c 20 69 76 5f 68 el_id, url, iv_h
13d0: 61 73 68 2c 20 6f 77 6e 65 72 2c 20 63 68 61 6e ash, owner, chan
13e0: 6e 65 6c 2c 20 75 72 6c 5f 72 65 29 20 76 61 6c nel, url_re) val
13f0: 75 65 73 20 28 24 31 2c 20 24 32 2c 20 24 33 2c ues ($1, $2, $3,
1400: 20 24 34 2c 20 24 35 2c 20 24 36 29 22 29 0a 09 $4, $5, $6)")..
1410: 09 09 09 7d 2c 0a 09 09 09 7d 0a 09 09 09 09 2e ...},....}......
1420: 62 69 6e 64 28 63 68 61 6e 6e 65 6c 5f 69 64 29 bind(channel_id)
1430: 0a 09 09 09 09 2e 62 69 6e 64 28 75 72 6c 29 0a ......bind(url).
1440: 09 09 09 09 2e 62 69 6e 64 28 69 76 5f 68 61 73 .....bind(iv_has
1450: 68 29 0a 09 09 09 09 2e 62 69 6e 64 28 6f 77 6e h)......bind(own
1460: 65 72 29 0a 09 09 09 09 2e 62 69 6e 64 28 63 68 er)......bind(ch
1470: 61 6e 6e 65 6c 29 0a 09 09 09 09 2e 62 69 6e 64 annel)......bind
1480: 28 75 72 6c 5f 72 65 29 0a 09 09 09 09 2e 65 78 (url_re)......ex
1490: 65 63 75 74 65 28 26 6d 75 74 20 2a 73 65 6c 66 ecute(&mut *self
14a0: 2e 63 6f 6e 6e 29 2e 61 77 61 69 74 0a 09 09 09 .conn).await....
14b0: 7b 0a 09 09 09 4f 6b 28 5f 29 20 3d 3e 20 4f 6b {....Ok(_) => Ok
14c0: 28 6d 61 74 63 68 20 75 70 64 61 74 65 20 7b 0a (match update {.
14d0: 09 09 09 09 53 6f 6d 65 28 5f 29 20 3d 3e 20 22 ....Some(_) => "
14e0: 43 68 61 6e 6e 65 6c 20 75 70 64 61 74 65 64 2e Channel updated.
14f0: 22 2c 0a 09 09 09 09 4e 6f 6e 65 20 3d 3e 20 22 ",.....None => "
1500: 43 68 61 6e 6e 65 6c 20 61 64 64 65 64 2e 22 2c Channel added.",
1510: 0a 09 09 09 7d 29 2c 0a 09 09 09 45 72 72 28 73 ....}),....Err(s
1520: 71 6c 78 3a 3a 45 72 72 6f 72 3a 3a 44 61 74 61 qlx::Error::Data
1530: 62 61 73 65 28 65 72 72 29 29 20 3d 3e 20 7b 0a base(err)) => {.
1540: 09 09 09 09 6d 61 74 63 68 20 65 72 72 2e 64 6f ....match err.do
1550: 77 6e 63 61 73 74 3a 3a 3c 73 71 6c 78 3a 3a 70 wncast::<sqlx::p
1560: 6f 73 74 67 72 65 73 3a 3a 50 67 44 61 74 61 62 ostgres::PgDatab
1570: 61 73 65 45 72 72 6f 72 3e 28 29 2e 72 6f 75 74 aseError>().rout
1580: 69 6e 65 28 29 20 7b 0a 09 09 09 09 09 53 6f 6d ine() {......Som
1590: 65 28 22 5f 62 74 5f 63 68 65 63 6b 5f 75 6e 69 e("_bt_check_uni
15a0: 71 75 65 22 2c 20 29 20 3d 3e 20 7b 0a 09 09 09 que", ) => {....
15b0: 09 09 09 4f 6b 28 22 44 75 70 6c 69 63 61 74 65 ...Ok("Duplicate
15c0: 20 6b 65 79 2e 22 29 0a 09 09 09 09 09 7d 2c 0a key.")......},.
15d0: 09 09 09 09 09 53 6f 6d 65 28 5f 29 20 3d 3e 20 .....Some(_) =>
15e0: 7b 0a 09 09 09 09 09 09 4f 6b 28 22 44 61 74 61 {.......Ok("Data
15f0: 62 61 73 65 20 65 72 72 6f 72 2e 22 29 0a 09 09 base error.")...
1600: 09 09 09 7d 2c 0a 09 09 09 09 09 4e 6f 6e 65 20 ...},......None
1610: 3d 3e 20 7b 0a 09 09 09 09 09 09 4f 6b 28 22 4e => {.......Ok("N
1620: 6f 20 64 61 74 61 62 61 73 65 20 65 72 72 6f 72 o database error
1630: 20 65 78 74 72 61 63 74 65 64 2e 22 29 0a 09 09 extracted.")...
1640: 09 09 09 7d 2c 0a 09 09 09 09 7d 0a 09 09 09 7d ...},.....}....}
1650: 2c 0a 09 09 09 45 72 72 28 65 72 72 29 20 3d 3e ,....Err(err) =>
1660: 20 7b 0a 09 09 09 09 62 61 69 6c 21 28 22 53 6f {.....bail!("So
1670: 72 72 79 2c 20 75 6e 6b 6e 6f 77 6e 20 65 72 72 rry, unknown err
1680: 6f 72 3a 5c 6e 7b 65 72 72 3a 23 3f 7d 5c 6e 22 or:\n{err:#?}\n"
1690: 29 3b 0a 09 09 09 7d 2c 0a 09 09 7d 0a 09 7d 0a );....},...}..}.
16a0: 7d 0a }.