From bcef9bb9896749eb67675805d37a737d0167c880 Mon Sep 17 00:00:00 2001 From: silverpill Date: Sun, 12 Feb 2023 01:43:50 +0000 Subject: [PATCH] Remove database-specific code from utils::caip2 module --- src/mastodon_api/subscriptions/types.rs | 2 +- src/models/invoices/queries.rs | 10 ++-- src/models/invoices/types.rs | 66 ++++++++++++++++++++++++- src/models/subscriptions/queries.rs | 5 +- src/models/subscriptions/types.rs | 8 +-- src/monero/subscriptions.rs | 2 +- src/utils/caip2.rs | 42 ---------------- 7 files changed, 79 insertions(+), 56 deletions(-) diff --git a/src/mastodon_api/subscriptions/types.rs b/src/mastodon_api/subscriptions/types.rs index 872f511..9a8dcb1 100644 --- a/src/mastodon_api/subscriptions/types.rs +++ b/src/mastodon_api/subscriptions/types.rs @@ -34,7 +34,7 @@ impl From for Invoice { InvoiceStatus::Forwarded => "forwarded", InvoiceStatus::Timeout => "timeout", }; - let expires_at = if value.chain_id.is_monero() { + let expires_at = if value.chain_id.inner().is_monero() { value.created_at + Duration::seconds(MONERO_INVOICE_TIMEOUT) } else { // Epoch 0 diff --git a/src/models/invoices/queries.rs b/src/models/invoices/queries.rs index 2faed27..20a0dea 100644 --- a/src/models/invoices/queries.rs +++ b/src/models/invoices/queries.rs @@ -9,7 +9,7 @@ use crate::utils::{ caip2::ChainId, id::generate_ulid, }; -use super::types::{DbInvoice, InvoiceStatus}; +use super::types::{DbChainId, DbInvoice, InvoiceStatus}; pub async fn create_invoice( db_client: &impl DatabaseClient, @@ -37,7 +37,7 @@ pub async fn create_invoice( &invoice_id, &sender_id, &recipient_id, - &chain_id, + &DbChainId::new(chain_id), &payment_address, &amount, ], @@ -72,7 +72,7 @@ pub async fn get_invoice_by_address( SELECT invoice FROM invoice WHERE chain_id = $1 AND payment_address = $2 ", - &[&chain_id, &payment_address], + &[&DbChainId::new(chain_id), &payment_address], ).await?; let row = maybe_row.ok_or(DatabaseError::NotFound("invoice"))?; let invoice = row.try_get("invoice")?; @@ -89,7 +89,7 @@ pub async fn get_invoices_by_status( SELECT invoice FROM invoice WHERE chain_id = $1 AND invoice_status = $2 ", - &[&chain_id, &status], + &[&DbChainId::new(chain_id), &status], ).await?; let invoices = rows.iter() .map(|row| row.try_get("invoice")) @@ -157,7 +157,7 @@ mod tests { ).await.unwrap(); assert_eq!(invoice.sender_id, sender.id); assert_eq!(invoice.recipient_id, recipient.id); - assert_eq!(invoice.chain_id, chain_id); + assert_eq!(invoice.chain_id.into_inner(), chain_id); assert_eq!(invoice.payment_address, payment_address); assert_eq!(invoice.amount, amount); assert_eq!(invoice.invoice_status, InvoiceStatus::Open); diff --git a/src/models/invoices/types.rs b/src/models/invoices/types.rs index 4a5a6a7..42ca0f9 100644 --- a/src/models/invoices/types.rs +++ b/src/models/invoices/types.rs @@ -1,5 +1,14 @@ use chrono::{DateTime, Utc}; -use postgres_types::FromSql; +use postgres_protocol::types::{text_from_sql, text_to_sql}; +use postgres_types::{ + accepts, + private::BytesMut, + to_sql_checked, + FromSql, + IsNull, + ToSql, + Type, +}; use uuid::Uuid; use crate::database::{ @@ -8,6 +17,59 @@ use crate::database::{ }; use crate::utils::caip2::ChainId; +#[derive(Debug)] +pub struct DbChainId(ChainId); + +impl DbChainId { + pub fn new(chain_id: &ChainId) -> Self { + Self(chain_id.clone()) + } + + pub fn inner(&self) -> &ChainId { + let Self(chain_id) = self; + chain_id + } + + pub fn into_inner(self) -> ChainId { + let Self(chain_id) = self; + chain_id + } +} + +impl PartialEq for DbChainId { + fn eq(&self, other: &ChainId) -> bool { + self.inner() == other + } +} + +impl<'a> FromSql<'a> for DbChainId { + fn from_sql( + _: &Type, + raw: &'a [u8], + ) -> Result> { + let value_str = text_from_sql(raw)?; + let value: ChainId = value_str.parse()?; + Ok(Self(value)) + } + + accepts!(VARCHAR); +} + +impl ToSql for DbChainId { + fn to_sql( + &self, + _: &Type, + out: &mut BytesMut, + ) -> Result> { + let value_str = self.inner().to_string(); + text_to_sql(&value_str, out); + Ok(IsNull::No) + } + + accepts!(VARCHAR, TEXT); + to_sql_checked!(); +} + #[derive(Debug, PartialEq)] pub enum InvoiceStatus { Open, @@ -51,7 +113,7 @@ pub struct DbInvoice { pub id: Uuid, pub sender_id: Uuid, pub recipient_id: Uuid, - pub chain_id: ChainId, + pub chain_id: DbChainId, pub payment_address: String, pub amount: i64, // requested payment amount pub invoice_status: InvoiceStatus, diff --git a/src/models/subscriptions/queries.rs b/src/models/subscriptions/queries.rs index f02ec9a..746e9c5 100644 --- a/src/models/subscriptions/queries.rs +++ b/src/models/subscriptions/queries.rs @@ -7,6 +7,7 @@ use crate::database::{ DatabaseError, }; use crate::models::{ + invoices::types::DbChainId, profiles::types::PaymentType, relationships::queries::{subscribe, subscribe_opt}, relationships::types::RelationshipType, @@ -41,7 +42,7 @@ pub async fn create_subscription( &sender_id, &sender_address, &recipient_id, - &chain_id, + &DbChainId::new(chain_id), &expires_at, &updated_at, ], @@ -71,7 +72,7 @@ pub async fn update_subscription( ", &[ &subscription_id, - &chain_id, + &DbChainId::new(chain_id), &expires_at, &updated_at, ], diff --git a/src/models/subscriptions/types.rs b/src/models/subscriptions/types.rs index 8df5946..d95e642 100644 --- a/src/models/subscriptions/types.rs +++ b/src/models/subscriptions/types.rs @@ -4,8 +4,10 @@ use tokio_postgres::Row; use uuid::Uuid; use crate::database::DatabaseError; -use crate::models::profiles::types::DbActorProfile; -use crate::utils::caip2::ChainId; +use crate::models::{ + invoices::types::DbChainId, + profiles::types::DbActorProfile, +}; #[derive(FromSql)] #[postgres(name = "subscription")] @@ -14,7 +16,7 @@ pub struct DbSubscription { pub sender_id: Uuid, pub sender_address: Option, pub recipient_id: Uuid, - pub chain_id: ChainId, + pub chain_id: DbChainId, pub expires_at: DateTime, pub updated_at: DateTime, } diff --git a/src/monero/subscriptions.rs b/src/monero/subscriptions.rs index 3811d04..2bbe12b 100644 --- a/src/monero/subscriptions.rs +++ b/src/monero/subscriptions.rs @@ -173,7 +173,7 @@ pub async fn check_monero_subscriptions( update_subscription( db_client, subscription.id, - &subscription.chain_id, + subscription.chain_id.inner(), &expires_at, &Utc::now(), ).await?; diff --git a/src/utils/caip2.rs b/src/utils/caip2.rs index 2babe1b..ef23fc2 100644 --- a/src/utils/caip2.rs +++ b/src/utils/caip2.rs @@ -109,48 +109,6 @@ impl<'de> Deserialize<'de> for ChainId { } } -mod sql { - use postgres_protocol::types::{text_from_sql, text_to_sql}; - use postgres_types::{ - accepts, - private::BytesMut, - to_sql_checked, - FromSql, - IsNull, - ToSql, - Type, - }; - use super::ChainId; - - impl<'a> FromSql<'a> for ChainId { - fn from_sql( - _: &Type, - raw: &'a [u8], - ) -> Result> { - let value_str = text_from_sql(raw)?; - let value: Self = value_str.parse()?; - Ok(value) - } - - accepts!(VARCHAR); - } - - impl ToSql for ChainId { - fn to_sql( - &self, - _: &Type, - out: &mut BytesMut, - ) -> Result> { - let value_str = self.to_string(); - text_to_sql(&value_str, out); - Ok(IsNull::No) - } - - accepts!(VARCHAR, TEXT); - to_sql_checked!(); - } -} - #[cfg(test)] mod tests { use super::*;