Hex Artifact Content
Logged in as anonymous

Artifact 483b69f7a3cb2734e86edc2f9b5cf1fd5523c7f02434ea99ae0bea84412192e3:


0000: 75 73 65 20 73 74 64 3a 3a 7b 0a 09 62 6f 72 72  use std::{..borr
0010: 6f 77 3a 3a 43 6f 77 2c 0a 09 73 79 6e 63 3a 3a  ow::Cow,..sync::
0020: 7b 0a 09 09 41 72 63 2c 0a 09 09 4d 75 74 65 78  {...Arc,...Mutex
0030: 2c 0a 09 7d 2c 0a 7d 3b 0a 0a 75 73 65 20 61 6e  ,..},.};..use an
0040: 79 68 6f 77 3a 3a 7b 0a 09 52 65 73 75 6c 74 2c  yhow::{..Result,
0050: 0a 09 62 61 69 6c 2c 0a 7d 3b 0a 75 73 65 20 63  ..bail,.};.use c
0060: 68 72 6f 6e 6f 3a 3a 7b 0a 09 44 61 74 65 54 69  hrono::{..DateTi
0070: 6d 65 2c 0a 09 46 69 78 65 64 4f 66 66 73 65 74  me,..FixedOffset
0080: 2c 0a 09 4c 6f 63 61 6c 2c 0a 7d 3b 0a 75 73 65  ,..Local,.};.use
0090: 20 73 71 6c 78 3a 3a 7b 0a 09 50 6f 73 74 67 72   sqlx::{..Postgr
00a0: 65 73 2c 0a 09 52 6f 77 2c 0a 09 70 6f 73 74 67  es,..Row,..postg
00b0: 72 65 73 3a 3a 50 67 50 6f 6f 6c 4f 70 74 69 6f  res::PgPoolOptio
00c0: 6e 73 2c 0a 09 70 6f 6f 6c 3a 3a 50 6f 6f 6c 43  ns,..pool::PoolC
00d0: 6f 6e 6e 65 63 74 69 6f 6e 2c 0a 7d 3b 0a 0a 23  onnection,.};..#
00e0: 5b 64 65 72 69 76 65 28 73 71 6c 78 3a 3a 46 72  [derive(sqlx::Fr
00f0: 6f 6d 52 6f 77 2c 20 44 65 62 75 67 29 5d 0a 70  omRow, Debug)].p
0100: 75 62 20 73 74 72 75 63 74 20 4c 69 73 74 20 7b  ub struct List {
0110: 0a 09 70 75 62 20 73 6f 75 72 63 65 5f 69 64 3a  ..pub source_id:
0120: 20 69 33 32 2c 0a 09 70 75 62 20 63 68 61 6e 6e   i32,..pub chann
0130: 65 6c 3a 20 53 74 72 69 6e 67 2c 0a 09 70 75 62  el: String,..pub
0140: 20 65 6e 61 62 6c 65 64 3a 20 62 6f 6f 6c 2c 0a   enabled: bool,.
0150: 09 70 75 62 20 75 72 6c 3a 20 53 74 72 69 6e 67  .pub url: String
0160: 2c 0a 09 70 75 62 20 69 76 5f 68 61 73 68 3a 20  ,..pub iv_hash: 
0170: 4f 70 74 69 6f 6e 3c 53 74 72 69 6e 67 3e 2c 0a  Option<String>,.
0180: 09 70 75 62 20 75 72 6c 5f 72 65 3a 20 4f 70 74  .pub url_re: Opt
0190: 69 6f 6e 3c 53 74 72 69 6e 67 3e 2c 0a 7d 0a 0a  ion<String>,.}..
01a0: 23 5b 64 65 72 69 76 65 28 73 71 6c 78 3a 3a 46  #[derive(sqlx::F
01b0: 72 6f 6d 52 6f 77 2c 20 44 65 62 75 67 29 5d 0a  romRow, Debug)].
01c0: 70 75 62 20 73 74 72 75 63 74 20 53 6f 75 72 63  pub struct Sourc
01d0: 65 20 7b 0a 09 70 75 62 20 63 68 61 6e 6e 65 6c  e {..pub channel
01e0: 5f 69 64 3a 20 69 36 34 2c 0a 09 70 75 62 20 75  _id: i64,..pub u
01f0: 72 6c 3a 20 53 74 72 69 6e 67 2c 0a 09 70 75 62  rl: String,..pub
0200: 20 69 76 5f 68 61 73 68 3a 20 4f 70 74 69 6f 6e   iv_hash: Option
0210: 3c 53 74 72 69 6e 67 3e 2c 0a 09 70 75 62 20 6f  <String>,..pub o
0220: 77 6e 65 72 3a 20 69 36 34 2c 0a 09 70 75 62 20  wner: i64,..pub 
0230: 75 72 6c 5f 72 65 3a 20 4f 70 74 69 6f 6e 3c 53  url_re: Option<S
0240: 74 72 69 6e 67 3e 2c 0a 7d 0a 0a 23 5b 64 65 72  tring>,.}..#[der
0250: 69 76 65 28 73 71 6c 78 3a 3a 46 72 6f 6d 52 6f  ive(sqlx::FromRo
0260: 77 29 5d 0a 70 75 62 20 73 74 72 75 63 74 20 51  w)].pub struct Q
0270: 75 65 75 65 20 7b 0a 09 70 75 62 20 73 6f 75 72  ueue {..pub sour
0280: 63 65 5f 69 64 3a 20 4f 70 74 69 6f 6e 3c 69 33  ce_id: Option<i3
0290: 32 3e 2c 0a 09 70 75 62 20 6e 65 78 74 5f 66 65  2>,..pub next_fe
02a0: 74 63 68 3a 20 4f 70 74 69 6f 6e 3c 44 61 74 65  tch: Option<Date
02b0: 54 69 6d 65 3c 4c 6f 63 61 6c 3e 3e 2c 0a 09 70  Time<Local>>,..p
02c0: 75 62 20 6f 77 6e 65 72 3a 20 4f 70 74 69 6f 6e  ub owner: Option
02d0: 3c 69 36 34 3e 2c 0a 7d 0a 0a 23 5b 64 65 72 69  <i64>,.}..#[deri
02e0: 76 65 28 43 6c 6f 6e 65 29 5d 0a 70 75 62 20 73  ve(Clone)].pub s
02f0: 74 72 75 63 74 20 44 62 20 7b 0a 09 70 6f 6f 6c  truct Db {..pool
0300: 3a 20 41 72 63 3c 4d 75 74 65 78 3c 41 72 63 3c  : Arc<Mutex<Arc<
0310: 73 71 6c 78 3a 3a 50 6f 6f 6c 3c 73 71 6c 78 3a  sqlx::Pool<sqlx:
0320: 3a 50 6f 73 74 67 72 65 73 3e 3e 3e 3e 2c 0a 7d  :Postgres>>>>,.}
0330: 0a 0a 70 75 62 20 73 74 72 75 63 74 20 43 6f 6e  ..pub struct Con
0340: 6e 7b 0a 09 63 6f 6e 6e 3a 20 50 6f 6f 6c 43 6f  n{..conn: PoolCo
0350: 6e 6e 65 63 74 69 6f 6e 3c 50 6f 73 74 67 72 65  nnection<Postgre
0360: 73 3e 2c 0a 7d 0a 0a 69 6d 70 6c 20 44 62 20 7b  s>,.}..impl Db {
0370: 0a 09 70 75 62 20 66 6e 20 6e 65 77 20 28 70 67  ..pub fn new (pg
0380: 75 72 69 3a 20 26 73 74 72 29 20 2d 3e 20 52 65  uri: &str) -> Re
0390: 73 75 6c 74 3c 44 62 3e 20 7b 0a 09 09 4f 6b 28  sult<Db> {...Ok(
03a0: 44 62 7b 0a 09 09 09 70 6f 6f 6c 3a 20 41 72 63  Db{....pool: Arc
03b0: 3a 3a 6e 65 77 28 4d 75 74 65 78 3a 3a 6e 65 77  ::new(Mutex::new
03c0: 28 41 72 63 3a 3a 6e 65 77 28 50 67 50 6f 6f 6c  (Arc::new(PgPool
03d0: 4f 70 74 69 6f 6e 73 3a 3a 6e 65 77 28 29 0a 09  Options::new()..
03e0: 09 09 09 2e 6d 61 78 5f 63 6f 6e 6e 65 63 74 69  ....max_connecti
03f0: 6f 6e 73 28 35 29 0a 09 09 09 09 2e 61 63 71 75  ons(5)......acqu
0400: 69 72 65 5f 74 69 6d 65 6f 75 74 28 73 74 64 3a  ire_timeout(std:
0410: 3a 74 69 6d 65 3a 3a 44 75 72 61 74 69 6f 6e 3a  :time::Duration:
0420: 3a 6e 65 77 28 33 30 30 2c 20 30 29 29 0a 09 09  :new(300, 0))...
0430: 09 09 2e 69 64 6c 65 5f 74 69 6d 65 6f 75 74 28  ...idle_timeout(
0440: 73 74 64 3a 3a 74 69 6d 65 3a 3a 44 75 72 61 74  std::time::Durat
0450: 69 6f 6e 3a 3a 6e 65 77 28 36 30 2c 20 30 29 29  ion::new(60, 0))
0460: 0a 09 09 09 09 2e 63 6f 6e 6e 65 63 74 5f 6c 61  ......connect_la
0470: 7a 79 28 70 67 75 72 69 29 3f 29 29 29 2c 0a 09  zy(pguri)?))),..
0480: 09 7d 29 0a 09 7d 0a 0a 09 70 75 62 20 61 73 79  .})..}...pub asy
0490: 6e 63 20 66 6e 20 62 65 67 69 6e 28 26 73 65 6c  nc fn begin(&sel
04a0: 66 29 20 2d 3e 20 52 65 73 75 6c 74 3c 43 6f 6e  f) -> Result<Con
04b0: 6e 3e 20 7b 0a 09 09 6c 65 74 20 70 6f 6f 6c 20  n> {...let pool 
04c0: 3d 20 73 65 6c 66 2e 70 6f 6f 6c 2e 6c 6f 63 6b  = self.pool.lock
04d0: 28 29 2e 75 6e 77 72 61 70 28 29 2e 63 6c 6f 6e  ().unwrap().clon
04e0: 65 28 29 3b 0a 09 09 6c 65 74 20 63 6f 6e 6e 20  e();...let conn 
04f0: 3d 20 43 6f 6e 6e 3a 3a 6e 65 77 28 70 6f 6f 6c  = Conn::new(pool
0500: 2e 61 63 71 75 69 72 65 28 29 2e 61 77 61 69 74  .acquire().await
0510: 3f 29 2e 61 77 61 69 74 3f 3b 0a 09 09 4f 6b 28  ?).await?;...Ok(
0520: 63 6f 6e 6e 29 0a 09 7d 0a 7d 0a 0a 69 6d 70 6c  conn)..}.}..impl
0530: 20 43 6f 6e 6e 20 7b 0a 09 70 75 62 20 61 73 79   Conn {..pub asy
0540: 6e 63 20 66 6e 20 6e 65 77 20 28 63 6f 6e 6e 3a  nc fn new (conn:
0550: 20 50 6f 6f 6c 43 6f 6e 6e 65 63 74 69 6f 6e 3c   PoolConnection<
0560: 50 6f 73 74 67 72 65 73 3e 29 20 2d 3e 20 52 65  Postgres>) -> Re
0570: 73 75 6c 74 3c 43 6f 6e 6e 3e 20 7b 0a 09 09 4f  sult<Conn> {...O
0580: 6b 28 43 6f 6e 6e 7b 0a 09 09 09 63 6f 6e 6e 2c  k(Conn{....conn,
0590: 0a 09 09 7d 29 0a 09 7d 0a 0a 09 70 75 62 20 61  ...})..}...pub a
05a0: 73 79 6e 63 20 66 6e 20 61 64 64 5f 70 6f 73 74  sync fn add_post
05b0: 20 28 26 6d 75 74 20 73 65 6c 66 2c 20 73 6f 75   (&mut self, sou
05c0: 72 63 65 5f 69 64 3a 20 69 33 32 2c 20 64 61 74  rce_id: i32, dat
05d0: 65 3a 20 26 44 61 74 65 54 69 6d 65 3c 46 69 78  e: &DateTime<Fix
05e0: 65 64 4f 66 66 73 65 74 3e 2c 20 70 6f 73 74 5f  edOffset>, post_
05f0: 75 72 6c 3a 20 26 73 74 72 29 20 2d 3e 20 52 65  url: &str) -> Re
0600: 73 75 6c 74 3c 28 29 3e 20 7b 0a 09 09 73 71 6c  sult<()> {...sql
0610: 78 3a 3a 71 75 65 72 79 28 22 69 6e 73 65 72 74  x::query("insert
0620: 20 69 6e 74 6f 20 72 73 73 74 67 5f 70 6f 73 74   into rsstg_post
0630: 20 28 73 6f 75 72 63 65 5f 69 64 2c 20 70 6f 73   (source_id, pos
0640: 74 65 64 2c 20 75 72 6c 29 20 76 61 6c 75 65 73  ted, url) values
0650: 20 28 24 31 2c 20 24 32 2c 20 24 33 29 3b 22 29   ($1, $2, $3);")
0660: 0a 09 09 09 2e 62 69 6e 64 28 73 6f 75 72 63 65  .....bind(source
0670: 5f 69 64 29 0a 09 09 09 2e 62 69 6e 64 28 64 61  _id).....bind(da
0680: 74 65 29 0a 09 09 09 2e 62 69 6e 64 28 70 6f 73  te).....bind(pos
0690: 74 5f 75 72 6c 29 0a 09 09 09 2e 65 78 65 63 75  t_url).....execu
06a0: 74 65 28 26 6d 75 74 20 2a 73 65 6c 66 2e 63 6f  te(&mut *self.co
06b0: 6e 6e 29 2e 61 77 61 69 74 3f 3b 0a 09 09 4f 6b  nn).await?;...Ok
06c0: 28 28 29 29 0a 09 7d 0a 0a 09 70 75 62 20 61 73  (())..}...pub as
06d0: 79 6e 63 20 66 6e 20 63 6c 65 61 6e 20 3c 49 3e  ync fn clean <I>
06e0: 20 28 26 6d 75 74 20 73 65 6c 66 2c 20 73 6f 75   (&mut self, sou
06f0: 72 63 65 5f 69 64 3a 20 69 33 32 2c 20 6f 77 6e  rce_id: i32, own
0700: 65 72 3a 20 49 29 20 2d 3e 20 52 65 73 75 6c 74  er: I) -> Result
0710: 3c 43 6f 77 3c 27 5f 2c 20 73 74 72 3e 3e 0a 09  <Cow<'_, str>>..
0720: 77 68 65 72 65 20 49 3a 20 49 6e 74 6f 3c 69 36  where I: Into<i6
0730: 34 3e 20 7b 0a 09 09 6d 61 74 63 68 20 73 71 6c  4> {...match sql
0740: 78 3a 3a 71 75 65 72 79 28 22 64 65 6c 65 74 65  x::query("delete
0750: 20 66 72 6f 6d 20 72 73 73 74 67 5f 70 6f 73 74   from rsstg_post
0760: 20 70 20 75 73 69 6e 67 20 72 73 73 74 67 5f 73   p using rsstg_s
0770: 6f 75 72 63 65 20 73 20 77 68 65 72 65 20 70 2e  ource s where p.
0780: 73 6f 75 72 63 65 5f 69 64 20 3d 20 24 31 20 61  source_id = $1 a
0790: 6e 64 20 6f 77 6e 65 72 20 3d 20 24 32 20 61 6e  nd owner = $2 an
07a0: 64 20 70 2e 73 6f 75 72 63 65 5f 69 64 20 3d 20  d p.source_id = 
07b0: 73 2e 73 6f 75 72 63 65 5f 69 64 3b 22 29 0a 09  s.source_id;")..
07c0: 09 09 2e 62 69 6e 64 28 73 6f 75 72 63 65 5f 69  ...bind(source_i
07d0: 64 29 0a 09 09 09 2e 62 69 6e 64 28 6f 77 6e 65  d).....bind(owne
07e0: 72 2e 69 6e 74 6f 28 29 29 0a 09 09 09 2e 65 78  r.into()).....ex
07f0: 65 63 75 74 65 28 26 6d 75 74 20 2a 73 65 6c 66  ecute(&mut *self
0800: 2e 63 6f 6e 6e 29 2e 61 77 61 69 74 3f 2e 72 6f  .conn).await?.ro
0810: 77 73 5f 61 66 66 65 63 74 65 64 28 29 20 7b 0a  ws_affected() {.
0820: 09 09 09 30 20 3d 3e 20 7b 20 4f 6b 28 22 4e 6f  ...0 => { Ok("No
0830: 20 64 61 74 61 20 66 6f 75 6e 64 20 66 6f 75 6e   data found foun
0840: 64 2e 22 2e 69 6e 74 6f 28 29 29 20 7d 2c 0a 09  d.".into()) },..
0850: 09 09 78 20 3d 3e 20 7b 20 4f 6b 28 66 6f 72 6d  ..x => { Ok(form
0860: 61 74 21 28 22 7b 78 7d 20 70 6f 73 74 73 20 70  at!("{x} posts p
0870: 75 72 67 65 64 2e 22 29 2e 69 6e 74 6f 28 29 29  urged.").into())
0880: 20 7d 2c 0a 09 09 7d 0a 09 7d 0a 0a 09 70 75 62   },...}..}...pub
0890: 20 61 73 79 6e 63 20 66 6e 20 64 65 6c 65 74 65   async fn delete
08a0: 20 3c 49 3e 20 28 26 6d 75 74 20 73 65 6c 66 2c   <I> (&mut self,
08b0: 20 73 6f 75 72 63 65 5f 69 64 3a 20 69 33 32 2c   source_id: i32,
08c0: 20 6f 77 6e 65 72 3a 20 49 29 20 2d 3e 20 52 65   owner: I) -> Re
08d0: 73 75 6c 74 3c 43 6f 77 3c 27 5f 2c 20 73 74 72  sult<Cow<'_, str
08e0: 3e 3e 0a 09 77 68 65 72 65 20 49 3a 20 49 6e 74  >>..where I: Int
08f0: 6f 3c 69 36 34 3e 20 7b 0a 09 09 6d 61 74 63 68  o<i64> {...match
0900: 20 73 71 6c 78 3a 3a 71 75 65 72 79 28 22 64 65   sqlx::query("de
0910: 6c 65 74 65 20 66 72 6f 6d 20 72 73 73 74 67 5f  lete from rsstg_
0920: 73 6f 75 72 63 65 20 77 68 65 72 65 20 73 6f 75  source where sou
0930: 72 63 65 5f 69 64 20 3d 20 24 31 20 61 6e 64 20  rce_id = $1 and 
0940: 6f 77 6e 65 72 20 3d 20 24 32 3b 22 29 0a 09 09  owner = $2;")...
0950: 09 2e 62 69 6e 64 28 73 6f 75 72 63 65 5f 69 64  ..bind(source_id
0960: 29 0a 09 09 09 2e 62 69 6e 64 28 6f 77 6e 65 72  ).....bind(owner
0970: 2e 69 6e 74 6f 28 29 29 0a 09 09 09 2e 65 78 65  .into()).....exe
0980: 63 75 74 65 28 26 6d 75 74 20 2a 73 65 6c 66 2e  cute(&mut *self.
0990: 63 6f 6e 6e 29 2e 61 77 61 69 74 3f 2e 72 6f 77  conn).await?.row
09a0: 73 5f 61 66 66 65 63 74 65 64 28 29 20 7b 0a 09  s_affected() {..
09b0: 09 09 30 20 3d 3e 20 7b 20 4f 6b 28 22 4e 6f 20  ..0 => { Ok("No 
09c0: 64 61 74 61 20 66 6f 75 6e 64 20 66 6f 75 6e 64  data found found
09d0: 2e 22 2e 69 6e 74 6f 28 29 29 20 7d 2c 0a 09 09  .".into()) },...
09e0: 09 78 20 3d 3e 20 7b 20 4f 6b 28 66 6f 72 6d 61  .x => { Ok(forma
09f0: 74 21 28 22 7b 7d 20 73 6f 75 72 63 65 73 20 72  t!("{} sources r
0a00: 65 6d 6f 76 65 64 2e 22 2c 20 78 29 2e 69 6e 74  emoved.", x).int
0a10: 6f 28 29 29 20 7d 2c 0a 09 09 7d 0a 09 7d 0a 0a  o()) },...}..}..
0a20: 09 70 75 62 20 61 73 79 6e 63 20 66 6e 20 64 69  .pub async fn di
0a30: 73 61 62 6c 65 20 3c 49 3e 20 28 26 6d 75 74 20  sable <I> (&mut 
0a40: 73 65 6c 66 2c 20 73 6f 75 72 63 65 5f 69 64 3a  self, source_id:
0a50: 20 69 33 32 2c 20 6f 77 6e 65 72 3a 20 49 29 20   i32, owner: I) 
0a60: 2d 3e 20 52 65 73 75 6c 74 3c 26 73 74 72 3e 0a  -> Result<&str>.
0a70: 09 77 68 65 72 65 20 49 3a 20 49 6e 74 6f 3c 69  .where I: Into<i
0a80: 36 34 3e 20 7b 0a 09 09 6d 61 74 63 68 20 73 71  64> {...match sq
0a90: 6c 78 3a 3a 71 75 65 72 79 28 22 75 70 64 61 74  lx::query("updat
0aa0: 65 20 72 73 73 74 67 5f 73 6f 75 72 63 65 20 73  e rsstg_source s
0ab0: 65 74 20 65 6e 61 62 6c 65 64 20 3d 20 66 61 6c  et enabled = fal
0ac0: 73 65 20 77 68 65 72 65 20 73 6f 75 72 63 65 5f  se where source_
0ad0: 69 64 20 3d 20 24 31 20 61 6e 64 20 6f 77 6e 65  id = $1 and owne
0ae0: 72 20 3d 20 24 32 22 29 0a 09 09 09 2e 62 69 6e  r = $2").....bin
0af0: 64 28 73 6f 75 72 63 65 5f 69 64 29 0a 09 09 09  d(source_id)....
0b00: 2e 62 69 6e 64 28 6f 77 6e 65 72 2e 69 6e 74 6f  .bind(owner.into
0b10: 28 29 29 0a 09 09 09 2e 65 78 65 63 75 74 65 28  ()).....execute(
0b20: 26 6d 75 74 20 2a 73 65 6c 66 2e 63 6f 6e 6e 29  &mut *self.conn)
0b30: 2e 61 77 61 69 74 3f 2e 72 6f 77 73 5f 61 66 66  .await?.rows_aff
0b40: 65 63 74 65 64 28 29 20 7b 0a 09 09 09 31 20 3d  ected() {....1 =
0b50: 3e 20 7b 20 4f 6b 28 22 53 6f 75 72 63 65 20 64  > { Ok("Source d
0b60: 69 73 61 62 6c 65 64 2e 22 29 20 7d 2c 0a 09 09  isabled.") },...
0b70: 09 30 20 3d 3e 20 7b 20 4f 6b 28 22 53 6f 75 72  .0 => { Ok("Sour
0b80: 63 65 20 6e 6f 74 20 66 6f 75 6e 64 2e 22 29 20  ce not found.") 
0b90: 7d 2c 0a 09 09 09 5f 20 3d 3e 20 7b 20 62 61 69  },...._ => { bai
0ba0: 6c 21 28 22 44 61 74 61 62 61 73 65 20 65 72 72  l!("Database err
0bb0: 6f 72 2e 22 29 20 7d 2c 0a 09 09 7d 0a 09 7d 0a  or.") },...}..}.
0bc0: 0a 09 70 75 62 20 61 73 79 6e 63 20 66 6e 20 65  ..pub async fn e
0bd0: 6e 61 62 6c 65 20 3c 49 3e 20 28 26 6d 75 74 20  nable <I> (&mut 
0be0: 73 65 6c 66 2c 20 73 6f 75 72 63 65 5f 69 64 3a  self, source_id:
0bf0: 20 69 33 32 2c 20 6f 77 6e 65 72 3a 20 49 29 20   i32, owner: I) 
0c00: 2d 3e 20 52 65 73 75 6c 74 3c 26 73 74 72 3e 0a  -> Result<&str>.
0c10: 09 77 68 65 72 65 20 49 3a 20 49 6e 74 6f 3c 69  .where I: Into<i
0c20: 36 34 3e 20 7b 0a 09 09 6d 61 74 63 68 20 73 71  64> {...match sq
0c30: 6c 78 3a 3a 71 75 65 72 79 28 22 75 70 64 61 74  lx::query("updat
0c40: 65 20 72 73 73 74 67 5f 73 6f 75 72 63 65 20 73  e rsstg_source s
0c50: 65 74 20 65 6e 61 62 6c 65 64 20 3d 20 74 72 75  et enabled = tru
0c60: 65 20 77 68 65 72 65 20 73 6f 75 72 63 65 5f 69  e where source_i
0c70: 64 20 3d 20 24 31 20 61 6e 64 20 6f 77 6e 65 72  d = $1 and owner
0c80: 20 3d 20 24 32 22 29 0a 09 09 09 2e 62 69 6e 64   = $2").....bind
0c90: 28 73 6f 75 72 63 65 5f 69 64 29 0a 09 09 09 2e  (source_id).....
0ca0: 62 69 6e 64 28 6f 77 6e 65 72 2e 69 6e 74 6f 28  bind(owner.into(
0cb0: 29 29 0a 09 09 09 2e 65 78 65 63 75 74 65 28 26  )).....execute(&
0cc0: 6d 75 74 20 2a 73 65 6c 66 2e 63 6f 6e 6e 29 2e  mut *self.conn).
0cd0: 61 77 61 69 74 3f 2e 72 6f 77 73 5f 61 66 66 65  await?.rows_affe
0ce0: 63 74 65 64 28 29 20 7b 0a 09 09 09 31 20 3d 3e  cted() {....1 =>
0cf0: 20 7b 20 4f 6b 28 22 53 6f 75 72 63 65 20 65 6e   { Ok("Source en
0d00: 61 62 6c 65 64 2e 22 29 20 7d 2c 0a 09 09 09 30  abled.") },....0
0d10: 20 3d 3e 20 7b 20 4f 6b 28 22 53 6f 75 72 63 65   => { Ok("Source
0d20: 20 6e 6f 74 20 66 6f 75 6e 64 2e 22 29 20 7d 2c   not found.") },
0d30: 0a 09 09 09 5f 20 3d 3e 20 7b 20 62 61 69 6c 21  ...._ => { bail!
0d40: 28 22 44 61 74 61 62 61 73 65 20 65 72 72 6f 72  ("Database error
0d50: 2e 22 29 20 7d 2c 0a 09 09 7d 0a 09 7d 0a 0a 09  .") },...}..}...
0d60: 70 75 62 20 61 73 79 6e 63 20 66 6e 20 65 78 69  pub async fn exi
0d70: 73 74 73 20 3c 49 3e 20 28 26 6d 75 74 20 73 65  sts <I> (&mut se
0d80: 6c 66 2c 20 70 6f 73 74 5f 75 72 6c 3a 20 26 73  lf, post_url: &s
0d90: 74 72 2c 20 69 64 3a 20 49 29 20 2d 3e 20 52 65  tr, id: I) -> Re
0da0: 73 75 6c 74 3c 4f 70 74 69 6f 6e 3c 62 6f 6f 6c  sult<Option<bool
0db0: 3e 3e 0a 09 77 68 65 72 65 20 49 3a 20 49 6e 74  >>..where I: Int
0dc0: 6f 3c 69 36 34 3e 20 7b 0a 09 09 6c 65 74 20 72  o<i64> {...let r
0dd0: 6f 77 20 3d 20 73 71 6c 78 3a 3a 71 75 65 72 79  ow = sqlx::query
0de0: 28 22 73 65 6c 65 63 74 20 65 78 69 73 74 73 28  ("select exists(
0df0: 73 65 6c 65 63 74 20 74 72 75 65 20 66 72 6f 6d  select true from
0e00: 20 72 73 73 74 67 5f 70 6f 73 74 20 77 68 65 72   rsstg_post wher
0e10: 65 20 75 72 6c 20 3d 20 24 31 20 61 6e 64 20 73  e url = $1 and s
0e20: 6f 75 72 63 65 5f 69 64 20 3d 20 24 32 29 20 61  ource_id = $2) a
0e30: 73 20 65 78 69 73 74 73 3b 22 29 0a 09 09 09 2e  s exists;").....
0e40: 62 69 6e 64 28 70 6f 73 74 5f 75 72 6c 29 0a 09  bind(post_url)..
0e50: 09 09 2e 62 69 6e 64 28 69 64 2e 69 6e 74 6f 28  ...bind(id.into(
0e60: 29 29 0a 09 09 09 2e 66 65 74 63 68 5f 6f 6e 65  )).....fetch_one
0e70: 28 26 6d 75 74 20 2a 73 65 6c 66 2e 63 6f 6e 6e  (&mut *self.conn
0e80: 29 2e 61 77 61 69 74 3f 3b 0a 09 09 6c 65 74 20  ).await?;...let 
0e90: 65 78 69 73 74 73 3a 20 4f 70 74 69 6f 6e 3c 62  exists: Option<b
0ea0: 6f 6f 6c 3e 20 3d 20 72 6f 77 2e 74 72 79 5f 67  ool> = row.try_g
0eb0: 65 74 28 22 65 78 69 73 74 73 22 29 3f 3b 0a 09  et("exists")?;..
0ec0: 09 4f 6b 28 65 78 69 73 74 73 29 0a 09 7d 0a 0a  .Ok(exists)..}..
0ed0: 09 70 75 62 20 61 73 79 6e 63 20 66 6e 20 67 65  .pub async fn ge
0ee0: 74 5f 71 75 65 75 65 20 28 26 6d 75 74 20 73 65  t_queue (&mut se
0ef0: 6c 66 29 20 2d 3e 20 52 65 73 75 6c 74 3c 56 65  lf) -> Result<Ve
0f00: 63 3c 51 75 65 75 65 3e 3e 20 7b 0a 09 09 6c 65  c<Queue>> {...le
0f10: 74 20 62 6c 6f 63 6b 3a 20 56 65 63 3c 51 75 65  t block: Vec<Que
0f20: 75 65 3e 20 3d 20 73 71 6c 78 3a 3a 71 75 65 72  ue> = sqlx::quer
0f30: 79 5f 61 73 28 22 73 65 6c 65 63 74 20 73 6f 75  y_as("select sou
0f40: 72 63 65 5f 69 64 2c 20 6e 65 78 74 5f 66 65 74  rce_id, next_fet
0f50: 63 68 2c 20 6f 77 6e 65 72 20 66 72 6f 6d 20 72  ch, owner from r
0f60: 73 73 74 67 5f 6f 72 64 65 72 20 6e 61 74 75 72  sstg_order natur
0f70: 61 6c 20 6c 65 66 74 20 6a 6f 69 6e 20 72 73 73  al left join rss
0f80: 74 67 5f 73 6f 75 72 63 65 20 77 68 65 72 65 20  tg_source where 
0f90: 6e 65 78 74 5f 66 65 74 63 68 20 3c 20 6e 6f 77  next_fetch < now
0fa0: 28 29 20 2b 20 69 6e 74 65 72 76 61 6c 20 27 31  () + interval '1
0fb0: 20 6d 69 6e 75 74 65 27 3b 22 29 0a 09 09 09 2e   minute';").....
0fc0: 66 65 74 63 68 5f 61 6c 6c 28 26 6d 75 74 20 2a  fetch_all(&mut *
0fd0: 73 65 6c 66 2e 63 6f 6e 6e 29 2e 61 77 61 69 74  self.conn).await
0fe0: 3f 3b 0a 09 09 4f 6b 28 62 6c 6f 63 6b 29 0a 09  ?;...Ok(block)..
0ff0: 7d 0a 0a 09 70 75 62 20 61 73 79 6e 63 20 66 6e  }...pub async fn
1000: 20 67 65 74 5f 6c 69 73 74 20 3c 49 3e 20 28 26   get_list <I> (&
1010: 6d 75 74 20 73 65 6c 66 2c 20 6f 77 6e 65 72 3a  mut self, owner:
1020: 20 49 29 20 2d 3e 20 52 65 73 75 6c 74 3c 56 65   I) -> Result<Ve
1030: 63 3c 4c 69 73 74 3e 3e 0a 09 77 68 65 72 65 20  c<List>>..where 
1040: 49 3a 20 49 6e 74 6f 3c 69 36 34 3e 20 7b 0a 09  I: Into<i64> {..
1050: 09 6c 65 74 20 73 6f 75 72 63 65 3a 20 56 65 63  .let source: Vec
1060: 3c 4c 69 73 74 3e 20 3d 20 73 71 6c 78 3a 3a 71  <List> = sqlx::q
1070: 75 65 72 79 5f 61 73 28 22 73 65 6c 65 63 74 20  uery_as("select 
1080: 73 6f 75 72 63 65 5f 69 64 2c 20 63 68 61 6e 6e  source_id, chann
1090: 65 6c 2c 20 65 6e 61 62 6c 65 64 2c 20 75 72 6c  el, enabled, url
10a0: 2c 20 69 76 5f 68 61 73 68 2c 20 75 72 6c 5f 72  , iv_hash, url_r
10b0: 65 20 66 72 6f 6d 20 72 73 73 74 67 5f 73 6f 75  e from rsstg_sou
10c0: 72 63 65 20 77 68 65 72 65 20 6f 77 6e 65 72 20  rce where owner 
10d0: 3d 20 24 31 20 6f 72 64 65 72 20 62 79 20 73 6f  = $1 order by so
10e0: 75 72 63 65 5f 69 64 22 29 0a 09 09 09 2e 62 69  urce_id").....bi
10f0: 6e 64 28 6f 77 6e 65 72 2e 69 6e 74 6f 28 29 29  nd(owner.into())
1100: 0a 09 09 09 2e 66 65 74 63 68 5f 61 6c 6c 28 26  .....fetch_all(&
1110: 6d 75 74 20 2a 73 65 6c 66 2e 63 6f 6e 6e 29 2e  mut *self.conn).
1120: 61 77 61 69 74 3f 3b 0a 09 09 4f 6b 28 73 6f 75  await?;...Ok(sou
1130: 72 63 65 29 0a 09 7d 0a 0a 09 70 75 62 20 61 73  rce)..}...pub as
1140: 79 6e 63 20 66 6e 20 67 65 74 5f 73 6f 75 72 63  ync fn get_sourc
1150: 65 20 3c 49 3e 20 28 26 6d 75 74 20 73 65 6c 66  e <I> (&mut self
1160: 2c 20 69 64 3a 20 69 33 32 2c 20 6f 77 6e 65 72  , id: i32, owner
1170: 3a 20 49 29 20 2d 3e 20 52 65 73 75 6c 74 3c 53  : I) -> Result<S
1180: 6f 75 72 63 65 3e 0a 09 77 68 65 72 65 20 49 3a  ource>..where I:
1190: 20 49 6e 74 6f 3c 69 36 34 3e 20 7b 0a 09 09 6c   Into<i64> {...l
11a0: 65 74 20 73 6f 75 72 63 65 3a 20 53 6f 75 72 63  et source: Sourc
11b0: 65 20 3d 20 73 71 6c 78 3a 3a 71 75 65 72 79 5f  e = sqlx::query_
11c0: 61 73 28 22 73 65 6c 65 63 74 20 63 68 61 6e 6e  as("select chann
11d0: 65 6c 5f 69 64 2c 20 75 72 6c 2c 20 69 76 5f 68  el_id, url, iv_h
11e0: 61 73 68 2c 20 6f 77 6e 65 72 2c 20 75 72 6c 5f  ash, owner, url_
11f0: 72 65 20 66 72 6f 6d 20 72 73 73 74 67 5f 73 6f  re from rsstg_so
1200: 75 72 63 65 20 77 68 65 72 65 20 73 6f 75 72 63  urce where sourc
1210: 65 5f 69 64 20 3d 20 24 31 20 61 6e 64 20 6f 77  e_id = $1 and ow
1220: 6e 65 72 20 3d 20 24 32 22 29 0a 09 09 09 2e 62  ner = $2").....b
1230: 69 6e 64 28 69 64 29 0a 09 09 09 2e 62 69 6e 64  ind(id).....bind
1240: 28 6f 77 6e 65 72 2e 69 6e 74 6f 28 29 29 0a 09  (owner.into())..
1250: 09 09 2e 66 65 74 63 68 5f 6f 6e 65 28 26 6d 75  ...fetch_one(&mu
1260: 74 20 2a 73 65 6c 66 2e 63 6f 6e 6e 29 2e 61 77  t *self.conn).aw
1270: 61 69 74 3f 3b 0a 09 09 4f 6b 28 73 6f 75 72 63  ait?;...Ok(sourc
1280: 65 29 0a 09 7d 0a 0a 09 70 75 62 20 61 73 79 6e  e)..}...pub asyn
1290: 63 20 66 6e 20 73 65 74 5f 73 63 72 61 70 65 20  c fn set_scrape 
12a0: 3c 49 3e 20 28 26 6d 75 74 20 73 65 6c 66 2c 20  <I> (&mut self, 
12b0: 69 64 3a 20 49 29 20 2d 3e 20 52 65 73 75 6c 74  id: I) -> Result
12c0: 3c 28 29 3e 0a 09 77 68 65 72 65 20 49 3a 20 49  <()>..where I: I
12d0: 6e 74 6f 3c 69 36 34 3e 20 7b 0a 09 09 73 71 6c  nto<i64> {...sql
12e0: 78 3a 3a 71 75 65 72 79 28 22 75 70 64 61 74 65  x::query("update
12f0: 20 72 73 73 74 67 5f 73 6f 75 72 63 65 20 73 65   rsstg_source se
1300: 74 20 6c 61 73 74 5f 73 63 72 61 70 65 20 3d 20  t last_scrape = 
1310: 6e 6f 77 28 29 20 77 68 65 72 65 20 73 6f 75 72  now() where sour
1320: 63 65 5f 69 64 20 3d 20 24 31 3b 22 29 0a 09 09  ce_id = $1;")...
1330: 09 2e 62 69 6e 64 28 69 64 2e 69 6e 74 6f 28 29  ..bind(id.into()
1340: 29 0a 09 09 09 2e 65 78 65 63 75 74 65 28 26 6d  ).....execute(&m
1350: 75 74 20 2a 73 65 6c 66 2e 63 6f 6e 6e 29 2e 61  ut *self.conn).a
1360: 77 61 69 74 3f 3b 0a 09 09 4f 6b 28 28 29 29 0a  wait?;...Ok(()).
1370: 09 7d 0a 0a 09 70 75 62 20 61 73 79 6e 63 20 66  .}...pub async f
1380: 6e 20 75 70 64 61 74 65 20 3c 49 3e 20 28 26 6d  n update <I> (&m
1390: 75 74 20 73 65 6c 66 2c 20 75 70 64 61 74 65 3a  ut self, update:
13a0: 20 4f 70 74 69 6f 6e 3c 69 33 32 3e 2c 20 63 68   Option<i32>, ch
13b0: 61 6e 6e 65 6c 3a 20 26 73 74 72 2c 20 63 68 61  annel: &str, cha
13c0: 6e 6e 65 6c 5f 69 64 3a 20 69 36 34 2c 20 75 72  nnel_id: i64, ur
13d0: 6c 3a 20 26 73 74 72 2c 20 69 76 5f 68 61 73 68  l: &str, iv_hash
13e0: 3a 20 4f 70 74 69 6f 6e 3c 26 73 74 72 3e 2c 20  : Option<&str>, 
13f0: 75 72 6c 5f 72 65 3a 20 4f 70 74 69 6f 6e 3c 26  url_re: Option<&
1400: 73 74 72 3e 2c 20 6f 77 6e 65 72 3a 20 49 29 20  str>, owner: I) 
1410: 2d 3e 20 52 65 73 75 6c 74 3c 26 73 74 72 3e 0a  -> Result<&str>.
1420: 09 77 68 65 72 65 20 49 3a 20 49 6e 74 6f 3c 69  .where I: Into<i
1430: 36 34 3e 20 7b 0a 09 09 6d 61 74 63 68 20 6d 61  64> {...match ma
1440: 74 63 68 20 75 70 64 61 74 65 20 7b 0a 09 09 09  tch update {....
1450: 09 53 6f 6d 65 28 69 64 29 20 3d 3e 20 7b 0a 09  .Some(id) => {..
1460: 09 09 09 09 73 71 6c 78 3a 3a 71 75 65 72 79 28  ....sqlx::query(
1470: 22 75 70 64 61 74 65 20 72 73 73 74 67 5f 73 6f  "update rsstg_so
1480: 75 72 63 65 20 73 65 74 20 63 68 61 6e 6e 65 6c  urce set channel
1490: 5f 69 64 20 3d 20 24 32 2c 20 75 72 6c 20 3d 20  _id = $2, url = 
14a0: 24 33 2c 20 69 76 5f 68 61 73 68 20 3d 20 24 34  $3, iv_hash = $4
14b0: 2c 20 6f 77 6e 65 72 20 3d 20 24 35 2c 20 63 68  , owner = $5, ch
14c0: 61 6e 6e 65 6c 20 3d 20 24 36 2c 20 75 72 6c 5f  annel = $6, url_
14d0: 72 65 20 3d 20 24 37 20 77 68 65 72 65 20 73 6f  re = $7 where so
14e0: 75 72 63 65 5f 69 64 20 3d 20 24 31 22 29 0a 09  urce_id = $1")..
14f0: 09 09 09 09 09 2e 62 69 6e 64 28 69 64 29 0a 09  ......bind(id)..
1500: 09 09 09 7d 2c 0a 09 09 09 09 4e 6f 6e 65 20 3d  ...},.....None =
1510: 3e 20 7b 0a 09 09 09 09 09 73 71 6c 78 3a 3a 71  > {......sqlx::q
1520: 75 65 72 79 28 22 69 6e 73 65 72 74 20 69 6e 74  uery("insert int
1530: 6f 20 72 73 73 74 67 5f 73 6f 75 72 63 65 20 28  o rsstg_source (
1540: 63 68 61 6e 6e 65 6c 5f 69 64 2c 20 75 72 6c 2c  channel_id, url,
1550: 20 69 76 5f 68 61 73 68 2c 20 6f 77 6e 65 72 2c   iv_hash, owner,
1560: 20 63 68 61 6e 6e 65 6c 2c 20 75 72 6c 5f 72 65   channel, url_re
1570: 29 20 76 61 6c 75 65 73 20 28 24 31 2c 20 24 32  ) values ($1, $2
1580: 2c 20 24 33 2c 20 24 34 2c 20 24 35 2c 20 24 36  , $3, $4, $5, $6
1590: 29 22 29 0a 09 09 09 09 7d 2c 0a 09 09 09 7d 0a  )").....},....}.
15a0: 09 09 09 09 2e 62 69 6e 64 28 63 68 61 6e 6e 65  .....bind(channe
15b0: 6c 5f 69 64 29 0a 09 09 09 09 2e 62 69 6e 64 28  l_id)......bind(
15c0: 75 72 6c 29 0a 09 09 09 09 2e 62 69 6e 64 28 69  url)......bind(i
15d0: 76 5f 68 61 73 68 29 0a 09 09 09 09 2e 62 69 6e  v_hash)......bin
15e0: 64 28 6f 77 6e 65 72 2e 69 6e 74 6f 28 29 29 0a  d(owner.into()).
15f0: 09 09 09 09 2e 62 69 6e 64 28 63 68 61 6e 6e 65  .....bind(channe
1600: 6c 29 0a 09 09 09 09 2e 62 69 6e 64 28 75 72 6c  l)......bind(url
1610: 5f 72 65 29 0a 09 09 09 09 2e 65 78 65 63 75 74  _re)......execut
1620: 65 28 26 6d 75 74 20 2a 73 65 6c 66 2e 63 6f 6e  e(&mut *self.con
1630: 6e 29 2e 61 77 61 69 74 0a 09 09 09 7b 0a 09 09  n).await....{...
1640: 09 4f 6b 28 5f 29 20 3d 3e 20 4f 6b 28 6d 61 74  .Ok(_) => Ok(mat
1650: 63 68 20 75 70 64 61 74 65 20 7b 0a 09 09 09 09  ch update {.....
1660: 53 6f 6d 65 28 5f 29 20 3d 3e 20 22 43 68 61 6e  Some(_) => "Chan
1670: 6e 65 6c 20 75 70 64 61 74 65 64 2e 22 2c 0a 09  nel updated.",..
1680: 09 09 09 4e 6f 6e 65 20 3d 3e 20 22 43 68 61 6e  ...None => "Chan
1690: 6e 65 6c 20 61 64 64 65 64 2e 22 2c 0a 09 09 09  nel added.",....
16a0: 7d 29 2c 0a 09 09 09 45 72 72 28 73 71 6c 78 3a  }),....Err(sqlx:
16b0: 3a 45 72 72 6f 72 3a 3a 44 61 74 61 62 61 73 65  :Error::Database
16c0: 28 65 72 72 29 29 20 3d 3e 20 7b 0a 09 09 09 09  (err)) => {.....
16d0: 6d 61 74 63 68 20 65 72 72 2e 64 6f 77 6e 63 61  match err.downca
16e0: 73 74 3a 3a 3c 73 71 6c 78 3a 3a 70 6f 73 74 67  st::<sqlx::postg
16f0: 72 65 73 3a 3a 50 67 44 61 74 61 62 61 73 65 45  res::PgDatabaseE
1700: 72 72 6f 72 3e 28 29 2e 72 6f 75 74 69 6e 65 28  rror>().routine(
1710: 29 20 7b 0a 09 09 09 09 09 53 6f 6d 65 28 22 5f  ) {......Some("_
1720: 62 74 5f 63 68 65 63 6b 5f 75 6e 69 71 75 65 22  bt_check_unique"
1730: 2c 20 29 20 3d 3e 20 7b 0a 09 09 09 09 09 09 4f  , ) => {.......O
1740: 6b 28 22 44 75 70 6c 69 63 61 74 65 20 6b 65 79  k("Duplicate key
1750: 2e 22 29 0a 09 09 09 09 09 7d 2c 0a 09 09 09 09  .")......},.....
1760: 09 53 6f 6d 65 28 5f 29 20 3d 3e 20 7b 0a 09 09  .Some(_) => {...
1770: 09 09 09 09 4f 6b 28 22 44 61 74 61 62 61 73 65  ....Ok("Database
1780: 20 65 72 72 6f 72 2e 22 29 0a 09 09 09 09 09 7d   error.")......}
1790: 2c 0a 09 09 09 09 09 4e 6f 6e 65 20 3d 3e 20 7b  ,......None => {
17a0: 0a 09 09 09 09 09 09 4f 6b 28 22 4e 6f 20 64 61  .......Ok("No da
17b0: 74 61 62 61 73 65 20 65 72 72 6f 72 20 65 78 74  tabase error ext
17c0: 72 61 63 74 65 64 2e 22 29 0a 09 09 09 09 09 7d  racted.")......}
17d0: 2c 0a 09 09 09 09 7d 0a 09 09 09 7d 2c 0a 09 09  ,.....}....},...
17e0: 09 45 72 72 28 65 72 72 29 20 3d 3e 20 7b 0a 09  .Err(err) => {..
17f0: 09 09 09 62 61 69 6c 21 28 22 53 6f 72 72 79 2c  ...bail!("Sorry,
1800: 20 75 6e 6b 6e 6f 77 6e 20 65 72 72 6f 72 3a 5c   unknown error:\
1810: 6e 7b 65 72 72 3a 23 3f 7d 5c 6e 22 29 3b 0a 09  n{err:#?}\n");..
1820: 09 09 7d 2c 0a 09 09 7d 0a 09 7d 0a 7d 0a        ..},...}..}.}.