diff --git a/migrations/2024-04-11-210151_disable_zaps/down.sql b/migrations/2024-04-11-210151_disable_zaps/down.sql new file mode 100644 index 0000000..229b0a8 --- /dev/null +++ b/migrations/2024-04-11-210151_disable_zaps/down.sql @@ -0,0 +1 @@ +ALTER TABLE app_user DROP COLUMN disabled_zaps; diff --git a/migrations/2024-04-11-210151_disable_zaps/up.sql b/migrations/2024-04-11-210151_disable_zaps/up.sql new file mode 100644 index 0000000..56c58ae --- /dev/null +++ b/migrations/2024-04-11-210151_disable_zaps/up.sql @@ -0,0 +1 @@ +ALTER TABLE app_user ADD COLUMN disabled_zaps BOOLEAN NOT NULL DEFAULT FALSE; diff --git a/src/db.rs b/src/db.rs index 61aa75e..3e9bf21 100644 --- a/src/db.rs +++ b/src/db.rs @@ -29,6 +29,7 @@ pub(crate) trait DBConnection { federation_id: String, federation_invite_code: String, ) -> anyhow::Result<()>; + fn disable_user_zaps(&self, user: AppUser) -> anyhow::Result<()>; fn get_user_and_increment_counter(&self, name: &str) -> anyhow::Result>; fn insert_new_zap(&self, new_zap: Zap) -> anyhow::Result; fn get_zap_by_id(&self, id: i32) -> anyhow::Result>; @@ -73,6 +74,11 @@ impl DBConnection for PostgresConnection { user.update_federation(conn, federation_id, federation_invite_code) } + fn disable_user_zaps(&self, user: AppUser) -> anyhow::Result<()> { + let conn = &mut self.db.get()?; + user.disable_zaps(conn) + } + fn get_pending_invoices(&self) -> anyhow::Result> { let conn = &mut self.db.get()?; Invoice::get_by_state(conn, 0) diff --git a/src/lnurlp.rs b/src/lnurlp.rs index 071949c..b0f50f5 100644 --- a/src/lnurlp.rs +++ b/src/lnurlp.rs @@ -61,6 +61,12 @@ pub async fn lnurl_callback( } let user = user.expect("just checked"); + if user.disabled_zaps { + return Err(anyhow!( + "Internal error: User has disabled their address temporarily" + )); + } + let amount_msats = match params.amount { Some(amt) => amt, None => return Err(anyhow!(INVALID_AMT_ERR)), diff --git a/src/models/app_user.rs b/src/models/app_user.rs index 4863e96..3c4c78e 100644 --- a/src/models/app_user.rs +++ b/src/models/app_user.rs @@ -17,6 +17,7 @@ pub struct AppUser { pub federation_id: String, pub federation_invite_code: String, pub invoice_index: i32, + pub disabled_zaps: bool, } impl AppUser { @@ -99,11 +100,21 @@ impl AppUser { .set(( app_user::federation_id.eq(new_federation_id), app_user::federation_invite_code.eq(new_federation_invite_code), + app_user::disabled_zaps.eq(false), )) .execute(conn)?; Ok(()) } + + pub fn disable_zaps(&self, conn: &mut PgConnection) -> anyhow::Result<()> { + diesel::update(app_user::table) + .filter(app_user::name.eq(&self.name)) + .set((app_user::disabled_zaps.eq(true),)) + .execute(conn)?; + + Ok(()) + } } #[derive(Insertable)] diff --git a/src/models/schema.rs b/src/models/schema.rs index 6a61296..5e4e720 100644 --- a/src/models/schema.rs +++ b/src/models/schema.rs @@ -14,6 +14,7 @@ diesel::table! { #[max_length = 255] federation_invite_code -> Varchar, invoice_index -> Int4, + disabled_zaps -> Bool, } }