From 2d66292350d352c3ea41db08d4c2de33898e9806 Mon Sep 17 00:00:00 2001 From: GeekCornerGH <45696571+GeekCornerGH@users.noreply.github.com> Date: Sun, 11 Jun 2023 13:28:18 +0200 Subject: feat: Push Notifications Co-authored-by: samb-devel <125741162+samb-devel@users.noreply.github.com> Co-authored-by: Zoruk --- src/db/models/device.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'src/db/models') diff --git a/src/db/models/device.rs b/src/db/models/device.rs index e47ccadc..a1f4aee9 100644 --- a/src/db/models/device.rs +++ b/src/db/models/device.rs @@ -15,7 +15,8 @@ db_object! { pub user_uuid: String, pub name: String, - pub atype: i32, // https://github.com/bitwarden/server/blob/master/src/Core/Enums/DeviceType.cs + pub atype: i32, // https://github.com/bitwarden/server/blob/master/src/Core/Enums/DeviceType.cs + pub push_uuid: Option, pub push_token: Option, pub refresh_token: String, @@ -38,6 +39,7 @@ impl Device { name, atype, + push_uuid: None, push_token: None, refresh_token: String::new(), twofactor_remember: None, @@ -155,6 +157,35 @@ impl Device { }} } + pub async fn find_by_user(user_uuid: &str, conn: &mut DbConn) -> Vec { + db_run! { conn: { + devices::table + .filter(devices::user_uuid.eq(user_uuid)) + .load::(conn) + .expect("Error loading devices") + .from_db() + }} + } + + pub async fn find_by_uuid(uuid: &str, conn: &mut DbConn) -> Option { + db_run! { conn: { + devices::table + .filter(devices::uuid.eq(uuid)) + .first::(conn) + .ok() + .from_db() + }} + } + + pub async fn clear_push_token_by_uuid(uuid: &str, conn: &mut DbConn) -> EmptyResult { + db_run! { conn: { + diesel::update(devices::table) + .filter(devices::uuid.eq(uuid)) + .set(devices::push_token.eq::>(None)) + .execute(conn) + .map_res("Error removing push token") + }} + } pub async fn find_by_refresh_token(refresh_token: &str, conn: &mut DbConn) -> Option { db_run! { conn: { devices::table @@ -175,4 +206,14 @@ impl Device { .from_db() }} } + pub async fn find_push_device_by_user(user_uuid: &str, conn: &mut DbConn) -> Vec { + db_run! { conn: { + devices::table + .filter(devices::user_uuid.eq(user_uuid)) + .filter(devices::push_token.is_not_null()) + .load::(conn) + .expect("Error loading push devices") + .from_db() + }} + } } -- cgit v1.2.3