Skip to content

Commit

Permalink
Add ability to disable zaps
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyGiorgio committed Apr 11, 2024
1 parent 08d88cf commit ea66c78
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions migrations/2024-04-11-210151_disable_zaps/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE app_user DROP COLUMN disabled_zaps;
1 change: 1 addition & 0 deletions migrations/2024-04-11-210151_disable_zaps/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE app_user ADD COLUMN disabled_zaps BOOLEAN NOT NULL DEFAULT FALSE;
6 changes: 6 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<AppUser>>;
fn insert_new_zap(&self, new_zap: Zap) -> anyhow::Result<Zap>;
fn get_zap_by_id(&self, id: i32) -> anyhow::Result<Option<Zap>>;
Expand Down Expand Up @@ -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<Vec<Invoice>> {
let conn = &mut self.db.get()?;
Invoice::get_by_state(conn, 0)
Expand Down
6 changes: 6 additions & 0 deletions src/lnurlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
11 changes: 11 additions & 0 deletions src/models/app_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)]
Expand Down
1 change: 1 addition & 0 deletions src/models/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ diesel::table! {
#[max_length = 255]
federation_invite_code -> Varchar,
invoice_index -> Int4,
disabled_zaps -> Bool,
}
}

Expand Down

0 comments on commit ea66c78

Please sign in to comment.