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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
use anyhow::{anyhow, bail, Context, Result};
use async_std::task;
use chrono::DateTime;
use sqlx::postgres::PgPoolOptions;
use teloxide::{
Bot,
payloads::SendMessage,
requests::Requester,
types::{
Me,
UserId,
},
};
use thiserror::Error;
use std::{
borrow::Cow,
collections::{
BTreeMap,
HashSet,
},
num::TryFromIntError,
sync::{
Arc,
Mutex
},
};
#[derive(Error, Debug)]
pub enum RssError {
// #[error(transparent)]
// Tg(#[from] TgError),
#[error(transparent)]
Int(#[from] TryFromIntError),
}
#[derive(Clone)]
pub struct Core {
owner_chat: UserId,
pub tg: Bot,
pub my: Me,
pool: sqlx::Pool<sqlx::Postgres>,
sources: Arc<Mutex<HashSet<Arc<i32>>>>,
http_client: reqwest::Client,
}
impl Core {
pub fn new(settings: config::Config) -> Result<Arc<Core>> {
let owner: u64 = settings.get_int("owner")?.try_into()?;
let api_key = settings.get_string("api_key")?;
let tg = Bot::new(api_key);
let tg_cloned = tg.clone();
let mut client = reqwest::Client::builder();
if let Ok(proxy) = settings.get_string("proxy") {
let proxy = reqwest::Proxy::all(proxy)?;
client = client.proxy(proxy);
}
let http_client = client.build()?;
let core = Arc::new(Core {
tg,
my: task::block_on(async {
tg_cloned.get_me().await
})?,
owner_chat: UserId(owner),
pool: PgPoolOptions::new()
.max_connections(5)
.acquire_timeout(std::time::Duration::new(300, 0))
.idle_timeout(std::time::Duration::new(60, 0))
.connect_lazy(&settings.get_string("pg")?)?,
sources: Arc::new(Mutex::new(HashSet::new())),
http_client,
});
/* let clone = core.clone();
task::spawn(async move {
loop {
let delay = match &clone.autofetch().await {
Err(err) => {
if let Err(err) = clone.send(format!("š {:?}", err), None, None).await {
eprintln!("Autofetch error: {}", err);
};
std::time::Duration::from_secs(60)
},
Ok(time) => *time,
};
task::sleep(delay).await;
}
}); */
Ok(core)
}
pub fn stream(&self) -> Result<()> {
let mut last_update: Option<i32> = None;
loop {
let updates = self.tg.get_updates(last_update, None, 300, Some(vec!["message"]));
}
Ok(())
}
/*
pub async fn send<'a, S>(&self, msg: S, target: Option<telegram_bot::UserId>, mode: Option<telegram_bot::types::ParseMode>) -> Result<()>
where S: Into<Cow<'a, str>> {
let mode = mode.unwrap_or(telegram_bot::types::ParseMode::Html);
let target = target.unwrap_or(self.owner_chat);
self.request(telegram_bot::SendMessage::new(target, msg).parse_mode(mode)).await?;
Ok(())
} */
/* pub async fn request<Req: telegram_bot::Request> (&self, req: Req) -> Result<<Req::Response as telegram_bot::ResponseType>::Type, RssError> {
loop {
let res = self.tg.send(&req).await;
match res {
Ok(_) => return Ok(res?),
Err(err) => {
match &err {
TgError::Raw(TgrError::TelegramError { description: _, parameters: Some(params) }) => {
if let Some(delay) = params.retry_after {
println!("Throttled, waiting {} senconds.", delay);
task::sleep(std::time::Duration::from_secs(delay.try_into()?)).await;
} else {
return Err(err.into());
}
},
_ => return Err(err.into()),
}
},
};
}
} */
/* pub async fn check<S>(&self, id: &i32, owner: S, real: bool) -> Result<Cow<'_, str>>
where S: Into<i64> {
let owner = owner.into();
let mut posted: i32 = 0;
let mut conn = self.pool.acquire().await?;
let id = {
let mut set = self.sources.lock().unwrap();
match set.get(id) {
Some(id) => id.clone(),
None => {
let id = Arc::new(*id);
set.insert(id.clone());
id.clone()
},
}
};
let count = Arc::strong_count(&id);
if count == 2 {
let source = sqlx::query!("select source_id, channel_id, url, iv_hash, owner, url_re from rsstg_source where source_id = $1 and owner = $2",
*id, owner).fetch_one(&mut *conn).await?;
let destination = match real {
true => telegram_bot::UserId::new(source.channel_id),
false => telegram_bot::UserId::new(source.owner),
};
let mut this_fetch: Option<DateTime<chrono::FixedOffset>> = None;
let mut posts: BTreeMap<DateTime<chrono::FixedOffset>, String> = BTreeMap::new();
let response = self.http_client.get(&source.url).send().await?;
let status = response.status();
let content = response.bytes().await?;
|
<
<
|
<
<
<
<
<
<
<
<
<
<
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
<
>
>
<
|
<
|
|
|
|
|
|
|
>
>
>
>
>
>
|
>
>
>
>
|
<
>
>
>
>
|
|
<
<
<
<
|
<
<
>
>
>
>
>
|
>
>
>
>
>
|
<
<
|
<
<
|
>
|
<
|
<
|
|
<
|
|
<
|
|
|
|
<
|
>
|
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
use crate::command;
use std::{
borrow::Cow,
collections::{
BTreeMap,
HashSet,
},
num::TryFromIntError,
sync::{
Arc,
Mutex
},
};
use anyhow::{
anyhow,
bail,
Result,
};
use async_std::task;
use chrono::DateTime;
use frankenstein::{
client_reqwest::Bot,
methods::{
GetUpdatesParams,
SendMessageParams
},
types::{
AllowedUpdate,
MessageEntityType,
User,
},
updates::UpdateContent,
AsyncTelegramApi,
ParseMode,
};
use sqlx::postgres::PgPoolOptions;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum RssError {
// #[error(transparent)]
// Tg(#[from] TgError),
#[error(transparent)]
Int(#[from] TryFromIntError),
}
#[derive(Clone)]
pub struct Core {
owner_chat: i64,
pub tg: Bot,
pub me: User,
pool: sqlx::Pool<sqlx::Postgres>,
sources: Arc<Mutex<HashSet<Arc<i32>>>>,
http_client: reqwest::Client,
}
impl Core {
pub async fn new(settings: config::Config) -> Result<Arc<Core>> {
let owner_chat = settings.get_int("owner")?;
let api_key = settings.get_string("api_key")?;
let tg = Bot::new(&api_key);
let mut client = reqwest::Client::builder();
if let Ok(proxy) = settings.get_string("proxy") {
let proxy = reqwest::Proxy::all(proxy)?;
client = client.proxy(proxy);
}
let http_client = client.build()?;
let me = tg.get_me().await?;
let me = me.result;
let core = Arc::new(Core {
tg,
me,
owner_chat,
pool: PgPoolOptions::new()
.max_connections(5)
.acquire_timeout(std::time::Duration::new(300, 0))
.idle_timeout(std::time::Duration::new(60, 0))
.connect_lazy(&settings.get_string("pg")?)?,
sources: Arc::new(Mutex::new(HashSet::new())),
http_client,
});
let clone = core.clone();
task::spawn(async move {
loop {
let delay = match &clone.autofetch().await {
Err(err) => {
if let Err(err) = clone.send(format!("š {err:?}"), None, None).await {
eprintln!("Autofetch error: {err:?}");
};
std::time::Duration::from_secs(60)
},
Ok(time) => *time,
};
task::sleep(delay).await;
}
});
Ok(core)
}
pub async fn stream(&self) -> Result<()> {
let mut offset: i64 = 0;
let mut params = GetUpdatesParams {
offset: None,
limit: Some(100),
timeout: Some(300),
allowed_updates: Some(vec![AllowedUpdate::Message]),
};
loop {
let updates = self.tg.get_updates(¶ms).await?.result;
if updates.is_empty() {
offset = 0;
params.offset = None;
continue;
}
for update in updates {
if i64::from(update.update_id) >= offset {
offset = i64::from(update.update_id) + 1;
params.offset = Some(offset);
}
if let UpdateContent::Message(msg) = update.content {
if let Some(text) = msg.text {
if let Some(entities) = msg.entities {
let chars: Vec<u16> = text.encode_utf16().collect();
for entity in entities {
if entity.type_field == MessageEntityType::BotCommand && entity.offset != 0 {
bail!("commands should be at message start");
};
let cmd = String::from_utf16_lossy(&chars[entity.offset as usize..entity.length as usize]);
let words: Vec<&str> = text.split_whitespace().collect();
match cmd.as_ref() {
"/check" | "/clean" | "/enable" | "/delete" | "/disable" => { command::command(self, msg.chat.id, words).await? },
"/start" => { command::start(self, msg.chat.id).await?; },
"/list" => { command::list(self, msg.chat.id).await?; },
"/add" | "/update" => { command::update(self, msg.chat.id, words).await?; },
any => {
self.send(format!("\\#error\n```\nUnknown command: {any}\n```"),
Some(msg.chat.id),
Some(ParseMode::MarkdownV2)
).await?;
},
};
};
};
};
};
}
}
}
pub async fn send <S>(&self, msg: S, target: Option<i64>, mode: Option<ParseMode>) -> Result<()>
where S: Into<String> {
let msg = msg.into();
let mode = mode.unwrap_or(ParseMode::Html);
let target = target.unwrap_or(self.owner_chat);
let send = SendMessageParams::builder()
.chat_id(target)
.text(msg)
.parse_mode(mode)
.build();
self.tg.send_message(&send).await?;
Ok(())
}
pub async fn check (&self, id: &i32, owner: i64, real: bool) -> Result<Cow<'_, str>> {
let mut posted: i32 = 0;
let mut conn = self.pool.acquire().await?;
let id = {
let mut set = self.sources.lock().unwrap();
match set.get(id) {
Some(id) => id.clone(),
None => {
let id = Arc::new(*id);
set.insert(id.clone());
id.clone()
},
}
};
let count = Arc::strong_count(&id);
if count == 2 {
let source = sqlx::query!("select source_id, channel_id, url, iv_hash, owner, url_re from rsstg_source where source_id = $1 and owner = $2",
*id, owner).fetch_one(&mut *conn).await?;
let destination = match real {
true => source.channel_id,
false => source.owner,
};
let mut this_fetch: Option<DateTime<chrono::FixedOffset>> = None;
let mut posts: BTreeMap<DateTime<chrono::FixedOffset>, String> = BTreeMap::new();
let response = self.http_client.get(&source.url).send().await?;
let status = response.status();
let content = response.bytes().await?;
|
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
for item in feed.entries() {
let date = item.published().unwrap();
let url = item.links()[0].href();
posts.insert(*date, url.to_string());
};
},
Err(err) => {
bail!("Unsupported or mangled content:\n{:?}\n{:#?}\n{:#?}\n", &source.url, err, status)
},
}
},
rss::Error::Eof => (),
_ => bail!("Unsupported or mangled content:\n{:?}\n{:#?}\n{:#?}\n", &source.url, err, status)
}
};
for (date, url) in posts.iter() {
let post_url: Cow<str> = match source.url_re {
Some(ref x) => sedregex::ReplaceCommand::new(x)?.execute(url),
None => url.into(),
};
if let Some(exists) = sqlx::query!("select exists(select true from rsstg_post where url = $1 and source_id = $2) as exists;",
&post_url, *id).fetch_one(&mut *conn).await?.exists {
if ! exists {
if this_fetch.is_none() || *date > this_fetch.unwrap() {
this_fetch = Some(*date);
};
self.request( match &source.iv_hash {
Some(hash) => telegram_bot::SendMessage::new(destination, format!("<a href=\"https://t.me/iv?url={}&rhash={}\"> </a>{0}", &post_url, hash)),
None => telegram_bot::SendMessage::new(destination, format!("{}", post_url)),
}.parse_mode(telegram_bot::types::ParseMode::Html)).await
.context("Can't post message:")?;
sqlx::query!("insert into rsstg_post (source_id, posted, url) values ($1, $2, $3);",
*id, date, &post_url).execute(&mut *conn).await?;
};
};
posted += 1;
};
posts.clear();
};
sqlx::query!("update rsstg_source set last_scrape = now() where source_id = $1;",
*id).execute(&mut *conn).await?;
Ok(format!("Posted: {}", &posted).into())
} */
/* pub async fn delete<S>(&self, source_id: &i32, owner: S) -> Result<Cow<'_, str>>
where S: Into<i64> {
let owner = owner.into();
match sqlx::query!("delete from rsstg_source where source_id = $1 and owner = $2;",
source_id, owner).execute(&mut *self.pool.acquire().await?).await?.rows_affected() {
0 => { Ok("No data found found.".into()) },
x => { Ok(format!("{} sources removed.", x).into()) },
}
} */
/* pub async fn clean<S>(&self, source_id: &i32, owner: S) -> Result<Cow<'_, str>>
where S: Into<i64> {
let owner = owner.into();
match sqlx::query!("delete from rsstg_post p using rsstg_source s where p.source_id = $1 and owner = $2 and p.source_id = s.source_id;",
source_id, owner).execute(&mut *self.pool.acquire().await?).await?.rows_affected() {
0 => { Ok("No data found found.".into()) },
x => { Ok(format!("{} posts purged.", x).into()) },
}
} */
/* pub async fn enable<S>(&self, source_id: &i32, owner: S) -> Result<&str>
where S: Into<i64> {
let owner = owner.into();
match sqlx::query!("update rsstg_source set enabled = true where source_id = $1 and owner = $2",
source_id, owner).execute(&mut *self.pool.acquire().await?).await?.rows_affected() {
1 => { Ok("Source enabled.") },
0 => { Ok("Source not found.") },
_ => { Err(anyhow!("Database error.")) },
}
} */
/* pub async fn disable<S>(&self, source_id: &i32, owner: S) -> Result<&str>
where S: Into<i64> {
let owner = owner.into();
match sqlx::query!("update rsstg_source set enabled = false where source_id = $1 and owner = $2",
source_id, owner).execute(&mut *self.pool.acquire().await?).await?.rows_affected() {
1 => { Ok("Source disabled.") },
0 => { Ok("Source not found.") },
_ => { Err(anyhow!("Database error.")) },
}
} */
/* pub async fn update<S>(&self, update: Option<i32>, channel: &str, channel_id: i64, url: &str, iv_hash: Option<&str>, url_re: Option<&str>, owner: S) -> Result<&str>
where S: Into<i64> {
let owner = owner.into();
let mut conn = self.pool.acquire().await?;
match match update {
Some(id) => {
sqlx::query!("update rsstg_source set channel_id = $2, url = $3, iv_hash = $4, owner = $5, channel = $6, url_re = $7 where source_id = $1",
id, channel_id, url, iv_hash, owner, channel, url_re).execute(&mut *conn).await
},
|
|
|
|
|
|
|
<
|
<
|
<
<
<
>
<
|
<
<
<
>
|
<
|
<
<
<
>
<
|
<
<
<
>
<
|
>
|
<
<
|
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
for item in feed.entries() {
let date = item.published().unwrap();
let url = item.links()[0].href();
posts.insert(*date, url.to_string());
};
},
Err(err) => {
bail!("Unsupported or mangled content:\n{:?}\n{err:#?}\n{status:#?}\n", &source.url)
},
}
},
rss::Error::Eof => (),
_ => bail!("Unsupported or mangled content:\n{:?}\n{err:#?}\n{status:#?}\n", &source.url)
}
};
for (date, url) in posts.iter() {
let post_url: Cow<str> = match source.url_re {
Some(ref x) => sedregex::ReplaceCommand::new(x)?.execute(url),
None => url.into(),
};
if let Some(exists) = sqlx::query!("select exists(select true from rsstg_post where url = $1 and source_id = $2) as exists;",
&post_url, *id).fetch_one(&mut *conn).await?.exists {
if ! exists {
if this_fetch.is_none() || *date > this_fetch.unwrap() {
this_fetch = Some(*date);
};
self.send( 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?;
sqlx::query!("insert into rsstg_post (source_id, posted, url) values ($1, $2, $3);",
*id, date, &post_url).execute(&mut *conn).await?;
};
};
posted += 1;
};
posts.clear();
};
sqlx::query!("update rsstg_source set last_scrape = now() where source_id = $1;",
*id).execute(&mut *conn).await?;
Ok(format!("Posted: {posted}").into())
}
pub async fn delete (&self, source_id: &i32, owner: i64) -> Result<Cow<'_, str>> {
match sqlx::query!("delete from rsstg_source where source_id = $1 and owner = $2;",
source_id, owner).execute(&mut *self.pool.acquire().await?).await?.rows_affected() {
0 => { Ok("No data found found.".into()) },
x => { Ok(format!("{} sources removed.", x).into()) },
}
}
pub async fn clean (&self, source_id: &i32, owner: i64) -> Result<Cow<'_, str>> {
match sqlx::query!("delete from rsstg_post p using rsstg_source s where p.source_id = $1 and owner = $2 and p.source_id = s.source_id;",
source_id, owner).execute(&mut *self.pool.acquire().await?).await?.rows_affected() {
0 => { Ok("No data found found.".into()) },
x => { Ok(format!("{x} posts purged.").into()) },
}
}
pub async fn enable (&self, source_id: &i32, owner: i64) -> Result<&str> {
match sqlx::query!("update rsstg_source set enabled = true where source_id = $1 and owner = $2",
source_id, owner).execute(&mut *self.pool.acquire().await?).await?.rows_affected() {
1 => { Ok("Source enabled.") },
0 => { Ok("Source not found.") },
_ => { Err(anyhow!("Database error.")) },
}
}
pub async fn disable (&self, source_id: &i32, owner: i64) -> Result<&str> {
match sqlx::query!("update rsstg_source set enabled = false where source_id = $1 and owner = $2",
source_id, owner).execute(&mut *self.pool.acquire().await?).await?.rows_affected() {
1 => { Ok("Source disabled.") },
0 => { Ok("Source not found.") },
_ => { Err(anyhow!("Database error.")) },
}
}
pub async fn update (&self, update: Option<i32>, channel: &str, channel_id: i64, url: &str, iv_hash: Option<&str>, url_re: Option<&str>, owner: i64) -> Result<&str> {
let mut conn = self.pool.acquire().await?;
match match update {
Some(id) => {
sqlx::query!("update rsstg_source set channel_id = $2, url = $3, iv_hash = $4, owner = $5, channel = $6, url_re = $7 where source_id = $1",
id, channel_id, url, iv_hash, owner, channel, url_re).execute(&mut *conn).await
},
|
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
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
|
},
None => {
Ok("No database error extracted.")
},
}
},
Err(err) => {
bail!("Sorry, unknown error:\n{:#?}\n", err);
},
}
}
async fn autofetch(&self) -> Result<std::time::Duration> {
let mut delay = chrono::Duration::minutes(1);
let now = chrono::Local::now();
let mut queue = sqlx::query!(r#"select source_id, next_fetch as "next_fetch: DateTime<chrono::Local>", owner from rsstg_order natural left join rsstg_source where next_fetch < now() + interval '1 minute';"#)
.fetch_all(&mut *self.pool.acquire().await?).await?;
for row in queue.iter() {
if let Some(next_fetch) = row.next_fetch {
if next_fetch < now {
if let (Some(owner), Some(source_id)) = (row.owner, row.source_id) {
let clone = Core {
owner_chat: telegram_bot::UserId::new(owner),
..self.clone()
};
task::spawn(async move {
if let Err(err) = clone.check(&source_id, owner, true).await {
if let Err(err) = clone.send(&format!("š {:?}", err), None, None).await {
dbg!("Check error: {}", err);
// clone.disable(&source_id, owner).await.unwrap();
};
};
});
}
} else if next_fetch - now < delay {
delay = next_fetch - now;
}
}
};
queue.clear();
Ok(delay.to_std()?)
}
pub async fn list<S>(&self, owner: S) -> Result<String>
where S: Into<i64> {
let owner = owner.into();
let mut reply: Vec<Cow<str>> = vec![];
reply.push("Channels:".into());
let rows = sqlx::query!("select source_id, channel, enabled, url, iv_hash, url_re from rsstg_source where owner = $1 order by source_id",
owner).fetch_all(&mut *self.pool.acquire().await?).await?;
for row in rows.iter() {
reply.push(format!("\n\\#ļøā£ {} \\*ļøā£ `{}` {}\nš `{}`", row.source_id, row.channel,
match row.enabled {
true => "š enabled",
false => "ā disabled",
}, row.url).into());
if let Some(hash) = &row.iv_hash {
reply.push(format!("IV: `{}`", hash).into());
}
if let Some(re) = &row.url_re {
reply.push(format!("RE: `{}`", re).into());
}
};
Ok(reply.join("\n"))
} */
}
|
|
|
|
|
|
<
<
<
|
|
<
|
>
|
315
316
317
318
319
320
321
322
323
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
|
},
None => {
Ok("No database error extracted.")
},
}
},
Err(err) => {
bail!("Sorry, unknown error:\n{err:#?}\n");
},
}
}
async fn autofetch(&self) -> Result<std::time::Duration> {
let mut delay = chrono::Duration::minutes(1);
let now = chrono::Local::now();
let mut queue = sqlx::query!(r#"select source_id, next_fetch as "next_fetch: DateTime<chrono::Local>", owner from rsstg_order natural left join rsstg_source where next_fetch < now() + interval '1 minute';"#)
.fetch_all(&mut *self.pool.acquire().await?).await?;
for row in queue.iter() {
if let Some(next_fetch) = row.next_fetch {
if next_fetch < now {
if let (Some(owner), Some(source_id)) = (row.owner, row.source_id) {
let clone = Core {
owner_chat: owner,
..self.clone()
};
task::spawn(async move {
if let Err(err) = clone.check(&source_id, owner, true).await {
if let Err(err) = clone.send(&format!("š {err:?}"), None, None).await {
eprintln!("Check error: {err:?}");
// clone.disable(&source_id, owner).await.unwrap();
};
};
});
}
} else if next_fetch - now < delay {
delay = next_fetch - now;
}
}
};
queue.clear();
Ok(delay.to_std()?)
}
pub async fn list (&self, owner: i64) -> Result<String> {
let mut reply: Vec<Cow<str>> = vec![];
reply.push("Channels:".into());
let rows = sqlx::query!("select source_id, channel, enabled, url, iv_hash, url_re from rsstg_source where owner = $1 order by source_id",
owner).fetch_all(&mut *self.pool.acquire().await?).await?;
for row in rows.iter() {
reply.push(format!("\n\\#ļøā£ {} \\*ļøā£ `{}` {}\nš `{}`", row.source_id, row.channel,
match row.enabled {
true => "š enabled",
false => "ā disabled",
}, row.url).into());
if let Some(hash) = &row.iv_hash {
reply.push(format!("IV: `{hash}`").into());
}
if let Some(re) = &row.url_re {
reply.push(format!("RE: `{re}`").into());
}
};
Ok(reply.join("\n"))
}
}
|