mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 13:36:59 +00:00
Basic Web Push support.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[serde(tag = "type")]
|
||||
@@ -17,5 +17,5 @@ pub struct File {
|
||||
metadata: Metadata,
|
||||
content_type: String,
|
||||
|
||||
message_id: Option<String>
|
||||
message_id: Option<String>,
|
||||
}
|
||||
|
||||
@@ -46,9 +46,9 @@ pub enum Channel {
|
||||
impl Channel {
|
||||
pub fn id(&self) -> &str {
|
||||
match self {
|
||||
Channel::SavedMessages { id, .. } => id,
|
||||
Channel::DirectMessage { id, .. } => id,
|
||||
Channel::Group { id, .. } => id,
|
||||
Channel::SavedMessages { id, .. }
|
||||
| Channel::DirectMessage { id, .. }
|
||||
| Channel::Group { id, .. } => id,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
use crate::util::variables::VAPID_PRIVATE_KEY;
|
||||
use crate::{
|
||||
database::*,
|
||||
notifications::events::ClientboundNotification,
|
||||
notifications::{events::ClientboundNotification, websocket::is_online},
|
||||
util::result::{Error, Result},
|
||||
};
|
||||
use mongodb::bson::{doc, to_bson, DateTime};
|
||||
|
||||
use futures::StreamExt;
|
||||
use mongodb::{
|
||||
bson::{doc, to_bson, DateTime},
|
||||
options::FindOptions,
|
||||
};
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use ulid::Ulid;
|
||||
use web_push::{ContentEncoding, SubscriptionInfo, VapidSignatureBuilder, WebPushClient, WebPushMessageBuilder};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Message {
|
||||
@@ -49,7 +56,7 @@ impl Message {
|
||||
|
||||
// ! FIXME: temp code
|
||||
let channels = get_collection("channels");
|
||||
match channel {
|
||||
match &channel {
|
||||
Channel::DirectMessage { id, .. } => {
|
||||
channels
|
||||
.update_one(
|
||||
@@ -96,11 +103,84 @@ impl Message {
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let enc = serde_json::to_string(&self).unwrap();
|
||||
ClientboundNotification::Message(self)
|
||||
.publish(channel.id().to_string())
|
||||
.await
|
||||
.ok();
|
||||
|
||||
/*
|
||||
Web Push Test Code
|
||||
! FIXME: temp code
|
||||
*/
|
||||
|
||||
// Find all offline users.
|
||||
let mut target_ids = vec![];
|
||||
match &channel {
|
||||
Channel::DirectMessage { recipients, .. } | Channel::Group { recipients, .. } => {
|
||||
for recipient in recipients {
|
||||
// if !is_online(recipient) {
|
||||
target_ids.push(recipient.clone());
|
||||
// }
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// Fetch their corresponding sessions.
|
||||
let mut cursor = get_collection("accounts")
|
||||
.find(
|
||||
doc! {
|
||||
"_id": {
|
||||
"$in": target_ids
|
||||
},
|
||||
"sessions.subscription": {
|
||||
"$exists": true
|
||||
}
|
||||
},
|
||||
FindOptions::builder()
|
||||
.projection(doc! { "sessions": 1 })
|
||||
.build(),
|
||||
)
|
||||
.await
|
||||
.unwrap(); // !FIXME
|
||||
|
||||
let mut subscriptions = vec![];
|
||||
while let Some(result) = cursor.next().await {
|
||||
if let Ok(doc) = result {
|
||||
if let Ok(sessions) = doc.get_array("sessions") {
|
||||
for session in sessions {
|
||||
if let Some(doc) = session.as_document() {
|
||||
if let Ok(sub) = doc.get_document("subscription") {
|
||||
let endpoint = sub.get_str("endpoint").unwrap().to_string();
|
||||
let p256dh = sub.get_str("p256dh").unwrap().to_string();
|
||||
let auth = sub.get_str("auth").unwrap().to_string();
|
||||
|
||||
subscriptions.push(SubscriptionInfo::new(endpoint, p256dh, auth));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if subscriptions.len() > 0 {
|
||||
let client = WebPushClient::new();
|
||||
let key = base64::decode_config(VAPID_PRIVATE_KEY.clone(), base64::URL_SAFE).unwrap();
|
||||
|
||||
for subscription in subscriptions {
|
||||
let mut builder = WebPushMessageBuilder::new(&subscription).unwrap();
|
||||
let sig_builder =
|
||||
VapidSignatureBuilder::from_pem(std::io::Cursor::new(&key), &subscription).unwrap();
|
||||
let signature = sig_builder.build().unwrap();
|
||||
builder.set_vapid_signature(signature);
|
||||
builder.set_payload(ContentEncoding::AesGcm, enc.as_bytes());
|
||||
let m = builder.build().unwrap();
|
||||
let response = client.send(m).await.unwrap();
|
||||
dbg!(response);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
mod autumn;
|
||||
mod channel;
|
||||
mod guild;
|
||||
mod message;
|
||||
mod user;
|
||||
mod autumn;
|
||||
|
||||
pub use autumn::*;
|
||||
pub use channel::*;
|
||||
pub use guild::*;
|
||||
pub use message::*;
|
||||
pub use user::*;
|
||||
pub use autumn::*;
|
||||
|
||||
Reference in New Issue
Block a user