37 lines
1.2 KiB
Rust
37 lines
1.2 KiB
Rust
use async_std::{channel::Receiver, task};
|
|
use log::{debug, info, warn};
|
|
|
|
use crate::{db::Database, sbot};
|
|
|
|
pub enum Task {
|
|
Cancel,
|
|
FetchAll(String),
|
|
}
|
|
|
|
pub async fn spawn(rx: Receiver<Task>, db: Database) {
|
|
task::spawn(async move {
|
|
while let Ok(task) = rx.recv().await {
|
|
match task {
|
|
// Fetch all messages authored by the given peer, filter
|
|
// the root posts and insert them into the peer tree of the
|
|
// database.
|
|
Task::FetchAll(peer) => {
|
|
let peer_msgs = sbot::get_message_stream(&peer).await;
|
|
let root_posts = sbot::get_root_posts(peer_msgs).await;
|
|
match db.insert_post_batch(&peer, root_posts) {
|
|
Ok(_) => debug!("inserted message batch into peer tree for {}", &peer),
|
|
Err(e) => warn!(
|
|
"failed to insert message batch into peer tree for {}: {}",
|
|
&peer, e
|
|
),
|
|
}
|
|
}
|
|
Task::Cancel => {
|
|
info!("exiting task loop...");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|