chore: update es code to lapin
Signed-off-by: Zomatree <me@zomatree.live>
This commit is contained in:
@@ -31,7 +31,7 @@ default = ["mongodb", "tokio-runtime", "tasks"]
|
||||
|
||||
[dependencies]
|
||||
# Core
|
||||
revolt-config = { workspace = true, features = ["report-macros"] }
|
||||
revolt-config = { workspace = true, features = ["report-macros", "anyhow"] }
|
||||
revolt-result = { workspace = true }
|
||||
revolt-models = { workspace = true, features = ["validator"] }
|
||||
revolt-presence = { workspace = true }
|
||||
@@ -56,6 +56,7 @@ validator = { workspace = true, features = ["derive"] }
|
||||
isahc = { workspace = true, features = ["json"], optional = true }
|
||||
base32 = { workspace = true }
|
||||
sha1 = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
|
||||
# Serialisation
|
||||
serde_json = { workspace = true }
|
||||
|
||||
95
crates/core/database/src/amqp/consumer.rs
Normal file
95
crates/core/database/src/amqp/consumer.rs
Normal file
@@ -0,0 +1,95 @@
|
||||
use std::{
|
||||
future::{Future, ready}, marker::PhantomData, pin::Pin, sync::Arc
|
||||
};
|
||||
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use lapin::{
|
||||
message::{Delivery, DeliveryResult},
|
||||
options::BasicPublishOptions,
|
||||
BasicProperties, Channel, Connection, ConsumerDelegate, Error as AMQPError,
|
||||
};
|
||||
use log::debug;
|
||||
use crate::Database;
|
||||
|
||||
#[async_trait]
|
||||
pub trait Consumer<T: Clone = ()>: Clone + Send + Sync + 'static {
|
||||
async fn create(
|
||||
db: Database,
|
||||
connection: Arc<Connection>,
|
||||
channel: Arc<Channel>,
|
||||
data: T,
|
||||
) -> Self;
|
||||
fn channel(&self) -> &Arc<Channel>;
|
||||
async fn consume(&self, delivery: Delivery) -> Result<()>;
|
||||
|
||||
async fn publish_message_with_options(
|
||||
&self,
|
||||
payload: &[u8],
|
||||
exchange: &str,
|
||||
routing_key: &str,
|
||||
options: BasicPublishOptions,
|
||||
properties: BasicProperties,
|
||||
) -> Result<(), AMQPError> {
|
||||
let channel = self.channel();
|
||||
|
||||
channel
|
||||
.basic_publish(
|
||||
exchange.into(),
|
||||
routing_key.into(),
|
||||
options,
|
||||
payload,
|
||||
properties,
|
||||
)
|
||||
.await?;
|
||||
debug!("Sent message to queue for target {}", routing_key);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn publish_message(
|
||||
&self,
|
||||
payload: &[u8],
|
||||
exchange: &str,
|
||||
routing_key: &str,
|
||||
) -> Result<(), AMQPError> {
|
||||
self.publish_message_with_options(
|
||||
payload,
|
||||
exchange,
|
||||
routing_key,
|
||||
BasicPublishOptions::default(),
|
||||
BasicProperties::default(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Delegate<C: Consumer<D>, D: Clone>(C, PhantomData<D>);
|
||||
|
||||
impl<C: Consumer<D>, D: Clone> Delegate<C, D> {
|
||||
pub fn new(consumer: C) -> Self {
|
||||
Self(consumer, PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: Consumer<D>, D: Clone + Send + Sync> ConsumerDelegate for Delegate<C, D> {
|
||||
fn on_new_delivery(
|
||||
&self,
|
||||
delivery: DeliveryResult,
|
||||
) -> Pin<Box<dyn Future<Output = ()> + Send>> {
|
||||
match delivery {
|
||||
Ok(Some(delivery)) => {
|
||||
let consumer = self.0.clone();
|
||||
|
||||
Box::pin(async move {
|
||||
if let Err(e) = consumer.consume(delivery).await {
|
||||
revolt_config::capture_anyhow(&e);
|
||||
log::error!("{e:?}");
|
||||
};
|
||||
})
|
||||
}
|
||||
Ok(None) => Box::pin(ready(())),
|
||||
Err(e) => Box::pin(async move { log::error!("Received bad delivery: {e:?}") }),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,3 @@
|
||||
#[allow(clippy::module_inception)]
|
||||
pub mod amqp;
|
||||
pub mod consumer;
|
||||
@@ -109,7 +109,7 @@ pub mod events;
|
||||
#[cfg(feature = "tasks")]
|
||||
pub mod tasks;
|
||||
|
||||
mod amqp;
|
||||
pub mod amqp;
|
||||
pub use amqp::amqp::AMQP;
|
||||
|
||||
#[cfg(feature = "voice")]
|
||||
|
||||
@@ -882,13 +882,13 @@ impl User {
|
||||
/// - deletes owned bots, servers and messages
|
||||
/// - removes user from all groups
|
||||
/// - clears relationships
|
||||
pub async fn delete(&mut self, db: &Database) -> Result<()> {
|
||||
pub async fn delete(&mut self, db: &Database, amqp: Option<&AMQP>) -> Result<()> {
|
||||
for bot in db.fetch_bots_by_user(&self.id).await? {
|
||||
bot.delete(db).await?;
|
||||
}
|
||||
|
||||
for server in db.fetch_owned_servers(&self.id).await? {
|
||||
server.delete(db).await?;
|
||||
server.delete(db, amqp).await?;
|
||||
}
|
||||
|
||||
self.remove_from_all_groups(db).await?;
|
||||
|
||||
@@ -12,7 +12,6 @@ pub mod permissions;
|
||||
pub mod reference;
|
||||
pub mod shield;
|
||||
pub mod test_fixtures;
|
||||
pub mod chunked;
|
||||
|
||||
pub use chunked::ChunkedDatabaseGenerator;
|
||||
pub use funcs::*;
|
||||
pub use chunked::ChunkedDatabaseGenerator;
|
||||
Reference in New Issue
Block a user