Diff
Logged in as anonymous

Differences From Artifact [1b5d4c54cf]:

To Artifact [d0a8570642]:


1

2

3
4




5
6
7
8
9
10

11
12
13

14
15
16
17
18
19
20
21
22
23
24
25
26






27
28
29
30
31
32
33
34
35
36
37
38
39

40
41
42
43
44
45
46
47
48
49
50
51
1
2
3
4
5

6
7
8
9
10
11
12
13
14
15
16
17
18

19
20
21
22
23
24
25
26
27
28
29



30
31
32
33
34
35
36
37
38
39
40
41

42
43
44
45
46

47





48
49
50
51
52
53
54

+

+

-
+
+
+
+






+


-
+










-
-
-
+
+
+
+
+
+






-





-
+
-
-
-
-
-







use crate::{
	Arc,
	command,
	Mutex,
	sql::Db,
	tg_bot::Tg,
	tg_bot::{
		MyMessage,
		Tg,
	},
};

use std::{
	borrow::Cow,
	collections::{
		BTreeMap,
		HashMap,
		HashSet,
	},
	sync::Arc,
	time::Duration,
};

use async_compat::Compat;
use chrono::{
	DateTime,
	Local,
};
use lazy_static::lazy_static;
use regex::Regex;
use reqwest::header::LAST_MODIFIED;
use smol::{
	Timer,
	lock::Mutex,
use smol::Timer;
use stacked_errors::{
	Result,
	StackableErr,
	anyhow,
	bail,
};
use tgbot::{
	handler::UpdateHandler,
	types::{
		ChatPeerId,
		Command,
		ParseMode,
		Update,
		UpdateType,
		UserPeerId,
	},
};
use stacked_errors::{
use ttl_cache::TtlCache;
	Result,
	StackableErr,
	anyhow,
	bail,
};

lazy_static!{
	pub static ref RE_SPECIAL: Regex = Regex::new(r"([\-_*\[\]()~`>#+|{}\.!])").unwrap();
}

/// Escape characters that are special in Telegram MarkdownV2 by prefixing them with a backslash.
///
102
103
104
105
106
107
108



109
110
111
112

113
114
115
116
117
118
119
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126







+
+
+




+







		smol::block_on(async {
			let mut set = self.running.lock_arc().await;
			set.remove(&self.my_id);
		})
	}
}

type FeedList = HashMap<i32, String>;
type UserCache = TtlCache<i64, Arc<Mutex<FeedList>>>;

#[derive(Clone)]
pub struct Core {
	pub tg: Tg,
	pub db: Db,
	pub feeds: Arc<Mutex<UserCache>>,
	running: Arc<Mutex<HashSet<i32>>>,
	http_client: reqwest::Client,
}

pub struct Post {
	uri: String,
	_title: String,
140
141
142
143
144
145
146

147
148
149
150
151
152
153
154
155
156

157
158
159
160
161
162
163
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

164
165
166
167
168
169
170
171







+









-
+







