Annotation For src/main.rs
Logged in as anonymous

Lines of src/main.rs from check-in ec616a2a43 that are changed by the sequence of edits moving toward check-in 8db3dfecf8:

                         1: use std::collections::BTreeMap;
                         2: 
                         3: use config;
                         4: 
                         5: use tokio;
                         6: use rss;
                         7: use chrono::DateTime;
                         8: 
                         9: use regex::Regex;
                        10: 
                        11: use tokio::stream::StreamExt;
                        12: use telegram_bot::*;
                        13: 
                        14: use sqlx::postgres::PgPoolOptions;
                        15: use sqlx::Row;
                        16: 
                        17: type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
                        18: 
                        19: #[derive(Clone)]
                        20: struct Core {
                        21: 	owner: i64,
                        22: 	api_key: String,
                        23: 	owner_chat: UserId,
                        24: 	tg: telegram_bot::Api,
                        25: 	my: User,
                        26: 	pool: sqlx::Pool<sqlx::Postgres>,
                        27: }
                        28: 
                        29: impl Core {
                        30: 	async fn new(settings: config::Config) -> Result<Core> {
                        31: 		let owner = settings.get_int("owner")?;
                        32: 		let api_key = settings.get_str("api_key")?;
                        33: 		let tg = Api::new(&api_key);
                        34: 		let core = Core {
                        35: 			owner: owner,
                        36: 			api_key: api_key.clone(),
                        37: 			my: tg.send(telegram_bot::GetMe).await?,
                        38: 			tg: tg,
                        39: 			owner_chat: UserId::new(owner),
                        40: 			pool: PgPoolOptions::new().max_connections(5).connect(&settings.get_str("pg")?).await?,
                        41: 		};
                        42: 		let clone = core.clone();
                        43: 		tokio::spawn(async move {
                        44: 			if let Err(err) = clone.autofetch().await {
                        45: 				eprintln!("connection error: {}", err);
                        46: 			}
                        47: 		});
                        48: 		Ok(core)
                        49: 	}
                        50: 
                        51: 	fn stream(&self) -> telegram_bot::UpdatesStream {
                        52: 		self.tg.stream()
                        53: 	}
                        54: 
                        55: 	fn debug(&self, msg: &str) -> Result<()> {
                        56: 		self.tg.spawn(SendMessage::new(self.owner_chat, msg));
                        57: 		Ok(())
                        58: 	}
                        59: 
                        60: 	async fn check(&self, id: &i32, real: Option<bool>) -> Result<()> {
                        61: 		match sqlx::query("select source_id, channel_id, url, iv_hash, owner from rsstg_source where source_id = $1")
                        62: 			.bind(id)
                        63: 			.fetch_one(&self.pool).await {
                        64: 			Ok(row) => {
                        65: 				let channel_id: i64 = row.try_get("channel_id")?;
                        66: 				let destination = match real {
                        67: 					Some(true) => UserId::new(channel_id),
                        68: 					Some(false) | None => UserId::new(row.try_get("owner")?),
                        69: 				};
                        70: 				let url: &str = row.try_get("url")?;
                        71: 				let mut this_fetch: Option<DateTime<chrono::FixedOffset>> = None;
                        72: 				let iv_hash: Option<&str> = row.try_get("iv_hash")?;
                        73: 				let mut posts: BTreeMap<DateTime<chrono::FixedOffset>, String> = BTreeMap::new();
                        74: 				match rss::Channel::from_url(url) {
                        75: 					Ok(feed) => {
                        76: 						for item in feed.items() {
                        77: 							let date = match item.pub_date() {
                        78: 								Some(feed_date) => DateTime::parse_from_rfc2822(feed_date),
                        79: 								None => DateTime::parse_from_rfc3339(&item.dublin_core_ext().unwrap().dates()[0]),
                        80: 							}?;
                        81: 							let url = item.link().unwrap().to_string();
                        82: 							posts.insert(date.clone(), url.clone());
                        83: 						};
                        84: 						for (date, url) in posts.iter() {
                        85: 							match sqlx::query("select exists(select true from rsstg_post where url = $1 and source_id = $2) as exists;")
                        86: 								.bind(&url)
                        87: 								.bind(id)
                        88: 								.fetch_one(&self.pool).await {
                        89: 								Ok(row) => {
                        90: 									let exists: bool = row.try_get("exists")?;
                        91: 									if ! exists {
                        92: 										if this_fetch == None || *date > this_fetch.unwrap() {
                        93: 											this_fetch = Some(*date);
                        94: 										}
                        95: 										match self.tg.send( match iv_hash {
                        96: 												Some(x) => SendMessage::new(destination, format!("<a href=\"https://t.me/iv?url={}&rhash={}\"> </a>{0}", url, x)),
                        97: 												None => SendMessage::new(destination, format!("{}", url)),
                        98: 											}.parse_mode(types::ParseMode::Html)).await {
                        99: 											Ok(_) => {
                       100: 												match sqlx::query("insert into rsstg_post (source_id, posted, url) values ($1, $2, $3);")
                       101: 													.bind(id)
                       102: 													.bind(date)
                       103: 													.bind(url)
                       104: 													.execute(&self.pool).await {
                       105: 														Ok(_) => {},
                       106: 														Err(err) => {
                       107: 															self.debug(&err.to_string())?;
                       108: 														},
                       109: 												};
                       110: 											},
                       111: 											Err(err) => {
                       112: 												self.debug(&err.to_string())?;
                       113: 											},
                       114: 										};
                       115: 										tokio::time::delay_for(std::time::Duration::new(4, 0)).await;
                       116: 									}
                       117: 								},
                       118: 								Err(err) => {
                       119: 									self.debug(&err.to_string())?;
                       120: 								},
                       121: 							};
                       122: 						};
                       123: 						posts.clear();
                       124: 					},
                       125: 					Err(err) => {
                       126: 						self.debug(&format!("Problem opening feed url:\n{}\n{}", &url, &err.to_string()))?;
                       127: 					},
                       128: 				};
                       129: 				match sqlx::query("update rsstg_source set last_scrape = now() where source_id = $1;")
                       130: 					.bind(id)
                       131: 					.execute(&self.pool).await {
                       132: 					Ok(_) => {},
                       133: 					Err(err) => {
                       134: 						self.debug(&err.to_string())?;
                       135: 					},
                       136: 				};
                       137: 			},
                       138: 			Err(err) => {
                       139: 				self.debug(&err.to_string())?;
                       140: 			},
                       141: 		};
                       142: 		Ok(())
                       143: 	}
                       144: 
                       145: 	async fn clean(&self, source_id: i32) -> Result<()> {
                       146: 		match sqlx::query("delete from rsstg_post where source_id = $1;")
                       147: 			.bind(source_id)
                       148: 			.execute(&self.pool).await {
                       149: 			Ok(_) => {},
                       150: 			Err(err) => {
                       151: 				self.debug(&err.to_string())?;
                       152: 			},
                       153: 		};
                       154: 		Ok(())
                       155: 	}
                       156: 
                       157: 	async fn enable(&self, source_id: &i32) -> Result<()> {
                       158: 		match sqlx::query("update rsstg_source set enabled = true where source_id = $1")
                       159: 			.bind(source_id)
                       160: 			.execute(&self.pool).await {
                       161: 			Ok(_) => {},
                       162: 			Err(err) => {
                       163: 				self.debug(&err.to_string())?;
                       164: 			},
                       165: 		}
                       166: 		Ok(())
                       167: 	}
                       168: 
                       169: 	async fn disable(&self, source_id: &i32) -> Result<()> {
                       170: 		match sqlx::query("update rsstg_source set enabled = false where source_id = $1")
                       171: 			.bind(source_id)
                       172: 			.execute(&self.pool).await {
                       173: 			Ok(_) => {},
                       174: 			Err(err) => {
                       175: 				self.debug(&err.to_string())?;
                       176: 			},
                       177: 		}
                       178: 		Ok(())
                       179: 	}
                       180: 
                       181: 	async fn autofetch(&self) -> Result<()> {
                       182: 		let mut delay = chrono::Duration::minutes(5);
                       183: 		let mut next_fetch: DateTime<chrono::Local>;
                       184: 		let mut now;
                       185: 		loop {
                       186: 			now = chrono::Local::now();
                       187: 			let mut rows = sqlx::query("select source_id, next_fetch from rsstg_order natural left join rsstg_source natural left join rsstg_channel where next_fetch < now();")
                       188: 				.fetch(&self.pool);
                       189: 			while let Some(row) = rows.try_next().await.unwrap() {
                       190: 				let source_id: i32 = row.try_get("source_id")?;
                       191: 				next_fetch = row.try_get("next_fetch")?;
                       192: 				if next_fetch < now {
                       193: 					match sqlx::query("update rsstg_source set last_scrape = now() + interval '1 hour' where source_id = $1;")
                       194: 						.bind(source_id)
                       195: 						.execute(&self.pool).await {
                       196: 						Ok(_) => {},
                       197: 						Err(err) => {
                       198: 							self.debug(&err.to_string())?;
                       199: 						},
                       200: 					};
                       201: 					let clone = self.clone();
                       202: 					tokio::spawn(async move {
                       203: 						if let Err(err) = clone.check(&source_id.clone(), Some(true)).await {
                       204: 							eprintln!("connection error: {}", err);
                       205: 						}
                       206: 					});
                       207: 				} else {
                       208: 					if next_fetch - now < delay {
                       209: 						delay = next_fetch - now;
                       210: 					}
                       211: 				}
                       212: 			};
                       213: 			tokio::time::delay_for(delay.to_std()?).await;
                       214: 			delay = chrono::Duration::minutes(5);
                       215: 		}
                       216: 		//Ok(())
                       217: 	}
                       218: 
                       219: }
                       220: 
                       221: #[tokio::main]
                       222: async fn main() -> Result<()> {
                       223: 	let mut settings = config::Config::default();
                       224: 	settings.merge(config::File::with_name("rsstg"))?;
                       225: 
                       226: 	let re_username = Regex::new(r"^@[a-zA-Z][a-zA-Z0-9_]+$")?;
                       227: 	let re_link = Regex::new(r"^https?://[a-zA-Z.0-9-]+/[-_a-zA-Z.0-9/?=]+$")?;
                       228: 	let re_iv_hash = Regex::new(r"^[a-f0-9]{14}$")?;
                       229: 
                       230: 	let core = Core::new(settings).await?;
                       231: 
                       232: 	let mut stream = core.stream();
                       233: 
                       234: 	while let Some(update) = stream.next().await {
                       235: 		match update {
                       236: 			Ok(update) => {
                       237: 				match update.kind {
                       238: 					UpdateKind::Message(message) => {
                       239: 						let mut reply: Vec<String> = vec![];
                       240: 						match message.kind {
                       241: 							MessageKind::Text { ref data, .. } => {
                       242: 								let mut words = data.split_whitespace();
                       243: 								let cmd = words.next().unwrap();
                       244: 								match cmd {
                       245: 
                       246: 		// start
                       247: 
                       248: 									"/start" => {
ec616a2a43 2020-11-19  249: 										reply.push("Not in service yet\\. Try later\\.".to_string());
                       250: 									},
                       251: 
                       252: 		// list
                       253: 
                       254: 									"/list" => {
                       255: 										reply.push("Channels:".to_string());
                       256: 										let mut rows = sqlx::query("select source_id, username, enabled, url, iv_hash from rsstg_source left join rsstg_channel using (channel_id) where owner = $1 order by source_id")
                       257: 											.bind(i64::from(message.from.id))
                       258: 											.fetch(&core.pool);
                       259: 										while let Some(row) = rows.try_next().await? {
                       260: 											let source_id: i32 = row.try_get("source_id")?;
                       261: 											let username: &str = row.try_get("username")?;
                       262: 											let enabled: bool = row.try_get("enabled")?;
                       263: 											let url: &str = row.try_get("url")?;
                       264: 											let iv_hash: Option<&str> = row.try_get("iv_hash")?;
                       265: 											reply.push(format!("\n\\#️⃣ {} \\*️⃣ `{}` {}\n🔗 `{}`", source_id, username,  
                       266: 												match enabled {
                       267: 													true  => "🔄 enabled",
                       268: 													false => "⛔ disabled",
                       269: 												}, url));
                       270: 											if let Some(hash) = iv_hash {
                       271: 												reply.push(format!("IV `{}`", hash));
                       272: 											}
                       273: 										}
                       274: 									},
                       275: 
                       276: 		// add
                       277: 
                       278: 									"/add" | "/update" => {
                       279: 										let mut source_id: i32 = 0;
                       280: 										if cmd == "/update" {
                       281: 											source_id = words.next().unwrap().parse::<i32>()?;
                       282: 										}
                       283: 										let (channel, url, iv_hash) = (words.next().unwrap(), words.next().unwrap(), words.next());
                       284: 										let ok_link = re_link.is_match(&url);
                       285: 										let ok_hash = match iv_hash {
                       286: 											Some(hash) => re_iv_hash.is_match(&hash),
                       287: 											None => true,
                       288: 										};
                       289: 										if ! ok_link {
                       290: 											reply.push("Link should be link to atom/rss feed, something like \"https://domain/path\"\\.".to_string());
                       291: 											core.debug(&format!("Url: {:?}", &url))?;
                       292: 										}
                       293: 										if ! ok_hash {
                       294: 											reply.push("IV hash should be 14 hex digits.".to_string());
                       295: 											core.debug(&format!("IV: {:?}", &iv_hash))?;
                       296: 										}
                       297: 										if ok_link && ok_hash {
                       298: 											let chan: Option<i64> = match sqlx::query("select channel_id from rsstg_channel where username = $1")
                       299: 												.bind(channel)
                       300: 												.fetch_one(&core.pool).await {
                       301: 													Ok(chan) => Some(chan.try_get("channel_id")?),
                       302: 													Err(sqlx::Error::RowNotFound) => {
                       303: 														reply.push("Sorry, I don't know about that channel. Please, add a channel with /addchan.".to_string());
                       304: 														None
                       305: 													},
                       306: 													Err(err) => {
                       307: 														reply.push("Sorry, unknown error\\.".to_string());
                       308: 														core.debug(&format!("Sorry, unknown error: {:#?}\n", err))?;
                       309: 														None
                       310: 													},
                       311: 											};
                       312: 											if let Some(chan) = chan {
                       313: 												match if cmd == "/update" {
                       314: 														sqlx::query("update rsstg_source set channel_id = $2, url = $3, iv_hash = $4, owner = $4 where source_id = $1").bind(source_id)
                       315: 													} else {
                       316: 														sqlx::query("insert into rsstg_source (channel_id, url, iv_hash, owner) values ($1, $2, $3, $4)")
                       317: 													}
                       318: 													.bind(chan)
                       319: 													.bind(url)
                       320: 													.bind(iv_hash)
                       321: 													.bind(i64::from(message.from.id))
                       322: 													.execute(&core.pool).await {
                       323: 													Ok(_) => reply.push("Channel added\\.".to_string()),
                       324: 													Err(sqlx::Error::Database(err)) => {
                       325: 														match err.downcast::<sqlx::postgres::PgDatabaseError>().routine() {
                       326: 															Some("_bt_check_unique", ) => {
                       327: 																reply.push("Duplicate key\\.".to_string());
                       328: 															},
                       329: 															Some(_) => {
                       330: 																reply.push("Database error\\.".to_string());
                       331: 															},
                       332: 															None => {
                       333: 																reply.push("No database error extracted\\.".to_string());
                       334: 															},
                       335: 														};
                       336: 													},
                       337: 													Err(err) => {
                       338: 														reply.push("Sorry, unknown error\\.".to_string());
                       339: 														core.debug(&format!("Sorry, unknown error: {:#?}\n", err))?;
                       340: 													},
                       341: 												};
                       342: 											};
                       343: 										};
                       344: 									},
                       345: 
                       346: 		// addchan
                       347: 
                       348: 									"/addchan" => {
                       349: 										let channel = words.next().unwrap();
                       350: 										if ! re_username.is_match(&channel) {
                       351: 											reply.push("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?".to_string());
                       352: 										} else {
                       353: 											let chan: Option<i64> = match sqlx::query("select channel_id from rsstg_channel where username = $1")
                       354: 												.bind(channel)
                       355: 												.fetch_one(&core.pool).await {
                       356: 													Ok(chan) => Some(chan.try_get("channel_id")?),
                       357: 													Err(sqlx::Error::RowNotFound) => None,
                       358: 													Err(err) => {
                       359: 														reply.push("Sorry, unknown error\\.".to_string());
                       360: 														core.debug(&format!("Sorry, unknown error: {:#?}\n", err))?;
                       361: 														None
                       362: 													},
                       363: 											};
                       364: 											match chan {
                       365: 												Some(chan) => {
                       366: 													let new_chan = core.tg.send(telegram_bot::GetChat::new(telegram_bot::types::ChatId::new(chan))).await?;
                       367: 													if i64::from(new_chan.id()) == chan {
                       368: 														reply.push("I already know that channel\\.".to_string());
                       369: 													} else {
                       370: 														reply.push("Hmm, channel has changed… I'll fix it later\\.".to_string());
                       371: 													};
                       372: 												},
                       373: 												None => {
                       374: 													match core.tg.send(telegram_bot::GetChatAdministrators::new(telegram_bot::types::ChatRef::ChannelUsername(channel.to_string()))).await {
                       375: 														Ok(chan_adm) => {
                       376: 															let (mut me, mut user) = (false, false);
                       377: 															for admin in &chan_adm {
                       378: 																if admin.user.id == core.my.id {
                       379: 																	me = true;
                       380: 																};
                       381: 																if admin.user.id == message.from.id {
                       382: 																	user = true;
                       383: 																};
                       384: 															};
                       385: 															if ! me   { reply.push("I need to be admin on that channel\\.".to_string()); };
                       386: 															if ! user { reply.push("You should be admin on that channel\\.".to_string()); };
                       387: 															if me && user {
                       388: 																let chan_id = core.tg.send(telegram_bot::GetChat::new(telegram_bot::types::ChatRef::ChannelUsername(channel.to_string()))).await?;
                       389: 																sqlx::query("insert into rsstg_channel (channel_id, username) values ($1, $2);")
                       390: 																	.bind(i64::from(chan_id.id()))
                       391: 																	.bind(channel)
                       392: 																	.execute(&core.pool).await?;
                       393: 																reply.push("Good, I know that channel now\\.\n".to_string());
                       394: 															};
                       395: 														},
                       396: 														Err(_) => {
                       397: 															reply.push("Sorry, I have no access to that chat\\.".to_string());
                       398: 														},
                       399: 													};
                       400: 												},
                       401: 											};
                       402: 										};
                       403: 									},
                       404: 
                       405: 		// check
                       406: 
                       407: 									"/check" => {
                       408: 										&core.check(&words.next().unwrap().parse::<i32>()?, None).await?;
                       409: 									},
                       410: 
ec616a2a43 2020-11-19  411: 		// clear
                       412: 
                       413: 									"/clean" => {
                       414: 										if core.owner != i64::from(message.from.id) {
                       415: 											reply.push("Reserved for testing\\.".to_string());
                       416: 										} else {
                       417: 											let source_id = words.next().unwrap().parse::<i32>().unwrap_or(0);
                       418: 											&core.clean(source_id).await?;
                       419: 										}
                       420: 									},
                       421: 
                       422: 		// enable
                       423: 
                       424: 									"/enable" => {
                       425: 										match core.enable(&words.next().unwrap().parse::<i32>()?).await {
                       426: 											Ok(_) => {
                       427: 												reply.push("Channel enabled\\.".to_string());
                       428: 											}
                       429: 											Err(err) => {
                       430: 												core.debug(&err.to_string())?;
                       431: 											},
                       432: 										};
                       433: 									},
                       434: 
                       435: 		// disable
                       436: 
                       437: 									"/disable" => {
                       438: 										match core.disable(&words.next().unwrap().parse::<i32>()?).await {
                       439: 											Ok(_) => {
                       440: 												reply.push("Channel disabled\\.".to_string());
                       441: 											}
                       442: 											Err(err) => {
                       443: 												core.debug(&err.to_string())?;
                       444: 											},
                       445: 										};
                       446: 									},
                       447: 
                       448: 									_ => {
                       449: 									},
                       450: 								};
                       451: 							},
                       452: 							_ => {
                       453: 							},
                       454: 						};
                       455: 						if reply.len() > 0 {
                       456: 							match core.tg.send(message.text_reply(reply.join("\n")).parse_mode(types::ParseMode::MarkdownV2)).await {
                       457: 								Ok(_) => {},
                       458: 								Err(err) => {
                       459: 									dbg!(reply.join("\n"));
                       460: 									println!("{}", err);
                       461: 								},
                       462: 							}
                       463: 						}
                       464: 					},
                       465: 					_ => {},
                       466: 				};
                       467: 			},
                       468: 
                       469: 			Err(err) => {
                       470: 				core.debug(&err.to_string())?;
                       471: 			},
                       472: 		};
                       473: 	}
                       474: 
                       475: 	Ok(())
                       476: }