chore: update es code to lapin
Signed-off-by: Zomatree <me@zomatree.live>
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::utils::Consumer;
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use lapin::{message::Delivery, Channel, Connection};
|
||||
use revolt_database::{events::rabbit::*, Database};
|
||||
use revolt_database::{amqp::consumer::Consumer, events::rabbit::*, Database};
|
||||
|
||||
#[derive(Clone)]
|
||||
#[allow(unused)]
|
||||
@@ -16,11 +15,7 @@ pub struct AckConsumer {
|
||||
|
||||
#[async_trait]
|
||||
impl Consumer for AckConsumer {
|
||||
async fn create(
|
||||
db: Database,
|
||||
connection: Arc<Connection>,
|
||||
channel: Arc<Channel>,
|
||||
) -> Self {
|
||||
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<Channel>, _: ()) -> Self {
|
||||
Self {
|
||||
db,
|
||||
connection,
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use crate::utils::Consumer;
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use lapin::{message::Delivery, Channel, Connection};
|
||||
use log::debug;
|
||||
use revolt_database::{events::rabbit::*, Database};
|
||||
use revolt_database::{amqp::consumer::Consumer, events::rabbit::*, Database};
|
||||
|
||||
#[derive(Clone)]
|
||||
#[allow(unused)]
|
||||
@@ -17,11 +16,7 @@ pub struct DmCallConsumer {
|
||||
|
||||
#[async_trait]
|
||||
impl Consumer for DmCallConsumer {
|
||||
async fn create(
|
||||
db: Database,
|
||||
connection: Arc<Connection>,
|
||||
channel: Arc<Channel>,
|
||||
) -> Self {
|
||||
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<Channel>, _: ()) -> Self {
|
||||
Self {
|
||||
db,
|
||||
connection,
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use crate::utils::Consumer;
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use lapin::{message::Delivery, Channel, Connection};
|
||||
use log::debug;
|
||||
use revolt_database::{events::rabbit::*, Database};
|
||||
use revolt_database::{amqp::consumer::Consumer, events::rabbit::*, Database};
|
||||
|
||||
#[derive(Clone)]
|
||||
#[allow(unused)]
|
||||
@@ -17,11 +16,7 @@ pub struct FRAcceptedConsumer {
|
||||
|
||||
#[async_trait]
|
||||
impl Consumer for FRAcceptedConsumer {
|
||||
async fn create(
|
||||
db: Database,
|
||||
connection: Arc<Connection>,
|
||||
channel: Arc<Channel>,
|
||||
) -> Self {
|
||||
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<Channel>, _: ()) -> Self {
|
||||
Self {
|
||||
db,
|
||||
connection,
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use crate::utils::Consumer;
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use lapin::{message::Delivery, Channel, Connection};
|
||||
use log::debug;
|
||||
use revolt_database::{events::rabbit::*, Database};
|
||||
use revolt_database::{amqp::consumer::Consumer, events::rabbit::*, Database};
|
||||
|
||||
#[derive(Clone)]
|
||||
#[allow(unused)]
|
||||
@@ -17,11 +16,7 @@ pub struct FRReceivedConsumer {
|
||||
|
||||
#[async_trait]
|
||||
impl Consumer for FRReceivedConsumer {
|
||||
async fn create(
|
||||
db: Database,
|
||||
connection: Arc<Connection>,
|
||||
channel: Arc<Channel>,
|
||||
) -> Self {
|
||||
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<Channel>, _: ()) -> Self {
|
||||
Self {
|
||||
db,
|
||||
connection,
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use crate::utils::Consumer;
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use lapin::{message::Delivery, Channel, Connection};
|
||||
use log::debug;
|
||||
use revolt_database::{events::rabbit::*, Database};
|
||||
use revolt_database::{amqp::consumer::Consumer, events::rabbit::*, Database};
|
||||
|
||||
#[derive(Clone)]
|
||||
#[allow(unused)]
|
||||
@@ -17,11 +16,7 @@ pub struct GenericConsumer {
|
||||
|
||||
#[async_trait]
|
||||
impl Consumer for GenericConsumer {
|
||||
async fn create(
|
||||
db: Database,
|
||||
connection: Arc<Connection>,
|
||||
channel: Arc<Channel>,
|
||||
) -> Self {
|
||||
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<Channel>, _: ()) -> Self {
|
||||
Self {
|
||||
db,
|
||||
connection,
|
||||
|
||||
@@ -4,13 +4,13 @@ use std::{
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use crate::utils::{render_notification_content, Consumer};
|
||||
use crate::utils::render_notification_content;
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use lapin::{message::Delivery, Channel, Connection};
|
||||
use revolt_database::{
|
||||
events::rabbit::*, util::bulk_permissions::BulkDatabasePermissionQuery, Database, Member,
|
||||
MessageFlagsValue,
|
||||
amqp::consumer::Consumer, events::rabbit::*,
|
||||
util::bulk_permissions::BulkDatabasePermissionQuery, Database, Member, MessageFlagsValue,
|
||||
};
|
||||
use revolt_models::v0::{MessageFlags, PushNotification};
|
||||
use revolt_result::ToRevoltError;
|
||||
@@ -29,11 +29,7 @@ impl MassMessageConsumer {
|
||||
push: &PushNotification,
|
||||
users: &[String],
|
||||
) -> Result<()> {
|
||||
if let Ok(sessions) = self
|
||||
.db
|
||||
.fetch_sessions_with_subscription(users)
|
||||
.await
|
||||
{
|
||||
if let Ok(sessions) = self.db.fetch_sessions_with_subscription(users).await {
|
||||
let config = revolt_config::config().await;
|
||||
for session in sessions {
|
||||
if let Some(sub) = session.subscription {
|
||||
@@ -72,11 +68,7 @@ impl MassMessageConsumer {
|
||||
|
||||
#[async_trait]
|
||||
impl Consumer for MassMessageConsumer {
|
||||
async fn create(
|
||||
db: Database,
|
||||
connection: Arc<Connection>,
|
||||
channel: Arc<Channel>,
|
||||
) -> Self {
|
||||
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<Channel>, _: ()) -> Self {
|
||||
Self {
|
||||
db,
|
||||
connection,
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use crate::utils::{render_notification_content, Consumer};
|
||||
use crate::utils::render_notification_content;
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use lapin::{message::Delivery, Channel, Connection};
|
||||
use log::debug;
|
||||
use revolt_database::{events::rabbit::*, Database};
|
||||
use revolt_database::{amqp::consumer::Consumer, events::rabbit::*, Database};
|
||||
|
||||
#[derive(Clone)]
|
||||
#[allow(unused)]
|
||||
@@ -17,11 +17,7 @@ pub struct MessageConsumer {
|
||||
|
||||
#[async_trait]
|
||||
impl Consumer for MessageConsumer {
|
||||
async fn create(
|
||||
db: Database,
|
||||
connection: Arc<Connection>,
|
||||
channel: Arc<Channel>,
|
||||
) -> Self {
|
||||
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<Channel>, _: ()) -> Self {
|
||||
Self {
|
||||
db,
|
||||
connection,
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use std::{borrow::Cow, collections::BTreeMap, io::Cursor, sync::Arc};
|
||||
|
||||
use crate::utils::Consumer;
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use base64::{
|
||||
@@ -15,7 +14,7 @@ use revolt_a2::{
|
||||
},
|
||||
Client, ClientConfig, Endpoint, Error, ErrorBody, ErrorReason, Priority, PushType, Response,
|
||||
};
|
||||
use revolt_database::{events::rabbit::*, Database};
|
||||
use revolt_database::{amqp::consumer::Consumer, events::rabbit::*, Database};
|
||||
use revolt_models::v0::{Channel, Message, PushNotification};
|
||||
use serde::Serialize;
|
||||
|
||||
@@ -123,11 +122,7 @@ impl ApnsOutboundConsumer {
|
||||
|
||||
#[async_trait]
|
||||
impl Consumer for ApnsOutboundConsumer {
|
||||
async fn create(
|
||||
db: Database,
|
||||
connection: Arc<Connection>,
|
||||
channel: Arc<AMQPChannel>,
|
||||
) -> Self {
|
||||
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<AMQPChannel>, _: ()) -> Self {
|
||||
let config = revolt_config::config().await;
|
||||
|
||||
if config.pushd.apn.pkcs8.is_empty()
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use std::{collections::HashMap, sync::Arc, time::Duration};
|
||||
|
||||
use crate::utils::Consumer;
|
||||
use anyhow::{bail, Result};
|
||||
use async_trait::async_trait;
|
||||
use fcm_v1::{
|
||||
@@ -10,7 +9,7 @@ use fcm_v1::{
|
||||
};
|
||||
use lapin::{message::Delivery, Channel as AMQPChannel, Connection};
|
||||
use revolt_config::config;
|
||||
use revolt_database::{events::rabbit::*, Database};
|
||||
use revolt_database::{amqp::consumer::Consumer, events::rabbit::*, Database};
|
||||
use serde_json::Value;
|
||||
|
||||
/// Custom notification data
|
||||
@@ -126,11 +125,7 @@ pub struct FcmOutboundConsumer {
|
||||
|
||||
#[async_trait]
|
||||
impl Consumer for FcmOutboundConsumer {
|
||||
async fn create(
|
||||
db: Database,
|
||||
connection: Arc<Connection>,
|
||||
channel: Arc<AMQPChannel>,
|
||||
) -> Self {
|
||||
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<AMQPChannel>, _: ()) -> Self {
|
||||
let config = revolt_config::config().await;
|
||||
|
||||
Self {
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use crate::utils::Consumer;
|
||||
|
||||
use anyhow::{anyhow, bail, Result};
|
||||
use async_trait::async_trait;
|
||||
use base64::{
|
||||
@@ -9,7 +7,9 @@ use base64::{
|
||||
Engine as _,
|
||||
};
|
||||
use lapin::{message::Delivery, Channel as AMQPChannel, Connection};
|
||||
use revolt_database::{events::rabbit::*, util::format_display_name, Database};
|
||||
use revolt_database::{
|
||||
amqp::consumer::Consumer, events::rabbit::*, util::format_display_name, Database,
|
||||
};
|
||||
use web_push::{
|
||||
ContentEncoding, IsahcWebPushClient, SubscriptionInfo, SubscriptionKeys, VapidSignatureBuilder,
|
||||
WebPushClient, WebPushError, WebPushMessageBuilder,
|
||||
@@ -27,11 +27,7 @@ pub struct VapidOutboundConsumer {
|
||||
|
||||
#[async_trait]
|
||||
impl Consumer for VapidOutboundConsumer {
|
||||
async fn create(
|
||||
db: Database,
|
||||
connection: Arc<Connection>,
|
||||
channel: Arc<AMQPChannel>,
|
||||
) -> Self {
|
||||
async fn create(db: Database, connection: Arc<Connection>, channel: Arc<AMQPChannel>, _: ()) -> Self {
|
||||
let config = revolt_config::config().await;
|
||||
|
||||
if config.pushd.vapid.private_key.is_empty() || config.pushd.vapid.public_key.is_empty() {
|
||||
|
||||
@@ -9,7 +9,7 @@ use lapin::{
|
||||
Channel, Connection, ConnectionProperties,
|
||||
};
|
||||
use revolt_config::{config, Settings};
|
||||
use revolt_database::Database;
|
||||
use revolt_database::{Database, amqp::consumer::{Consumer, Delegate}};
|
||||
use tokio::signal::ctrl_c;
|
||||
|
||||
mod consumers;
|
||||
@@ -23,8 +23,6 @@ use consumers::{
|
||||
outbound::{apn::ApnsOutboundConsumer, fcm::FcmOutboundConsumer, vapid::VapidOutboundConsumer},
|
||||
};
|
||||
|
||||
use crate::utils::{Consumer, Delegate};
|
||||
|
||||
#[tokio::main(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn main() {
|
||||
// Configure logging and environment
|
||||
@@ -275,11 +273,12 @@ where
|
||||
consumer.tag()
|
||||
);
|
||||
|
||||
let delegate = Delegate(
|
||||
let delegate = Delegate::new(
|
||||
F::create(
|
||||
db.clone(),
|
||||
connection.clone(),
|
||||
channel.clone(),
|
||||
()
|
||||
)
|
||||
.await,
|
||||
);
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
use std::{
|
||||
future::{ready, Future},
|
||||
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 revolt_database::Database;
|
||||
|
||||
#[async_trait]
|
||||
pub trait Consumer: Clone + Send + Sync + 'static {
|
||||
async fn create(
|
||||
db: Database,
|
||||
connection: Arc<Connection>,
|
||||
channel: Arc<Channel>,
|
||||
) -> 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>(pub C);
|
||||
|
||||
impl<C: Consumer> ConsumerDelegate for Delegate<C> {
|
||||
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,5 +1,3 @@
|
||||
mod renderer;
|
||||
mod consumer;
|
||||
|
||||
pub use renderer::render_notification_content;
|
||||
pub use consumer::{Consumer, Delegate};
|
||||
pub use renderer::render_notification_content;
|
||||
Reference in New Issue
Block a user