forked from jmug/stoatchat
feat: add link embed generation
This commit is contained in:
@@ -9,9 +9,9 @@ extern crate lazy_static;
|
||||
extern crate ctrlc;
|
||||
|
||||
pub mod routes;
|
||||
pub mod tasks;
|
||||
pub mod util;
|
||||
pub mod version;
|
||||
//pub mod task_queue;
|
||||
|
||||
use log::info;
|
||||
use rauth::{
|
||||
@@ -112,6 +112,9 @@ async fn main() {
|
||||
|
||||
rauth::entities::sync_models(&mongo_db.database("revolt")).await;
|
||||
|
||||
// Launch background task workers.
|
||||
async_std::task::spawn(tasks::start_workers(db.clone()));
|
||||
|
||||
let auth = Auth::new(mongo_db.database("revolt"), config);
|
||||
let rocket = rocket::build();
|
||||
routes::mount(rocket)
|
||||
@@ -127,7 +130,6 @@ async fn main() {
|
||||
.manage(auth)
|
||||
.manage(db)
|
||||
.manage(cors.clone())
|
||||
//.manage(RatelimitState::new())
|
||||
.attach(util::ratelimiter::RatelimitFairing)
|
||||
.attach(cors)
|
||||
.launch()
|
||||
|
||||
@@ -54,8 +54,8 @@ pub async fn req(
|
||||
};
|
||||
|
||||
// 1. Handle content update
|
||||
if let Some(content) = edit.content {
|
||||
partial.content = Some(Content::Text(content));
|
||||
if let Some(content) = &edit.content {
|
||||
partial.content = Some(Content::Text(content.clone()));
|
||||
}
|
||||
|
||||
// 2. Clear any auto generated embeds
|
||||
@@ -77,8 +77,19 @@ pub async fn req(
|
||||
}
|
||||
}
|
||||
|
||||
partial.embeds = message.embeds.clone();
|
||||
partial.embeds = Some(new_embeds);
|
||||
|
||||
message.update(db, partial).await?;
|
||||
|
||||
// Queue up a task for processing embeds
|
||||
if let Some(content) = edit.content {
|
||||
crate::tasks::process_embeds::queue(
|
||||
message.channel.to_string(),
|
||||
message.id.to_string(),
|
||||
content,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
Ok(Json(message))
|
||||
}
|
||||
|
||||
@@ -166,11 +166,20 @@ pub async fn message_send(
|
||||
}
|
||||
|
||||
// 6. Set content
|
||||
message.content = Content::Text(data.content);
|
||||
message.content = Content::Text(data.content.clone());
|
||||
|
||||
// 7. Pass-through nonce value for clients
|
||||
message.nonce = Some(idempotency.into_key());
|
||||
|
||||
message.create(db).await?;
|
||||
|
||||
// Queue up a task for processing embeds
|
||||
crate::tasks::process_embeds::queue(
|
||||
channel.id().to_string(),
|
||||
message.id.to_string(),
|
||||
data.content,
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(Json(message))
|
||||
}
|
||||
|
||||
15
src/tasks/mod.rs
Normal file
15
src/tasks/mod.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
//! Semi-important background task management
|
||||
|
||||
use async_std::task;
|
||||
use revolt_quark::Database;
|
||||
|
||||
const WORKER_COUNT: usize = 10;
|
||||
|
||||
pub mod process_embeds;
|
||||
|
||||
/// Spawn background workers
|
||||
pub async fn start_workers(db: Database) {
|
||||
for _ in 0..WORKER_COUNT {
|
||||
task::spawn(process_embeds::worker(db.clone()));
|
||||
}
|
||||
}
|
||||
49
src/tasks/process_embeds.rs
Normal file
49
src/tasks/process_embeds.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
use crate::util::variables::{JANUARY_URL, MAX_EMBED_COUNT};
|
||||
|
||||
use deadqueue::limited::Queue;
|
||||
use log::error;
|
||||
use revolt_quark::{
|
||||
models::{message::AppendMessage, Message},
|
||||
types::january::Embed,
|
||||
Database,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct EmbedTask {
|
||||
channel: String,
|
||||
id: String,
|
||||
content: String,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref Q: Queue<EmbedTask> = Queue::new(10_000);
|
||||
}
|
||||
|
||||
pub async fn queue(channel: String, id: String, content: String) {
|
||||
Q.push(EmbedTask {
|
||||
channel,
|
||||
id,
|
||||
content,
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
pub async fn worker(db: Database) {
|
||||
loop {
|
||||
let task = Q.pop().await;
|
||||
if let Ok(embeds) = Embed::generate(task.content, &*JANUARY_URL, *MAX_EMBED_COUNT).await {
|
||||
if let Err(err) = Message::append(
|
||||
&db,
|
||||
task.id,
|
||||
task.channel,
|
||||
AppendMessage {
|
||||
embeds: Some(embeds),
|
||||
},
|
||||
)
|
||||
.await
|
||||
{
|
||||
error!("Encountered an error appending to message: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user