chore: switch to lapin (#767)
* chore: begin switching to lapin fully Signed-off-by: Zomatree <me@zomatree.live> * chore: update rest of pushd to lapin Signed-off-by: Zomatree <me@zomatree.live> * chore: cleanup code Signed-off-by: Zomatree <me@zomatree.live> * chore: cleanup code Signed-off-by: Zomatree <me@zomatree.live> * fix: github webui sucks Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> --------- Signed-off-by: Zomatree <me@zomatree.live> Signed-off-by: Tom <iamtomahawkx@gmail.com> Signed-off-by: IAmTomahawkx <iamtomahawkx@gmail.com> Co-authored-by: Tom <iamtomahawkx@gmail.com> Release-As: 0.13.6
This commit is contained in:
@@ -1,96 +1,69 @@
|
||||
use crate::consumers::inbound::internal::*;
|
||||
use amqprs::{
|
||||
channel::{BasicPublishArguments, Channel},
|
||||
connection::Connection,
|
||||
consumer::AsyncConsumer,
|
||||
BasicProperties, Deliver,
|
||||
};
|
||||
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};
|
||||
|
||||
#[derive(Clone)]
|
||||
#[allow(unused)]
|
||||
pub struct AckConsumer {
|
||||
#[allow(dead_code)]
|
||||
db: Database,
|
||||
authifier_db: authifier::Database,
|
||||
conn: Option<Connection>,
|
||||
channel: Option<Channel>,
|
||||
connection: Arc<Connection>,
|
||||
channel: Arc<Channel>,
|
||||
}
|
||||
|
||||
impl Channeled for AckConsumer {
|
||||
fn get_connection(&self) -> Option<&Connection> {
|
||||
if self.conn.is_none() {
|
||||
None
|
||||
} else {
|
||||
Some(self.conn.as_ref().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
fn get_channel(&self) -> Option<&Channel> {
|
||||
if self.channel.is_none() {
|
||||
None
|
||||
} else {
|
||||
Some(self.channel.as_ref().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
fn set_connection(&mut self, conn: Connection) {
|
||||
self.conn = Some(conn);
|
||||
}
|
||||
|
||||
fn set_channel(&mut self, channel: Channel) {
|
||||
self.channel = Some(channel)
|
||||
}
|
||||
}
|
||||
|
||||
impl AckConsumer {
|
||||
pub fn new(db: Database, authifier_db: authifier::Database) -> AckConsumer {
|
||||
AckConsumer {
|
||||
#[async_trait]
|
||||
impl Consumer for AckConsumer {
|
||||
async fn create(
|
||||
db: Database,
|
||||
authifier_db: authifier::Database,
|
||||
connection: Arc<Connection>,
|
||||
channel: Arc<Channel>,
|
||||
) -> Self {
|
||||
Self {
|
||||
db,
|
||||
authifier_db,
|
||||
conn: None,
|
||||
channel: None,
|
||||
connection,
|
||||
channel,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
#[async_trait]
|
||||
impl AsyncConsumer for AckConsumer {
|
||||
fn channel(&self) -> &Arc<Channel> {
|
||||
&self.channel
|
||||
}
|
||||
|
||||
/// This consumer processes all acks the platform receives, and sends relevant badge updates to apple platforms.
|
||||
async fn consume(
|
||||
&mut self,
|
||||
channel: &Channel,
|
||||
deliver: Deliver,
|
||||
basic_properties: BasicProperties,
|
||||
content: Vec<u8>,
|
||||
) {
|
||||
let content = String::from_utf8(content).unwrap();
|
||||
let payload: AckPayload = serde_json::from_str(content.as_str()).unwrap();
|
||||
async fn consume(&self, delivery: Delivery) -> Result<()> {
|
||||
let payload: AckPayload = serde_json::from_slice(&delivery.data)?;
|
||||
|
||||
// Step 1: fetch unreads and don't continue if there's no unreads
|
||||
#[allow(clippy::disallowed_methods)]
|
||||
let unreads = self.db.fetch_unread_mentions(&payload.user_id).await;
|
||||
// #[allow(clippy::disallowed_methods)]
|
||||
|
||||
debug!("Processing unreads for {:}", &payload.user_id);
|
||||
|
||||
if let Ok(u) = &unreads {
|
||||
let unreads = if let Ok(u) = self.db.fetch_unread_mentions(&payload.user_id).await {
|
||||
if u.is_empty() {
|
||||
debug!(
|
||||
"Discarding unread task (no mentions found) for {:}",
|
||||
&payload.user_id
|
||||
);
|
||||
return;
|
||||
}
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
u
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
if let Ok(sessions) = self.authifier_db.find_sessions(&payload.user_id).await {
|
||||
let config = revolt_config::config().await;
|
||||
// Step 2: find any apple sessions, since we don't need to calculate this for anything else.
|
||||
// If there's no apple sessions, we can return early
|
||||
let apple_sessions: Vec<&authifier::models::Session> = sessions
|
||||
.iter()
|
||||
let mut apple_sessions = sessions
|
||||
.into_iter()
|
||||
.filter(|session| {
|
||||
if let Some(sub) = &session.subscription {
|
||||
sub.endpoint == "apn"
|
||||
@@ -98,19 +71,19 @@ impl AsyncConsumer for AckConsumer {
|
||||
false
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
.peekable();
|
||||
|
||||
if apple_sessions.is_empty() {
|
||||
if apple_sessions.peek().is_none() {
|
||||
debug!(
|
||||
"Discarding unread task (no apn sessions found) for {:}",
|
||||
&payload.user_id
|
||||
);
|
||||
return;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Step 3: calculate the actual mention count, since we have to send it out
|
||||
let mut mention_count = 0;
|
||||
for u in &unreads.unwrap() {
|
||||
for u in &unreads {
|
||||
mention_count += u.mentions.as_ref().unwrap().len()
|
||||
}
|
||||
|
||||
@@ -123,26 +96,22 @@ impl AsyncConsumer for AckConsumer {
|
||||
token: session.subscription.as_ref().unwrap().auth.clone(),
|
||||
extras: Default::default(),
|
||||
};
|
||||
let raw_service_payload = serde_json::to_string(&service_payload);
|
||||
let payload = serde_json::to_string(&service_payload)?;
|
||||
|
||||
if let Ok(p) = raw_service_payload {
|
||||
let args = BasicPublishArguments::new(
|
||||
config.pushd.exchange.as_str(),
|
||||
config.pushd.apn.queue.as_str(),
|
||||
)
|
||||
.finish();
|
||||
log::debug!(
|
||||
"Publishing ack to apn session {}",
|
||||
session.subscription.as_ref().unwrap().auth
|
||||
);
|
||||
|
||||
log::debug!(
|
||||
"Publishing ack to apn session {}",
|
||||
session.subscription.as_ref().unwrap().auth
|
||||
);
|
||||
|
||||
publish_message(self, p.into(), args).await;
|
||||
} else {
|
||||
log::warn!("Failed to serialize ack badge update payload!");
|
||||
revolt_config::capture_error(&raw_service_payload.unwrap_err());
|
||||
}
|
||||
self.publish_message(
|
||||
payload.as_bytes(),
|
||||
&config.pushd.exchange,
|
||||
&config.pushd.apn.queue,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user