0000: 75 73 65 20 61 6e 79 68 6f 77 3a 3a 7b 61 6e 79 use anyhow::{any
0010: 68 6f 77 2c 20 62 61 69 6c 2c 20 43 6f 6e 74 65 how, bail, Conte
0020: 78 74 2c 20 52 65 73 75 6c 74 7d 3b 0a 75 73 65 xt, Result};.use
0030: 20 61 74 6f 6d 5f 73 79 6e 64 69 63 61 74 69 6f atom_syndicatio
0040: 6e 3b 0a 75 73 65 20 63 68 72 6f 6e 6f 3a 3a 44 n;.use chrono::D
0050: 61 74 65 54 69 6d 65 3b 0a 75 73 65 20 63 6f 6e ateTime;.use con
0060: 66 69 67 3b 0a 75 73 65 20 72 65 71 77 65 73 74 fig;.use reqwest
0070: 3b 0a 75 73 65 20 73 71 6c 78 3a 3a 7b 0a 09 70 ;.use sqlx::{..p
0080: 6f 73 74 67 72 65 73 3a 3a 50 67 50 6f 6f 6c 4f ostgres::PgPoolO
0090: 70 74 69 6f 6e 73 2c 0a 09 52 6f 77 2c 0a 7d 3b ptions,..Row,.};
00a0: 0a 75 73 65 20 72 73 73 3b 0a 75 73 65 20 73 74 .use rss;.use st
00b0: 64 3a 3a 7b 0a 09 62 6f 72 72 6f 77 3a 3a 43 6f d::{..borrow::Co
00c0: 77 2c 0a 09 63 6f 6c 6c 65 63 74 69 6f 6e 73 3a w,..collections:
00d0: 3a 7b 0a 09 09 42 54 72 65 65 4d 61 70 2c 0a 09 :{...BTreeMap,..
00e0: 09 48 61 73 68 53 65 74 2c 0a 09 7d 2c 0a 09 73 .HashSet,..},..s
00f0: 79 6e 63 3a 3a 7b 41 72 63 2c 20 4d 75 74 65 78 ync::{Arc, Mutex
0100: 7d 2c 0a 7d 3b 0a 75 73 65 20 74 65 6c 65 67 72 },.};.use telegr
0110: 61 6d 5f 62 6f 74 3b 0a 0a 23 5b 64 65 72 69 76 am_bot;..#[deriv
0120: 65 28 43 6c 6f 6e 65 29 5d 0a 70 75 62 20 73 74 e(Clone)].pub st
0130: 72 75 63 74 20 43 6f 72 65 20 7b 0a 09 6f 77 6e ruct Core {..own
0140: 65 72 3a 20 69 36 34 2c 0a 09 61 70 69 5f 6b 65 er: i64,..api_ke
0150: 79 3a 20 53 74 72 69 6e 67 2c 0a 09 6f 77 6e 65 y: String,..owne
0160: 72 5f 63 68 61 74 3a 20 74 65 6c 65 67 72 61 6d r_chat: telegram
0170: 5f 62 6f 74 3a 3a 55 73 65 72 49 64 2c 0a 09 70 _bot::UserId,..p
0180: 75 62 20 74 67 3a 20 74 65 6c 65 67 72 61 6d 5f ub tg: telegram_
0190: 62 6f 74 3a 3a 41 70 69 2c 0a 09 70 75 62 20 6d bot::Api,..pub m
01a0: 79 3a 20 74 65 6c 65 67 72 61 6d 5f 62 6f 74 3a y: telegram_bot:
01b0: 3a 55 73 65 72 2c 0a 09 70 6f 6f 6c 3a 20 73 71 :User,..pool: sq
01c0: 6c 78 3a 3a 50 6f 6f 6c 3c 73 71 6c 78 3a 3a 50 lx::Pool<sqlx::P
01d0: 6f 73 74 67 72 65 73 3e 2c 0a 09 73 6f 75 72 63 ostgres>,..sourc
01e0: 65 73 3a 20 41 72 63 3c 4d 75 74 65 78 3c 48 61 es: Arc<Mutex<Ha
01f0: 73 68 53 65 74 3c 41 72 63 3c 69 33 32 3e 3e 3e shSet<Arc<i32>>>
0200: 3e 2c 0a 7d 0a 0a 69 6d 70 6c 20 43 6f 72 65 20 >,.}..impl Core
0210: 7b 0a 09 70 75 62 20 61 73 79 6e 63 20 66 6e 20 {..pub async fn
0220: 6e 65 77 28 73 65 74 74 69 6e 67 73 3a 20 63 6f new(settings: co
0230: 6e 66 69 67 3a 3a 43 6f 6e 66 69 67 29 20 2d 3e nfig::Config) ->
0240: 20 52 65 73 75 6c 74 3c 43 6f 72 65 3e 20 7b 0a Result<Core> {.
0250: 09 09 6c 65 74 20 6f 77 6e 65 72 20 3d 20 73 65 ..let owner = se
0260: 74 74 69 6e 67 73 2e 67 65 74 5f 69 6e 74 28 22 ttings.get_int("
0270: 6f 77 6e 65 72 22 29 3f 3b 0a 09 09 6c 65 74 20 owner")?;...let
0280: 61 70 69 5f 6b 65 79 20 3d 20 73 65 74 74 69 6e api_key = settin
0290: 67 73 2e 67 65 74 5f 73 74 72 28 22 61 70 69 5f gs.get_str("api_
02a0: 6b 65 79 22 29 3f 3b 0a 09 09 6c 65 74 20 74 67 key")?;...let tg
02b0: 20 3d 20 74 65 6c 65 67 72 61 6d 5f 62 6f 74 3a = telegram_bot:
02c0: 3a 41 70 69 3a 3a 6e 65 77 28 26 61 70 69 5f 6b :Api::new(&api_k
02d0: 65 79 29 3b 0a 09 09 6c 65 74 20 63 6f 72 65 20 ey);...let core
02e0: 3d 20 43 6f 72 65 20 7b 0a 09 09 09 6f 77 6e 65 = Core {....owne
02f0: 72 3a 20 6f 77 6e 65 72 2c 0a 09 09 09 61 70 69 r: owner,....api
0300: 5f 6b 65 79 3a 20 61 70 69 5f 6b 65 79 2e 63 6c _key: api_key.cl
0310: 6f 6e 65 28 29 2c 0a 09 09 09 6d 79 3a 20 74 67 one(),....my: tg
0320: 2e 73 65 6e 64 28 74 65 6c 65 67 72 61 6d 5f 62 .send(telegram_b
0330: 6f 74 3a 3a 47 65 74 4d 65 29 2e 61 77 61 69 74 ot::GetMe).await
0340: 3f 2c 0a 09 09 09 74 67 3a 20 74 67 2c 0a 09 09 ?,....tg: tg,...
0350: 09 6f 77 6e 65 72 5f 63 68 61 74 3a 20 74 65 6c .owner_chat: tel
0360: 65 67 72 61 6d 5f 62 6f 74 3a 3a 55 73 65 72 49 egram_bot::UserI
0370: 64 3a 3a 6e 65 77 28 6f 77 6e 65 72 29 2c 0a 09 d::new(owner),..
0380: 09 09 70 6f 6f 6c 3a 20 50 67 50 6f 6f 6c 4f 70 ..pool: PgPoolOp
0390: 74 69 6f 6e 73 3a 3a 6e 65 77 28 29 0a 09 09 09 tions::new()....
03a0: 09 2e 6d 61 78 5f 63 6f 6e 6e 65 63 74 69 6f 6e ..max_connection
03b0: 73 28 35 29 0a 09 09 09 09 2e 63 6f 6e 6e 65 63 s(5)......connec
03c0: 74 5f 74 69 6d 65 6f 75 74 28 73 74 64 3a 3a 74 t_timeout(std::t
03d0: 69 6d 65 3a 3a 44 75 72 61 74 69 6f 6e 3a 3a 6e ime::Duration::n
03e0: 65 77 28 33 30 30 2c 20 30 29 29 0a 09 09 09 09 ew(300, 0)).....
03f0: 2e 69 64 6c 65 5f 74 69 6d 65 6f 75 74 28 73 74 .idle_timeout(st
0400: 64 3a 3a 74 69 6d 65 3a 3a 44 75 72 61 74 69 6f d::time::Duratio
0410: 6e 3a 3a 6e 65 77 28 36 30 2c 20 30 29 29 0a 09 n::new(60, 0))..
0420: 09 09 09 2e 63 6f 6e 6e 65 63 74 5f 6c 61 7a 79 ....connect_lazy
0430: 28 26 73 65 74 74 69 6e 67 73 2e 67 65 74 5f 73 (&settings.get_s
0440: 74 72 28 22 70 67 22 29 3f 29 3f 2c 0a 09 09 09 tr("pg")?)?,....
0450: 73 6f 75 72 63 65 73 3a 20 41 72 63 3a 3a 6e 65 sources: Arc::ne
0460: 77 28 4d 75 74 65 78 3a 3a 6e 65 77 28 48 61 73 w(Mutex::new(Has
0470: 68 53 65 74 3a 3a 6e 65 77 28 29 29 29 2c 0a 09 hSet::new())),..
0480: 09 7d 3b 0a 09 09 6c 65 74 20 63 6c 6f 6e 65 20 .};...let clone
0490: 3d 20 63 6f 72 65 2e 63 6c 6f 6e 65 28 29 3b 0a = core.clone();.
04a0: 09 09 74 6f 6b 69 6f 3a 3a 73 70 61 77 6e 28 61 ..tokio::spawn(a
04b0: 73 79 6e 63 20 6d 6f 76 65 20 7b 0a 09 09 09 69 sync move {....i
04c0: 66 20 6c 65 74 20 45 72 72 28 65 72 72 29 20 3d f let Err(err) =
04d0: 20 26 63 6c 6f 6e 65 2e 61 75 74 6f 66 65 74 63 &clone.autofetc
04e0: 68 28 29 2e 61 77 61 69 74 20 7b 0a 09 09 09 09 h().await {.....
04f0: 69 66 20 6c 65 74 20 45 72 72 28 65 72 72 29 20 if let Err(err)
0500: 3d 20 63 6c 6f 6e 65 2e 73 65 6e 64 28 26 66 6f = clone.send(&fo
0510: 72 6d 61 74 21 28 22 f0 9f 9b 91 20 7b 3a 3f 7d rmat!("š {:?}
0520: 22 2c 20 65 72 72 29 2c 20 4e 6f 6e 65 2c 20 4e ", err), None, N
0530: 6f 6e 65 29 20 7b 0a 09 09 09 09 09 65 70 72 69 one) {......epri
0540: 6e 74 6c 6e 21 28 22 41 75 74 6f 66 65 74 63 68 ntln!("Autofetch
0550: 20 65 72 72 6f 72 3a 20 7b 7d 22 2c 20 65 72 72 error: {}", err
0560: 29 3b 0a 09 09 09 09 7d 3b 0a 09 09 09 7d 0a 09 );.....};....}..
0570: 09 7d 29 3b 0a 09 09 4f 6b 28 63 6f 72 65 29 0a .});...Ok(core).
0580: 09 7d 0a 0a 09 70 75 62 20 66 6e 20 73 74 72 65 .}...pub fn stre
0590: 61 6d 28 26 73 65 6c 66 29 20 2d 3e 20 74 65 6c am(&self) -> tel
05a0: 65 67 72 61 6d 5f 62 6f 74 3a 3a 55 70 64 61 74 egram_bot::Updat
05b0: 65 73 53 74 72 65 61 6d 20 7b 0a 09 09 73 65 6c esStream {...sel
05c0: 66 2e 74 67 2e 73 74 72 65 61 6d 28 29 0a 09 7d f.tg.stream()..}
05d0: 0a 0a 09 70 75 62 20 66 6e 20 73 65 6e 64 3c 27 ...pub fn send<'
05e0: 61 2c 20 53 3e 28 26 73 65 6c 66 2c 20 6d 73 67 a, S>(&self, msg
05f0: 3a 20 53 2c 20 74 61 72 67 65 74 3a 20 4f 70 74 : S, target: Opt
0600: 69 6f 6e 3c 74 65 6c 65 67 72 61 6d 5f 62 6f 74 ion<telegram_bot
0610: 3a 3a 55 73 65 72 49 64 3e 2c 20 70 61 72 73 65 ::UserId>, parse
0620: 5f 6d 6f 64 65 3a 20 4f 70 74 69 6f 6e 3c 74 65 _mode: Option<te
0630: 6c 65 67 72 61 6d 5f 62 6f 74 3a 3a 74 79 70 65 legram_bot::type
0640: 73 3a 3a 50 61 72 73 65 4d 6f 64 65 3e 29 20 2d s::ParseMode>) -
0650: 3e 20 52 65 73 75 6c 74 3c 28 29 3e 0a 09 77 68 > Result<()>..wh
0660: 65 72 65 20 53 3a 20 49 6e 74 6f 3c 43 6f 77 3c ere S: Into<Cow<
0670: 27 61 2c 20 73 74 72 3e 3e 20 7b 0a 09 09 6c 65 'a, str>> {...le
0680: 74 20 6d 73 67 20 3d 20 6d 73 67 2e 69 6e 74 6f t msg = msg.into
0690: 28 29 3b 0a 0a 09 09 6c 65 74 20 70 61 72 73 65 ();....let parse
06a0: 5f 6d 6f 64 65 20 3d 20 6d 61 74 63 68 20 70 61 _mode = match pa
06b0: 72 73 65 5f 6d 6f 64 65 20 7b 0a 09 09 09 53 6f rse_mode {....So
06c0: 6d 65 28 6d 6f 64 65 29 20 3d 3e 20 6d 6f 64 65 me(mode) => mode
06d0: 2c 0a 09 09 09 4e 6f 6e 65 20 3d 3e 20 74 65 6c ,....None => tel
06e0: 65 67 72 61 6d 5f 62 6f 74 3a 3a 74 79 70 65 73 egram_bot::types
06f0: 3a 3a 50 61 72 73 65 4d 6f 64 65 3a 3a 48 74 6d ::ParseMode::Htm
0700: 6c 2c 0a 09 09 7d 3b 0a 09 09 73 65 6c 66 2e 74 l,...};...self.t
0710: 67 2e 73 70 61 77 6e 28 74 65 6c 65 67 72 61 6d g.spawn(telegram
0720: 5f 62 6f 74 3a 3a 53 65 6e 64 4d 65 73 73 61 67 _bot::SendMessag
0730: 65 3a 3a 6e 65 77 28 6d 61 74 63 68 20 74 61 72 e::new(match tar
0740: 67 65 74 20 7b 0a 09 09 09 53 6f 6d 65 28 75 73 get {....Some(us
0750: 65 72 29 20 3d 3e 20 75 73 65 72 2c 0a 09 09 09 er) => user,....
0760: 4e 6f 6e 65 20 3d 3e 20 73 65 6c 66 2e 6f 77 6e None => self.own
0770: 65 72 5f 63 68 61 74 2c 0a 09 09 7d 2c 20 6d 73 er_chat,...}, ms
0780: 67 29 2e 70 61 72 73 65 5f 6d 6f 64 65 28 70 61 g).parse_mode(pa
0790: 72 73 65 5f 6d 6f 64 65 29 29 3b 0a 09 09 4f 6b rse_mode));...Ok
07a0: 28 28 29 29 0a 09 7d 0a 0a 09 70 75 62 20 61 73 (())..}...pub as
07b0: 79 6e 63 20 66 6e 20 63 68 65 63 6b 3c 53 3e 28 ync fn check<S>(
07c0: 26 73 65 6c 66 2c 20 69 64 3a 20 26 69 33 32 2c &self, id: &i32,
07d0: 20 6f 77 6e 65 72 3a 20 53 2c 20 72 65 61 6c 3a owner: S, real:
07e0: 20 62 6f 6f 6c 29 20 2d 3e 20 52 65 73 75 6c 74 bool) -> Result
07f0: 3c 53 74 72 69 6e 67 3e 0a 09 77 68 65 72 65 20 <String>..where
0800: 53 3a 20 49 6e 74 6f 3c 69 36 34 3e 20 7b 0a 09 S: Into<i64> {..
0810: 09 6c 65 74 20 6f 77 6e 65 72 20 3d 20 6f 77 6e .let owner = own
0820: 65 72 2e 69 6e 74 6f 28 29 3b 0a 0a 09 09 6c 65 er.into();....le
0830: 74 20 6d 75 74 20 70 6f 73 74 65 64 3a 20 69 33 t mut posted: i3
0840: 32 20 3d 20 30 3b 0a 09 09 6c 65 74 20 69 64 20 2 = 0;...let id
0850: 3d 20 7b 0a 09 09 09 6c 65 74 20 6d 75 74 20 73 = {....let mut s
0860: 65 74 20 3d 20 73 65 6c 66 2e 73 6f 75 72 63 65 et = self.source
0870: 73 2e 6c 6f 63 6b 28 29 2e 75 6e 77 72 61 70 28 s.lock().unwrap(
0880: 29 3b 0a 09 09 09 6d 61 74 63 68 20 73 65 74 2e );....match set.
0890: 67 65 74 28 69 64 29 20 7b 0a 09 09 09 09 53 6f get(id) {.....So
08a0: 6d 65 28 69 64 29 20 3d 3e 20 69 64 2e 63 6c 6f me(id) => id.clo
08b0: 6e 65 28 29 2c 0a 09 09 09 09 4e 6f 6e 65 20 3d ne(),.....None =
08c0: 3e 20 7b 0a 09 09 09 09 09 6c 65 74 20 69 64 20 > {......let id
08d0: 3d 20 41 72 63 3a 3a 6e 65 77 28 2a 69 64 29 3b = Arc::new(*id);
08e0: 0a 09 09 09 09 09 73 65 74 2e 69 6e 73 65 72 74 ......set.insert
08f0: 28 69 64 2e 63 6c 6f 6e 65 28 29 29 3b 0a 09 09 (id.clone());...
0900: 09 09 09 69 64 2e 63 6c 6f 6e 65 28 29 0a 09 09 ...id.clone()...
0910: 09 09 7d 2c 0a 09 09 09 7d 0a 09 09 7d 3b 0a 09 ..},....}...};..
0920: 09 6c 65 74 20 63 6f 75 6e 74 20 3d 20 41 72 63 .let count = Arc
0930: 3a 3a 73 74 72 6f 6e 67 5f 63 6f 75 6e 74 28 26 ::strong_count(&
0940: 69 64 29 3b 0a 09 09 69 66 20 63 6f 75 6e 74 20 id);...if count
0950: 3d 3d 20 32 20 7b 0a 09 09 09 6c 65 74 20 6d 75 == 2 {....let mu
0960: 74 20 63 6f 6e 6e 20 3d 20 73 65 6c 66 2e 70 6f t conn = self.po
0970: 6f 6c 2e 61 63 71 75 69 72 65 28 29 2e 61 77 61 ol.acquire().awa
0980: 69 74 0a 09 09 09 09 2e 77 69 74 68 5f 63 6f 6e it......with_con
0990: 74 65 78 74 28 7c 7c 20 66 6f 72 6d 61 74 21 28 text(|| format!(
09a0: 22 51 75 65 72 79 20 71 75 65 75 65 20 66 65 74 "Query queue fet
09b0: 63 68 20 63 6f 6e 6e 3a 5c 6e 7b 3a 3f 7d 22 2c ch conn:\n{:?}",
09c0: 20 26 73 65 6c 66 2e 70 6f 6f 6c 29 29 3f 3b 0a &self.pool))?;.
09d0: 09 09 09 6c 65 74 20 72 6f 77 20 3d 20 73 71 6c ...let row = sql
09e0: 78 3a 3a 71 75 65 72 79 28 22 73 65 6c 65 63 74 x::query("select
09f0: 20 73 6f 75 72 63 65 5f 69 64 2c 20 63 68 61 6e source_id, chan
0a00: 6e 65 6c 5f 69 64 2c 20 75 72 6c 2c 20 69 76 5f nel_id, url, iv_
0a10: 68 61 73 68 2c 20 6f 77 6e 65 72 2c 20 75 72 6c hash, owner, url
0a20: 5f 72 65 20 66 72 6f 6d 20 72 73 73 74 67 5f 73 _re from rsstg_s
0a30: 6f 75 72 63 65 20 77 68 65 72 65 20 73 6f 75 72 ource where sour
0a40: 63 65 5f 69 64 20 3d 20 24 31 20 61 6e 64 20 6f ce_id = $1 and o
0a50: 77 6e 65 72 20 3d 20 24 32 22 29 0a 09 09 09 09 wner = $2").....
0a60: 2e 62 69 6e 64 28 2a 69 64 29 0a 09 09 09 09 2e .bind(*id)......
0a70: 62 69 6e 64 28 6f 77 6e 65 72 29 0a 09 09 09 09 bind(owner).....
0a80: 2e 66 65 74 63 68 5f 6f 6e 65 28 26 6d 75 74 20 .fetch_one(&mut
0a90: 63 6f 6e 6e 29 2e 61 77 61 69 74 0a 09 09 09 09 conn).await.....
0aa0: 2e 77 69 74 68 5f 63 6f 6e 74 65 78 74 28 7c 7c .with_context(||
0ab0: 20 66 6f 72 6d 61 74 21 28 22 51 75 65 72 79 20 format!("Query
0ac0: 73 6f 75 72 63 65 3a 5c 6e 7b 3a 3f 7d 22 2c 20 source:\n{:?}",
0ad0: 26 73 65 6c 66 2e 70 6f 6f 6c 29 29 3f 3b 0a 09 &self.pool))?;..
0ae0: 09 09 64 72 6f 70 28 63 6f 6e 6e 29 3b 0a 09 09 ..drop(conn);...
0af0: 09 6c 65 74 20 63 68 61 6e 6e 65 6c 5f 69 64 3a .let channel_id:
0b00: 20 69 36 34 20 3d 20 72 6f 77 2e 74 72 79 5f 67 i64 = row.try_g
0b10: 65 74 28 22 63 68 61 6e 6e 65 6c 5f 69 64 22 29 et("channel_id")
0b20: 3f 3b 0a 09 09 09 6c 65 74 20 75 72 6c 3a 20 26 ?;....let url: &
0b30: 73 74 72 20 3d 20 72 6f 77 2e 74 72 79 5f 67 65 str = row.try_ge
0b40: 74 28 22 75 72 6c 22 29 3f 3b 0a 09 09 09 6c 65 t("url")?;....le
0b50: 74 20 69 76 5f 68 61 73 68 3a 20 4f 70 74 69 6f t iv_hash: Optio
0b60: 6e 3c 26 73 74 72 3e 20 3d 20 72 6f 77 2e 74 72 n<&str> = row.tr
0b70: 79 5f 67 65 74 28 22 69 76 5f 68 61 73 68 22 29 y_get("iv_hash")
0b80: 3f 3b 0a 09 09 09 6c 65 74 20 75 72 6c 5f 72 65 ?;....let url_re
0b90: 20 3d 20 6d 61 74 63 68 20 72 6f 77 2e 74 72 79 = match row.try
0ba0: 5f 67 65 74 28 22 75 72 6c 5f 72 65 22 29 3f 20 _get("url_re")?
0bb0: 7b 0a 09 09 09 09 53 6f 6d 65 28 78 29 20 3d 3e {.....Some(x) =>
0bc0: 20 53 6f 6d 65 28 73 65 64 72 65 67 65 78 3a 3a Some(sedregex::
0bd0: 52 65 70 6c 61 63 65 43 6f 6d 6d 61 6e 64 3a 3a ReplaceCommand::
0be0: 6e 65 77 28 78 29 3f 29 2c 0a 09 09 09 09 4e 6f new(x)?),.....No
0bf0: 6e 65 20 3d 3e 20 4e 6f 6e 65 2c 0a 09 09 09 7d ne => None,....}
0c00: 3b 0a 09 09 09 6c 65 74 20 64 65 73 74 69 6e 61 ;....let destina
0c10: 74 69 6f 6e 20 3d 20 6d 61 74 63 68 20 72 65 61 tion = match rea
0c20: 6c 20 7b 0a 09 09 09 09 74 72 75 65 20 3d 3e 20 l {.....true =>
0c30: 74 65 6c 65 67 72 61 6d 5f 62 6f 74 3a 3a 55 73 telegram_bot::Us
0c40: 65 72 49 64 3a 3a 6e 65 77 28 63 68 61 6e 6e 65 erId::new(channe
0c50: 6c 5f 69 64 29 2c 0a 09 09 09 09 66 61 6c 73 65 l_id),.....false
0c60: 20 3d 3e 20 74 65 6c 65 67 72 61 6d 5f 62 6f 74 => telegram_bot
0c70: 3a 3a 55 73 65 72 49 64 3a 3a 6e 65 77 28 72 6f ::UserId::new(ro
0c80: 77 2e 74 72 79 5f 67 65 74 28 22 6f 77 6e 65 72 w.try_get("owner
0c90: 22 29 3f 29 2c 0a 09 09 09 7d 3b 0a 09 09 09 6c ")?),....};....l
0ca0: 65 74 20 6d 75 74 20 74 68 69 73 5f 66 65 74 63 et mut this_fetc
0cb0: 68 3a 20 4f 70 74 69 6f 6e 3c 44 61 74 65 54 69 h: Option<DateTi
0cc0: 6d 65 3c 63 68 72 6f 6e 6f 3a 3a 46 69 78 65 64 me<chrono::Fixed
0cd0: 4f 66 66 73 65 74 3e 3e 20 3d 20 4e 6f 6e 65 3b Offset>> = None;
0ce0: 0a 09 09 09 6c 65 74 20 6d 75 74 20 70 6f 73 74 ....let mut post
0cf0: 73 3a 20 42 54 72 65 65 4d 61 70 3c 44 61 74 65 s: BTreeMap<Date
0d00: 54 69 6d 65 3c 63 68 72 6f 6e 6f 3a 3a 46 69 78 Time<chrono::Fix
0d10: 65 64 4f 66 66 73 65 74 3e 2c 20 53 74 72 69 6e edOffset>, Strin
0d20: 67 3e 20 3d 20 42 54 72 65 65 4d 61 70 3a 3a 6e g> = BTreeMap::n
0d30: 65 77 28 29 3b 0a 09 09 09 6c 65 74 20 72 65 73 ew();....let res
0d40: 70 6f 6e 73 65 20 3d 20 72 65 71 77 65 73 74 3a ponse = reqwest:
0d50: 3a 67 65 74 28 75 72 6c 29 2e 61 77 61 69 74 3f :get(url).await?
0d60: 3b 0a 09 09 09 6c 65 74 20 73 74 61 74 75 73 20 ;....let status
0d70: 3d 20 72 65 73 70 6f 6e 73 65 2e 73 74 61 74 75 = response.statu
0d80: 73 28 29 3b 0a 09 09 09 6c 65 74 20 63 6f 6e 74 s();....let cont
0d90: 65 6e 74 20 3d 20 72 65 73 70 6f 6e 73 65 2e 62 ent = response.b
0da0: 79 74 65 73 28 29 2e 61 77 61 69 74 3f 3b 0a 09 ytes().await?;..
0db0: 09 09 6d 61 74 63 68 20 72 73 73 3a 3a 43 68 61 ..match rss::Cha
0dc0: 6e 6e 65 6c 3a 3a 72 65 61 64 5f 66 72 6f 6d 28 nnel::read_from(
0dd0: 26 63 6f 6e 74 65 6e 74 5b 2e 2e 5d 29 20 7b 0a &content[..]) {.
0de0: 09 09 09 09 4f 6b 28 66 65 65 64 29 20 3d 3e 20 ....Ok(feed) =>
0df0: 7b 0a 09 09 09 09 09 66 6f 72 20 69 74 65 6d 20 {......for item
0e00: 69 6e 20 66 65 65 64 2e 69 74 65 6d 73 28 29 20 in feed.items()
0e10: 7b 0a 09 09 09 09 09 09 6d 61 74 63 68 20 69 74 {.......match it
0e20: 65 6d 2e 6c 69 6e 6b 28 29 20 7b 0a 09 09 09 09 em.link() {.....
0e30: 09 09 09 53 6f 6d 65 28 6c 69 6e 6b 29 20 3d 3e ...Some(link) =>
0e40: 20 7b 0a 09 09 09 09 09 09 09 09 6c 65 74 20 64 {.........let d
0e50: 61 74 65 20 3d 20 6d 61 74 63 68 20 69 74 65 6d ate = match item
0e60: 2e 70 75 62 5f 64 61 74 65 28 29 20 7b 0a 09 09 .pub_date() {...
0e70: 09 09 09 09 09 09 09 53 6f 6d 65 28 66 65 65 64 .......Some(feed
0e80: 5f 64 61 74 65 29 20 3d 3e 20 44 61 74 65 54 69 _date) => DateTi
0e90: 6d 65 3a 3a 70 61 72 73 65 5f 66 72 6f 6d 5f 72 me::parse_from_r
0ea0: 66 63 32 38 32 32 28 66 65 65 64 5f 64 61 74 65 fc2822(feed_date
0eb0: 29 2c 0a 09 09 09 09 09 09 09 09 09 4e 6f 6e 65 ),..........None
0ec0: 20 3d 3e 20 44 61 74 65 54 69 6d 65 3a 3a 70 61 => DateTime::pa
0ed0: 72 73 65 5f 66 72 6f 6d 5f 72 66 63 33 33 33 39 rse_from_rfc3339
0ee0: 28 26 69 74 65 6d 2e 64 75 62 6c 69 6e 5f 63 6f (&item.dublin_co
0ef0: 72 65 5f 65 78 74 28 29 2e 75 6e 77 72 61 70 28 re_ext().unwrap(
0f00: 29 2e 64 61 74 65 73 28 29 5b 30 5d 29 2c 0a 09 ).dates()[0]),..
0f10: 09 09 09 09 09 09 09 7d 3f 3b 0a 09 09 09 09 09 .......}?;......
0f20: 09 09 09 6c 65 74 20 75 72 6c 20 3d 20 6c 69 6e ...let url = lin
0f30: 6b 3b 0a 09 09 09 09 09 09 09 09 70 6f 73 74 73 k;.........posts
0f40: 2e 69 6e 73 65 72 74 28 64 61 74 65 2e 63 6c 6f .insert(date.clo
0f50: 6e 65 28 29 2c 20 75 72 6c 2e 69 6e 74 6f 28 29 ne(), url.into()
0f60: 29 3b 0a 09 09 09 09 09 09 09 7d 2c 0a 09 09 09 );........},....
0f70: 09 09 09 09 4e 6f 6e 65 20 3d 3e 20 7b 7d 0a 09 ....None => {}..
0f80: 09 09 09 09 09 7d 0a 09 09 09 09 09 7d 3b 0a 09 .....}......};..
0f90: 09 09 09 7d 2c 0a 09 09 09 09 45 72 72 28 65 72 ...},.....Err(er
0fa0: 72 29 20 3d 3e 20 6d 61 74 63 68 20 65 72 72 20 r) => match err
0fb0: 7b 0a 09 09 09 09 09 72 73 73 3a 3a 45 72 72 6f {......rss::Erro
0fc0: 72 3a 3a 49 6e 76 61 6c 69 64 53 74 61 72 74 54 r::InvalidStartT
0fd0: 61 67 20 3d 3e 20 7b 0a 09 09 09 09 09 09 6c 65 ag => {.......le
0fe0: 74 20 66 65 65 64 20 3d 20 61 74 6f 6d 5f 73 79 t feed = atom_sy
0ff0: 6e 64 69 63 61 74 69 6f 6e 3a 3a 46 65 65 64 3a ndication::Feed:
1000: 3a 72 65 61 64 5f 66 72 6f 6d 28 26 63 6f 6e 74 :read_from(&cont
1010: 65 6e 74 5b 2e 2e 5d 29 0a 09 09 09 09 09 09 09 ent[..])........
1020: 2e 77 69 74 68 5f 63 6f 6e 74 65 78 74 28 7c 7c .with_context(||
1030: 20 66 6f 72 6d 61 74 21 28 22 50 72 6f 62 6c 65 format!("Proble
1040: 6d 20 6f 70 65 6e 69 6e 67 20 66 65 65 64 20 75 m opening feed u
1050: 72 6c 3a 5c 6e 7b 7d 5c 6e 7b 7d 22 2c 20 26 75 rl:\n{}\n{}", &u
1060: 72 6c 2c 20 73 74 61 74 75 73 29 29 3f 3b 0a 09 rl, status))?;..
1070: 09 09 09 09 09 66 6f 72 20 69 74 65 6d 20 69 6e .....for item in
1080: 20 66 65 65 64 2e 65 6e 74 72 69 65 73 28 29 20 feed.entries()
1090: 7b 0a 09 09 09 09 09 09 09 6c 65 74 20 64 61 74 {........let dat
10a0: 65 20 3d 20 69 74 65 6d 2e 70 75 62 6c 69 73 68 e = item.publish
10b0: 65 64 28 29 2e 75 6e 77 72 61 70 28 29 3b 0a 09 ed().unwrap();..
10c0: 09 09 09 09 09 09 6c 65 74 20 75 72 6c 20 3d 20 ......let url =
10d0: 69 74 65 6d 2e 6c 69 6e 6b 73 28 29 5b 30 5d 2e item.links()[0].
10e0: 68 72 65 66 28 29 3b 0a 09 09 09 09 09 09 09 70 href();........p
10f0: 6f 73 74 73 2e 69 6e 73 65 72 74 28 64 61 74 65 osts.insert(date
1100: 2e 63 6c 6f 6e 65 28 29 2c 20 75 72 6c 2e 69 6e .clone(), url.in
1110: 74 6f 28 29 29 3b 0a 09 09 09 09 09 09 7d 3b 0a to());.......};.
1120: 09 09 09 09 09 7d 2c 0a 09 09 09 09 09 72 73 73 .....},......rss
1130: 3a 3a 45 72 72 6f 72 3a 3a 45 6f 66 20 3d 3e 20 ::Error::Eof =>
1140: 28 29 2c 0a 09 09 09 09 09 5f 20 3d 3e 20 62 61 (),......_ => ba
1150: 69 6c 21 28 22 55 6e 73 75 70 70 6f 72 74 65 64 il!("Unsupported
1160: 20 6f 72 20 6d 61 6e 67 6c 65 64 20 63 6f 6e 74 or mangled cont
1170: 65 6e 74 3a 5c 6e 7b 3a 3f 7d 5c 6e 7b 3a 23 3f ent:\n{:?}\n{:#?
1180: 7d 5c 6e 7b 3a 23 3f 7d 5c 6e 22 2c 20 26 75 72 }\n{:#?}\n", &ur
1190: 6c 2c 20 65 72 72 2c 20 73 74 61 74 75 73 29 0a l, err, status).
11a0: 09 09 09 09 7d 0a 09 09 09 7d 3b 0a 09 09 09 66 ....}....};....f
11b0: 6f 72 20 28 64 61 74 65 2c 20 75 72 6c 29 20 69 or (date, url) i
11c0: 6e 20 70 6f 73 74 73 2e 69 74 65 72 28 29 20 7b n posts.iter() {
11d0: 0a 09 09 09 09 6c 65 74 20 6d 75 74 20 63 6f 6e .....let mut con
11e0: 6e 20 3d 20 73 65 6c 66 2e 70 6f 6f 6c 2e 61 63 n = self.pool.ac
11f0: 71 75 69 72 65 28 29 2e 61 77 61 69 74 0a 09 09 quire().await...
1200: 09 09 09 2e 77 69 74 68 5f 63 6f 6e 74 65 78 74 ....with_context
1210: 28 7c 7c 20 66 6f 72 6d 61 74 21 28 22 43 68 65 (|| format!("Che
1220: 63 6b 20 70 6f 73 74 20 66 65 74 63 68 20 63 6f ck post fetch co
1230: 6e 6e 3a 5c 6e 7b 3a 3f 7d 22 2c 20 26 73 65 6c nn:\n{:?}", &sel
1240: 66 2e 70 6f 6f 6c 29 29 3f 3b 0a 09 09 09 09 6c f.pool))?;.....l
1250: 65 74 20 70 6f 73 74 5f 75 72 6c 20 3d 20 6d 61 et post_url = ma
1260: 74 63 68 20 75 72 6c 5f 72 65 20 7b 0a 09 09 09 tch url_re {....
1270: 09 09 53 6f 6d 65 28 72 65 66 20 78 29 20 3d 3e ..Some(ref x) =>
1280: 20 78 2e 65 78 65 63 75 74 65 28 75 72 6c 29 2e x.execute(url).
1290: 74 6f 5f 73 74 72 69 6e 67 28 29 2c 0a 09 09 09 to_string(),....
12a0: 09 09 4e 6f 6e 65 20 3d 3e 20 75 72 6c 2e 74 6f ..None => url.to
12b0: 5f 73 74 72 69 6e 67 28 29 2c 0a 09 09 09 09 7d _string(),.....}
12c0: 3b 0a 09 09 09 09 6c 65 74 20 72 6f 77 20 3d 20 ;.....let row =
12d0: 73 71 6c 78 3a 3a 71 75 65 72 79 28 22 73 65 6c sqlx::query("sel
12e0: 65 63 74 20 65 78 69 73 74 73 28 73 65 6c 65 63 ect exists(selec
12f0: 74 20 74 72 75 65 20 66 72 6f 6d 20 72 73 73 74 t true from rsst
1300: 67 5f 70 6f 73 74 20 77 68 65 72 65 20 75 72 6c g_post where url
1310: 20 3d 20 24 31 20 61 6e 64 20 73 6f 75 72 63 65 = $1 and source
1320: 5f 69 64 20 3d 20 24 32 29 20 61 73 20 65 78 69 _id = $2) as exi
1330: 73 74 73 3b 22 29 0a 09 09 09 09 09 2e 62 69 6e sts;").......bin
1340: 64 28 26 70 6f 73 74 5f 75 72 6c 29 0a 09 09 09 d(&post_url)....
1350: 09 09 2e 62 69 6e 64 28 2a 69 64 29 0a 09 09 09 ...bind(*id)....
1360: 09 09 2e 66 65 74 63 68 5f 6f 6e 65 28 26 6d 75 ...fetch_one(&mu
1370: 74 20 63 6f 6e 6e 29 2e 61 77 61 69 74 0a 09 09 t conn).await...
1380: 09 09 09 2e 77 69 74 68 5f 63 6f 6e 74 65 78 74 ....with_context
1390: 28 7c 7c 20 66 6f 72 6d 61 74 21 28 22 43 68 65 (|| format!("Che
13a0: 63 6b 20 70 6f 73 74 3a 5c 6e 7b 3a 3f 7d 22 2c ck post:\n{:?}",
13b0: 20 26 63 6f 6e 6e 29 29 3f 3b 0a 09 09 09 09 6c &conn))?;.....l
13c0: 65 74 20 65 78 69 73 74 73 3a 20 62 6f 6f 6c 20 et exists: bool
13d0: 3d 20 72 6f 77 2e 74 72 79 5f 67 65 74 28 22 65 = row.try_get("e
13e0: 78 69 73 74 73 22 29 3f 3b 0a 09 09 09 09 69 66 xists")?;.....if
13f0: 20 21 20 65 78 69 73 74 73 20 7b 0a 09 09 09 09 ! exists {.....
1400: 09 69 66 20 74 68 69 73 5f 66 65 74 63 68 20 3d .if this_fetch =
1410: 3d 20 4e 6f 6e 65 20 7c 7c 20 2a 64 61 74 65 20 = None || *date
1420: 3e 20 74 68 69 73 5f 66 65 74 63 68 2e 75 6e 77 > this_fetch.unw
1430: 72 61 70 28 29 20 7b 0a 09 09 09 09 09 09 74 68 rap() {.......th
1440: 69 73 5f 66 65 74 63 68 20 3d 20 53 6f 6d 65 28 is_fetch = Some(
1450: 2a 64 61 74 65 29 3b 0a 09 09 09 09 09 7d 3b 0a *date);......};.
1460: 09 09 09 09 09 73 65 6c 66 2e 74 67 2e 73 65 6e .....self.tg.sen
1470: 64 28 20 6d 61 74 63 68 20 69 76 5f 68 61 73 68 d( match iv_hash
1480: 20 7b 0a 09 09 09 09 09 09 09 53 6f 6d 65 28 68 {........Some(h
1490: 61 73 68 29 20 3d 3e 20 74 65 6c 65 67 72 61 6d ash) => telegram
14a0: 5f 62 6f 74 3a 3a 53 65 6e 64 4d 65 73 73 61 67 _bot::SendMessag
14b0: 65 3a 3a 6e 65 77 28 64 65 73 74 69 6e 61 74 69 e::new(destinati
14c0: 6f 6e 2c 20 66 6f 72 6d 61 74 21 28 22 3c 61 20 on, format!("<a
14d0: 68 72 65 66 3d 5c 22 68 74 74 70 73 3a 2f 2f 74 href=\"https://t
14e0: 2e 6d 65 2f 69 76 3f 75 72 6c 3d 7b 7d 26 72 68 .me/iv?url={}&rh
14f0: 61 73 68 3d 7b 7d 5c 22 3e 20 3c 2f 61 3e 7b 30 ash={}\"> </a>{0
1500: 7d 22 2c 20 26 70 6f 73 74 5f 75 72 6c 2c 20 68 }", &post_url, h
1510: 61 73 68 29 29 2c 0a 09 09 09 09 09 09 09 4e 6f ash)),........No
1520: 6e 65 20 3d 3e 20 74 65 6c 65 67 72 61 6d 5f 62 ne => telegram_b
1530: 6f 74 3a 3a 53 65 6e 64 4d 65 73 73 61 67 65 3a ot::SendMessage:
1540: 3a 6e 65 77 28 64 65 73 74 69 6e 61 74 69 6f 6e :new(destination
1550: 2c 20 66 6f 72 6d 61 74 21 28 22 7b 7d 22 2c 20 , format!("{}",
1560: 70 6f 73 74 5f 75 72 6c 29 29 2c 0a 09 09 09 09 post_url)),.....
1570: 09 09 7d 2e 70 61 72 73 65 5f 6d 6f 64 65 28 74 ..}.parse_mode(t
1580: 65 6c 65 67 72 61 6d 5f 62 6f 74 3a 3a 74 79 70 elegram_bot::typ
1590: 65 73 3a 3a 50 61 72 73 65 4d 6f 64 65 3a 3a 48 es::ParseMode::H
15a0: 74 6d 6c 29 29 2e 61 77 61 69 74 0a 09 09 09 09 tml)).await.....
15b0: 09 09 2e 63 6f 6e 74 65 78 74 28 22 43 61 6e 27 ...context("Can'
15c0: 74 20 70 6f 73 74 20 6d 65 73 73 61 67 65 3a 22 t post message:"
15d0: 29 3f 3b 0a 09 09 09 09 09 73 71 6c 78 3a 3a 71 )?;......sqlx::q
15e0: 75 65 72 79 28 22 69 6e 73 65 72 74 20 69 6e 74 uery("insert int
15f0: 6f 20 72 73 73 74 67 5f 70 6f 73 74 20 28 73 6f o rsstg_post (so
1600: 75 72 63 65 5f 69 64 2c 20 70 6f 73 74 65 64 2c urce_id, posted,
1610: 20 75 72 6c 29 20 76 61 6c 75 65 73 20 28 24 31 url) values ($1
1620: 2c 20 24 32 2c 20 24 33 29 3b 22 29 0a 09 09 09 , $2, $3);")....
1630: 09 09 09 2e 62 69 6e 64 28 2a 69 64 29 0a 09 09 ....bind(*id)...
1640: 09 09 09 09 2e 62 69 6e 64 28 64 61 74 65 29 0a .....bind(date).
1650: 09 09 09 09 09 09 2e 62 69 6e 64 28 70 6f 73 74 .......bind(post
1660: 5f 75 72 6c 29 0a 09 09 09 09 09 09 2e 65 78 65 _url)........exe
1670: 63 75 74 65 28 26 6d 75 74 20 63 6f 6e 6e 29 2e cute(&mut conn).
1680: 61 77 61 69 74 0a 09 09 09 09 09 09 2e 77 69 74 await........wit
1690: 68 5f 63 6f 6e 74 65 78 74 28 7c 7c 20 66 6f 72 h_context(|| for
16a0: 6d 61 74 21 28 22 52 65 63 6f 72 64 20 70 6f 73 mat!("Record pos
16b0: 74 3a 5c 6e 7b 3a 3f 7d 22 2c 20 26 63 6f 6e 6e t:\n{:?}", &conn
16c0: 29 29 3f 3b 0a 09 09 09 09 09 64 72 6f 70 28 63 ))?;......drop(c
16d0: 6f 6e 6e 29 3b 0a 09 09 09 09 09 74 6f 6b 69 6f onn);......tokio
16e0: 3a 3a 74 69 6d 65 3a 3a 73 6c 65 65 70 28 73 74 ::time::sleep(st
16f0: 64 3a 3a 74 69 6d 65 3a 3a 44 75 72 61 74 69 6f d::time::Duratio
1700: 6e 3a 3a 6e 65 77 28 34 2c 20 30 29 29 2e 61 77 n::new(4, 0)).aw
1710: 61 69 74 3b 0a 09 09 09 09 7d 3b 0a 09 09 09 09 ait;.....};.....
1720: 70 6f 73 74 65 64 20 2b 3d 20 31 3b 0a 09 09 09 posted += 1;....
1730: 7d 3b 0a 09 09 09 70 6f 73 74 73 2e 63 6c 65 61 };....posts.clea
1740: 72 28 29 3b 0a 09 09 7d 3b 0a 09 09 6c 65 74 20 r();...};...let
1750: 6d 75 74 20 63 6f 6e 6e 20 3d 20 73 65 6c 66 2e mut conn = self.
1760: 70 6f 6f 6c 2e 61 63 71 75 69 72 65 28 29 2e 61 pool.acquire().a
1770: 77 61 69 74 0a 09 09 09 2e 77 69 74 68 5f 63 6f wait.....with_co
1780: 6e 74 65 78 74 28 7c 7c 20 66 6f 72 6d 61 74 21 ntext(|| format!
1790: 28 22 55 70 64 61 74 65 20 73 63 72 61 70 65 20 ("Update scrape
17a0: 66 65 74 63 68 20 63 6f 6e 6e 3a 5c 6e 7b 3a 3f fetch conn:\n{:?
17b0: 7d 22 2c 20 26 73 65 6c 66 2e 70 6f 6f 6c 29 29 }", &self.pool))
17c0: 3f 3b 0a 09 09 73 71 6c 78 3a 3a 71 75 65 72 79 ?;...sqlx::query
17d0: 28 22 75 70 64 61 74 65 20 72 73 73 74 67 5f 73 ("update rsstg_s
17e0: 6f 75 72 63 65 20 73 65 74 20 6c 61 73 74 5f 73 ource set last_s
17f0: 63 72 61 70 65 20 3d 20 6e 6f 77 28 29 20 77 68 crape = now() wh
1800: 65 72 65 20 73 6f 75 72 63 65 5f 69 64 20 3d 20 ere source_id =
1810: 24 31 3b 22 29 0a 09 09 09 2e 62 69 6e 64 28 2a $1;").....bind(*
1820: 69 64 29 0a 09 09 09 2e 65 78 65 63 75 74 65 28 id).....execute(
1830: 26 6d 75 74 20 63 6f 6e 6e 29 2e 61 77 61 69 74 &mut conn).await
1840: 0a 09 09 09 2e 77 69 74 68 5f 63 6f 6e 74 65 78 .....with_contex
1850: 74 28 7c 7c 20 66 6f 72 6d 61 74 21 28 22 55 70 t(|| format!("Up
1860: 64 61 74 65 20 73 63 72 61 70 65 3a 5c 6e 7b 3a date scrape:\n{:
1870: 3f 7d 22 2c 20 26 63 6f 6e 6e 29 29 3f 3b 0a 09 ?}", &conn))?;..
1880: 09 4f 6b 28 66 6f 72 6d 61 74 21 28 22 50 6f 73 .Ok(format!("Pos
1890: 74 65 64 3a 20 7b 7d 22 2c 20 26 70 6f 73 74 65 ted: {}", &poste
18a0: 64 29 29 0a 09 7d 0a 0a 09 70 75 62 20 61 73 79 d))..}...pub asy
18b0: 6e 63 20 66 6e 20 64 65 6c 65 74 65 3c 53 3e 28 nc fn delete<S>(
18c0: 26 73 65 6c 66 2c 20 73 6f 75 72 63 65 5f 69 64 &self, source_id
18d0: 3a 20 26 69 33 32 2c 20 6f 77 6e 65 72 3a 20 53 : &i32, owner: S
18e0: 29 20 2d 3e 20 52 65 73 75 6c 74 3c 53 74 72 69 ) -> Result<Stri
18f0: 6e 67 3e 0a 09 77 68 65 72 65 20 53 3a 20 49 6e ng>..where S: In
1900: 74 6f 3c 69 36 34 3e 20 7b 0a 09 09 6c 65 74 20 to<i64> {...let
1910: 6f 77 6e 65 72 20 3d 20 6f 77 6e 65 72 2e 69 6e owner = owner.in
1920: 74 6f 28 29 3b 0a 0a 09 09 6c 65 74 20 6d 75 74 to();....let mut
1930: 20 63 6f 6e 6e 20 3d 20 73 65 6c 66 2e 70 6f 6f conn = self.poo
1940: 6c 2e 61 63 71 75 69 72 65 28 29 2e 61 77 61 69 l.acquire().awai
1950: 74 0a 09 09 09 2e 77 69 74 68 5f 63 6f 6e 74 65 t.....with_conte
1960: 78 74 28 7c 7c 20 66 6f 72 6d 61 74 21 28 22 44 xt(|| format!("D
1970: 65 6c 65 74 65 20 66 65 74 63 68 20 63 6f 6e 6e elete fetch conn
1980: 3a 5c 6e 7b 3a 3f 7d 22 2c 20 26 73 65 6c 66 2e :\n{:?}", &self.
1990: 70 6f 6f 6c 29 29 3f 3b 0a 09 09 6d 61 74 63 68 pool))?;...match
19a0: 20 73 71 6c 78 3a 3a 71 75 65 72 79 28 22 64 65 sqlx::query("de
19b0: 6c 65 74 65 20 66 72 6f 6d 20 72 73 73 74 67 5f lete from rsstg_
19c0: 73 6f 75 72 63 65 20 77 68 65 72 65 20 73 6f 75 source where sou
19d0: 72 63 65 5f 69 64 20 3d 20 24 31 20 61 6e 64 20 rce_id = $1 and
19e0: 6f 77 6e 65 72 20 3d 20 24 32 3b 22 29 0a 09 09 owner = $2;")...
19f0: 09 2e 62 69 6e 64 28 73 6f 75 72 63 65 5f 69 64 ..bind(source_id
1a00: 29 0a 09 09 09 2e 62 69 6e 64 28 6f 77 6e 65 72 ).....bind(owner
1a10: 29 0a 09 09 09 2e 65 78 65 63 75 74 65 28 26 6d ).....execute(&m
1a20: 75 74 20 63 6f 6e 6e 29 2e 61 77 61 69 74 0a 09 ut conn).await..
1a30: 09 09 2e 77 69 74 68 5f 63 6f 6e 74 65 78 74 28 ...with_context(
1a40: 7c 7c 20 66 6f 72 6d 61 74 21 28 22 44 65 6c 65 || format!("Dele
1a50: 74 65 20 73 6f 75 72 63 65 20 72 75 6c 65 3a 5c te source rule:\
1a60: 6e 7b 3a 3f 7d 22 2c 20 26 73 65 6c 66 2e 70 6f n{:?}", &self.po
1a70: 6f 6c 29 29 3f 0a 09 09 09 2e 72 6f 77 73 5f 61 ol))?.....rows_a
1a80: 66 66 65 63 74 65 64 28 29 20 7b 0a 09 09 09 30 ffected() {....0
1a90: 20 3d 3e 20 7b 20 4f 6b 28 22 4e 6f 20 64 61 74 => { Ok("No dat
1aa0: 61 20 66 6f 75 6e 64 20 66 6f 75 6e 64 2e 22 2e a found found.".
1ab0: 74 6f 5f 73 74 72 69 6e 67 28 29 29 20 7d 2c 0a to_string()) },.
1ac0: 09 09 09 78 20 3d 3e 20 7b 20 4f 6b 28 66 6f 72 ...x => { Ok(for
1ad0: 6d 61 74 21 28 22 7b 7d 20 73 6f 75 72 63 65 73 mat!("{} sources
1ae0: 20 72 65 6d 6f 76 65 64 2e 22 2c 20 78 29 29 20 removed.", x))
1af0: 7d 2c 0a 09 09 7d 0a 09 7d 0a 0a 09 70 75 62 20 },...}..}...pub
1b00: 61 73 79 6e 63 20 66 6e 20 63 6c 65 61 6e 3c 53 async fn clean<S
1b10: 3e 28 26 73 65 6c 66 2c 20 73 6f 75 72 63 65 5f >(&self, source_
1b20: 69 64 3a 20 26 69 33 32 2c 20 6f 77 6e 65 72 3a id: &i32, owner:
1b30: 20 53 29 20 2d 3e 20 52 65 73 75 6c 74 3c 53 74 S) -> Result<St
1b40: 72 69 6e 67 3e 0a 09 77 68 65 72 65 20 53 3a 20 ring>..where S:
1b50: 49 6e 74 6f 3c 69 36 34 3e 20 7b 0a 09 09 6c 65 Into<i64> {...le
1b60: 74 20 6f 77 6e 65 72 20 3d 20 6f 77 6e 65 72 2e t owner = owner.
1b70: 69 6e 74 6f 28 29 3b 0a 0a 09 09 6c 65 74 20 6d into();....let m
1b80: 75 74 20 63 6f 6e 6e 20 3d 20 73 65 6c 66 2e 70 ut conn = self.p
1b90: 6f 6f 6c 2e 61 63 71 75 69 72 65 28 29 2e 61 77 ool.acquire().aw
1ba0: 61 69 74 0a 09 09 09 2e 77 69 74 68 5f 63 6f 6e ait.....with_con
1bb0: 74 65 78 74 28 7c 7c 20 66 6f 72 6d 61 74 21 28 text(|| format!(
1bc0: 22 43 6c 65 61 6e 20 66 65 74 63 68 20 63 6f 6e "Clean fetch con
1bd0: 6e 3a 5c 6e 7b 3a 3f 7d 22 2c 20 26 73 65 6c 66 n:\n{:?}", &self
1be0: 2e 70 6f 6f 6c 29 29 3f 3b 0a 09 09 6d 61 74 63 .pool))?;...matc
1bf0: 68 20 73 71 6c 78 3a 3a 71 75 65 72 79 28 22 64 h sqlx::query("d
1c00: 65 6c 65 74 65 20 66 72 6f 6d 20 72 73 73 74 67 elete from rsstg
1c10: 5f 70 6f 73 74 20 70 20 75 73 69 6e 67 20 72 73 _post p using rs
1c20: 73 74 67 5f 73 6f 75 72 63 65 20 73 20 77 68 65 stg_source s whe
1c30: 72 65 20 70 2e 73 6f 75 72 63 65 5f 69 64 20 3d re p.source_id =
1c40: 20 24 31 20 61 6e 64 20 6f 77 6e 65 72 20 3d 20 $1 and owner =
1c50: 24 32 20 61 6e 64 20 70 2e 73 6f 75 72 63 65 5f $2 and p.source_
1c60: 69 64 20 3d 20 73 2e 73 6f 75 72 63 65 5f 69 64 id = s.source_id
1c70: 3b 22 29 0a 09 09 09 2e 62 69 6e 64 28 73 6f 75 ;").....bind(sou
1c80: 72 63 65 5f 69 64 29 0a 09 09 09 2e 62 69 6e 64 rce_id).....bind
1c90: 28 6f 77 6e 65 72 29 0a 09 09 09 2e 65 78 65 63 (owner).....exec
1ca0: 75 74 65 28 26 6d 75 74 20 63 6f 6e 6e 29 2e 61 ute(&mut conn).a
1cb0: 77 61 69 74 0a 09 09 09 2e 77 69 74 68 5f 63 6f wait.....with_co
1cc0: 6e 74 65 78 74 28 7c 7c 20 66 6f 72 6d 61 74 21 ntext(|| format!
1cd0: 28 22 43 6c 65 61 6e 20 73 65 65 6e 20 70 6f 73 ("Clean seen pos
1ce0: 74 73 3a 5c 6e 7b 3a 3f 7d 22 2c 20 26 73 65 6c ts:\n{:?}", &sel
1cf0: 66 2e 70 6f 6f 6c 29 29 3f 0a 09 09 09 2e 72 6f f.pool))?.....ro
1d00: 77 73 5f 61 66 66 65 63 74 65 64 28 29 20 7b 0a ws_affected() {.
1d10: 09 09 09 30 20 3d 3e 20 7b 20 4f 6b 28 22 4e 6f ...0 => { Ok("No
1d20: 20 64 61 74 61 20 66 6f 75 6e 64 20 66 6f 75 6e data found foun
1d30: 64 2e 22 2e 74 6f 5f 73 74 72 69 6e 67 28 29 29 d.".to_string())
1d40: 20 7d 2c 0a 09 09 09 78 20 3d 3e 20 7b 20 4f 6b },....x => { Ok
1d50: 28 66 6f 72 6d 61 74 21 28 22 7b 7d 20 70 6f 73 (format!("{} pos
1d60: 74 73 20 70 75 72 67 65 64 2e 22 2c 20 78 29 29 ts purged.", x))
1d70: 20 7d 2c 0a 09 09 7d 0a 09 7d 0a 0a 09 70 75 62 },...}..}...pub
1d80: 20 61 73 79 6e 63 20 66 6e 20 65 6e 61 62 6c 65 async fn enable
1d90: 3c 53 3e 28 26 73 65 6c 66 2c 20 73 6f 75 72 63 <S>(&self, sourc
1da0: 65 5f 69 64 3a 20 26 69 33 32 2c 20 6f 77 6e 65 e_id: &i32, owne
1db0: 72 3a 20 53 29 20 2d 3e 20 52 65 73 75 6c 74 3c r: S) -> Result<
1dc0: 26 73 74 72 3e 0a 09 77 68 65 72 65 20 53 3a 20 &str>..where S:
1dd0: 49 6e 74 6f 3c 69 36 34 3e 20 7b 0a 09 09 6c 65 Into<i64> {...le
1de0: 74 20 6f 77 6e 65 72 20 3d 20 6f 77 6e 65 72 2e t owner = owner.
1df0: 69 6e 74 6f 28 29 3b 0a 0a 09 09 6c 65 74 20 6d into();....let m
1e00: 75 74 20 63 6f 6e 6e 20 3d 20 73 65 6c 66 2e 70 ut conn = self.p
1e10: 6f 6f 6c 2e 61 63 71 75 69 72 65 28 29 2e 61 77 ool.acquire().aw
1e20: 61 69 74 0a 09 09 09 2e 77 69 74 68 5f 63 6f 6e ait.....with_con
1e30: 74 65 78 74 28 7c 7c 20 66 6f 72 6d 61 74 21 28 text(|| format!(
1e40: 22 45 6e 61 62 6c 65 20 66 65 74 63 68 20 63 6f "Enable fetch co
1e50: 6e 6e 3a 5c 6e 7b 3a 3f 7d 22 2c 20 26 73 65 6c nn:\n{:?}", &sel
1e60: 66 2e 70 6f 6f 6c 29 29 3f 3b 0a 09 09 6d 61 74 f.pool))?;...mat
1e70: 63 68 20 73 71 6c 78 3a 3a 71 75 65 72 79 28 22 ch sqlx::query("
1e80: 75 70 64 61 74 65 20 72 73 73 74 67 5f 73 6f 75 update rsstg_sou
1e90: 72 63 65 20 73 65 74 20 65 6e 61 62 6c 65 64 20 rce set enabled
1ea0: 3d 20 74 72 75 65 20 77 68 65 72 65 20 73 6f 75 = true where sou
1eb0: 72 63 65 5f 69 64 20 3d 20 24 31 20 61 6e 64 20 rce_id = $1 and
1ec0: 6f 77 6e 65 72 20 3d 20 24 32 22 29 0a 09 09 09 owner = $2")....
1ed0: 2e 62 69 6e 64 28 73 6f 75 72 63 65 5f 69 64 29 .bind(source_id)
1ee0: 0a 09 09 09 2e 62 69 6e 64 28 6f 77 6e 65 72 29 .....bind(owner)
1ef0: 0a 09 09 09 2e 65 78 65 63 75 74 65 28 26 6d 75 .....execute(&mu
1f00: 74 20 63 6f 6e 6e 29 2e 61 77 61 69 74 0a 09 09 t conn).await...
1f10: 09 2e 77 69 74 68 5f 63 6f 6e 74 65 78 74 28 7c ..with_context(|
1f20: 7c 20 66 6f 72 6d 61 74 21 28 22 45 6e 61 62 6c | format!("Enabl
1f30: 65 20 73 6f 75 72 63 65 3a 5c 6e 7b 3a 3f 7d 22 e source:\n{:?}"
1f40: 2c 20 26 73 65 6c 66 2e 70 6f 6f 6c 29 29 3f 0a , &self.pool))?.
1f50: 09 09 09 2e 72 6f 77 73 5f 61 66 66 65 63 74 65 ....rows_affecte
1f60: 64 28 29 20 7b 0a 09 09 09 31 20 3d 3e 20 7b 20 d() {....1 => {
1f70: 4f 6b 28 22 53 6f 75 72 63 65 20 65 6e 61 62 6c Ok("Source enabl
1f80: 65 64 2e 22 29 20 7d 2c 0a 09 09 09 30 20 3d 3e ed.") },....0 =>
1f90: 20 7b 20 4f 6b 28 22 53 6f 75 72 63 65 20 6e 6f { Ok("Source no
1fa0: 74 20 66 6f 75 6e 64 2e 22 29 20 7d 2c 0a 09 09 t found.") },...
1fb0: 09 5f 20 3d 3e 20 7b 20 45 72 72 28 61 6e 79 68 ._ => { Err(anyh
1fc0: 6f 77 21 28 22 44 61 74 61 62 61 73 65 20 65 72 ow!("Database er
1fd0: 72 6f 72 2e 22 29 29 20 7d 2c 0a 09 09 7d 0a 09 ror.")) },...}..
1fe0: 7d 0a 0a 09 70 75 62 20 61 73 79 6e 63 20 66 6e }...pub async fn
1ff0: 20 64 69 73 61 62 6c 65 3c 53 3e 28 26 73 65 6c disable<S>(&sel
2000: 66 2c 20 73 6f 75 72 63 65 5f 69 64 3a 20 26 69 f, source_id: &i
2010: 33 32 2c 20 6f 77 6e 65 72 3a 20 53 29 20 2d 3e 32, owner: S) ->
2020: 20 52 65 73 75 6c 74 3c 26 73 74 72 3e 0a 09 77 Result<&str>..w
2030: 68 65 72 65 20 53 3a 20 49 6e 74 6f 3c 69 36 34 here S: Into<i64
2040: 3e 20 7b 0a 09 09 6c 65 74 20 6f 77 6e 65 72 20 > {...let owner
2050: 3d 20 6f 77 6e 65 72 2e 69 6e 74 6f 28 29 3b 0a = owner.into();.
2060: 0a 09 09 6c 65 74 20 6d 75 74 20 63 6f 6e 6e 20 ...let mut conn
2070: 3d 20 73 65 6c 66 2e 70 6f 6f 6c 2e 61 63 71 75 = self.pool.acqu
2080: 69 72 65 28 29 2e 61 77 61 69 74 0a 09 09 09 2e ire().await.....
2090: 77 69 74 68 5f 63 6f 6e 74 65 78 74 28 7c 7c 20 with_context(||
20a0: 66 6f 72 6d 61 74 21 28 22 44 69 73 61 62 6c 65 format!("Disable
20b0: 20 66 65 74 63 68 20 63 6f 6e 6e 3a 5c 6e 7b 3a fetch conn:\n{:
20c0: 3f 7d 22 2c 20 26 73 65 6c 66 2e 70 6f 6f 6c 29 ?}", &self.pool)
20d0: 29 3f 3b 0a 09 09 6d 61 74 63 68 20 73 71 6c 78 )?;...match sqlx
20e0: 3a 3a 71 75 65 72 79 28 22 75 70 64 61 74 65 20 ::query("update
20f0: 72 73 73 74 67 5f 73 6f 75 72 63 65 20 73 65 74 rsstg_source set
2100: 20 65 6e 61 62 6c 65 64 20 3d 20 66 61 6c 73 65 enabled = false
2110: 20 77 68 65 72 65 20 73 6f 75 72 63 65 5f 69 64 where source_id
2120: 20 3d 20 24 31 20 61 6e 64 20 6f 77 6e 65 72 20 = $1 and owner
2130: 3d 20 24 32 22 29 0a 09 09 09 2e 62 69 6e 64 28 = $2").....bind(
2140: 73 6f 75 72 63 65 5f 69 64 29 0a 09 09 09 2e 62 source_id).....b
2150: 69 6e 64 28 6f 77 6e 65 72 29 0a 09 09 09 2e 65 ind(owner).....e
2160: 78 65 63 75 74 65 28 26 6d 75 74 20 63 6f 6e 6e xecute(&mut conn
2170: 29 2e 61 77 61 69 74 0a 09 09 09 2e 77 69 74 68 ).await.....with
2180: 5f 63 6f 6e 74 65 78 74 28 7c 7c 20 66 6f 72 6d _context(|| form
2190: 61 74 21 28 22 44 69 73 61 62 6c 65 20 73 6f 75 at!("Disable sou
21a0: 72 63 65 3a 5c 6e 7b 3a 3f 7d 22 2c 20 26 73 65 rce:\n{:?}", &se
21b0: 6c 66 2e 70 6f 6f 6c 29 29 3f 0a 09 09 09 2e 72 lf.pool))?.....r
21c0: 6f 77 73 5f 61 66 66 65 63 74 65 64 28 29 20 7b ows_affected() {
21d0: 0a 09 09 09 31 20 3d 3e 20 7b 20 4f 6b 28 22 53 ....1 => { Ok("S
21e0: 6f 75 72 63 65 20 64 69 73 61 62 6c 65 64 2e 22 ource disabled."
21f0: 29 20 7d 2c 0a 09 09 09 30 20 3d 3e 20 7b 20 4f ) },....0 => { O
2200: 6b 28 22 53 6f 75 72 63 65 20 6e 6f 74 20 66 6f k("Source not fo
2210: 75 6e 64 2e 22 29 20 7d 2c 0a 09 09 09 5f 20 3d und.") },...._ =
2220: 3e 20 7b 20 45 72 72 28 61 6e 79 68 6f 77 21 28 > { Err(anyhow!(
2230: 22 44 61 74 61 62 61 73 65 20 65 72 72 6f 72 2e "Database error.
2240: 22 29 29 20 7d 2c 0a 09 09 7d 0a 09 7d 0a 0a 09 ")) },...}..}...
2250: 70 75 62 20 61 73 79 6e 63 20 66 6e 20 75 70 64 pub async fn upd
2260: 61 74 65 3c 53 3e 28 26 73 65 6c 66 2c 20 75 70 ate<S>(&self, up
2270: 64 61 74 65 3a 20 4f 70 74 69 6f 6e 3c 69 33 32 date: Option<i32
2280: 3e 2c 20 63 68 61 6e 6e 65 6c 3a 20 26 73 74 72 >, channel: &str
2290: 2c 20 63 68 61 6e 6e 65 6c 5f 69 64 3a 20 69 36 , channel_id: i6
22a0: 34 2c 20 75 72 6c 3a 20 26 73 74 72 2c 20 69 76 4, url: &str, iv
22b0: 5f 68 61 73 68 3a 20 4f 70 74 69 6f 6e 3c 26 73 _hash: Option<&s
22c0: 74 72 3e 2c 20 75 72 6c 5f 72 65 3a 20 4f 70 74 tr>, url_re: Opt
22d0: 69 6f 6e 3c 26 73 74 72 3e 2c 20 6f 77 6e 65 72 ion<&str>, owner
22e0: 3a 20 53 29 20 2d 3e 20 52 65 73 75 6c 74 3c 53 : S) -> Result<S
22f0: 74 72 69 6e 67 3e 0a 09 77 68 65 72 65 20 53 3a tring>..where S:
2300: 20 49 6e 74 6f 3c 69 36 34 3e 20 7b 0a 09 09 6c Into<i64> {...l
2310: 65 74 20 6f 77 6e 65 72 20 3d 20 6f 77 6e 65 72 et owner = owner
2320: 2e 69 6e 74 6f 28 29 3b 0a 0a 09 09 6c 65 74 20 .into();....let
2330: 6d 75 74 20 63 6f 6e 6e 20 3d 20 73 65 6c 66 2e mut conn = self.
2340: 70 6f 6f 6c 2e 61 63 71 75 69 72 65 28 29 2e 61 pool.acquire().a
2350: 77 61 69 74 0a 09 09 09 2e 77 69 74 68 5f 63 6f wait.....with_co
2360: 6e 74 65 78 74 28 7c 7c 20 66 6f 72 6d 61 74 21 ntext(|| format!
2370: 28 22 55 70 64 61 74 65 20 66 65 74 63 68 20 63 ("Update fetch c
2380: 6f 6e 6e 3a 5c 6e 7b 3a 3f 7d 22 2c 20 26 73 65 onn:\n{:?}", &se
2390: 6c 66 2e 70 6f 6f 6c 29 29 3f 3b 0a 0a 09 09 6d lf.pool))?;....m
23a0: 61 74 63 68 20 6d 61 74 63 68 20 75 70 64 61 74 atch match updat
23b0: 65 20 7b 0a 09 09 09 09 53 6f 6d 65 28 69 64 29 e {.....Some(id)
23c0: 20 3d 3e 20 7b 0a 09 09 09 09 09 73 71 6c 78 3a => {......sqlx:
23d0: 3a 71 75 65 72 79 28 22 75 70 64 61 74 65 20 72 :query("update r
23e0: 73 73 74 67 5f 73 6f 75 72 63 65 20 73 65 74 20 sstg_source set
23f0: 63 68 61 6e 6e 65 6c 5f 69 64 20 3d 20 24 32 2c channel_id = $2,
2400: 20 75 72 6c 20 3d 20 24 33 2c 20 69 76 5f 68 61 url = $3, iv_ha
2410: 73 68 20 3d 20 24 34 2c 20 6f 77 6e 65 72 20 3d sh = $4, owner =
2420: 20 24 35 2c 20 63 68 61 6e 6e 65 6c 20 3d 20 24 $5, channel = $
2430: 36 2c 20 75 72 6c 5f 72 65 20 3d 20 24 37 20 77 6, url_re = $7 w
2440: 68 65 72 65 20 73 6f 75 72 63 65 5f 69 64 20 3d here source_id =
2450: 20 24 31 22 29 2e 62 69 6e 64 28 69 64 29 0a 09 $1").bind(id)..
2460: 09 09 09 7d 2c 0a 09 09 09 09 4e 6f 6e 65 20 3d ...},.....None =
2470: 3e 20 7b 0a 09 09 09 09 09 73 71 6c 78 3a 3a 71 > {......sqlx::q
2480: 75 65 72 79 28 22 69 6e 73 65 72 74 20 69 6e 74 uery("insert int
2490: 6f 20 72 73 73 74 67 5f 73 6f 75 72 63 65 20 28 o rsstg_source (
24a0: 63 68 61 6e 6e 65 6c 5f 69 64 2c 20 75 72 6c 2c channel_id, url,
24b0: 20 69 76 5f 68 61 73 68 2c 20 6f 77 6e 65 72 2c iv_hash, owner,
24c0: 20 63 68 61 6e 6e 65 6c 2c 20 75 72 6c 5f 72 65 channel, url_re
24d0: 29 20 76 61 6c 75 65 73 20 28 24 31 2c 20 24 32 ) values ($1, $2
24e0: 2c 20 24 33 2c 20 24 34 2c 20 24 35 2c 20 24 36 , $3, $4, $5, $6
24f0: 29 22 29 0a 09 09 09 09 7d 2c 0a 09 09 09 7d 0a )").....},....}.
2500: 09 09 09 2e 62 69 6e 64 28 63 68 61 6e 6e 65 6c ....bind(channel
2510: 5f 69 64 29 0a 09 09 09 2e 62 69 6e 64 28 75 72 _id).....bind(ur
2520: 6c 29 0a 09 09 09 2e 62 69 6e 64 28 69 76 5f 68 l).....bind(iv_h
2530: 61 73 68 29 0a 09 09 09 2e 62 69 6e 64 28 6f 77 ash).....bind(ow
2540: 6e 65 72 29 0a 09 09 09 2e 62 69 6e 64 28 63 68 ner).....bind(ch
2550: 61 6e 6e 65 6c 29 0a 09 09 09 2e 62 69 6e 64 28 annel).....bind(
2560: 75 72 6c 5f 72 65 29 0a 09 09 09 2e 65 78 65 63 url_re).....exec
2570: 75 74 65 28 26 6d 75 74 20 63 6f 6e 6e 29 2e 61 ute(&mut conn).a
2580: 77 61 69 74 20 7b 0a 09 09 09 4f 6b 28 5f 29 20 wait {....Ok(_)
2590: 3d 3e 20 72 65 74 75 72 6e 20 4f 6b 28 53 74 72 => return Ok(Str
25a0: 69 6e 67 3a 3a 66 72 6f 6d 28 6d 61 74 63 68 20 ing::from(match
25b0: 75 70 64 61 74 65 20 7b 0a 09 09 09 09 53 6f 6d update {.....Som
25c0: 65 28 5f 29 20 3d 3e 20 22 43 68 61 6e 6e 65 6c e(_) => "Channel
25d0: 20 75 70 64 61 74 65 64 2e 22 2c 0a 09 09 09 09 updated.",.....
25e0: 4e 6f 6e 65 20 3d 3e 20 22 43 68 61 6e 6e 65 6c None => "Channel
25f0: 20 61 64 64 65 64 2e 22 2c 0a 09 09 09 7d 29 29 added.",....}))
2600: 2c 0a 09 09 09 45 72 72 28 73 71 6c 78 3a 3a 45 ,....Err(sqlx::E
2610: 72 72 6f 72 3a 3a 44 61 74 61 62 61 73 65 28 65 rror::Database(e
2620: 72 72 29 29 20 3d 3e 20 7b 0a 09 09 09 09 6d 61 rr)) => {.....ma
2630: 74 63 68 20 65 72 72 2e 64 6f 77 6e 63 61 73 74 tch err.downcast
2640: 3a 3a 3c 73 71 6c 78 3a 3a 70 6f 73 74 67 72 65 ::<sqlx::postgre
2650: 73 3a 3a 50 67 44 61 74 61 62 61 73 65 45 72 72 s::PgDatabaseErr
2660: 6f 72 3e 28 29 2e 72 6f 75 74 69 6e 65 28 29 20 or>().routine()
2670: 7b 0a 09 09 09 09 09 53 6f 6d 65 28 22 5f 62 74 {......Some("_bt
2680: 5f 63 68 65 63 6b 5f 75 6e 69 71 75 65 22 2c 20 _check_unique",
2690: 29 20 3d 3e 20 7b 0a 09 09 09 09 09 09 72 65 74 ) => {.......ret
26a0: 75 72 6e 20 4f 6b 28 22 44 75 70 6c 69 63 61 74 urn Ok("Duplicat
26b0: 65 20 6b 65 79 2e 22 2e 74 6f 5f 73 74 72 69 6e e key.".to_strin
26c0: 67 28 29 29 0a 09 09 09 09 09 7d 2c 0a 09 09 09 g())......},....
26d0: 09 09 53 6f 6d 65 28 5f 29 20 3d 3e 20 7b 0a 09 ..Some(_) => {..
26e0: 09 09 09 09 09 72 65 74 75 72 6e 20 4f 6b 28 22 .....return Ok("
26f0: 44 61 74 61 62 61 73 65 20 65 72 72 6f 72 2e 22 Database error."
2700: 2e 74 6f 5f 73 74 72 69 6e 67 28 29 29 0a 09 09 .to_string())...
2710: 09 09 09 7d 2c 0a 09 09 09 09 09 4e 6f 6e 65 20 ...},......None
2720: 3d 3e 20 7b 0a 09 09 09 09 09 09 72 65 74 75 72 => {.......retur
2730: 6e 20 4f 6b 28 22 4e 6f 20 64 61 74 61 62 61 73 n Ok("No databas
2740: 65 20 65 72 72 6f 72 20 65 78 74 72 61 63 74 65 e error extracte
2750: 64 2e 22 2e 74 6f 5f 73 74 72 69 6e 67 28 29 29 d.".to_string())
2760: 0a 09 09 09 09 09 7d 2c 0a 09 09 09 09 7d 3b 0a ......},.....};.
2770: 09 09 09 7d 2c 0a 09 09 09 45 72 72 28 65 72 72 ...},....Err(err
2780: 29 20 3d 3e 20 7b 0a 09 09 09 09 62 61 69 6c 21 ) => {.....bail!
2790: 28 22 53 6f 72 72 79 2c 20 75 6e 6b 6e 6f 77 6e ("Sorry, unknown
27a0: 20 65 72 72 6f 72 3a 5c 6e 7b 3a 23 3f 7d 5c 6e error:\n{:#?}\n
27b0: 22 2c 20 65 72 72 29 3b 0a 09 09 09 7d 2c 0a 09 ", err);....},..
27c0: 09 7d 3b 0a 09 7d 0a 0a 09 61 73 79 6e 63 20 66 .};..}...async f
27d0: 6e 20 61 75 74 6f 66 65 74 63 68 28 26 73 65 6c n autofetch(&sel
27e0: 66 29 20 2d 3e 20 52 65 73 75 6c 74 3c 28 29 3e f) -> Result<()>
27f0: 20 7b 0a 09 09 6c 65 74 20 6d 75 74 20 64 65 6c {...let mut del
2800: 61 79 20 3d 20 63 68 72 6f 6e 6f 3a 3a 44 75 72 ay = chrono::Dur
2810: 61 74 69 6f 6e 3a 3a 6d 69 6e 75 74 65 73 28 31 ation::minutes(1
2820: 29 3b 0a 09 09 6c 65 74 20 6d 75 74 20 6e 6f 77 );...let mut now
2830: 3b 0a 09 09 6c 6f 6f 70 20 7b 0a 09 09 09 6c 65 ;...loop {....le
2840: 74 20 6d 75 74 20 63 6f 6e 6e 20 3d 20 73 65 6c t mut conn = sel
2850: 66 2e 70 6f 6f 6c 2e 61 63 71 75 69 72 65 28 29 f.pool.acquire()
2860: 2e 61 77 61 69 74 0a 09 09 09 09 2e 77 69 74 68 .await......with
2870: 5f 63 6f 6e 74 65 78 74 28 7c 7c 20 66 6f 72 6d _context(|| form
2880: 61 74 21 28 22 41 75 74 6f 66 65 74 63 68 20 66 at!("Autofetch f
2890: 65 74 63 68 20 63 6f 6e 6e 3a 5c 6e 7b 3a 3f 7d etch conn:\n{:?}
28a0: 22 2c 20 26 73 65 6c 66 2e 70 6f 6f 6c 29 29 3f ", &self.pool))?
28b0: 3b 0a 09 09 09 6e 6f 77 20 3d 20 63 68 72 6f 6e ;....now = chron
28c0: 6f 3a 3a 4c 6f 63 61 6c 3a 3a 6e 6f 77 28 29 3b o::Local::now();
28d0: 0a 09 09 09 6c 65 74 20 6d 75 74 20 71 75 65 75 ....let mut queu
28e0: 65 20 3d 20 73 71 6c 78 3a 3a 71 75 65 72 79 28 e = sqlx::query(
28f0: 22 73 65 6c 65 63 74 20 73 6f 75 72 63 65 5f 69 "select source_i
2900: 64 2c 20 6e 65 78 74 5f 66 65 74 63 68 2c 20 6f d, next_fetch, o
2910: 77 6e 65 72 20 66 72 6f 6d 20 72 73 73 74 67 5f wner from rsstg_
2920: 6f 72 64 65 72 20 6e 61 74 75 72 61 6c 20 6c 65 order natural le
2930: 66 74 20 6a 6f 69 6e 20 72 73 73 74 67 5f 73 6f ft join rsstg_so
2940: 75 72 63 65 20 77 68 65 72 65 20 6e 65 78 74 5f urce where next_
2950: 66 65 74 63 68 20 3c 20 6e 6f 77 28 29 20 2b 20 fetch < now() +
2960: 69 6e 74 65 72 76 61 6c 20 27 31 20 6d 69 6e 75 interval '1 minu
2970: 74 65 27 3b 22 29 0a 09 09 09 09 2e 66 65 74 63 te';")......fetc
2980: 68 5f 61 6c 6c 28 26 6d 75 74 20 63 6f 6e 6e 29 h_all(&mut conn)
2990: 2e 61 77 61 69 74 3f 3b 0a 09 09 09 66 6f 72 20 .await?;....for
29a0: 72 6f 77 20 69 6e 20 71 75 65 75 65 2e 69 74 65 row in queue.ite
29b0: 72 28 29 20 7b 0a 09 09 09 09 6c 65 74 20 73 6f r() {.....let so
29c0: 75 72 63 65 5f 69 64 3a 20 69 33 32 20 3d 20 72 urce_id: i32 = r
29d0: 6f 77 2e 74 72 79 5f 67 65 74 28 22 73 6f 75 72 ow.try_get("sour
29e0: 63 65 5f 69 64 22 29 3f 3b 0a 09 09 09 09 6c 65 ce_id")?;.....le
29f0: 74 20 6f 77 6e 65 72 3a 20 69 36 34 20 3d 20 72 t owner: i64 = r
2a00: 6f 77 2e 74 72 79 5f 67 65 74 28 22 6f 77 6e 65 ow.try_get("owne
2a10: 72 22 29 3f 3b 0a 09 09 09 09 6c 65 74 20 6e 65 r")?;.....let ne
2a20: 78 74 5f 66 65 74 63 68 3a 20 44 61 74 65 54 69 xt_fetch: DateTi
2a30: 6d 65 3c 63 68 72 6f 6e 6f 3a 3a 4c 6f 63 61 6c me<chrono::Local
2a40: 3e 20 3d 20 72 6f 77 2e 74 72 79 5f 67 65 74 28 > = row.try_get(
2a50: 22 6e 65 78 74 5f 66 65 74 63 68 22 29 3f 3b 0a "next_fetch")?;.
2a60: 09 09 09 09 69 66 20 6e 65 78 74 5f 66 65 74 63 ....if next_fetc
2a70: 68 20 3c 20 6e 6f 77 20 7b 0a 09 09 09 09 09 6c h < now {......l
2a80: 65 74 20 63 6c 6f 6e 65 20 3d 20 43 6f 72 65 20 et clone = Core
2a90: 7b 0a 09 09 09 09 09 09 6f 77 6e 65 72 5f 63 68 {.......owner_ch
2aa0: 61 74 3a 20 74 65 6c 65 67 72 61 6d 5f 62 6f 74 at: telegram_bot
2ab0: 3a 3a 55 73 65 72 49 64 3a 3a 6e 65 77 28 6f 77 ::UserId::new(ow
2ac0: 6e 65 72 29 2c 0a 09 09 09 09 09 09 2e 2e 73 65 ner),.........se
2ad0: 6c 66 2e 63 6c 6f 6e 65 28 29 0a 09 09 09 09 09 lf.clone()......
2ae0: 7d 3b 0a 09 09 09 09 09 74 6f 6b 69 6f 3a 3a 73 };......tokio::s
2af0: 70 61 77 6e 28 61 73 79 6e 63 20 6d 6f 76 65 20 pawn(async move
2b00: 7b 0a 09 09 09 09 09 09 69 66 20 6c 65 74 20 45 {.......if let E
2b10: 72 72 28 65 72 72 29 20 3d 20 63 6c 6f 6e 65 2e rr(err) = clone.
2b20: 63 68 65 63 6b 28 26 73 6f 75 72 63 65 5f 69 64 check(&source_id
2b30: 2c 20 6f 77 6e 65 72 2c 20 74 72 75 65 29 2e 61 , owner, true).a
2b40: 77 61 69 74 20 7b 0a 09 09 09 09 09 09 09 69 66 wait {........if
2b50: 20 6c 65 74 20 45 72 72 28 65 72 72 29 20 3d 20 let Err(err) =
2b60: 63 6c 6f 6e 65 2e 73 65 6e 64 28 26 66 6f 72 6d clone.send(&form
2b70: 61 74 21 28 22 f0 9f 9b 91 20 7b 3a 3f 7d 22 2c at!("š {:?}",
2b80: 20 65 72 72 29 2c 20 4e 6f 6e 65 2c 20 4e 6f 6e err), None, Non
2b90: 65 29 20 7b 0a 09 09 09 09 09 09 09 09 65 70 72 e) {.........epr
2ba0: 69 6e 74 6c 6e 21 28 22 43 68 65 63 6b 20 65 72 intln!("Check er
2bb0: 72 6f 72 3a 20 7b 7d 22 2c 20 65 72 72 29 3b 0a ror: {}", err);.
2bc0: 09 09 09 09 09 09 09 7d 3b 0a 09 09 09 09 09 09 .......};.......
2bd0: 7d 3b 0a 09 09 09 09 09 7d 29 3b 0a 09 09 09 09 };......});.....
2be0: 7d 20 65 6c 73 65 20 7b 0a 09 09 09 09 09 69 66 } else {......if
2bf0: 20 6e 65 78 74 5f 66 65 74 63 68 20 2d 20 6e 6f next_fetch - no
2c00: 77 20 3c 20 64 65 6c 61 79 20 7b 0a 09 09 09 09 w < delay {.....
2c10: 09 09 64 65 6c 61 79 20 3d 20 6e 65 78 74 5f 66 ..delay = next_f
2c20: 65 74 63 68 20 2d 20 6e 6f 77 3b 0a 09 09 09 09 etch - now;.....
2c30: 09 7d 0a 09 09 09 09 7d 0a 09 09 09 7d 3b 0a 09 .}.....}....};..
2c40: 09 09 71 75 65 75 65 2e 63 6c 65 61 72 28 29 3b ..queue.clear();
2c50: 0a 09 09 09 74 6f 6b 69 6f 3a 3a 74 69 6d 65 3a ....tokio::time:
2c60: 3a 73 6c 65 65 70 28 64 65 6c 61 79 2e 74 6f 5f :sleep(delay.to_
2c70: 73 74 64 28 29 3f 29 2e 61 77 61 69 74 3b 0a 09 std()?).await;..
2c80: 09 09 64 65 6c 61 79 20 3d 20 63 68 72 6f 6e 6f ..delay = chrono
2c90: 3a 3a 44 75 72 61 74 69 6f 6e 3a 3a 6d 69 6e 75 ::Duration::minu
2ca0: 74 65 73 28 31 29 3b 0a 09 09 7d 0a 09 7d 0a 0a tes(1);...}..}..
2cb0: 09 70 75 62 20 61 73 79 6e 63 20 66 6e 20 6c 69 .pub async fn li
2cc0: 73 74 3c 53 3e 28 26 73 65 6c 66 2c 20 6f 77 6e st<S>(&self, own
2cd0: 65 72 3a 20 53 29 20 2d 3e 20 52 65 73 75 6c 74 er: S) -> Result
2ce0: 3c 53 74 72 69 6e 67 3e 0a 09 77 68 65 72 65 20 <String>..where
2cf0: 53 3a 20 49 6e 74 6f 3c 69 36 34 3e 20 7b 0a 09 S: Into<i64> {..
2d00: 09 6c 65 74 20 6f 77 6e 65 72 20 3d 20 6f 77 6e .let owner = own
2d10: 65 72 2e 69 6e 74 6f 28 29 3b 0a 0a 09 09 6c 65 er.into();....le
2d20: 74 20 6d 75 74 20 72 65 70 6c 79 20 3d 20 76 65 t mut reply = ve
2d30: 63 21 5b 5d 3b 0a 09 09 6c 65 74 20 6d 75 74 20 c![];...let mut
2d40: 63 6f 6e 6e 20 3d 20 73 65 6c 66 2e 70 6f 6f 6c conn = self.pool
2d50: 2e 61 63 71 75 69 72 65 28 29 2e 61 77 61 69 74 .acquire().await
2d60: 0a 09 09 09 2e 77 69 74 68 5f 63 6f 6e 74 65 78 .....with_contex
2d70: 74 28 7c 7c 20 66 6f 72 6d 61 74 21 28 22 4c 69 t(|| format!("Li
2d80: 73 74 20 66 65 74 63 68 20 63 6f 6e 6e 3a 5c 6e st fetch conn:\n
2d90: 7b 3a 3f 7d 22 2c 20 26 73 65 6c 66 2e 70 6f 6f {:?}", &self.poo
2da0: 6c 29 29 3f 3b 0a 09 09 72 65 70 6c 79 2e 70 75 l))?;...reply.pu
2db0: 73 68 28 22 43 68 61 6e 6e 65 6c 73 3a 22 2e 74 sh("Channels:".t
2dc0: 6f 5f 73 74 72 69 6e 67 28 29 29 3b 0a 09 09 6c o_string());...l
2dd0: 65 74 20 72 6f 77 73 20 3d 20 73 71 6c 78 3a 3a et rows = sqlx::
2de0: 71 75 65 72 79 28 22 73 65 6c 65 63 74 20 73 6f query("select so
2df0: 75 72 63 65 5f 69 64 2c 20 63 68 61 6e 6e 65 6c urce_id, channel
2e00: 2c 20 65 6e 61 62 6c 65 64 2c 20 75 72 6c 2c 20 , enabled, url,
2e10: 69 76 5f 68 61 73 68 2c 20 75 72 6c 5f 72 65 20 iv_hash, url_re
2e20: 66 72 6f 6d 20 72 73 73 74 67 5f 73 6f 75 72 63 from rsstg_sourc
2e30: 65 20 77 68 65 72 65 20 6f 77 6e 65 72 20 3d 20 e where owner =
2e40: 24 31 20 6f 72 64 65 72 20 62 79 20 73 6f 75 72 $1 order by sour
2e50: 63 65 5f 69 64 22 29 0a 09 09 09 2e 62 69 6e 64 ce_id").....bind
2e60: 28 6f 77 6e 65 72 29 0a 09 09 09 2e 66 65 74 63 (owner).....fetc
2e70: 68 5f 61 6c 6c 28 26 6d 75 74 20 63 6f 6e 6e 29 h_all(&mut conn)
2e80: 2e 61 77 61 69 74 3f 3b 0a 09 09 66 6f 72 20 72 .await?;...for r
2e90: 6f 77 20 69 6e 20 72 6f 77 73 2e 69 74 65 72 28 ow in rows.iter(
2ea0: 29 20 7b 0a 09 09 09 6c 65 74 20 73 6f 75 72 63 ) {....let sourc
2eb0: 65 5f 69 64 3a 20 69 33 32 20 3d 20 72 6f 77 2e e_id: i32 = row.
2ec0: 74 72 79 5f 67 65 74 28 22 73 6f 75 72 63 65 5f try_get("source_
2ed0: 69 64 22 29 3f 3b 0a 09 09 09 6c 65 74 20 75 73 id")?;....let us
2ee0: 65 72 6e 61 6d 65 3a 20 26 73 74 72 20 3d 20 72 ername: &str = r
2ef0: 6f 77 2e 74 72 79 5f 67 65 74 28 22 63 68 61 6e ow.try_get("chan
2f00: 6e 65 6c 22 29 3f 3b 0a 09 09 09 6c 65 74 20 65 nel")?;....let e
2f10: 6e 61 62 6c 65 64 3a 20 62 6f 6f 6c 20 3d 20 72 nabled: bool = r
2f20: 6f 77 2e 74 72 79 5f 67 65 74 28 22 65 6e 61 62 ow.try_get("enab
2f30: 6c 65 64 22 29 3f 3b 0a 09 09 09 6c 65 74 20 75 led")?;....let u
2f40: 72 6c 3a 20 26 73 74 72 20 3d 20 72 6f 77 2e 74 rl: &str = row.t
2f50: 72 79 5f 67 65 74 28 22 75 72 6c 22 29 3f 3b 0a ry_get("url")?;.
2f60: 09 09 09 6c 65 74 20 69 76 5f 68 61 73 68 3a 20 ...let iv_hash:
2f70: 4f 70 74 69 6f 6e 3c 26 73 74 72 3e 20 3d 20 72 Option<&str> = r
2f80: 6f 77 2e 74 72 79 5f 67 65 74 28 22 69 76 5f 68 ow.try_get("iv_h
2f90: 61 73 68 22 29 3f 3b 0a 09 09 09 6c 65 74 20 75 ash")?;....let u
2fa0: 72 6c 5f 72 65 3a 20 4f 70 74 69 6f 6e 3c 26 73 rl_re: Option<&s
2fb0: 74 72 3e 20 3d 20 72 6f 77 2e 74 72 79 5f 67 65 tr> = row.try_ge
2fc0: 74 28 22 75 72 6c 5f 72 65 22 29 3f 3b 0a 09 09 t("url_re")?;...
2fd0: 09 72 65 70 6c 79 2e 70 75 73 68 28 66 6f 72 6d .reply.push(form
2fe0: 61 74 21 28 22 5c 6e 5c 5c 23 ef b8 8f e2 83 a3 at!("\n\\#ļøā£
2ff0: 20 7b 7d 20 5c 5c 2a ef b8 8f e2 83 a3 20 60 7b {} \\*ļøā£ `{
3000: 7d 60 20 7b 7d 5c 6e f0 9f 94 97 20 60 7b 7d 60 }` {}\nš `{}`
3010: 22 2c 20 73 6f 75 72 63 65 5f 69 64 2c 20 75 73 ", source_id, us
3020: 65 72 6e 61 6d 65 2c 20 20 0a 09 09 09 09 6d 61 ername, .....ma
3030: 74 63 68 20 65 6e 61 62 6c 65 64 20 7b 0a 09 09 tch enabled {...
3040: 09 09 09 74 72 75 65 20 20 3d 3e 20 22 f0 9f 94 ...true => "š
3050: 84 20 65 6e 61 62 6c 65 64 22 2c 0a 09 09 09 09 enabled",.....
3060: 09 66 61 6c 73 65 20 3d 3e 20 22 e2 9b 94 20 64 .false => "ā d
3070: 69 73 61 62 6c 65 64 22 2c 0a 09 09 09 09 7d 2c isabled",.....},
3080: 20 75 72 6c 29 29 3b 0a 09 09 09 69 66 20 6c 65 url));....if le
3090: 74 20 53 6f 6d 65 28 68 61 73 68 29 20 3d 20 69 t Some(hash) = i
30a0: 76 5f 68 61 73 68 20 7b 0a 09 09 09 09 72 65 70 v_hash {.....rep
30b0: 6c 79 2e 70 75 73 68 28 66 6f 72 6d 61 74 21 28 ly.push(format!(
30c0: 22 49 56 3a 20 60 7b 7d 60 22 2c 20 68 61 73 68 "IV: `{}`", hash
30d0: 29 29 3b 0a 09 09 09 7d 0a 09 09 09 69 66 20 6c ));....}....if l
30e0: 65 74 20 53 6f 6d 65 28 72 65 29 20 3d 20 75 72 et Some(re) = ur
30f0: 6c 5f 72 65 20 7b 0a 09 09 09 09 72 65 70 6c 79 l_re {.....reply
3100: 2e 70 75 73 68 28 66 6f 72 6d 61 74 21 28 22 52 .push(format!("R
3110: 45 3a 20 60 7b 7d 60 22 2c 20 72 65 29 29 3b 0a E: `{}`", re));.
3120: 09 09 09 7d 0a 09 09 7d 3b 0a 09 09 4f 6b 28 72 ...}...};...Ok(r
3130: 65 70 6c 79 2e 6a 6f 69 6e 28 22 5c 6e 22 29 29 eply.join("\n"))
3140: 0a 09 7d 0a 7d 0a ..}.}.