| ︙ | | | ︙ | |
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
},
};
use std::{
borrow::Cow,
collections::{
BTreeMap,
HashMap,
HashSet,
},
time::Duration,
};
use async_compat::Compat;
use chrono::{
|
<
|
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
},
};
use std::{
borrow::Cow,
collections::{
BTreeMap,
HashSet,
},
time::Duration,
};
use async_compat::Compat;
use chrono::{
|
| ︙ | | | ︙ | |
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
smol::block_on(async {
let mut set = self.running.lock_arc().await;
set.remove(&self.my_id);
})
}
}
pub 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>>,
|
|
|
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
smol::block_on(async {
let mut set = self.running.lock_arc().await;
set.remove(&self.my_id);
})
}
}
pub type FeedList = BTreeMap<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>>,
|
| ︙ | | | ︙ | |
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/// 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<FeedList>>> {
let mut feeds = self.feeds.lock_arc().await;
Ok(match feeds.get(&owner) {
None => {
let mut conn = self.db.begin().await.stack()?;
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
},
|
|
|
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/// 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<FeedList>>> {
let mut feeds = self.feeds.lock_arc().await;
Ok(match feeds.get(&owner) {
None => {
let mut conn = self.db.begin().await.stack()?;
let feed_list = conn.get_feeds(owner).await.stack()?;
let mut map = BTreeMap::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
},
|
| ︙ | | | ︙ | |
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
} else {
// not a command
}
},
UpdateType::CallbackQuery(query) => {
if let Some(ref cb) = query.data
&& let Err(err) = self.cb(&query, cb).await
{
if let Err(err) = self.tg.answer_cb(query.id, err.to_string()).await {
println!("{err:?}");
}
}
},
_ => {
println!("Unhandled UpdateKind:\n{update:?}")
},
}
}
}
|
>
<
|
<
|
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
} else {
// not a command
}
},
UpdateType::CallbackQuery(query) => {
if let Some(ref cb) = query.data
&& let Err(err) = self.cb(&query, cb).await
&& let Err(err) = self.tg.answer_cb(query.id, err.to_string()).await
{
println!("{err:?}");
}
},
_ => {
println!("Unhandled UpdateKind:\n{update:?}")
},
}
}
}
|