forked from jmug/stoatchat
Clean up subscription code; handle error properly.
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -2473,7 +2473,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "revolt"
|
name = "revolt"
|
||||||
version = "0.3.3-alpha.6"
|
version = "0.3.3-alpha.7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-std",
|
"async-std",
|
||||||
"async-tungstenite",
|
"async-tungstenite",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "revolt"
|
name = "revolt"
|
||||||
version = "0.3.3-alpha.6"
|
version = "0.3.3-alpha.7"
|
||||||
authors = ["Paul Makles <paulmakles@gmail.com>"]
|
authors = ["Paul Makles <paulmakles@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,9 @@ use mongodb::{
|
|||||||
use rocket_contrib::json::JsonValue;
|
use rocket_contrib::json::JsonValue;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use ulid::Ulid;
|
use ulid::Ulid;
|
||||||
use web_push::{ContentEncoding, SubscriptionInfo, VapidSignatureBuilder, WebPushClient, WebPushMessageBuilder};
|
use web_push::{
|
||||||
|
ContentEncoding, SubscriptionInfo, VapidSignatureBuilder, WebPushClient, WebPushMessageBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
@@ -171,7 +173,8 @@ impl Message {
|
|||||||
for subscription in subscriptions {
|
for subscription in subscriptions {
|
||||||
let mut builder = WebPushMessageBuilder::new(&subscription).unwrap();
|
let mut builder = WebPushMessageBuilder::new(&subscription).unwrap();
|
||||||
let sig_builder =
|
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();
|
let signature = sig_builder.build().unwrap();
|
||||||
builder.set_vapid_signature(signature);
|
builder.set_vapid_signature(signature);
|
||||||
builder.set_payload(ContentEncoding::AesGcm, enc.as_bytes());
|
builder.set_payload(ContentEncoding::AesGcm, enc.as_bytes());
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ pub struct Subscription {
|
|||||||
#[post("/subscribe", data = "<data>")]
|
#[post("/subscribe", data = "<data>")]
|
||||||
pub async fn req(session: Session, data: Json<Subscription>) -> Result<()> {
|
pub async fn req(session: Session, data: Json<Subscription>) -> Result<()> {
|
||||||
let data = data.into_inner();
|
let data = data.into_inner();
|
||||||
let col = get_collection("accounts")
|
get_collection("accounts")
|
||||||
.update_one(
|
.update_one(
|
||||||
doc! {
|
doc! {
|
||||||
"_id": session.user_id,
|
"_id": session.user_id,
|
||||||
@@ -24,13 +24,14 @@ pub async fn req(session: Session, data: Json<Subscription>) -> Result<()> {
|
|||||||
},
|
},
|
||||||
doc! {
|
doc! {
|
||||||
"$set": {
|
"$set": {
|
||||||
"sessions.$.subscription": to_document(&data).unwrap()
|
"sessions.$.subscription": to_document(&data)
|
||||||
|
.map_err(|_| Error::DatabaseError { operation: "to_document", with: "subscription" })?
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.map_err(|_| Error::DatabaseError { operation: "update_one", with: "account" })?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use rauth::auth::Session;
|
|||||||
|
|
||||||
#[post("/unsubscribe")]
|
#[post("/unsubscribe")]
|
||||||
pub async fn req(session: Session) -> Result<()> {
|
pub async fn req(session: Session) -> Result<()> {
|
||||||
let col = get_collection("accounts")
|
get_collection("accounts")
|
||||||
.update_one(
|
.update_one(
|
||||||
doc! {
|
doc! {
|
||||||
"_id": session.user_id,
|
"_id": session.user_id,
|
||||||
@@ -20,7 +20,10 @@ pub async fn req(session: Session) -> Result<()> {
|
|||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.map_err(|_| Error::DatabaseError {
|
||||||
|
operation: "to_document",
|
||||||
|
with: "subscription",
|
||||||
|
})?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use rocket_contrib::json::JsonValue;
|
|||||||
#[get("/")]
|
#[get("/")]
|
||||||
pub async fn root() -> JsonValue {
|
pub async fn root() -> JsonValue {
|
||||||
json!({
|
json!({
|
||||||
"revolt": "0.3.3-alpha.6",
|
"revolt": "0.3.3-alpha.7",
|
||||||
"features": {
|
"features": {
|
||||||
"registration": !*DISABLE_REGISTRATION,
|
"registration": !*DISABLE_REGISTRATION,
|
||||||
"captcha": {
|
"captcha": {
|
||||||
|
|||||||
@@ -52,8 +52,6 @@ lazy_static! {
|
|||||||
// Application Logic Settings
|
// Application Logic Settings
|
||||||
pub static ref MAX_GROUP_SIZE: usize =
|
pub static ref MAX_GROUP_SIZE: usize =
|
||||||
env::var("REVOLT_MAX_GROUP_SIZE").unwrap_or_else(|_| "50".to_string()).parse().unwrap();
|
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() {
|
pub fn preflight_checks() {
|
||||||
|
|||||||
Reference in New Issue
Block a user