feat(task_queue): process embeds task

closes #112
This commit is contained in:
Paul
2021-11-01 22:04:11 +00:00
parent 6366e0ac24
commit 997d1fffc0
4 changed files with 61 additions and 63 deletions
+47
View File
@@ -0,0 +1,47 @@
// Queue Type: Linear
use async_channel::{ Sender, Receiver, bounded };
use mongodb::bson::{doc, to_bson};
use crate::{database::*, notifications::events::ClientboundNotification};
type Message = (String, String, String);
lazy_static! {
static ref CHANNEL: (Sender<Message>, Receiver<Message>) = bounded(100);
}
pub async fn queue(channel: String, id: String, content: String) {
CHANNEL.0.send((channel, id, content)).await.ok();
}
pub async fn run() {
let messages = get_collection("messages");
while let Ok((channel, id, content)) = CHANNEL.1.recv().await {
if let Ok(embeds) = Embed::generate(content).await {
if let Ok(bson) = to_bson(&embeds) {
if let Ok(_) = messages
.update_one(
doc! {
"_id": &id
},
doc! {
"$set": {
"embeds": bson
}
},
None,
)
.await
{
ClientboundNotification::MessageUpdate {
id,
channel: channel.clone(),
data: json!({ "embeds": embeds }),
}
.publish(channel);
}
}
}
}
}