mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 13:36:59 +00:00
Clean up subscription code; handle error properly.
This commit is contained in:
@@ -13,7 +13,9 @@ use mongodb::{
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use ulid::Ulid;
|
||||
use web_push::{ContentEncoding, SubscriptionInfo, VapidSignatureBuilder, WebPushClient, WebPushMessageBuilder};
|
||||
use web_push::{
|
||||
ContentEncoding, SubscriptionInfo, VapidSignatureBuilder, WebPushClient, WebPushMessageBuilder,
|
||||
};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Message {
|
||||
@@ -171,7 +173,8 @@ impl Message {
|
||||
for subscription in subscriptions {
|
||||
let mut builder = WebPushMessageBuilder::new(&subscription).unwrap();
|
||||
let sig_builder =
|
||||
VapidSignatureBuilder::from_pem(std::io::Cursor::new(&key), &subscription).unwrap();
|
||||
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());
|
||||
|
||||
@@ -16,7 +16,7 @@ pub struct Subscription {
|
||||
#[post("/subscribe", data = "<data>")]
|
||||
pub async fn req(session: Session, data: Json<Subscription>) -> Result<()> {
|
||||
let data = data.into_inner();
|
||||
let col = get_collection("accounts")
|
||||
get_collection("accounts")
|
||||
.update_one(
|
||||
doc! {
|
||||
"_id": session.user_id,
|
||||
@@ -24,13 +24,14 @@ pub async fn req(session: Session, data: Json<Subscription>) -> Result<()> {
|
||||
},
|
||||
doc! {
|
||||
"$set": {
|
||||
"sessions.$.subscription": to_document(&data).unwrap()
|
||||
"sessions.$.subscription": to_document(&data)
|
||||
.map_err(|_| Error::DatabaseError { operation: "to_document", with: "subscription" })?
|
||||
}
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
.map_err(|_| Error::DatabaseError { operation: "update_one", with: "account" })?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use rauth::auth::Session;
|
||||
|
||||
#[post("/unsubscribe")]
|
||||
pub async fn req(session: Session) -> Result<()> {
|
||||
let col = get_collection("accounts")
|
||||
get_collection("accounts")
|
||||
.update_one(
|
||||
doc! {
|
||||
"_id": session.user_id,
|
||||
@@ -20,7 +20,10 @@ pub async fn req(session: Session) -> Result<()> {
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "to_document",
|
||||
with: "subscription",
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ use rocket_contrib::json::JsonValue;
|
||||
#[get("/")]
|
||||
pub async fn root() -> JsonValue {
|
||||
json!({
|
||||
"revolt": "0.3.3-alpha.6",
|
||||
"revolt": "0.3.3-alpha.7",
|
||||
"features": {
|
||||
"registration": !*DISABLE_REGISTRATION,
|
||||
"captcha": {
|
||||
|
||||
@@ -52,8 +52,6 @@ lazy_static! {
|
||||
// Application Logic Settings
|
||||
pub static ref MAX_GROUP_SIZE: usize =
|
||||
env::var("REVOLT_MAX_GROUP_SIZE").unwrap_or_else(|_| "50".to_string()).parse().unwrap();
|
||||
pub static ref PUSH_LIMIT: usize =
|
||||
env::var("REVOLT_PUSH_LIMIT").unwrap_or_else(|_| "50".to_string()).parse().unwrap();
|
||||
}
|
||||
|
||||
pub fn preflight_checks() {
|
||||
|
||||
Reference in New Issue
Block a user