forked from jmug/stoatchat
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 parent
018afaf38f
commit
5b1985381a
26 files changed
+911
-1323
No files matched your search
@@ -1,6 +1,6 @@
|
||||
use std::collections::HashMap;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use amqprs::{channel::Channel as AmqpChannel, consumer::AsyncConsumer, BasicProperties, Deliver};
|
||||
use crate::utils::Consumer;
|
||||
|
||||
use anyhow::{anyhow, bail, Result};
|
||||
use async_trait::async_trait;
|
||||
@@ -8,46 +8,60 @@ use base64::{
|
||||
engine::{self},
|
||||
Engine as _,
|
||||
};
|
||||
use lapin::{message::Delivery, Channel as AMQPChannel, Connection};
|
||||
use revolt_database::{events::rabbit::*, util::format_display_name, Database};
|
||||
use web_push::{
|
||||
ContentEncoding, IsahcWebPushClient, SubscriptionInfo, SubscriptionKeys, VapidSignatureBuilder,
|
||||
WebPushClient, WebPushError, WebPushMessageBuilder,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
#[allow(unused)]
|
||||
pub struct VapidOutboundConsumer {
|
||||
db: Database,
|
||||
authifier_db: authifier::Database,
|
||||
connection: Arc<Connection>,
|
||||
channel: Arc<AMQPChannel>,
|
||||
client: IsahcWebPushClient,
|
||||
pkey: Vec<u8>,
|
||||
pkey: Arc<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl VapidOutboundConsumer {
|
||||
pub async fn new(db: Database) -> Result<VapidOutboundConsumer> {
|
||||
#[async_trait]
|
||||
impl Consumer for VapidOutboundConsumer {
|
||||
async fn create(
|
||||
db: Database,
|
||||
authifier_db: authifier::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() {
|
||||
bail!("no Vapid keys present");
|
||||
if config.pushd.vapid.private_key.is_empty() || config.pushd.vapid.public_key.is_empty() {
|
||||
panic!("no Vapid keys present");
|
||||
}
|
||||
|
||||
let web_push_private_key = engine::general_purpose::URL_SAFE_NO_PAD
|
||||
.decode(config.pushd.vapid.private_key)
|
||||
.expect("valid `VAPID_PRIVATE_KEY`");
|
||||
let web_push_private_key = Arc::new(
|
||||
engine::general_purpose::URL_SAFE_NO_PAD
|
||||
.decode(config.pushd.vapid.private_key)
|
||||
.expect("valid `VAPID_PRIVATE_KEY`"),
|
||||
);
|
||||
|
||||
Ok(VapidOutboundConsumer {
|
||||
Self {
|
||||
db,
|
||||
authifier_db,
|
||||
connection,
|
||||
channel,
|
||||
client: IsahcWebPushClient::new().unwrap(),
|
||||
pkey: web_push_private_key,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async fn consume_event(
|
||||
&mut self,
|
||||
_channel: &AmqpChannel,
|
||||
_deliver: Deliver,
|
||||
_basic_properties: BasicProperties,
|
||||
content: Vec<u8>,
|
||||
) -> Result<()> {
|
||||
let content = String::from_utf8(content)?;
|
||||
let payload: PayloadToService = serde_json::from_str(content.as_str())?;
|
||||
fn channel(&self) -> &Arc<AMQPChannel> {
|
||||
&self.channel
|
||||
}
|
||||
|
||||
async fn consume(&self, delivery: Delivery) -> Result<()> {
|
||||
let payload: PayloadToService = serde_json::from_slice(&delivery.data)?;
|
||||
|
||||
let subscription = SubscriptionInfo {
|
||||
endpoint: payload
|
||||
@@ -65,10 +79,7 @@ impl VapidOutboundConsumer {
|
||||
},
|
||||
};
|
||||
|
||||
#[allow(clippy::needless_late_init)]
|
||||
let payload_body: String;
|
||||
|
||||
match payload.notification {
|
||||
let payload_body = match payload.notification {
|
||||
PayloadKind::FRReceived(alert) => {
|
||||
let name = alert
|
||||
.from_user
|
||||
@@ -83,7 +94,7 @@ impl VapidOutboundConsumer {
|
||||
let mut body = HashMap::new();
|
||||
body.insert("body", format!("{} sent you a friend request", name));
|
||||
|
||||
payload_body = serde_json::to_string(&body)?;
|
||||
serde_json::to_string(&body)?
|
||||
}
|
||||
PayloadKind::FRAccepted(alert) => {
|
||||
let name = alert
|
||||
@@ -99,14 +110,10 @@ impl VapidOutboundConsumer {
|
||||
let mut body = HashMap::new();
|
||||
body.insert("body", format!("{} accepted your friend request", name));
|
||||
|
||||
payload_body = serde_json::to_string(&body)?;
|
||||
}
|
||||
PayloadKind::Generic(alert) => {
|
||||
payload_body = serde_json::to_string(&alert)?;
|
||||
}
|
||||
PayloadKind::MessageNotification(alert) => {
|
||||
payload_body = serde_json::to_string(&alert)?;
|
||||
serde_json::to_string(&body)?
|
||||
}
|
||||
PayloadKind::Generic(alert) => serde_json::to_string(&alert)?,
|
||||
PayloadKind::MessageNotification(alert) => serde_json::to_string(&alert)?,
|
||||
PayloadKind::DmCallStartEnd(alert) => {
|
||||
let initiator_name = if let Some(server_id) =
|
||||
self.db.fetch_channel(&alert.channel_id).await?.server()
|
||||
@@ -132,59 +139,41 @@ impl VapidOutboundConsumer {
|
||||
_ => bail!("Invalid DmCallStart/End channel type"),
|
||||
}
|
||||
|
||||
payload_body = serde_json::to_string(&body)?;
|
||||
serde_json::to_string(&body)?
|
||||
}
|
||||
PayloadKind::BadgeUpdate(_) => {
|
||||
bail!("Vapid cannot handle badge updates and they should not be sent here.");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
match VapidSignatureBuilder::from_pem(std::io::Cursor::new(&self.pkey), &subscription) {
|
||||
Ok(sig_builder) => match sig_builder.build() {
|
||||
Ok(signature) => {
|
||||
let mut builder = WebPushMessageBuilder::new(&subscription);
|
||||
builder.set_vapid_signature(signature);
|
||||
let signature = VapidSignatureBuilder::from_pem(
|
||||
std::io::Cursor::new(self.pkey.as_ref()),
|
||||
&subscription,
|
||||
)?
|
||||
.build()?;
|
||||
|
||||
builder.set_payload(ContentEncoding::AesGcm, payload_body.as_bytes());
|
||||
let mut builder = WebPushMessageBuilder::new(&subscription);
|
||||
builder.set_vapid_signature(signature);
|
||||
|
||||
match builder.build() {
|
||||
Ok(msg) => {
|
||||
if let Err(err) = self.client.send(msg).await {
|
||||
if err == WebPushError::Unauthorized {
|
||||
self.db
|
||||
.remove_push_subscription_by_session_id(&payload.session_id)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
builder.set_payload(ContentEncoding::AesGcm, payload_body.as_bytes());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Err(err) => Err(err.into()),
|
||||
}
|
||||
let msg = builder.build()?;
|
||||
|
||||
match self.client.send(msg).await {
|
||||
Err(WebPushError::Unauthorized) => {
|
||||
if let Err(err) = self
|
||||
.db
|
||||
.remove_push_subscription_by_session_id(&payload.session_id)
|
||||
.await
|
||||
{
|
||||
revolt_config::capture_error(&err);
|
||||
}
|
||||
Err(err) => Err(err.into()),
|
||||
},
|
||||
Err(err) => Err(err.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
res => {
|
||||
res?;
|
||||
}
|
||||
};
|
||||
|
||||
#[allow(unused_variables)]
|
||||
#[async_trait]
|
||||
impl AsyncConsumer for VapidOutboundConsumer {
|
||||
async fn consume(
|
||||
&mut self,
|
||||
channel: &AmqpChannel,
|
||||
deliver: Deliver,
|
||||
basic_properties: BasicProperties,
|
||||
content: Vec<u8>,
|
||||
) {
|
||||
if let Err(err) = self
|
||||
.consume_event(channel, deliver, basic_properties, content)
|
||||
.await
|
||||
{
|
||||
revolt_config::capture_anyhow(&err);
|
||||
eprintln!("Failed to process Vapid event: {err:?}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user