0000: 75 73 65 20 63 72 61 74 65 3a 3a 7b 0a 09 63 6f use crate::{..co
0010: 6d 6d 61 6e 64 2c 0a 09 73 71 6c 3a 3a 44 62 2c mmand,..sql::Db,
0020: 0a 7d 3b 0a 0a 75 73 65 20 73 74 64 3a 3a 7b 0a .};..use std::{.
0030: 09 62 6f 72 72 6f 77 3a 3a 43 6f 77 2c 0a 09 63 .borrow::Cow,..c
0040: 6f 6c 6c 65 63 74 69 6f 6e 73 3a 3a 7b 0a 09 09 ollections::{...
0050: 42 54 72 65 65 4d 61 70 2c 0a 09 09 48 61 73 68 BTreeMap,...Hash
0060: 53 65 74 2c 0a 09 7d 2c 0a 09 73 79 6e 63 3a 3a Set,..},..sync::
0070: 41 72 63 2c 0a 7d 3b 0a 0a 75 73 65 20 61 73 79 Arc,.};..use asy
0080: 6e 63 5f 63 6f 6d 70 61 74 3a 3a 43 6f 6d 70 61 nc_compat::Compa
0090: 74 3b 0a 75 73 65 20 63 68 72 6f 6e 6f 3a 3a 7b t;.use chrono::{
00a0: 0a 09 44 61 74 65 54 69 6d 65 2c 0a 09 4c 6f 63 ..DateTime,..Loc
00b0: 61 6c 2c 0a 7d 3b 0a 75 73 65 20 6c 61 7a 79 5f al,.};.use lazy_
00c0: 73 74 61 74 69 63 3a 3a 6c 61 7a 79 5f 73 74 61 static::lazy_sta
00d0: 74 69 63 3b 0a 75 73 65 20 72 65 67 65 78 3a 3a tic;.use regex::
00e0: 52 65 67 65 78 3b 0a 75 73 65 20 72 65 71 77 65 Regex;.use reqwe
00f0: 73 74 3a 3a 68 65 61 64 65 72 3a 3a 7b 0a 09 43 st::header::{..C
0100: 41 43 48 45 5f 43 4f 4e 54 52 4f 4c 2c 0a 09 45 ACHE_CONTROL,..E
0110: 58 50 49 52 45 53 2c 0a 09 4c 41 53 54 5f 4d 4f XPIRES,..LAST_MO
0120: 44 49 46 49 45 44 0a 7d 3b 0a 75 73 65 20 73 6d DIFIED.};.use sm
0130: 6f 6c 3a 3a 7b 0a 09 54 69 6d 65 72 2c 0a 09 6c ol::{..Timer,..l
0140: 6f 63 6b 3a 3a 4d 75 74 65 78 2c 0a 7d 3b 0a 75 ock::Mutex,.};.u
0150: 73 65 20 74 67 62 6f 74 3a 3a 7b 0a 09 61 70 69 se tgbot::{..api
0160: 3a 3a 43 6c 69 65 6e 74 2c 0a 09 68 61 6e 64 6c ::Client,..handl
0170: 65 72 3a 3a 55 70 64 61 74 65 48 61 6e 64 6c 65 er::UpdateHandle
0180: 72 2c 0a 09 74 79 70 65 73 3a 3a 7b 0a 09 09 42 r,..types::{...B
0190: 6f 74 2c 0a 09 09 43 68 61 74 50 65 65 72 49 64 ot,...ChatPeerId
01a0: 2c 0a 09 09 43 6f 6d 6d 61 6e 64 2c 0a 09 09 47 ,...Command,...G
01b0: 65 74 42 6f 74 2c 0a 09 09 4d 65 73 73 61 67 65 etBot,...Message
01c0: 2c 0a 09 09 50 61 72 73 65 4d 6f 64 65 2c 0a 09 ,...ParseMode,..
01d0: 09 53 65 6e 64 4d 65 73 73 61 67 65 2c 0a 09 09 .SendMessage,...
01e0: 55 70 64 61 74 65 2c 0a 09 09 55 70 64 61 74 65 Update,...Update
01f0: 54 79 70 65 2c 0a 09 09 55 73 65 72 50 65 65 72 Type,...UserPeer
0200: 49 64 2c 0a 09 7d 2c 0a 7d 3b 0a 75 73 65 20 73 Id,..},.};.use s
0210: 74 61 63 6b 65 64 5f 65 72 72 6f 72 73 3a 3a 7b tacked_errors::{
0220: 0a 09 52 65 73 75 6c 74 2c 0a 09 53 74 61 63 6b ..Result,..Stack
0230: 61 62 6c 65 45 72 72 2c 0a 09 61 6e 79 68 6f 77 ableErr,..anyhow
0240: 2c 0a 09 62 61 69 6c 2c 0a 7d 3b 0a 0a 6c 61 7a ,..bail,.};..laz
0250: 79 5f 73 74 61 74 69 63 21 7b 0a 09 70 75 62 20 y_static!{..pub
0260: 73 74 61 74 69 63 20 72 65 66 20 52 45 5f 53 50 static ref RE_SP
0270: 45 43 49 41 4c 3a 20 52 65 67 65 78 20 3d 20 52 ECIAL: Regex = R
0280: 65 67 65 78 3a 3a 6e 65 77 28 72 22 28 5b 5c 2d egex::new(r"([\-
0290: 5f 2a 5c 5b 5c 5d 28 29 7e 60 3e 23 2b 7c 7b 7d _*\[\]()~`>#+|{}
02a0: 5c 2e 21 5d 29 22 29 2e 75 6e 77 72 61 70 28 29 \.!])").unwrap()
02b0: 3b 0a 7d 0a 0a 2f 2f 2f 20 45 73 63 61 70 65 20 ;.}../// Escape
02c0: 63 68 61 72 61 63 74 65 72 73 20 74 68 61 74 20 characters that
02d0: 61 72 65 20 73 70 65 63 69 61 6c 20 69 6e 20 54 are special in T
02e0: 65 6c 65 67 72 61 6d 20 4d 61 72 6b 64 6f 77 6e elegram Markdown
02f0: 56 32 20 62 79 20 70 72 65 66 69 78 69 6e 67 20 V2 by prefixing
0300: 74 68 65 6d 20 77 69 74 68 20 61 20 62 61 63 6b them with a back
0310: 73 6c 61 73 68 2e 0a 2f 2f 2f 0a 2f 2f 2f 20 54 slash..///./// T
0320: 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 65 20 his ensures the
0330: 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 returned string
0340: 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 4d can be used as M
0350: 61 72 6b 64 6f 77 6e 56 32 2d 66 6f 72 6d 61 74 arkdownV2-format
0360: 74 65 64 20 54 65 6c 65 67 72 61 6d 20 6d 65 73 ted Telegram mes
0370: 73 61 67 65 20 63 6f 6e 74 65 6e 74 0a 2f 2f 2f sage content.///
0380: 20 77 69 74 68 6f 75 74 20 73 70 65 63 69 61 6c without special
0390: 20 63 68 61 72 61 63 74 65 72 73 20 62 65 69 6e characters bein
03a0: 67 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 g interpreted as
03b0: 20 4d 61 72 6b 64 6f 77 6e 56 32 20 6d 61 72 6b MarkdownV2 mark
03c0: 75 70 2e 0a 70 75 62 20 66 6e 20 65 6e 63 6f 64 up..pub fn encod
03d0: 65 20 28 74 65 78 74 3a 20 26 73 74 72 29 20 2d e (text: &str) -
03e0: 3e 20 43 6f 77 3c 27 5f 2c 20 73 74 72 3e 20 7b > Cow<'_, str> {
03f0: 0a 09 52 45 5f 53 50 45 43 49 41 4c 2e 72 65 70 ..RE_SPECIAL.rep
0400: 6c 61 63 65 5f 61 6c 6c 28 74 65 78 74 2c 20 22 lace_all(text, "
0410: 5c 5c 24 31 22 29 0a 7d 0a 0a 2f 2f 20 54 68 69 \\$1").}..// Thi
0420: 73 20 6f 6e 65 20 64 6f 65 73 20 6e 6f 74 68 69 s one does nothi
0430: 6e 67 20 65 78 63 65 70 74 20 6d 61 6b 69 6e 67 ng except making
0440: 20 73 75 72 65 20 6f 6e 6c 79 20 6f 6e 65 20 74 sure only one t
0450: 6f 6b 65 6e 20 65 78 69 73 74 73 20 66 6f 72 20 oken exists for
0460: 65 61 63 68 20 69 64 0a 70 75 62 20 73 74 72 75 each id.pub stru
0470: 63 74 20 54 6f 6b 65 6e 20 7b 0a 09 72 75 6e 6e ct Token {..runn
0480: 69 6e 67 3a 20 41 72 63 3c 4d 75 74 65 78 3c 48 ing: Arc<Mutex<H
0490: 61 73 68 53 65 74 3c 69 33 32 3e 3e 3e 2c 0a 09 ashSet<i32>>>,..
04a0: 6d 79 5f 69 64 3a 20 69 33 32 2c 0a 7d 0a 0a 69 my_id: i32,.}..i
04b0: 6d 70 6c 20 54 6f 6b 65 6e 20 7b 0a 09 2f 2f 2f mpl Token {..///
04c0: 20 41 74 74 65 6d 70 74 73 20 74 6f 20 61 63 71 Attempts to acq
04d0: 75 69 72 65 20 61 20 70 65 72 2d 69 64 20 74 6f uire a per-id to
04e0: 6b 65 6e 20 62 79 20 69 6e 73 65 72 74 69 6e 67 ken by inserting
04f0: 20 60 6d 79 5f 69 64 60 20 69 6e 74 6f 20 74 68 `my_id` into th
0500: 65 20 73 68 61 72 65 64 20 60 72 75 6e 6e 69 6e e shared `runnin
0510: 67 60 20 73 65 74 2e 0a 09 2f 2f 2f 0a 09 2f 2f g` set...///..//
0520: 2f 20 49 66 20 74 68 65 20 69 64 20 77 61 73 20 / If the id was
0530: 6e 6f 74 20 61 6c 72 65 61 64 79 20 70 72 65 73 not already pres
0540: 65 6e 74 2c 20 74 68 65 20 66 75 6e 63 74 69 6f ent, the functio
0550: 6e 20 69 6e 73 65 72 74 73 20 69 74 20 61 6e 64 n inserts it and
0560: 20 72 65 74 75 72 6e 73 20 60 53 6f 6d 65 28 54 returns `Some(T
0570: 6f 6b 65 6e 29 60 2e 0a 09 2f 2f 2f 20 57 68 65 oken)`.../// Whe
0580: 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 60 n the returned `
0590: 54 6f 6b 65 6e 60 20 69 73 20 64 72 6f 70 70 65 Token` is droppe
05a0: 64 2c 20 74 68 65 20 69 64 20 77 69 6c 6c 20 62 d, the id will b
05b0: 65 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 e removed from t
05c0: 68 65 20 60 72 75 6e 6e 69 6e 67 60 20 73 65 74 he `running` set
05d0: 2c 0a 09 2f 2f 2f 20 61 6c 6c 6f 77 69 6e 67 20 ,../// allowing
05e0: 73 75 62 73 65 71 75 65 6e 74 20 61 63 71 75 69 subsequent acqui
05f0: 73 69 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 sitions for the
0600: 73 61 6d 65 20 69 64 2e 0a 09 2f 2f 2f 0a 09 2f same id...///../
0610: 2f 2f 20 23 20 50 61 72 61 6d 65 74 65 72 73 0a // # Parameters.
0620: 09 2f 2f 2f 0a 09 2f 2f 2f 20 2d 20 60 72 75 6e .///../// - `run
0630: 6e 69 6e 67 60 3a 20 53 68 61 72 65 64 20 73 65 ning`: Shared se
0640: 74 20 74 72 61 63 6b 69 6e 67 20 61 63 74 69 76 t tracking activ
0650: 65 20 69 64 73 2e 0a 09 2f 2f 2f 20 2d 20 60 6d e ids.../// - `m
0660: 79 5f 69 64 60 3a 20 49 64 65 6e 74 69 66 69 65 y_id`: Identifie
0670: 72 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 74 r to acquire a t
0680: 6f 6b 65 6e 20 66 6f 72 2e 0a 09 2f 2f 2f 0a 09 oken for...///..
0690: 2f 2f 2f 20 23 20 52 65 74 75 72 6e 73 0a 09 2f /// # Returns../
06a0: 2f 2f 0a 09 2f 2f 2f 20 60 4f 6b 28 54 6f 6b 65 //../// `Ok(Toke
06b0: 6e 29 60 20 69 66 20 74 68 65 20 69 64 20 77 61 n)` if the id wa
06c0: 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 s successfully a
06d0: 63 71 75 69 72 65 64 2c 20 60 45 72 72 6f 72 60 cquired, `Error`
06e0: 20 69 66 20 61 20 74 6f 6b 65 6e 20 66 6f 72 20 if a token for
06f0: 74 68 65 20 69 64 20 69 73 20 61 6c 72 65 61 64 the id is alread
0700: 79 20 61 63 74 69 76 65 2e 0a 09 61 73 79 6e 63 y active...async
0710: 20 66 6e 20 6e 65 77 20 28 72 75 6e 6e 69 6e 67 fn new (running
0720: 3a 20 26 41 72 63 3c 4d 75 74 65 78 3c 48 61 73 : &Arc<Mutex<Has
0730: 68 53 65 74 3c 69 33 32 3e 3e 3e 2c 20 6d 79 5f hSet<i32>>>, my_
0740: 69 64 3a 20 69 33 32 29 20 2d 3e 20 52 65 73 75 id: i32) -> Resu
0750: 6c 74 3c 54 6f 6b 65 6e 3e 20 7b 0a 09 09 6c 65 lt<Token> {...le
0760: 74 20 72 75 6e 6e 69 6e 67 20 3d 20 72 75 6e 6e t running = runn
0770: 69 6e 67 2e 63 6c 6f 6e 65 28 29 3b 0a 09 09 6c ing.clone();...l
0780: 65 74 20 6d 75 74 20 73 65 74 20 3d 20 72 75 6e et mut set = run
0790: 6e 69 6e 67 2e 6c 6f 63 6b 5f 61 72 63 28 29 2e ning.lock_arc().
07a0: 61 77 61 69 74 3b 0a 09 09 69 66 20 73 65 74 2e await;...if set.
07b0: 63 6f 6e 74 61 69 6e 73 28 26 6d 79 5f 69 64 29 contains(&my_id)
07c0: 20 7b 0a 09 09 09 62 61 69 6c 21 28 22 54 6f 6b {....bail!("Tok
07d0: 65 6e 20 61 6c 72 65 61 64 79 20 74 61 6b 65 6e en already taken
07e0: 22 29 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 ");...} else {..
07f0: 09 09 73 65 74 2e 69 6e 73 65 72 74 28 6d 79 5f ..set.insert(my_
0800: 69 64 29 3b 0a 09 09 09 4f 6b 28 54 6f 6b 65 6e id);....Ok(Token
0810: 20 7b 0a 09 09 09 09 72 75 6e 6e 69 6e 67 2c 0a {.....running,.
0820: 09 09 09 09 6d 79 5f 69 64 2c 0a 09 09 09 7d 29 ....my_id,....})
0830: 0a 09 09 7d 0a 09 7d 0a 7d 0a 0a 69 6d 70 6c 20 ...}..}.}..impl
0840: 44 72 6f 70 20 66 6f 72 20 54 6f 6b 65 6e 20 7b Drop for Token {
0850: 0a 09 2f 2f 2f 20 52 65 6c 65 61 73 65 73 20 74 ../// Releases t
0860: 68 69 73 20 74 6f 6b 65 6e 27 73 20 63 6c 61 69 his token's clai
0870: 6d 20 6f 6e 20 74 68 65 20 73 68 61 72 65 64 20 m on the shared
0880: 72 75 6e 6e 69 6e 67 2d 73 65 74 20 77 68 65 6e running-set when
0890: 20 74 68 65 20 74 6f 6b 65 6e 20 69 73 20 64 72 the token is dr
08a0: 6f 70 70 65 64 2e 0a 09 2f 2f 2f 0a 09 2f 2f 2f opped...///..///
08b0: 20 54 68 65 20 74 6f 6b 65 6e 27 73 20 69 64 65 The token's ide
08c0: 6e 74 69 66 69 65 72 20 69 73 20 72 65 6d 6f 76 ntifier is remov
08d0: 65 64 20 66 72 6f 6d 20 74 68 65 20 73 68 61 72 ed from the shar
08e0: 65 64 20 60 72 75 6e 6e 69 6e 67 60 20 73 65 74 ed `running` set
08f0: 20 73 6f 20 74 68 61 74 20 66 75 74 75 72 65 0a so that future.
0900: 09 2f 2f 2f 20 6f 70 65 72 61 74 69 6f 6e 73 20 ./// operations
0910: 66 6f 72 20 74 68 65 20 73 61 6d 65 20 69 64 20 for the same id
0920: 6d 61 79 20 70 72 6f 63 65 65 64 2e 0a 09 2f 2f may proceed...//
0930: 2f 0a 09 2f 2f 2f 20 54 4f 44 4f 3a 20 69 73 20 /../// TODO: is
0940: 75 73 69 6e 67 20 62 6c 6f 63 6b 5f 6f 6e 20 69 using block_on i
0950: 6e 73 69 64 65 20 62 6c 6f 63 6b 5f 6f 6e 20 73 nside block_on s
0960: 61 66 65 3f 20 43 75 72 72 65 6e 74 6c 79 20 74 afe? Currently t
0970: 65 73 74 65 64 20 61 6e 64 20 77 6f 72 6b 69 6e ested and workin
0980: 67 20 66 69 6e 65 2e 0a 09 66 6e 20 64 72 6f 70 g fine...fn drop
0990: 20 28 26 6d 75 74 20 73 65 6c 66 29 20 7b 0a 09 (&mut self) {..
09a0: 09 73 6d 6f 6c 3a 3a 62 6c 6f 63 6b 5f 6f 6e 28 .smol::block_on(
09b0: 61 73 79 6e 63 20 7b 0a 09 09 09 6c 65 74 20 6d async {....let m
09c0: 75 74 20 73 65 74 20 3d 20 73 65 6c 66 2e 72 75 ut set = self.ru
09d0: 6e 6e 69 6e 67 2e 6c 6f 63 6b 5f 61 72 63 28 29 nning.lock_arc()
09e0: 2e 61 77 61 69 74 3b 0a 09 09 09 73 65 74 2e 72 .await;....set.r
09f0: 65 6d 6f 76 65 28 26 73 65 6c 66 2e 6d 79 5f 69 emove(&self.my_i
0a00: 64 29 3b 0a 09 09 7d 29 0a 09 7d 0a 7d 0a 0a 23 d);...})..}.}..#
0a10: 5b 64 65 72 69 76 65 28 43 6c 6f 6e 65 29 5d 0a [derive(Clone)].
0a20: 70 75 62 20 73 74 72 75 63 74 20 43 6f 72 65 20 pub struct Core
0a30: 7b 0a 09 6f 77 6e 65 72 5f 63 68 61 74 3a 20 43 {..owner_chat: C
0a40: 68 61 74 50 65 65 72 49 64 2c 0a 09 2f 2f 20 6d hatPeerId,..// m
0a50: 61 78 5f 64 65 6c 61 79 3a 20 75 31 36 2c 0a 09 ax_delay: u16,..
0a60: 70 75 62 20 74 67 3a 20 43 6c 69 65 6e 74 2c 0a pub tg: Client,.
0a70: 09 70 75 62 20 6d 65 3a 20 42 6f 74 2c 0a 09 70 .pub me: Bot,..p
0a80: 75 62 20 64 62 3a 20 44 62 2c 0a 09 72 75 6e 6e ub db: Db,..runn
0a90: 69 6e 67 3a 20 41 72 63 3c 4d 75 74 65 78 3c 48 ing: Arc<Mutex<H
0aa0: 61 73 68 53 65 74 3c 69 33 32 3e 3e 3e 2c 0a 09 ashSet<i32>>>,..
0ab0: 68 74 74 70 5f 63 6c 69 65 6e 74 3a 20 72 65 71 http_client: req
0ac0: 77 65 73 74 3a 3a 43 6c 69 65 6e 74 2c 0a 7d 0a west::Client,.}.
0ad0: 0a 2f 2f 20 58 58 58 20 52 69 67 68 74 20 6e 6f .// XXX Right no
0ae0: 77 20 74 68 61 74 20 70 61 72 74 20 69 73 20 75 w that part is u
0af0: 6e 66 69 6e 69 73 68 65 64 20 61 6e 64 20 49 20 nfinished and I
0b00: 67 75 65 73 73 20 49 20 6e 65 65 64 20 74 6f 20 guess I need to
0b10: 66 69 6e 69 73 68 20 6d 65 6e 75 20 66 69 72 73 finish menu firs
0b20: 74 0a 23 5b 61 6c 6c 6f 77 28 75 6e 75 73 65 64 t.#[allow(unused
0b30: 29 5d 0a 70 75 62 20 73 74 72 75 63 74 20 50 6f )].pub struct Po
0b40: 73 74 20 7b 0a 09 75 72 69 3a 20 53 74 72 69 6e st {..uri: Strin
0b50: 67 2c 0a 09 74 69 74 6c 65 3a 20 53 74 72 69 6e g,..title: Strin
0b60: 67 2c 0a 09 61 75 74 68 6f 72 73 3a 20 53 74 72 g,..authors: Str
0b70: 69 6e 67 2c 0a 09 73 75 6d 6d 61 72 79 3a 20 53 ing,..summary: S
0b80: 74 72 69 6e 67 2c 0a 7d 0a 0a 69 6d 70 6c 20 43 tring,.}..impl C
0b90: 6f 72 65 20 7b 0a 09 2f 2f 2f 20 43 72 65 61 74 ore {../// Creat
0ba0: 65 20 61 20 43 6f 72 65 20 69 6e 73 74 61 6e 63 e a Core instanc
0bb0: 65 20 66 72 6f 6d 20 63 6f 6e 66 69 67 75 72 61 e from configura
0bc0: 74 69 6f 6e 20 61 6e 64 20 73 74 61 72 74 20 69 tion and start i
0bd0: 74 73 20 62 61 63 6b 67 72 6f 75 6e 64 20 61 75 ts background au
0be0: 74 6f 66 65 74 63 68 20 6c 6f 6f 70 2e 0a 09 2f tofetch loop.../
0bf0: 2f 2f 0a 09 2f 2f 2f 20 54 68 65 20 70 72 6f 76 //../// The prov
0c00: 69 64 65 64 20 60 73 65 74 74 69 6e 67 73 60 20 ided `settings`
0c10: 6d 75 73 74 20 69 6e 63 6c 75 64 65 3a 0a 09 2f must include:../
0c20: 2f 2f 20 2d 20 60 6f 77 6e 65 72 60 20 28 69 6e // - `owner` (in
0c30: 74 65 67 65 72 29 3a 20 63 68 61 74 20 69 64 20 teger): chat id
0c40: 74 6f 20 75 73 65 20 61 73 20 74 68 65 20 64 65 to use as the de
0c50: 66 61 75 6c 74 20 64 65 73 74 69 6e 61 74 69 6f fault destinatio
0c60: 6e 2c 0a 09 2f 2f 2f 20 2d 20 60 61 70 69 5f 6b n,../// - `api_k
0c70: 65 79 60 20 28 73 74 72 69 6e 67 29 3a 20 54 65 ey` (string): Te
0c80: 6c 65 67 72 61 6d 20 62 6f 74 20 41 50 49 20 6b legram bot API k
0c90: 65 79 2c 0a 09 2f 2f 2f 20 2d 20 60 61 70 69 5f ey,../// - `api_
0ca0: 67 61 74 65 77 61 79 60 20 28 73 74 72 69 6e 67 gateway` (string
0cb0: 29 3a 20 54 65 6c 65 67 72 61 6d 20 41 50 49 20 ): Telegram API
0cc0: 67 61 74 65 77 61 79 20 68 6f 73 74 2c 0a 09 2f gateway host,../
0cd0: 2f 2f 20 2d 20 60 70 67 60 20 28 73 74 72 69 6e // - `pg` (strin
0ce0: 67 29 3a 20 50 6f 73 74 67 72 65 53 51 4c 20 63 g): PostgreSQL c
0cf0: 6f 6e 6e 65 63 74 69 6f 6e 20 73 74 72 69 6e 67 onnection string
0d00: 2c 0a 09 2f 2f 2f 20 2d 20 6f 70 74 69 6f 6e 61 ,../// - optiona
0d10: 6c 20 60 70 72 6f 78 79 60 20 28 73 74 72 69 6e l `proxy` (strin
0d20: 67 29 3a 20 70 72 6f 78 79 20 55 52 4c 20 66 6f g): proxy URL fo
0d30: 72 20 74 68 65 20 48 54 54 50 20 63 6c 69 65 6e r the HTTP clien
0d40: 74 2e 0a 09 2f 2f 2f 0a 09 2f 2f 2f 20 4f 6e 20 t...///../// On
0d50: 73 75 63 63 65 73 73 20 72 65 74 75 72 6e 73 20 success returns
0d60: 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 60 an initialized `
0d70: 43 6f 72 65 60 20 77 69 74 68 20 54 65 6c 65 67 Core` with Teleg
0d80: 72 61 6d 20 61 6e 64 20 48 54 54 50 20 63 6c 69 ram and HTTP cli
0d90: 65 6e 74 73 2c 20 64 61 74 61 62 61 73 65 20 63 ents, database c
0da0: 6f 6e 6e 65 63 74 69 6f 6e 2c 0a 09 2f 2f 2f 20 onnection,..///
0db0: 61 6e 20 65 6d 70 74 79 20 72 75 6e 6e 69 6e 67 an empty running
0dc0: 20 73 65 74 20 66 6f 72 20 70 65 72 2d 69 64 20 set for per-id
0dd0: 74 6f 6b 65 6e 73 2c 20 61 6e 64 20 61 20 73 70 tokens, and a sp
0de0: 61 77 6e 65 64 20 62 61 63 6b 67 72 6f 75 6e 64 awned background
0df0: 20 74 61 73 6b 20 74 68 61 74 20 70 65 72 69 6f task that perio
0e00: 64 69 63 61 6c 6c 79 20 72 75 6e 73 0a 09 2f 2f dically runs..//
0e10: 2f 20 60 61 75 74 6f 66 65 74 63 68 60 2e 20 49 / `autofetch`. I
0e20: 66 20 61 6e 79 20 72 65 71 75 69 72 65 64 20 73 f any required s
0e30: 65 74 74 69 6e 67 20 69 73 20 6d 69 73 73 69 6e etting is missin
0e40: 67 20 6f 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 g or initializat
0e50: 69 6f 6e 20 66 61 69 6c 73 2c 20 61 6e 20 65 72 ion fails, an er
0e60: 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e ror is returned.
0e70: 0a 09 70 75 62 20 61 73 79 6e 63 20 66 6e 20 6e ..pub async fn n
0e80: 65 77 28 73 65 74 74 69 6e 67 73 3a 20 63 6f 6e ew(settings: con
0e90: 66 69 67 3a 3a 43 6f 6e 66 69 67 29 20 2d 3e 20 fig::Config) ->
0ea0: 52 65 73 75 6c 74 3c 43 6f 72 65 3e 20 7b 0a 09 Result<Core> {..
0eb0: 09 6c 65 74 20 6f 77 6e 65 72 5f 63 68 61 74 20 .let owner_chat
0ec0: 3d 20 43 68 61 74 50 65 65 72 49 64 3a 3a 66 72 = ChatPeerId::fr
0ed0: 6f 6d 28 73 65 74 74 69 6e 67 73 2e 67 65 74 5f om(settings.get_
0ee0: 69 6e 74 28 22 6f 77 6e 65 72 22 29 2e 73 74 61 int("owner").sta
0ef0: 63 6b 28 29 3f 29 3b 0a 09 09 6c 65 74 20 61 70 ck()?);...let ap
0f00: 69 5f 6b 65 79 20 3d 20 73 65 74 74 69 6e 67 73 i_key = settings
0f10: 2e 67 65 74 5f 73 74 72 69 6e 67 28 22 61 70 69 .get_string("api
0f20: 5f 6b 65 79 22 29 2e 73 74 61 63 6b 28 29 3f 3b _key").stack()?;
0f30: 0a 09 09 6c 65 74 20 74 67 20 3d 20 43 6c 69 65 ...let tg = Clie
0f40: 6e 74 3a 3a 6e 65 77 28 26 61 70 69 5f 6b 65 79 nt::new(&api_key
0f50: 29 2e 73 74 61 63 6b 28 29 3f 0a 09 09 09 2e 77 ).stack()?.....w
0f60: 69 74 68 5f 68 6f 73 74 28 73 65 74 74 69 6e 67 ith_host(setting
0f70: 73 2e 67 65 74 5f 73 74 72 69 6e 67 28 22 61 70 s.get_string("ap
0f80: 69 5f 67 61 74 65 77 61 79 22 29 2e 73 74 61 63 i_gateway").stac
0f90: 6b 28 29 3f 29 3b 0a 0a 09 09 6c 65 74 20 6d 75 k()?);....let mu
0fa0: 74 20 63 6c 69 65 6e 74 20 3d 20 72 65 71 77 65 t client = reqwe
0fb0: 73 74 3a 3a 43 6c 69 65 6e 74 3a 3a 62 75 69 6c st::Client::buil
0fc0: 64 65 72 28 29 3b 0a 09 09 69 66 20 6c 65 74 20 der();...if let
0fd0: 4f 6b 28 70 72 6f 78 79 29 20 3d 20 73 65 74 74 Ok(proxy) = sett
0fe0: 69 6e 67 73 2e 67 65 74 5f 73 74 72 69 6e 67 28 ings.get_string(
0ff0: 22 70 72 6f 78 79 22 29 20 7b 0a 09 09 09 6c 65 "proxy") {....le
1000: 74 20 70 72 6f 78 79 20 3d 20 72 65 71 77 65 73 t proxy = reqwes
1010: 74 3a 3a 50 72 6f 78 79 3a 3a 61 6c 6c 28 70 72 t::Proxy::all(pr
1020: 6f 78 79 29 2e 73 74 61 63 6b 28 29 3f 3b 0a 09 oxy).stack()?;..
1030: 09 09 63 6c 69 65 6e 74 20 3d 20 63 6c 69 65 6e ..client = clien
1040: 74 2e 70 72 6f 78 79 28 70 72 6f 78 79 29 3b 0a t.proxy(proxy);.
1050: 09 09 7d 0a 09 09 6c 65 74 20 68 74 74 70 5f 63 ..}...let http_c
1060: 6c 69 65 6e 74 20 3d 20 63 6c 69 65 6e 74 2e 62 lient = client.b
1070: 75 69 6c 64 28 29 2e 73 74 61 63 6b 28 29 3f 3b uild().stack()?;
1080: 0a 09 09 6c 65 74 20 6d 65 20 3d 20 74 67 2e 65 ...let me = tg.e
1090: 78 65 63 75 74 65 28 47 65 74 42 6f 74 29 2e 61 xecute(GetBot).a
10a0: 77 61 69 74 2e 73 74 61 63 6b 28 29 3f 3b 0a 09 wait.stack()?;..
10b0: 09 6c 65 74 20 63 6f 72 65 20 3d 20 43 6f 72 65 .let core = Core
10c0: 20 7b 0a 09 09 09 74 67 2c 0a 09 09 09 6d 65 2c {....tg,....me,
10d0: 0a 09 09 09 6f 77 6e 65 72 5f 63 68 61 74 2c 0a ....owner_chat,.
10e0: 09 09 09 64 62 3a 20 44 62 3a 3a 6e 65 77 28 26 ...db: Db::new(&
10f0: 73 65 74 74 69 6e 67 73 2e 67 65 74 5f 73 74 72 settings.get_str
1100: 69 6e 67 28 22 70 67 22 29 2e 73 74 61 63 6b 28 ing("pg").stack(
1110: 29 3f 29 3f 2c 0a 09 09 09 72 75 6e 6e 69 6e 67 )?)?,....running
1120: 3a 20 41 72 63 3a 3a 6e 65 77 28 4d 75 74 65 78 : Arc::new(Mutex
1130: 3a 3a 6e 65 77 28 48 61 73 68 53 65 74 3a 3a 6e ::new(HashSet::n
1140: 65 77 28 29 29 29 2c 0a 09 09 09 68 74 74 70 5f ew())),....http_
1150: 63 6c 69 65 6e 74 2c 0a 09 09 09 2f 2f 20 6d 61 client,....// ma
1160: 78 5f 64 65 6c 61 79 3a 20 36 30 2c 0a 09 09 7d x_delay: 60,...}
1170: 3b 0a 09 09 6c 65 74 20 63 6c 6f 6e 65 20 3d 20 ;...let clone =
1180: 63 6f 72 65 2e 63 6c 6f 6e 65 28 29 3b 0a 09 09 core.clone();...
1190: 73 6d 6f 6c 3a 3a 73 70 61 77 6e 28 43 6f 6d 70 smol::spawn(Comp
11a0: 61 74 3a 3a 6e 65 77 28 61 73 79 6e 63 20 6d 6f at::new(async mo
11b0: 76 65 20 7b 0a 09 09 09 6c 6f 6f 70 20 7b 0a 09 ve {....loop {..
11c0: 09 09 09 6c 65 74 20 64 65 6c 61 79 20 3d 20 6d ...let delay = m
11d0: 61 74 63 68 20 26 63 6c 6f 6e 65 2e 61 75 74 6f atch &clone.auto
11e0: 66 65 74 63 68 28 29 2e 61 77 61 69 74 20 7b 0a fetch().await {.
11f0: 09 09 09 09 09 45 72 72 28 65 72 72 29 20 3d 3e .....Err(err) =>
1200: 20 7b 0a 09 09 09 09 09 09 69 66 20 6c 65 74 20 {.......if let
1210: 45 72 72 28 65 72 72 29 20 3d 20 63 6c 6f 6e 65 Err(err) = clone
1220: 2e 73 65 6e 64 28 66 6f 72 6d 61 74 21 28 22 f0 .send(format!("ð
1230: 9f 9b 91 20 7b 65 72 72 7d 22 29 2c 20 4e 6f 6e Ÿ›‘ {err}"), Non
1240: 65 2c 20 4e 6f 6e 65 29 2e 61 77 61 69 74 20 7b e, None).await {
1250: 0a 09 09 09 09 09 09 09 65 70 72 69 6e 74 6c 6e ........eprintln
1260: 21 28 22 41 75 74 6f 66 65 74 63 68 20 65 72 72 !("Autofetch err
1270: 6f 72 3a 20 7b 65 72 72 3a 3f 7d 22 29 3b 0a 09 or: {err:?}");..
1280: 09 09 09 09 09 7d 3b 0a 09 09 09 09 09 09 73 74 .....};.......st
1290: 64 3a 3a 74 69 6d 65 3a 3a 44 75 72 61 74 69 6f d::time::Duratio
12a0: 6e 3a 3a 66 72 6f 6d 5f 73 65 63 73 28 36 30 29 n::from_secs(60)
12b0: 0a 09 09 09 09 09 7d 2c 0a 09 09 09 09 09 4f 6b ......},......Ok
12c0: 28 74 69 6d 65 29 20 3d 3e 20 2a 74 69 6d 65 2c (time) => *time,
12d0: 0a 09 09 09 09 7d 3b 0a 09 09 09 09 54 69 6d 65 .....};.....Time
12e0: 72 3a 3a 61 66 74 65 72 28 64 65 6c 61 79 29 2e r::after(delay).
12f0: 61 77 61 69 74 3b 0a 09 09 09 7d 0a 09 09 7d 29 await;....}...})
1300: 29 2e 64 65 74 61 63 68 28 29 3b 0a 09 09 4f 6b ).detach();...Ok
1310: 28 63 6f 72 65 29 0a 09 7d 0a 0a 09 70 75 62 20 (core)..}...pub
1320: 61 73 79 6e 63 20 66 6e 20 73 65 6e 64 20 3c 53 async fn send <S
1330: 3e 28 26 73 65 6c 66 2c 20 6d 73 67 3a 20 53 2c >(&self, msg: S,
1340: 20 74 61 72 67 65 74 3a 20 4f 70 74 69 6f 6e 3c target: Option<
1350: 43 68 61 74 50 65 65 72 49 64 3e 2c 20 6d 6f 64 ChatPeerId>, mod
1360: 65 3a 20 4f 70 74 69 6f 6e 3c 50 61 72 73 65 4d e: Option<ParseM
1370: 6f 64 65 3e 29 20 2d 3e 20 52 65 73 75 6c 74 3c ode>) -> Result<
1380: 4d 65 73 73 61 67 65 3e 0a 09 77 68 65 72 65 20 Message>..where
1390: 53 3a 20 49 6e 74 6f 3c 53 74 72 69 6e 67 3e 20 S: Into<String>
13a0: 7b 0a 09 09 6c 65 74 20 6d 73 67 20 3d 20 6d 73 {...let msg = ms
13b0: 67 2e 69 6e 74 6f 28 29 3b 0a 0a 09 09 6c 65 74 g.into();....let
13c0: 20 6d 6f 64 65 20 3d 20 6d 6f 64 65 2e 75 6e 77 mode = mode.unw
13d0: 72 61 70 5f 6f 72 28 50 61 72 73 65 4d 6f 64 65 rap_or(ParseMode
13e0: 3a 3a 48 74 6d 6c 29 3b 0a 09 09 6c 65 74 20 74 ::Html);...let t
13f0: 61 72 67 65 74 20 3d 20 74 61 72 67 65 74 2e 75 arget = target.u
1400: 6e 77 72 61 70 5f 6f 72 28 73 65 6c 66 2e 6f 77 nwrap_or(self.ow
1410: 6e 65 72 5f 63 68 61 74 29 3b 0a 09 09 73 65 6c ner_chat);...sel
1420: 66 2e 74 67 2e 65 78 65 63 75 74 65 28 0a 09 09 f.tg.execute(...
1430: 09 53 65 6e 64 4d 65 73 73 61 67 65 3a 3a 6e 65 .SendMessage::ne
1440: 77 28 74 61 72 67 65 74 2c 20 6d 73 67 29 0a 09 w(target, msg)..
1450: 09 09 09 2e 77 69 74 68 5f 70 61 72 73 65 5f 6d ....with_parse_m
1460: 6f 64 65 28 6d 6f 64 65 29 0a 09 09 29 2e 61 77 ode(mode)...).aw
1470: 61 69 74 2e 73 74 61 63 6b 28 29 0a 09 7d 0a 0a ait.stack()..}..
1480: 09 2f 2f 2f 20 46 65 74 63 68 65 73 20 74 68 65 ./// Fetches the
1490: 20 66 65 65 64 20 66 6f 72 20 61 20 73 6f 75 72 feed for a sour
14a0: 63 65 2c 20 73 65 6e 64 73 20 61 6e 79 20 6e 65 ce, sends any ne
14b0: 77 6c 79 20 64 69 73 63 6f 76 65 72 65 64 20 70 wly discovered p
14c0: 6f 73 74 73 20 74 6f 20 74 68 65 20 61 70 70 72 osts to the appr
14d0: 6f 70 72 69 61 74 65 20 63 68 61 74 2c 20 61 6e opriate chat, an
14e0: 64 20 72 65 63 6f 72 64 73 20 74 68 65 6d 20 69 d records them i
14f0: 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a n the database..
1500: 09 2f 2f 2f 0a 09 2f 2f 2f 20 54 68 69 73 20 61 .///../// This a
1510: 63 71 75 69 72 65 73 20 61 20 70 65 72 2d 73 6f cquires a per-so
1520: 75 72 63 65 20 67 75 61 72 64 20 74 6f 20 70 72 urce guard to pr
1530: 65 76 65 6e 74 20 63 6f 6e 63 75 72 72 65 6e 74 event concurrent
1540: 20 63 68 65 63 6b 73 20 66 6f 72 20 74 68 65 20 checks for the
1550: 73 61 6d 65 20 60 69 64 60 2e 20 49 66 20 61 20 same `id`. If a
1560: 63 68 65 63 6b 20 69 73 20 61 6c 72 65 61 64 79 check is already
1570: 20 72 75 6e 6e 69 6e 67 20 66 6f 72 0a 09 2f 2f running for..//
1580: 2f 20 74 68 65 20 67 69 76 65 6e 20 60 69 64 60 / the given `id`
1590: 2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 , the function r
15a0: 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2e eturns an error.
15b0: 20 49 66 20 60 6c 61 73 74 5f 73 63 72 61 70 65 If `last_scrape
15c0: 60 20 69 73 20 70 72 6f 76 69 64 65 64 2c 20 69 ` is provided, i
15d0: 74 20 69 73 20 73 65 6e 74 20 61 73 20 74 68 65 t is sent as the
15e0: 20 60 49 66 2d 4d 6f 64 69 66 69 65 64 2d 53 69 `If-Modified-Si
15f0: 6e 63 65 60 0a 09 2f 2f 2f 20 68 65 61 64 65 72 nce`../// header
1600: 20 74 6f 20 74 68 65 20 66 65 65 64 20 72 65 71 to the feed req
1610: 75 65 73 74 2e 20 54 68 65 20 66 75 6e 63 74 69 uest. The functi
1620: 6f 6e 20 70 61 72 73 65 73 20 52 53 53 20 6f 72 on parses RSS or
1630: 20 41 74 6f 6d 20 66 65 65 64 73 2c 20 73 65 6e Atom feeds, sen
1640: 64 73 20 75 6e 73 65 65 6e 20 70 6f 73 74 20 55 ds unseen post U
1650: 52 4c 73 20 74 6f 20 65 69 74 68 65 72 20 74 68 RLs to either th
1660: 65 20 73 6f 75 72 63 65 27 73 0a 09 2f 2f 2f 20 e source's..///
1670: 63 68 61 6e 6e 65 6c 20 28 77 68 65 6e 20 60 72 channel (when `r
1680: 65 61 6c 60 20 69 73 20 74 72 75 65 29 20 6f 72 eal` is true) or
1690: 20 74 68 65 20 73 6f 75 72 63 65 20 6f 77 6e 65 the source owne
16a0: 72 20 28 77 68 65 6e 20 60 72 65 61 6c 60 20 69 r (when `real` i
16b0: 73 20 66 61 6c 73 65 29 2c 20 61 6e 64 20 70 65 s false), and pe
16c0: 72 73 69 73 74 73 20 70 6f 73 74 65 64 20 65 6e rsists posted en
16d0: 74 72 69 65 73 20 73 6f 20 74 68 65 79 20 61 72 tries so they ar
16e0: 65 0a 09 2f 2f 2f 20 6e 6f 74 20 72 65 70 6f 73 e../// not repos
16f0: 74 65 64 20 6c 61 74 65 72 2e 0a 09 2f 2f 2f 0a ted later...///.
1700: 09 2f 2f 2f 20 50 61 72 61 6d 65 74 65 72 73 3a ./// Parameters:
1710: 0a 09 2f 2f 2f 20 2d 20 60 69 64 60 3a 20 49 64 ../// - `id`: Id
1720: 65 6e 74 69 66 69 65 72 20 6f 66 20 74 68 65 20 entifier of the
1730: 73 6f 75 72 63 65 20 74 6f 20 63 68 65 63 6b 2e source to check.
1740: 0a 09 2f 2f 2f 20 2d 20 60 72 65 61 6c 60 3a 20 ../// - `real`:
1750: 57 68 65 6e 20 60 74 72 75 65 60 2c 20 73 65 6e When `true`, sen
1760: 64 20 70 6f 73 74 73 20 74 6f 20 74 68 65 20 73 d posts to the s
1770: 6f 75 72 63 65 27 73 20 63 68 61 6e 6e 65 6c 3b ource's channel;
1780: 20 77 68 65 6e 20 60 66 61 6c 73 65 60 2c 20 73 when `false`, s
1790: 65 6e 64 20 74 6f 20 74 68 65 20 73 6f 75 72 63 end to the sourc
17a0: 65 20 6f 77 6e 65 72 2e 0a 09 2f 2f 2f 20 2d 20 e owner.../// -
17b0: 60 6c 61 73 74 5f 73 63 72 61 70 65 60 3a 20 4f `last_scrape`: O
17c0: 70 74 69 6f 6e 61 6c 20 74 69 6d 65 73 74 61 6d ptional timestam
17d0: 70 20 75 73 65 64 20 74 6f 20 73 65 74 20 74 68 p used to set th
17e0: 65 20 60 49 66 2d 4d 6f 64 69 66 69 65 64 2d 53 e `If-Modified-S
17f0: 69 6e 63 65 60 20 68 65 61 64 65 72 20 66 6f 72 ince` header for
1800: 20 74 68 65 20 48 54 54 50 20 72 65 71 75 65 73 the HTTP reques
1810: 74 2e 0a 09 2f 2f 2f 0a 09 2f 2f 2f 20 23 20 52 t...///../// # R
1820: 65 74 75 72 6e 73 0a 09 2f 2f 2f 0a 09 2f 2f 2f eturns..///..///
1830: 20 60 50 6f 73 74 65 64 3a 20 4e 60 20 77 68 65 `Posted: N` whe
1840: 72 65 20 60 4e 60 20 69 73 20 74 68 65 20 6e 75 re `N` is the nu
1850: 6d 62 65 72 20 6f 66 20 70 6f 73 74 73 20 70 72 mber of posts pr
1860: 6f 63 65 73 73 65 64 20 61 6e 64 20 73 65 6e 74 ocessed and sent
1870: 2e 0a 09 70 75 62 20 61 73 79 6e 63 20 66 6e 20 ...pub async fn
1880: 63 68 65 63 6b 20 28 26 73 65 6c 66 2c 20 69 64 check (&self, id
1890: 3a 20 69 33 32 2c 20 72 65 61 6c 3a 20 62 6f 6f : i32, real: boo
18a0: 6c 2c 20 6c 61 73 74 5f 73 63 72 61 70 65 3a 20 l, last_scrape:
18b0: 4f 70 74 69 6f 6e 3c 44 61 74 65 54 69 6d 65 3c Option<DateTime<
18c0: 4c 6f 63 61 6c 3e 3e 29 20 2d 3e 20 52 65 73 75 Local>>) -> Resu
18d0: 6c 74 3c 53 74 72 69 6e 67 3e 20 7b 0a 09 09 6c lt<String> {...l
18e0: 65 74 20 6d 75 74 20 70 6f 73 74 65 64 3a 20 69 et mut posted: i
18f0: 33 32 20 3d 20 30 3b 0a 09 09 6c 65 74 20 6d 75 32 = 0;...let mu
1900: 74 20 63 6f 6e 6e 20 3d 20 73 65 6c 66 2e 64 62 t conn = self.db
1910: 2e 62 65 67 69 6e 28 29 2e 61 77 61 69 74 2e 73 .begin().await.s
1920: 74 61 63 6b 28 29 3f 3b 0a 0a 09 09 6c 65 74 20 tack()?;....let
1930: 5f 74 6f 6b 65 6e 20 3d 20 54 6f 6b 65 6e 3a 3a _token = Token::
1940: 6e 65 77 28 26 73 65 6c 66 2e 72 75 6e 6e 69 6e new(&self.runnin
1950: 67 2c 20 69 64 29 2e 61 77 61 69 74 2e 73 74 61 g, id).await.sta
1960: 63 6b 28 29 3f 3b 0a 09 09 6c 65 74 20 73 6f 75 ck()?;...let sou
1970: 72 63 65 20 3d 20 63 6f 6e 6e 2e 67 65 74 5f 73 rce = conn.get_s
1980: 6f 75 72 63 65 28 69 64 2c 20 73 65 6c 66 2e 6f ource(id, self.o
1990: 77 6e 65 72 5f 63 68 61 74 29 2e 61 77 61 69 74 wner_chat).await
19a0: 2e 73 74 61 63 6b 28 29 3f 3b 0a 09 09 63 6f 6e .stack()?;...con
19b0: 6e 2e 73 65 74 5f 73 63 72 61 70 65 28 69 64 29 n.set_scrape(id)
19c0: 2e 61 77 61 69 74 2e 73 74 61 63 6b 28 29 3f 3b .await.stack()?;
19d0: 0a 09 09 6c 65 74 20 64 65 73 74 69 6e 61 74 69 ...let destinati
19e0: 6f 6e 20 3d 20 43 68 61 74 50 65 65 72 49 64 3a on = ChatPeerId:
19f0: 3a 66 72 6f 6d 28 6d 61 74 63 68 20 72 65 61 6c :from(match real
1a00: 20 7b 0a 09 09 09 74 72 75 65 20 3d 3e 20 73 6f {....true => so
1a10: 75 72 63 65 2e 63 68 61 6e 6e 65 6c 5f 69 64 2c urce.channel_id,
1a20: 0a 09 09 09 66 61 6c 73 65 20 3d 3e 20 73 6f 75 ....false => sou
1a30: 72 63 65 2e 6f 77 6e 65 72 2c 0a 09 09 7d 29 3b rce.owner,...});
1a40: 0a 09 09 6c 65 74 20 6d 75 74 20 74 68 69 73 5f ...let mut this_
1a50: 66 65 74 63 68 3a 20 4f 70 74 69 6f 6e 3c 44 61 fetch: Option<Da
1a60: 74 65 54 69 6d 65 3c 63 68 72 6f 6e 6f 3a 3a 46 teTime<chrono::F
1a70: 69 78 65 64 4f 66 66 73 65 74 3e 3e 20 3d 20 4e ixedOffset>> = N
1a80: 6f 6e 65 3b 0a 09 09 6c 65 74 20 6d 75 74 20 70 one;...let mut p
1a90: 6f 73 74 73 3a 20 42 54 72 65 65 4d 61 70 3c 44 osts: BTreeMap<D
1aa0: 61 74 65 54 69 6d 65 3c 63 68 72 6f 6e 6f 3a 3a ateTime<chrono::
1ab0: 46 69 78 65 64 4f 66 66 73 65 74 3e 2c 20 50 6f FixedOffset>, Po
1ac0: 73 74 3e 20 3d 20 42 54 72 65 65 4d 61 70 3a 3a st> = BTreeMap::
1ad0: 6e 65 77 28 29 3b 0a 0a 09 09 6c 65 74 20 6d 75 new();....let mu
1ae0: 74 20 62 75 69 6c 64 65 72 20 3d 20 73 65 6c 66 t builder = self
1af0: 2e 68 74 74 70 5f 63 6c 69 65 6e 74 2e 67 65 74 .http_client.get
1b00: 28 26 73 6f 75 72 63 65 2e 75 72 6c 29 3b 0a 09 (&source.url);..
1b10: 09 69 66 20 6c 65 74 20 53 6f 6d 65 28 6c 61 73 .if let Some(las
1b20: 74 5f 73 63 72 61 70 65 29 20 3d 20 6c 61 73 74 t_scrape) = last
1b30: 5f 73 63 72 61 70 65 20 7b 0a 09 09 09 62 75 69 _scrape {....bui
1b40: 6c 64 65 72 20 3d 20 62 75 69 6c 64 65 72 2e 68 lder = builder.h
1b50: 65 61 64 65 72 28 4c 41 53 54 5f 4d 4f 44 49 46 eader(LAST_MODIF
1b60: 49 45 44 2c 20 6c 61 73 74 5f 73 63 72 61 70 65 IED, last_scrape
1b70: 2e 74 6f 5f 72 66 63 32 38 32 32 28 29 29 3b 0a .to_rfc2822());.
1b80: 09 09 7d 3b 0a 09 09 6c 65 74 20 72 65 73 70 6f ..};...let respo
1b90: 6e 73 65 20 3d 20 62 75 69 6c 64 65 72 2e 73 65 nse = builder.se
1ba0: 6e 64 28 29 2e 61 77 61 69 74 2e 73 74 61 63 6b nd().await.stack
1bb0: 28 29 3f 3b 0a 09 09 23 5b 63 66 67 28 64 65 62 ()?;...#[cfg(deb
1bc0: 75 67 5f 61 73 73 65 72 74 69 6f 6e 73 29 5d 0a ug_assertions)].
1bd0: 09 09 7b 0a 09 09 09 6c 65 74 20 68 65 61 64 65 ..{....let heade
1be0: 72 73 20 3d 20 72 65 73 70 6f 6e 73 65 2e 68 65 rs = response.he
1bf0: 61 64 65 72 73 28 29 3b 0a 09 09 09 6c 65 74 20 aders();....let
1c00: 65 78 70 69 72 65 73 20 3d 20 68 65 61 64 65 72 expires = header
1c10: 73 2e 67 65 74 28 45 58 50 49 52 45 53 29 3b 0a s.get(EXPIRES);.
1c20: 09 09 09 6c 65 74 20 63 61 63 68 65 20 3d 20 68 ...let cache = h
1c30: 65 61 64 65 72 73 2e 67 65 74 28 43 41 43 48 45 eaders.get(CACHE
1c40: 5f 43 4f 4e 54 52 4f 4c 29 3b 0a 09 09 09 69 66 _CONTROL);....if
1c50: 20 65 78 70 69 72 65 73 2e 69 73 5f 73 6f 6d 65 expires.is_some
1c60: 28 29 20 7c 7c 20 63 61 63 68 65 2e 69 73 5f 73 () || cache.is_s
1c70: 6f 6d 65 28 29 20 7b 0a 09 09 09 09 70 72 69 6e ome() {.....prin
1c80: 74 6c 6e 21 28 22 7b 7d 20 7b 7d 20 7b 3a 3f 7d tln!("{} {} {:?}
1c90: 20 7b 3a 3f 7d 20 7b 3a 3f 7d 22 2c 20 4c 6f 63 {:?} {:?}", Loc
1ca0: 61 6c 3a 3a 6e 6f 77 28 29 2e 74 6f 5f 72 66 63 al::now().to_rfc
1cb0: 32 38 32 32 28 29 2c 20 73 6f 75 72 63 65 2e 75 2822(), source.u
1cc0: 72 6c 2c 20 6c 61 73 74 5f 73 63 72 61 70 65 2c rl, last_scrape,
1cd0: 20 65 78 70 69 72 65 73 2c 20 63 61 63 68 65 29 expires, cache)
1ce0: 3b 0a 09 09 09 7d 0a 09 09 7d 0a 09 09 6c 65 74 ;....}...}...let
1cf0: 20 73 74 61 74 75 73 20 3d 20 72 65 73 70 6f 6e status = respon
1d00: 73 65 2e 73 74 61 74 75 73 28 29 3b 0a 09 09 6c se.status();...l
1d10: 65 74 20 63 6f 6e 74 65 6e 74 20 3d 20 72 65 73 et content = res
1d20: 70 6f 6e 73 65 2e 62 79 74 65 73 28 29 2e 61 77 ponse.bytes().aw
1d30: 61 69 74 2e 73 74 61 63 6b 28 29 3f 3b 0a 09 09 ait.stack()?;...
1d40: 6d 61 74 63 68 20 72 73 73 3a 3a 43 68 61 6e 6e match rss::Chann
1d50: 65 6c 3a 3a 72 65 61 64 5f 66 72 6f 6d 28 26 63 el::read_from(&c
1d60: 6f 6e 74 65 6e 74 5b 2e 2e 5d 29 20 7b 0a 09 09 ontent[..]) {...
1d70: 09 4f 6b 28 66 65 65 64 29 20 3d 3e 20 7b 0a 09 .Ok(feed) => {..
1d80: 09 09 09 66 6f 72 20 69 74 65 6d 20 69 6e 20 66 ...for item in f
1d90: 65 65 64 2e 69 74 65 6d 73 28 29 20 7b 0a 09 09 eed.items() {...
1da0: 09 09 09 69 66 20 6c 65 74 20 53 6f 6d 65 28 6c ...if let Some(l
1db0: 69 6e 6b 29 20 3d 20 69 74 65 6d 2e 6c 69 6e 6b ink) = item.link
1dc0: 28 29 20 7b 0a 09 09 09 09 09 09 6c 65 74 20 64 () {.......let d
1dd0: 61 74 65 20 3d 20 6d 61 74 63 68 20 69 74 65 6d ate = match item
1de0: 2e 70 75 62 5f 64 61 74 65 28 29 20 7b 0a 09 09 .pub_date() {...
1df0: 09 09 09 09 09 53 6f 6d 65 28 66 65 65 64 5f 64 .....Some(feed_d
1e00: 61 74 65 29 20 3d 3e 20 44 61 74 65 54 69 6d 65 ate) => DateTime
1e10: 3a 3a 70 61 72 73 65 5f 66 72 6f 6d 5f 72 66 63 ::parse_from_rfc
1e20: 32 38 32 32 28 66 65 65 64 5f 64 61 74 65 29 2c 2822(feed_date),
1e30: 0a 09 09 09 09 09 09 09 4e 6f 6e 65 20 3d 3e 20 ........None =>
1e40: 44 61 74 65 54 69 6d 65 3a 3a 70 61 72 73 65 5f DateTime::parse_
1e50: 66 72 6f 6d 5f 72 66 63 33 33 33 39 28 6d 61 74 from_rfc3339(mat
1e60: 63 68 20 69 74 65 6d 2e 64 75 62 6c 69 6e 5f 63 ch item.dublin_c
1e70: 6f 72 65 5f 65 78 74 28 29 20 7b 0a 09 09 09 09 ore_ext() {.....
1e80: 09 09 09 09 53 6f 6d 65 28 65 78 74 29 20 3d 3e ....Some(ext) =>
1e90: 20 7b 0a 09 09 09 09 09 09 09 09 09 6c 65 74 20 {..........let
1ea0: 64 61 74 65 73 20 3d 20 65 78 74 2e 64 61 74 65 dates = ext.date
1eb0: 73 28 29 3b 0a 09 09 09 09 09 09 09 09 09 69 66 s();..........if
1ec0: 20 64 61 74 65 73 2e 69 73 5f 65 6d 70 74 79 28 dates.is_empty(
1ed0: 29 20 7b 0a 09 09 09 09 09 09 09 09 09 09 62 61 ) {...........ba
1ee0: 69 6c 21 28 22 46 65 65 64 20 69 74 65 6d 20 68 il!("Feed item h
1ef0: 61 73 20 44 75 62 6c 69 6e 20 43 6f 72 65 20 65 as Dublin Core e
1f00: 78 74 65 6e 73 69 6f 6e 20 62 75 74 20 6e 6f 20 xtension but no
1f10: 64 61 74 65 73 2e 22 29 0a 09 09 09 09 09 09 09 dates.")........
1f20: 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 09 09 ..} else {......
1f30: 09 09 09 09 09 26 64 61 74 65 73 5b 30 5d 0a 09 .....&dates[0]..
1f40: 09 09 09 09 09 09 09 09 7d 0a 09 09 09 09 09 09 ........}.......
1f50: 09 09 7d 2c 0a 09 09 09 09 09 09 09 09 4e 6f 6e ..},.........Non
1f60: 65 20 3d 3e 20 62 61 69 6c 21 28 22 46 65 65 64 e => bail!("Feed
1f70: 20 69 74 65 6d 20 6d 69 73 73 65 73 20 70 6f 73 item misses pos
1f80: 74 69 6e 67 20 64 61 74 65 2e 22 29 2c 0a 09 09 ting date."),...
1f90: 09 09 09 09 09 7d 29 2c 0a 09 09 09 09 09 09 7d .....}),.......}
1fa0: 2e 73 74 61 63 6b 28 29 3f 3b 0a 09 09 09 09 09 .stack()?;......
1fb0: 09 6c 65 74 20 75 72 69 20 3d 20 6c 69 6e 6b 2e .let uri = link.
1fc0: 74 6f 5f 73 74 72 69 6e 67 28 29 3b 0a 09 09 09 to_string();....
1fd0: 09 09 09 6c 65 74 20 74 69 74 6c 65 20 3d 20 69 ...let title = i
1fe0: 74 65 6d 2e 74 69 74 6c 65 28 29 2e 75 6e 77 72 tem.title().unwr
1ff0: 61 70 5f 6f 72 28 22 22 29 2e 74 6f 5f 73 74 72 ap_or("").to_str
2000: 69 6e 67 28 29 3b 0a 09 09 09 09 09 09 6c 65 74 ing();.......let
2010: 20 61 75 74 68 6f 72 73 20 3d 20 69 74 65 6d 2e authors = item.
2020: 61 75 74 68 6f 72 28 29 2e 75 6e 77 72 61 70 5f author().unwrap_
2030: 6f 72 28 22 22 29 2e 74 6f 5f 73 74 72 69 6e 67 or("").to_string
2040: 28 29 3b 0a 09 09 09 09 09 09 6c 65 74 20 73 75 ();.......let su
2050: 6d 6d 61 72 79 20 3d 20 69 74 65 6d 2e 63 6f 6e mmary = item.con
2060: 74 65 6e 74 28 29 2e 75 6e 77 72 61 70 5f 6f 72 tent().unwrap_or
2070: 28 22 22 29 2e 74 6f 5f 73 74 72 69 6e 67 28 29 ("").to_string()
2080: 3b 0a 09 09 09 09 09 09 70 6f 73 74 73 2e 69 6e ;.......posts.in
2090: 73 65 72 74 28 64 61 74 65 2c 20 50 6f 73 74 7b sert(date, Post{
20a0: 0a 09 09 09 09 09 09 09 75 72 69 2c 0a 09 09 09 ........uri,....
20b0: 09 09 09 09 74 69 74 6c 65 2c 0a 09 09 09 09 09 ....title,......
20c0: 09 09 61 75 74 68 6f 72 73 2c 0a 09 09 09 09 09 ..authors,......
20d0: 09 09 73 75 6d 6d 61 72 79 2c 0a 09 09 09 09 09 ..summary,......
20e0: 09 7d 29 3b 0a 09 09 09 09 09 7d 0a 09 09 09 09 .});......}.....
20f0: 7d 3b 0a 09 09 09 7d 2c 0a 09 09 09 45 72 72 28 };....},....Err(
2100: 65 72 72 29 20 3d 3e 20 6d 61 74 63 68 20 65 72 err) => match er
2110: 72 20 7b 0a 09 09 09 09 72 73 73 3a 3a 45 72 72 r {.....rss::Err
2120: 6f 72 3a 3a 49 6e 76 61 6c 69 64 53 74 61 72 74 or::InvalidStart
2130: 54 61 67 20 3d 3e 20 7b 0a 09 09 09 09 09 6d 61 Tag => {......ma
2140: 74 63 68 20 61 74 6f 6d 5f 73 79 6e 64 69 63 61 tch atom_syndica
2150: 74 69 6f 6e 3a 3a 46 65 65 64 3a 3a 72 65 61 64 tion::Feed::read
2160: 5f 66 72 6f 6d 28 26 63 6f 6e 74 65 6e 74 5b 2e _from(&content[.
2170: 2e 5d 29 20 7b 0a 09 09 09 09 09 09 4f 6b 28 66 .]) {.......Ok(f
2180: 65 65 64 29 20 3d 3e 20 7b 0a 09 09 09 09 09 09 eed) => {.......
2190: 09 66 6f 72 20 69 74 65 6d 20 69 6e 20 66 65 65 .for item in fee
21a0: 64 2e 65 6e 74 72 69 65 73 28 29 20 7b 0a 09 09 d.entries() {...
21b0: 09 09 09 09 09 09 6c 65 74 20 64 61 74 65 20 3d ......let date =
21c0: 20 69 74 65 6d 2e 70 75 62 6c 69 73 68 65 64 28 item.published(
21d0: 29 0a 09 09 09 09 09 09 09 09 09 2e 73 74 61 63 )...........stac
21e0: 6b 5f 65 72 72 28 22 46 65 65 64 20 69 74 65 6d k_err("Feed item
21f0: 20 6d 69 73 73 69 6e 67 20 70 75 62 6c 69 73 68 missing publish
2200: 69 6e 67 20 64 61 74 65 2e 22 29 3f 3b 0a 09 09 ing date.")?;...
2210: 09 09 09 09 09 09 6c 65 74 20 75 72 69 20 3d 20 ......let uri =
2220: 7b 0a 09 09 09 09 09 09 09 09 09 6c 65 74 20 6c {..........let l
2230: 69 6e 6b 73 20 3d 20 69 74 65 6d 2e 6c 69 6e 6b inks = item.link
2240: 73 28 29 3b 0a 09 09 09 09 09 09 09 09 09 69 66 s();..........if
2250: 20 6c 69 6e 6b 73 2e 69 73 5f 65 6d 70 74 79 28 links.is_empty(
2260: 29 20 7b 0a 09 09 09 09 09 09 09 09 09 09 62 61 ) {...........ba
2270: 69 6c 21 28 22 46 65 65 64 20 69 74 65 6d 20 6d il!("Feed item m
2280: 69 73 73 69 6e 67 20 70 6f 73 74 20 6c 69 6e 6b issing post link
2290: 73 2e 22 29 3b 0a 09 09 09 09 09 09 09 09 09 7d s.");..........}
22a0: 20 65 6c 73 65 20 7b 0a 09 09 09 09 09 09 09 09 else {.........
22b0: 09 09 6c 69 6e 6b 73 5b 30 5d 2e 68 72 65 66 28 ..links[0].href(
22c0: 29 2e 74 6f 5f 73 74 72 69 6e 67 28 29 0a 09 09 ).to_string()...
22d0: 09 09 09 09 09 09 09 7d 0a 09 09 09 09 09 09 09 .......}........
22e0: 09 7d 3b 0a 09 09 09 09 09 09 09 09 6c 65 74 20 .};.........let
22f0: 74 69 74 6c 65 20 3d 20 69 74 65 6d 2e 74 69 74 title = item.tit
2300: 6c 65 28 29 2e 74 6f 5f 73 74 72 69 6e 67 28 29 le().to_string()
2310: 3b 0a 09 09 09 09 09 09 09 09 6c 65 74 20 61 75 ;.........let au
2320: 74 68 6f 72 73 20 3d 20 69 74 65 6d 2e 61 75 74 thors = item.aut
2330: 68 6f 72 73 28 29 2e 69 74 65 72 28 29 2e 6d 61 hors().iter().ma
2340: 70 28 7c 78 7c 20 66 6f 72 6d 61 74 21 28 22 7b p(|x| format!("{
2350: 7d 20 3c 7b 3a 3f 7d 3e 22 2c 20 78 2e 6e 61 6d } <{:?}>", x.nam
2360: 65 28 29 2c 20 78 2e 65 6d 61 69 6c 28 29 29 29 e(), x.email()))
2370: 2e 63 6f 6c 6c 65 63 74 3a 3a 3c 56 65 63 3c 53 .collect::<Vec<S
2380: 74 72 69 6e 67 3e 3e 28 29 2e 6a 6f 69 6e 28 22 tring>>().join("
2390: 2c 20 22 29 3b 0a 09 09 09 09 09 09 09 09 6c 65 , ");.........le
23a0: 74 20 73 75 6d 6d 61 72 79 20 3d 20 69 66 20 6c t summary = if l
23b0: 65 74 20 53 6f 6d 65 28 73 75 6d 29 20 3d 20 69 et Some(sum) = i
23c0: 74 65 6d 2e 73 75 6d 6d 61 72 79 28 29 20 7b 20 tem.summary() {
23d0: 73 75 6d 2e 76 61 6c 75 65 2e 63 6c 6f 6e 65 28 sum.value.clone(
23e0: 29 20 7d 20 65 6c 73 65 20 7b 20 53 74 72 69 6e ) } else { Strin
23f0: 67 3a 3a 6e 65 77 28 29 20 7d 3b 0a 09 09 09 09 g::new() };.....
2400: 09 09 09 09 70 6f 73 74 73 2e 69 6e 73 65 72 74 ....posts.insert
2410: 28 2a 64 61 74 65 2c 20 50 6f 73 74 7b 0a 09 09 (*date, Post{...
2420: 09 09 09 09 09 09 09 75 72 69 2c 0a 09 09 09 09 .......uri,.....
2430: 09 09 09 09 09 74 69 74 6c 65 2c 0a 09 09 09 09 .....title,.....
2440: 09 09 09 09 09 61 75 74 68 6f 72 73 2c 0a 09 09 .....authors,...
2450: 09 09 09 09 09 09 09 73 75 6d 6d 61 72 79 2c 0a .......summary,.
2460: 09 09 09 09 09 09 09 09 7d 29 3b 0a 09 09 09 09 ........});.....
2470: 09 09 09 7d 3b 0a 09 09 09 09 09 09 7d 2c 0a 09 ...};.......},..
2480: 09 09 09 09 09 45 72 72 28 65 72 72 29 20 3d 3e .....Err(err) =>
2490: 20 7b 0a 09 09 09 09 09 09 09 62 61 69 6c 21 28 {........bail!(
24a0: 22 55 6e 73 75 70 70 6f 72 74 65 64 20 6f 72 20 "Unsupported or
24b0: 6d 61 6e 67 6c 65 64 20 63 6f 6e 74 65 6e 74 3a mangled content:
24c0: 5c 6e 7b 3a 3f 7d 5c 6e 7b 65 72 72 7d 5c 6e 7b \n{:?}\n{err}\n{
24d0: 73 74 61 74 75 73 3a 23 3f 7d 5c 6e 22 2c 20 26 status:#?}\n", &
24e0: 73 6f 75 72 63 65 2e 75 72 6c 29 0a 09 09 09 09 source.url).....
24f0: 09 09 7d 2c 0a 09 09 09 09 09 7d 0a 09 09 09 09 ..},......}.....
2500: 7d 2c 0a 09 09 09 09 72 73 73 3a 3a 45 72 72 6f },.....rss::Erro
2510: 72 3a 3a 45 6f 66 20 3d 3e 20 28 29 2c 0a 09 09 r::Eof => (),...
2520: 09 09 5f 20 3d 3e 20 62 61 69 6c 21 28 22 55 6e .._ => bail!("Un
2530: 73 75 70 70 6f 72 74 65 64 20 6f 72 20 6d 61 6e supported or man
2540: 67 6c 65 64 20 63 6f 6e 74 65 6e 74 3a 5c 6e 7b gled content:\n{
2550: 3a 3f 7d 5c 6e 7b 65 72 72 7d 5c 6e 7b 73 74 61 :?}\n{err}\n{sta
2560: 74 75 73 3a 23 3f 7d 5c 6e 22 2c 20 26 73 6f 75 tus:#?}\n", &sou
2570: 72 63 65 2e 75 72 6c 29 0a 09 09 09 7d 0a 09 09 rce.url)....}...
2580: 7d 3b 0a 09 09 66 6f 72 20 28 64 61 74 65 2c 20 };...for (date,
2590: 70 6f 73 74 29 20 69 6e 20 70 6f 73 74 73 2e 69 post) in posts.i
25a0: 74 65 72 28 29 20 7b 0a 09 09 09 6c 65 74 20 70 ter() {....let p
25b0: 6f 73 74 5f 75 72 6c 3a 20 43 6f 77 3c 73 74 72 ost_url: Cow<str
25c0: 3e 20 3d 20 6d 61 74 63 68 20 73 6f 75 72 63 65 > = match source
25d0: 2e 75 72 6c 5f 72 65 20 7b 0a 09 09 09 09 53 6f .url_re {.....So
25e0: 6d 65 28 72 65 66 20 78 29 20 3d 3e 20 73 65 64 me(ref x) => sed
25f0: 72 65 67 65 78 3a 3a 52 65 70 6c 61 63 65 43 6f regex::ReplaceCo
2600: 6d 6d 61 6e 64 3a 3a 6e 65 77 28 78 29 2e 73 74 mmand::new(x).st
2610: 61 63 6b 28 29 3f 2e 65 78 65 63 75 74 65 28 26 ack()?.execute(&
2620: 70 6f 73 74 2e 75 72 69 29 2c 0a 09 09 09 09 4e post.uri),.....N
2630: 6f 6e 65 20 3d 3e 20 70 6f 73 74 2e 75 72 69 2e one => post.uri.
2640: 63 6c 6f 6e 65 28 29 2e 69 6e 74 6f 28 29 2c 0a clone().into(),.
2650: 09 09 09 7d 3b 0a 09 09 09 69 66 20 21 20 63 6f ...};....if ! co
2660: 6e 6e 2e 65 78 69 73 74 73 28 26 70 6f 73 74 5f nn.exists(&post_
2670: 75 72 6c 2c 20 69 64 29 2e 61 77 61 69 74 2e 73 url, id).await.s
2680: 74 61 63 6b 28 29 3f 20 7b 0a 09 09 09 09 69 66 tack()? {.....if
2690: 20 74 68 69 73 5f 66 65 74 63 68 2e 69 73 5f 6e this_fetch.is_n
26a0: 6f 6e 65 28 29 20 7c 7c 20 2a 64 61 74 65 20 3e one() || *date >
26b0: 20 74 68 69 73 5f 66 65 74 63 68 2e 75 6e 77 72 this_fetch.unwr
26c0: 61 70 28 29 20 7b 0a 09 09 09 09 09 74 68 69 73 ap() {......this
26d0: 5f 66 65 74 63 68 20 3d 20 53 6f 6d 65 28 2a 64 _fetch = Some(*d
26e0: 61 74 65 29 3b 0a 09 09 09 09 7d 3b 0a 09 09 09 ate);.....};....
26f0: 09 73 65 6c 66 2e 73 65 6e 64 28 20 6d 61 74 63 .self.send( matc
2700: 68 20 26 73 6f 75 72 63 65 2e 69 76 5f 68 61 73 h &source.iv_has
2710: 68 20 7b 0a 09 09 09 09 09 53 6f 6d 65 28 68 61 h {......Some(ha
2720: 73 68 29 20 3d 3e 20 66 6f 72 6d 61 74 21 28 22 sh) => format!("
2730: 3c 61 20 68 72 65 66 3d 5c 22 68 74 74 70 73 3a <a href=\"https:
2740: 2f 2f 74 2e 6d 65 2f 69 76 3f 75 72 6c 3d 7b 70 //t.me/iv?url={p
2750: 6f 73 74 5f 75 72 6c 7d 26 72 68 61 73 68 3d 7b ost_url}&rhash={
2760: 68 61 73 68 7d 5c 22 3e 20 3c 2f 61 3e 7b 70 6f hash}\"> </a>{po
2770: 73 74 5f 75 72 6c 7d 22 29 2c 0a 09 09 09 09 09 st_url}"),......
2780: 4e 6f 6e 65 20 3d 3e 20 66 6f 72 6d 61 74 21 28 None => format!(
2790: 22 7b 70 6f 73 74 5f 75 72 6c 7d 22 29 2c 0a 09 "{post_url}"),..
27a0: 09 09 09 7d 2c 20 53 6f 6d 65 28 64 65 73 74 69 ...}, Some(desti
27b0: 6e 61 74 69 6f 6e 29 2c 20 53 6f 6d 65 28 50 61 nation), Some(Pa
27c0: 72 73 65 4d 6f 64 65 3a 3a 48 74 6d 6c 29 29 2e rseMode::Html)).
27d0: 61 77 61 69 74 2e 73 74 61 63 6b 28 29 3f 3b 0a await.stack()?;.
27e0: 09 09 09 09 63 6f 6e 6e 2e 61 64 64 5f 70 6f 73 ....conn.add_pos
27f0: 74 28 69 64 2c 20 64 61 74 65 2c 20 26 70 6f 73 t(id, date, &pos
2800: 74 5f 75 72 6c 29 2e 61 77 61 69 74 2e 73 74 61 t_url).await.sta
2810: 63 6b 28 29 3f 3b 0a 09 09 09 09 70 6f 73 74 65 ck()?;.....poste
2820: 64 20 2b 3d 20 31 3b 0a 09 09 09 7d 3b 0a 09 09 d += 1;....};...
2830: 7d 3b 0a 09 09 70 6f 73 74 73 2e 63 6c 65 61 72 };...posts.clear
2840: 28 29 3b 0a 09 09 4f 6b 28 66 6f 72 6d 61 74 21 ();...Ok(format!
2850: 28 22 50 6f 73 74 65 64 3a 20 7b 70 6f 73 74 65 ("Posted: {poste
2860: 64 7d 22 29 29 0a 09 7d 0a 0a 09 61 73 79 6e 63 d}"))..}...async
2870: 20 66 6e 20 61 75 74 6f 66 65 74 63 68 28 26 73 fn autofetch(&s
2880: 65 6c 66 29 20 2d 3e 20 52 65 73 75 6c 74 3c 73 elf) -> Result<s
2890: 74 64 3a 3a 74 69 6d 65 3a 3a 44 75 72 61 74 69 td::time::Durati
28a0: 6f 6e 3e 20 7b 0a 09 09 6c 65 74 20 6d 75 74 20 on> {...let mut
28b0: 64 65 6c 61 79 20 3d 20 63 68 72 6f 6e 6f 3a 3a delay = chrono::
28c0: 44 75 72 61 74 69 6f 6e 3a 3a 6d 69 6e 75 74 65 Duration::minute
28d0: 73 28 31 29 3b 0a 09 09 6c 65 74 20 6e 6f 77 20 s(1);...let now
28e0: 3d 20 63 68 72 6f 6e 6f 3a 3a 4c 6f 63 61 6c 3a = chrono::Local:
28f0: 3a 6e 6f 77 28 29 3b 0a 09 09 6c 65 74 20 71 75 :now();...let qu
2900: 65 75 65 20 3d 20 7b 0a 09 09 09 6c 65 74 20 6d eue = {....let m
2910: 75 74 20 63 6f 6e 6e 20 3d 20 73 65 6c 66 2e 64 ut conn = self.d
2920: 62 2e 62 65 67 69 6e 28 29 2e 61 77 61 69 74 2e b.begin().await.
2930: 73 74 61 63 6b 28 29 3f 3b 0a 09 09 09 63 6f 6e stack()?;....con
2940: 6e 2e 67 65 74 5f 71 75 65 75 65 28 29 2e 61 77 n.get_queue().aw
2950: 61 69 74 2e 73 74 61 63 6b 28 29 3f 0a 09 09 7d ait.stack()?...}
2960: 3b 0a 09 09 66 6f 72 20 72 6f 77 20 69 6e 20 71 ;...for row in q
2970: 75 65 75 65 20 7b 0a 09 09 09 69 66 20 6c 65 74 ueue {....if let
2980: 20 53 6f 6d 65 28 6e 65 78 74 5f 66 65 74 63 68 Some(next_fetch
2990: 29 20 3d 20 72 6f 77 2e 6e 65 78 74 5f 66 65 74 ) = row.next_fet
29a0: 63 68 20 7b 0a 09 09 09 09 69 66 20 6e 65 78 74 ch {.....if next
29b0: 5f 66 65 74 63 68 20 3c 20 6e 6f 77 20 7b 0a 09 _fetch < now {..
29c0: 09 09 09 09 69 66 20 6c 65 74 20 28 53 6f 6d 65 ....if let (Some
29d0: 28 6f 77 6e 65 72 29 2c 20 53 6f 6d 65 28 73 6f (owner), Some(so
29e0: 75 72 63 65 5f 69 64 29 2c 20 6c 61 73 74 5f 73 urce_id), last_s
29f0: 63 72 61 70 65 29 20 3d 20 28 72 6f 77 2e 6f 77 crape) = (row.ow
2a00: 6e 65 72 2c 20 72 6f 77 2e 73 6f 75 72 63 65 5f ner, row.source_
2a10: 69 64 2c 20 72 6f 77 2e 6c 61 73 74 5f 73 63 72 id, row.last_scr
2a20: 61 70 65 29 20 7b 0a 09 09 09 09 09 09 6c 65 74 ape) {.......let
2a30: 20 63 6c 6f 6e 65 20 3d 20 43 6f 72 65 20 7b 0a clone = Core {.
2a40: 09 09 09 09 09 09 09 6f 77 6e 65 72 5f 63 68 61 .......owner_cha
2a50: 74 3a 20 43 68 61 74 50 65 65 72 49 64 3a 3a 66 t: ChatPeerId::f
2a60: 72 6f 6d 28 6f 77 6e 65 72 29 2c 0a 09 09 09 09 rom(owner),.....
2a70: 09 09 09 2e 2e 73 65 6c 66 2e 63 6c 6f 6e 65 28 .....self.clone(
2a80: 29 0a 09 09 09 09 09 09 7d 3b 0a 09 09 09 09 09 ).......};......
2a90: 09 6c 65 74 20 73 6f 75 72 63 65 20 3d 20 7b 0a .let source = {.
2aa0: 09 09 09 09 09 09 09 6c 65 74 20 6d 75 74 20 63 .......let mut c
2ab0: 6f 6e 6e 20 3d 20 73 65 6c 66 2e 64 62 2e 62 65 onn = self.db.be
2ac0: 67 69 6e 28 29 2e 61 77 61 69 74 2e 73 74 61 63 gin().await.stac
2ad0: 6b 28 29 3f 3b 0a 09 09 09 09 09 09 09 6d 61 74 k()?;........mat
2ae0: 63 68 20 63 6f 6e 6e 2e 67 65 74 5f 6f 6e 65 28 ch conn.get_one(
2af0: 6f 77 6e 65 72 2c 20 73 6f 75 72 63 65 5f 69 64 owner, source_id
2b00: 29 2e 61 77 61 69 74 20 7b 0a 09 09 09 09 09 09 ).await {.......
2b10: 09 09 4f 6b 28 53 6f 6d 65 28 73 6f 75 72 63 65 ..Ok(Some(source
2b20: 29 29 20 3d 3e 20 73 6f 75 72 63 65 2e 74 6f 5f )) => source.to_
2b30: 73 74 72 69 6e 67 28 29 2c 0a 09 09 09 09 09 09 string(),.......
2b40: 09 09 4f 6b 28 4e 6f 6e 65 29 20 3d 3e 20 22 53 ..Ok(None) => "S
2b50: 6f 75 72 63 65 20 6e 6f 74 20 66 6f 75 6e 64 20 ource not found
2b60: 69 6e 20 64 61 74 61 62 61 73 65 3f 22 2e 74 6f in database?".to
2b70: 5f 73 74 72 69 6e 67 28 29 2c 0a 09 09 09 09 09 _string(),......
2b80: 09 09 09 45 72 72 28 65 72 72 29 20 3d 3e 20 66 ...Err(err) => f
2b90: 6f 72 6d 61 74 21 28 22 46 61 69 6c 65 64 20 74 ormat!("Failed t
2ba0: 6f 20 66 65 74 63 68 20 73 6f 75 72 63 65 20 64 o fetch source d
2bb0: 61 74 61 3a 5c 6e 7b 65 72 72 7d 22 29 2c 0a 09 ata:\n{err}"),..
2bc0: 09 09 09 09 09 09 7d 0a 09 09 09 09 09 09 7d 3b ......}.......};
2bd0: 0a 09 09 09 09 09 09 73 6d 6f 6c 3a 3a 73 70 61 .......smol::spa
2be0: 77 6e 28 43 6f 6d 70 61 74 3a 3a 6e 65 77 28 61 wn(Compat::new(a
2bf0: 73 79 6e 63 20 6d 6f 76 65 20 7b 0a 09 09 09 09 sync move {.....
2c00: 09 09 09 69 66 20 6c 65 74 20 45 72 72 28 65 72 ...if let Err(er
2c10: 72 29 20 3d 20 63 6c 6f 6e 65 2e 63 68 65 63 6b r) = clone.check
2c20: 28 73 6f 75 72 63 65 5f 69 64 2c 20 74 72 75 65 (source_id, true
2c30: 2c 20 53 6f 6d 65 28 6c 61 73 74 5f 73 63 72 61 , Some(last_scra
2c40: 70 65 29 29 2e 61 77 61 69 74 20 7b 0a 09 09 09 pe)).await {....
2c50: 09 09 09 09 09 69 66 20 6c 65 74 20 45 72 72 28 .....if let Err(
2c60: 65 72 72 29 20 3d 20 63 6c 6f 6e 65 2e 73 65 6e err) = clone.sen
2c70: 64 28 26 66 6f 72 6d 61 74 21 28 22 f0 9f 9b 91 d(&format!("🛑
2c80: 20 7b 73 6f 75 72 63 65 7d 5c 6e 7b 7d 22 2c 20 {source}\n{}",
2c90: 65 6e 63 6f 64 65 28 26 65 72 72 2e 74 6f 5f 73 encode(&err.to_s
2ca0: 74 72 69 6e 67 28 29 29 29 2c 20 4e 6f 6e 65 2c tring())), None,
2cb0: 20 53 6f 6d 65 28 50 61 72 73 65 4d 6f 64 65 3a Some(ParseMode:
2cc0: 3a 4d 61 72 6b 64 6f 77 6e 56 32 29 29 2e 61 77 :MarkdownV2)).aw
2cd0: 61 69 74 20 7b 0a 09 09 09 09 09 09 09 09 09 65 ait {..........e
2ce0: 70 72 69 6e 74 6c 6e 21 28 22 43 68 65 63 6b 20 println!("Check
2cf0: 65 72 72 6f 72 3a 20 7b 65 72 72 7d 22 29 3b 0a error: {err}");.
2d00: 09 09 09 09 09 09 09 09 09 2f 2f 20 63 6c 6f 6e .........// clon
2d10: 65 2e 64 69 73 61 62 6c 65 28 26 73 6f 75 72 63 e.disable(&sourc
2d20: 65 5f 69 64 2c 20 6f 77 6e 65 72 29 2e 61 77 61 e_id, owner).awa
2d30: 69 74 2e 75 6e 77 72 61 70 28 29 3b 0a 09 09 09 it.unwrap();....
2d40: 09 09 09 09 09 7d 3b 0a 09 09 09 09 09 09 09 7d .....};........}
2d50: 3b 0a 09 09 09 09 09 09 7d 29 29 2e 64 65 74 61 ;.......})).deta
2d60: 63 68 28 29 3b 0a 09 09 09 09 09 7d 0a 09 09 09 ch();......}....
2d70: 09 7d 20 65 6c 73 65 20 69 66 20 6e 65 78 74 5f .} else if next_
2d80: 66 65 74 63 68 20 2d 20 6e 6f 77 20 3c 20 64 65 fetch - now < de
2d90: 6c 61 79 20 7b 0a 09 09 09 09 09 64 65 6c 61 79 lay {......delay
2da0: 20 3d 20 6e 65 78 74 5f 66 65 74 63 68 20 2d 20 = next_fetch -
2db0: 6e 6f 77 3b 0a 09 09 09 09 7d 0a 09 09 09 7d 0a now;.....}....}.
2dc0: 09 09 7d 3b 0a 09 09 64 65 6c 61 79 2e 74 6f 5f ..};...delay.to_
2dd0: 73 74 64 28 29 2e 73 74 61 63 6b 28 29 0a 09 7d std().stack()..}
2de0: 0a 0a 09 70 75 62 20 61 73 79 6e 63 20 66 6e 20 ...pub async fn
2df0: 6c 69 73 74 20 28 26 73 65 6c 66 2c 20 6f 77 6e list (&self, own
2e00: 65 72 3a 20 55 73 65 72 50 65 65 72 49 64 29 20 er: UserPeerId)
2e10: 2d 3e 20 52 65 73 75 6c 74 3c 53 74 72 69 6e 67 -> Result<String
2e20: 3e 20 7b 0a 09 09 6c 65 74 20 6d 75 74 20 72 65 > {...let mut re
2e30: 70 6c 79 3a 20 56 65 63 3c 53 74 72 69 6e 67 3e ply: Vec<String>
2e40: 20 3d 20 76 65 63 21 5b 5d 3b 0a 09 09 72 65 70 = vec![];...rep
2e50: 6c 79 2e 70 75 73 68 28 22 43 68 61 6e 6e 65 6c ly.push("Channel
2e60: 73 3a 22 2e 69 6e 74 6f 28 29 29 3b 0a 09 09 6c s:".into());...l
2e70: 65 74 20 6d 75 74 20 63 6f 6e 6e 20 3d 20 73 65 et mut conn = se
2e80: 6c 66 2e 64 62 2e 62 65 67 69 6e 28 29 2e 61 77 lf.db.begin().aw
2e90: 61 69 74 2e 73 74 61 63 6b 28 29 3f 3b 0a 09 09 ait.stack()?;...
2ea0: 66 6f 72 20 72 6f 77 20 69 6e 20 63 6f 6e 6e 2e for row in conn.
2eb0: 67 65 74 5f 6c 69 73 74 28 6f 77 6e 65 72 29 2e get_list(owner).
2ec0: 61 77 61 69 74 2e 73 74 61 63 6b 28 29 3f 20 7b await.stack()? {
2ed0: 0a 09 09 09 72 65 70 6c 79 2e 70 75 73 68 28 72 ....reply.push(r
2ee0: 6f 77 2e 74 6f 5f 73 74 72 69 6e 67 28 29 29 3b ow.to_string());
2ef0: 0a 09 09 7d 3b 0a 09 09 4f 6b 28 72 65 70 6c 79 ...};...Ok(reply
2f00: 2e 6a 6f 69 6e 28 22 5c 6e 5c 6e 22 29 29 0a 09 .join("\n\n"))..
2f10: 7d 0a 7d 0a 0a 69 6d 70 6c 20 55 70 64 61 74 65 }.}..impl Update
2f20: 48 61 6e 64 6c 65 72 20 66 6f 72 20 43 6f 72 65 Handler for Core
2f30: 20 7b 0a 09 61 73 79 6e 63 20 66 6e 20 68 61 6e {..async fn han
2f40: 64 6c 65 20 28 26 73 65 6c 66 2c 20 75 70 64 61 dle (&self, upda
2f50: 74 65 3a 20 55 70 64 61 74 65 29 20 7b 0a 09 09 te: Update) {...
2f60: 69 66 20 6c 65 74 20 55 70 64 61 74 65 54 79 70 if let UpdateTyp
2f70: 65 3a 3a 4d 65 73 73 61 67 65 28 6d 73 67 29 20 e::Message(msg)
2f80: 3d 20 75 70 64 61 74 65 2e 75 70 64 61 74 65 5f = update.update_
2f90: 74 79 70 65 20 7b 0a 09 09 09 69 66 20 6c 65 74 type {....if let
2fa0: 20 4f 6b 28 63 6d 64 29 20 3d 20 43 6f 6d 6d 61 Ok(cmd) = Comma
2fb0: 6e 64 3a 3a 74 72 79 5f 66 72 6f 6d 28 2a 6d 73 nd::try_from(*ms
2fc0: 67 29 20 7b 0a 09 09 09 09 6c 65 74 20 6d 73 67 g) {.....let msg
2fd0: 20 3d 20 63 6d 64 2e 67 65 74 5f 6d 65 73 73 61 = cmd.get_messa
2fe0: 67 65 28 29 3b 0a 09 09 09 09 6c 65 74 20 77 6f ge();.....let wo
2ff0: 72 64 73 20 3d 20 63 6d 64 2e 67 65 74 5f 61 72 rds = cmd.get_ar
3000: 67 73 28 29 3b 0a 09 09 09 09 6c 65 74 20 63 6f gs();.....let co
3010: 6d 6d 61 6e 64 20 3d 20 63 6d 64 2e 67 65 74 5f mmand = cmd.get_
3020: 6e 61 6d 65 28 29 3b 0a 09 09 09 09 6c 65 74 20 name();.....let
3030: 72 65 73 20 3d 20 6d 61 74 63 68 20 63 6f 6d 6d res = match comm
3040: 61 6e 64 20 7b 0a 09 09 09 09 09 22 2f 63 68 65 and {......"/che
3050: 63 6b 22 20 7c 20 22 2f 63 6c 65 61 6e 22 20 7c ck" | "/clean" |
3060: 20 22 2f 65 6e 61 62 6c 65 22 20 7c 20 22 2f 64 "/enable" | "/d
3070: 65 6c 65 74 65 22 20 7c 20 22 2f 64 69 73 61 62 elete" | "/disab
3080: 6c 65 22 20 3d 3e 20 63 6f 6d 6d 61 6e 64 3a 3a le" => command::
3090: 63 6f 6d 6d 61 6e 64 28 73 65 6c 66 2c 20 63 6f command(self, co
30a0: 6d 6d 61 6e 64 2c 20 6d 73 67 2c 20 77 6f 72 64 mmand, msg, word
30b0: 73 29 2e 61 77 61 69 74 2c 0a 09 09 09 09 09 22 s).await,......"
30c0: 2f 73 74 61 72 74 22 20 3d 3e 20 63 6f 6d 6d 61 /start" => comma
30d0: 6e 64 3a 3a 73 74 61 72 74 28 73 65 6c 66 2c 20 nd::start(self,
30e0: 6d 73 67 29 2e 61 77 61 69 74 2c 0a 09 09 09 09 msg).await,.....
30f0: 09 22 2f 6c 69 73 74 22 20 3d 3e 20 63 6f 6d 6d ."/list" => comm
3100: 61 6e 64 3a 3a 6c 69 73 74 28 73 65 6c 66 2c 20 and::list(self,
3110: 6d 73 67 29 2e 61 77 61 69 74 2c 0a 09 09 09 09 msg).await,.....
3120: 09 22 2f 61 64 64 22 20 7c 20 22 2f 75 70 64 61 ."/add" | "/upda
3130: 74 65 22 20 3d 3e 20 63 6f 6d 6d 61 6e 64 3a 3a te" => command::
3140: 75 70 64 61 74 65 28 73 65 6c 66 2c 20 63 6f 6d update(self, com
3150: 6d 61 6e 64 2c 20 6d 73 67 2c 20 77 6f 72 64 73 mand, msg, words
3160: 29 2e 61 77 61 69 74 2c 0a 09 09 09 09 09 61 6e ).await,......an
3170: 79 20 3d 3e 20 45 72 72 28 61 6e 79 68 6f 77 21 y => Err(anyhow!
3180: 28 22 55 6e 6b 6e 6f 77 6e 20 63 6f 6d 6d 61 6e ("Unknown comman
3190: 64 3a 20 7b 61 6e 79 7d 22 29 29 2c 0a 09 09 09 d: {any}")),....
31a0: 09 7d 3b 0a 09 09 09 09 69 66 20 6c 65 74 20 45 .};.....if let E
31b0: 72 72 28 65 72 72 29 20 3d 20 72 65 73 20 7b 0a rr(err) = res {.
31c0: 09 09 09 09 09 69 66 20 6c 65 74 20 45 72 72 28 .....if let Err(
31d0: 65 72 72 32 29 20 3d 20 73 65 6c 66 2e 73 65 6e err2) = self.sen
31e0: 64 28 66 6f 72 6d 61 74 21 28 22 5c 5c 23 65 72 d(format!("\\#er
31f0: 72 6f 72 5c 6e 60 60 60 5c 6e 7b 65 72 72 7d 5c ror\n```\n{err}\
3200: 6e 60 60 60 22 29 2c 0a 09 09 09 09 09 09 53 6f n```"),.......So
3210: 6d 65 28 6d 73 67 2e 63 68 61 74 2e 67 65 74 5f me(msg.chat.get_
3220: 69 64 28 29 29 2c 0a 09 09 09 09 09 09 53 6f 6d id()),.......Som
3230: 65 28 50 61 72 73 65 4d 6f 64 65 3a 3a 4d 61 72 e(ParseMode::Mar
3240: 6b 64 6f 77 6e 56 32 29 0a 09 09 09 09 09 29 2e kdownV2)......).
3250: 61 77 61 69 74 7b 0a 09 09 09 09 09 09 64 62 67 await{.......dbg
3260: 21 28 65 72 72 32 29 3b 0a 09 09 09 09 09 7d 3b !(err2);......};
3270: 0a 09 09 09 09 7d 0a 09 09 09 7d 3b 0a 09 09 7d .....}....};...}
3280: 3b 0a 09 7d 0a 7d 0a ;..}.}.