			let proxy = reqwest::Proxy::all(proxy).stack()?;
			client = client.proxy(proxy);
		}

		let core = Core {
			tg: Tg::new(&settings).await.stack()?,
			db: Db::new(&settings.get_string("pg").stack()?)?,
			feeds: Arc::new(Mutex::new(TtlCache::new(10000))),
			running: Arc::new(Mutex::new(HashSet::new())),
			http_client: client.build().stack()?,
		};

		let clone = core.clone();
		smol::spawn(Compat::new(async move {
			loop {
				let delay = match &clone.autofetch().await {
					Err(err) => {
						if let Err(err) = clone.tg.send(format!("🛑 {err}"), None, None).await {
						if let Err(err) = clone.tg.send(MyMessage::html(format!("🛑 {err}"))).await {
							eprintln!("Autofetch error: {err:?}");
						};
						std::time::Duration::from_secs(60)
					},
					Ok(time) => *time,
				};
				Timer::after(delay).await;
282
283
284
285
286
287
288
289

290
291
292

293
294
295
296
297
298
299
290
291
292
293
294
295
296

297
298
299

300
301
302
303
304
305
306
307







-
+


-
+







				Some(ref x) => sedregex::ReplaceCommand::new(x).stack()?.execute(&post.uri),
				None => post.uri.clone().into(),
			};
			if ! conn.exists(&post_url, id).await.stack()? {
				if this_fetch.is_none() || *date > this_fetch.unwrap() {
					this_fetch = Some(*date);
				};
				self.tg.send( match &source.iv_hash {
				self.tg.send(MyMessage::html_to(match &source.iv_hash {
					Some(hash) => format!("<a href=\"https://t.me/iv?url={post_url}&rhash={hash}\"> </a>{post_url}"),
					None => format!("{post_url}"),
				}, Some(destination), Some(ParseMode::Html)).await.stack()?;
				}, destination)).await.stack()?;
				conn.add_post(id, date, &post_url).await.stack()?;
				posted += 1;
			};
		};
		posts.clear();
		Ok(format!("Posted: {posted}"))
	}
324
325
326
327
328
329
330
331

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
364
365

366
367
368
369
370
371
372
373

374
375
376
377
378
379



380
381

382
383
384
385
386
387
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427

428
429
430
431
432
433
434
435
436
437
438
439
440
441


442
443
444


445
446
447
448
449
450
451







-
+













+









+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+











-
+








+




-
-
+
+
+
-
-
+






								Ok(Some(source)) => source.to_string(),
								Ok(None) => "Source not found in database?".to_string(),
								Err(err) => format!("Failed to fetch source data:\n{err}"),
							}
						};
						smol::spawn(Compat::new(async move {
							if let Err(err) = clone.check(source_id, true, Some(last_scrape)).await
								&& let Err(err) = clone.tg.send(&format!("🛑 {source}\n{}", encode(&err.to_string())), None, Some(ParseMode::MarkdownV2)).await
								&& let Err(err) = clone.tg.send(MyMessage::text(format!("🛑 {source}\n{}", encode(&err.to_string())))).await
							{
								eprintln!("Check error: {err}");
							};
						})).detach();
					}
				} else if next_fetch - now < delay {
					delay = next_fetch - now;
				}
			}
		};
		delay.to_std().stack()
	}

	/// Displays full list of managed channels for specified user
	pub async fn list (&self, owner: UserPeerId) -> Result<String> {
		let mut reply: Vec<String> = vec![];
		reply.push("Channels:".into());
		let mut conn = self.db.begin().await.stack()?;
		for row in conn.get_list(owner).await.stack()? {
			reply.push(row.to_string());
		};
		Ok(reply.join("\n\n"))
	}

	/// Returns current cached list of feed for requested user, or loads data from database
	pub async fn get_feeds (&self, owner: i64) -> Result<Arc<Mutex<HashMap<i32, String>>>> {
		let mut conn = self.db.begin().await.stack()?;
		let mut feeds = self.feeds.lock_arc().await;
		Ok(match feeds.get(&owner) {
			None => {
				let feed_list = conn.get_feeds(owner).await.stack()?;
				let mut map = HashMap::new();
				for feed in feed_list {
					map.insert(feed.source_id, feed.channel);
				};
				let res = Arc::new(Mutex::new(map));
				feeds.insert(owner, res.clone(), Duration::from_secs(60 * 60 * 3));
				res
			},
			Some(res) => res.clone(),
		})
	}

	/// Adds feed to cached list
	pub async fn add_feed (&self, owner: i64, source_id: i32, channel: String) -> Result<()> {
		let mut inserted = true;
		{
			let mut feeds = self.feeds.lock_arc().await;
			if let Some(feed) = feeds.get_mut(&owner) {
				let mut feed = feed.lock_arc().await;
				feed.insert(source_id, channel);
			} else {
				inserted = false;
			}
		}
		if !inserted {
			self.get_feeds(owner).await.stack()?;
		}
		Ok(())
	}

	/// Removes feed from cached list
	pub async fn rm_feed (&self, owner: i64, source_id: &i32) -> Result<()> {
		let mut dropped = false;
		{
			let mut feeds = self.feeds.lock_arc().await;
			if let Some(feed) = feeds.get_mut(&owner) {
				let mut feed = feed.lock_arc().await;
				feed.remove(source_id);
				dropped = true;
			}
		}
		if !dropped {
			self.get_feeds(owner).await.stack()?;
		}
		Ok(())
	}
}

impl UpdateHandler for Core {
	/// Dispatches an incoming Telegram update to a matching command handler and reports handler errors to the originating chat.
	///
	/// This method inspects the update; if it contains a message that can be parsed as a bot command,
	/// it executes the corresponding command handler. If the handler returns an error, the error text
	/// is sent back to the message's chat using MarkdownV2 formatting. Unknown commands produce an error
	/// which is also reported to the chat.
	async fn handle (&self, update: Update) {
		if let UpdateType::Message(msg) = update.update_type 
			&& let Ok(cmd) = Command::try_from(msg)
			&& let Ok(cmd) = Command::try_from(*msg)
		{
			let msg = cmd.get_message();
			let words = cmd.get_args();
			let command = cmd.get_name();
			let res = match command {
				"/check" | "/clean" | "/enable" | "/delete" | "/disable" => command::command(self, command, msg, words).await,
				"/start" => command::start(self, msg).await,
				"/list" => command::list(self, msg).await,
				"/test" => command::test(self, msg).await,
				"/add" | "/update" => command::update(self, command, msg, words).await,
				any => Err(anyhow!("Unknown command: {any}")),
			};
			if let Err(err) = res 
				&& let Err(err2) = self.tg.send(format!("\\#error\n```\n{err}\n```"),
					Some(msg.chat.get_id()),
				&& let Err(err2) = self.tg.send(MyMessage::text_to(
					format!("\\#error\n```\n{err}\n```"),
					msg.chat.get_id(),
					Some(ParseMode::MarkdownV2)
				).await
				)).await
			{
				dbg!(err2);
			}
		} // TODO: debug log for skipped updates?;
	}
}