Add expires_at field to Invoice object

Also increase timeout to 3 hours

https://codeberg.org/silverpill/mitra/issues/23
This commit is contained in:
silverpill 2022-11-24 23:44:53 +00:00
parent 2a9794f8f7
commit 5ff2d19837
4 changed files with 27 additions and 9 deletions

View file

@ -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:

View file

@ -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<Utc>,
}
impl From<DbInvoice> for Invoice {
@ -30,6 +32,12 @@ impl From<DbInvoice> 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<DbInvoice> for Invoice {
payment_address: value.payment_address,
amount: value.amount,
status: status.to_string(),
expires_at,
}
}
}

View file

@ -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,

View file

@ -13,6 +13,7 @@ use serde::{
const CAIP2_RE: &str = r"(?P<namespace>[-a-z0-9]{3,8}):(?P<reference>[-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)]