Diff
Logged in as anonymous

Differences From Artifact [811a9b9ac2]:

To Artifact [d02050c335]:


51
52
53
54
55
56
57
58

59
60
61
62
63
64
65
66
67
68
69
70
71





72
73
74
75
76
77
78
51
52
53
54
55
56
57

58
59
60
61
62
63
64
65
66
67
68
69


70
71
72
73
74
75
76
77
78
79
80
81







-
+











-
-
+
+
+
+
+







				.idle_timeout(std::time::Duration::new(60, 0))
				.connect_lazy(&settings.get_str("pg")?)?,
			sources: Arc::new(Mutex::new(HashSet::new())),
		};
		let clone = core.clone();
		tokio::spawn(async move {
			if let Err(err) = &clone.autofetch().await {
				if let Err(err) = clone.debug(&format!("🛑 {:?}", err)) {
				if let Err(err) = clone.debug(&format!("🛑 {:?}", err), None) {
					eprintln!("Autofetch error: {}", err);
				};
			}
		});
		Ok(core)
	}

	fn stream(&self) -> telegram_bot::UpdatesStream {
		self.tg.stream()
	}

	fn debug(&self, msg: &str) -> Result<()> {
		self.tg.spawn(SendMessage::new(self.owner_chat, msg));
	fn debug(&self, msg: &str, target: Option<UserId>) -> Result<()> {
		self.tg.spawn(SendMessage::new(match target {
			Some(user) => user,
			None => self.owner_chat,
		}, msg));
		Ok(())
	}

	async fn check<S>(&self, id: i32, owner: S, real: bool) -> Result<()>
	where S: Into<i64> {
		let owner: i64 = owner.into();
		let id = {
278
279
280
281
282
283
284
285

286
287
288
289
290
291
292
281
282
283
284
285
286
287

288
289
290
291
292
293
294
295







-
+







					//clone.owner_chat(UserId::new(owner));
					let clone = Core {
						owner_chat: UserId::new(owner),
						..self.clone()
					};
					tokio::spawn(async move {
						if let Err(err) = clone.check(source_id, owner, true).await {
							if let Err(err) = clone.debug(&format!("🛑 {:?}", err)) {
							if let Err(err) = clone.debug(&format!("🛑 {:?}", err), None) {
								eprintln!("Check error: {}", err);
							};
						};
					});
				} else {
					if next_fetch - now < delay {
						delay = next_fetch - now;
332
333
334
335
336
337
338

339
340

341
342
343
344


345
346
347
348

349
350
351
352
353
354
355
356

357
358
359
360
361
362
363
335
336
337
338
339
340
341
342
343
344
345
346
347


348
349
350
351
352

353
354
355
356
357
358
359
360

361
362
363
364
365
366
367
368







+


+


-
-
+
+



-
+







-
+







async fn main() -> Result<()> {
	let mut settings = config::Config::default();
	settings.merge(config::File::with_name("rsstg"))?;

	let core = Core::new(settings).await?;

	let mut stream = core.stream();
	let mut reply_to: Option<UserId>;

	loop {
		reply_to = None;
		match stream.next().await {
			Some(update) => {
				if let Err(err) = handle(update?, &core).await {
					core.debug(&format!("🛑 {:?}", err))?;
				if let Err(err) = handle(update?, &core, &mut reply_to).await {
					core.debug(&format!("🛑 {:?}", err), reply_to)?;
				};
			},
			None => {
				core.debug(&format!("🛑 None error."))?;
				core.debug(&format!("🛑 None error."), None)?;
			}
		};
	}

	//Ok(())
}

async fn handle(update: telegram_bot::Update, core: &Core) -> Result<()> {
async fn handle(update: telegram_bot::Update, core: &Core, mut _reply_to: &Option<UserId>) -> Result<()> {
	lazy_static! {
		static ref RE_USERNAME: Regex = Regex::new(r"^@[a-zA-Z][a-zA-Z0-9_]+$").unwrap();
		static ref RE_LINK: Regex = Regex::new(r"^https?://[a-zA-Z.0-9-]+/[-_a-zA-Z.0-9/?=]+$").unwrap();
		static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap();
	}

	match update.kind {
380
381
382
383
384
385
386

387
388
389
390
391
392
393
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399







+







						"/list" => {
							reply.append(&mut core.list(message.from.id).await?);
						},

// add

						"/add" | "/update" => {
							_reply_to = &Some(message.from.id);
							let mut source_id: Option<i32> = None;
							let at_least = "Requires at least 3 parameters.";
							if cmd == "/update" {
								let first_word = words.next()
									.context(at_least)?;
								source_id = Some(first_word.parse::<i32>()
									.with_context(|| format!("I need a number, but got {}.", first_word))?);