From 78cfbf9d21f835e771bba043776087174cc89cb9 Mon Sep 17 00:00:00 2001
From: Paul <paulmakles@gmail.com>
Date: Fri, 19 Feb 2021 12:48:52 +0000
Subject: [PATCH] Clean up subscription code; handle error properly.

---
 Cargo.lock                       | 2 +-
 Cargo.toml                       | 2 +-
 src/database/entities/message.rs | 7 +++++--
 src/routes/push/subscribe.rs     | 7 ++++---
 src/routes/push/unsubscribe.rs   | 7 +++++--
 src/routes/root.rs               | 2 +-
 src/util/variables.rs            | 2 --
 7 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index d0b7d22..7682ea6 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2473,7 +2473,7 @@ dependencies = [
 
 [[package]]
 name = "revolt"
-version = "0.3.3-alpha.6"
+version = "0.3.3-alpha.7"
 dependencies = [
  "async-std",
  "async-tungstenite",
diff --git a/Cargo.toml b/Cargo.toml
index b02ce5c..b43a5be 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "revolt"
-version = "0.3.3-alpha.6"
+version = "0.3.3-alpha.7"
 authors = ["Paul Makles <paulmakles@gmail.com>"]
 edition = "2018"
 
diff --git a/src/database/entities/message.rs b/src/database/entities/message.rs
index aaf5f48..9fbffd3 100644
--- a/src/database/entities/message.rs
+++ b/src/database/entities/message.rs
@@ -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());
diff --git a/src/routes/push/subscribe.rs b/src/routes/push/subscribe.rs
index 57d30a3..d2a0604 100644
--- a/src/routes/push/subscribe.rs
+++ b/src/routes/push/subscribe.rs
@@ -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(())
 }
diff --git a/src/routes/push/unsubscribe.rs b/src/routes/push/unsubscribe.rs
index deb1026..41634e7 100644
--- a/src/routes/push/unsubscribe.rs
+++ b/src/routes/push/unsubscribe.rs
@@ -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(())
 }
diff --git a/src/routes/root.rs b/src/routes/root.rs
index 23f2769..9fc8e1c 100644
--- a/src/routes/root.rs
+++ b/src/routes/root.rs
@@ -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": {
diff --git a/src/util/variables.rs b/src/util/variables.rs
index 6431673..19b7249 100644
--- a/src/util/variables.rs
+++ b/src/util/variables.rs
@@ -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() {
-- 
GitLab