From 5ff2d198377efc5dff577609ad923e3a170815df Mon Sep 17 00:00:00 2001 From: silverpill Date: Thu, 24 Nov 2022 23:44:53 +0000 Subject: [PATCH] Add expires_at field to Invoice object Also increase timeout to 3 hours https://codeberg.org/silverpill/mitra/issues/23 --- docs/openapi.yaml | 16 ++++++++++------ src/mastodon_api/subscriptions/types.rs | 11 ++++++++++- src/monero/subscriptions.rs | 4 ++-- src/utils/caip2.rs | 5 +++++ 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/docs/openapi.yaml b/docs/openapi.yaml index d6774bd..84f34ec 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -928,7 +928,7 @@ paths: expires_at: description: The date when subscription expires. type: string - format: dateTime + format: date-time 404: description: Subscription not found /api/v1/subscriptions/invoices: @@ -1207,7 +1207,7 @@ components: verified_at: description: Timestamp of when the server verified the field value. type: string - format: dateTime + format: date-time Instance: type: object properties: @@ -1318,6 +1318,10 @@ components: - paid - forwarded - timeout + expires_at: + description: The date when invoice times out. + type: string + format: date-time Mention: type: object properties: @@ -1356,7 +1360,7 @@ components: created_at: description: The timestamp of the notification. type: string - format: dateTime + format: date-time account: $ref: '#/components/schemas/Account' status: @@ -1421,11 +1425,11 @@ components: created_at: description: The date when this post was created. type: string - format: dateTime + format: date-time edited_at: description: The date when this post was edited. type: string - format: dateTime + format: date-time nullable: true account: description: The profile that authored this post. @@ -1483,7 +1487,7 @@ components: expires_at: description: The date when subscription expires. type: string - format: dateTime + format: date-time SubscriptionOption: type: object properties: diff --git a/src/mastodon_api/subscriptions/types.rs b/src/mastodon_api/subscriptions/types.rs index c10a0f3..3230490 100644 --- a/src/mastodon_api/subscriptions/types.rs +++ b/src/mastodon_api/subscriptions/types.rs @@ -1,9 +1,10 @@ -use chrono::{DateTime, Utc}; +use chrono::{DateTime, Duration, Utc}; use serde::{Deserialize, Serialize}; use uuid::Uuid; use crate::models::invoices::types::{DbInvoice, InvoiceStatus}; use crate::models::profiles::types::PaymentOption; +use crate::monero::subscriptions::MONERO_INVOICE_TIMEOUT; #[derive(Deserialize)] pub struct InvoiceData { @@ -20,6 +21,7 @@ pub struct Invoice { pub payment_address: String, pub amount: i64, pub status: String, + pub expires_at: DateTime, } impl From for Invoice { @@ -30,6 +32,12 @@ impl From for Invoice { InvoiceStatus::Forwarded => "forwarded", InvoiceStatus::Timeout => "timeout", }; + let expires_at = if value.chain_id.is_monero() { + value.created_at + Duration::seconds(MONERO_INVOICE_TIMEOUT) + } else { + // Epoch 0 + Default::default() + }; Self { id: value.id, sender_id: value.sender_id, @@ -37,6 +45,7 @@ impl From for Invoice { payment_address: value.payment_address, amount: value.amount, status: status.to_string(), + expires_at, } } } diff --git a/src/monero/subscriptions.rs b/src/monero/subscriptions.rs index a5a2100..8761026 100644 --- a/src/monero/subscriptions.rs +++ b/src/monero/subscriptions.rs @@ -33,7 +33,7 @@ use super::wallet::{ MoneroError, }; -const INVOICE_TIMEOUT: i64 = 30 * 60; // 30 minutes +pub const MONERO_INVOICE_TIMEOUT: i64 = 3 * 60 * 60; // 3 hours pub async fn check_monero_subscriptions( instance: &Instance, @@ -57,7 +57,7 @@ pub async fn check_monero_subscriptions( ).await?; for invoice in open_invoices { let invoice_age = Utc::now() - invoice.created_at; - if invoice_age.num_seconds() >= INVOICE_TIMEOUT { + if invoice_age.num_seconds() >= MONERO_INVOICE_TIMEOUT { set_invoice_status( db_client, &invoice.id, diff --git a/src/utils/caip2.rs b/src/utils/caip2.rs index b044123..ee40fdb 100644 --- a/src/utils/caip2.rs +++ b/src/utils/caip2.rs @@ -13,6 +13,7 @@ use serde::{ const CAIP2_RE: &str = r"(?P[-a-z0-9]{3,8}):(?P[-a-zA-Z0-9]{1,32})"; const CAIP2_ETHEREUM_NAMESPACE: &str = "eip155"; +const CAIP2_MONERO_NAMESPACE: &str = "monero"; // unregistered namespace const ETHEREUM_MAINNET_ID: i32 = 1; const ETHEREUM_DEVNET_ID: i32 = 31337; @@ -40,6 +41,10 @@ impl ChainId { pub fn is_ethereum(&self) -> bool { self.namespace == CAIP2_ETHEREUM_NAMESPACE } + + pub fn is_monero(&self) -> bool { + self.namespace == CAIP2_MONERO_NAMESPACE + } } #[derive(thiserror::Error, Debug)]