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: expires_at:
description: The date when subscription expires. description: The date when subscription expires.
type: string type: string
format: dateTime format: date-time
404: 404:
description: Subscription not found description: Subscription not found
/api/v1/subscriptions/invoices: /api/v1/subscriptions/invoices:
@ -1207,7 +1207,7 @@ components:
verified_at: verified_at:
description: Timestamp of when the server verified the field value. description: Timestamp of when the server verified the field value.
type: string type: string
format: dateTime format: date-time
Instance: Instance:
type: object type: object
properties: properties:
@ -1318,6 +1318,10 @@ components:
- paid - paid
- forwarded - forwarded
- timeout - timeout
expires_at:
description: The date when invoice times out.
type: string
format: date-time
Mention: Mention:
type: object type: object
properties: properties:
@ -1356,7 +1360,7 @@ components:
created_at: created_at:
description: The timestamp of the notification. description: The timestamp of the notification.
type: string type: string
format: dateTime format: date-time
account: account:
$ref: '#/components/schemas/Account' $ref: '#/components/schemas/Account'
status: status:
@ -1421,11 +1425,11 @@ components:
created_at: created_at:
description: The date when this post was created. description: The date when this post was created.
type: string type: string
format: dateTime format: date-time
edited_at: edited_at:
description: The date when this post was edited. description: The date when this post was edited.
type: string type: string
format: dateTime format: date-time
nullable: true nullable: true
account: account:
description: The profile that authored this post. description: The profile that authored this post.
@ -1483,7 +1487,7 @@ components:
expires_at: expires_at:
description: The date when subscription expires. description: The date when subscription expires.
type: string type: string
format: dateTime format: date-time
SubscriptionOption: SubscriptionOption:
type: object type: object
properties: properties:

View file

@ -1,9 +1,10 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use uuid::Uuid; use uuid::Uuid;
use crate::models::invoices::types::{DbInvoice, InvoiceStatus}; use crate::models::invoices::types::{DbInvoice, InvoiceStatus};
use crate::models::profiles::types::PaymentOption; use crate::models::profiles::types::PaymentOption;
use crate::monero::subscriptions::MONERO_INVOICE_TIMEOUT;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct InvoiceData { pub struct InvoiceData {
@ -20,6 +21,7 @@ pub struct Invoice {
pub payment_address: String, pub payment_address: String,
pub amount: i64, pub amount: i64,
pub status: String, pub status: String,
pub expires_at: DateTime<Utc>,
} }
impl From<DbInvoice> for Invoice { impl From<DbInvoice> for Invoice {
@ -30,6 +32,12 @@ impl From<DbInvoice> for Invoice {
InvoiceStatus::Forwarded => "forwarded", InvoiceStatus::Forwarded => "forwarded",
InvoiceStatus::Timeout => "timeout", 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 { Self {
id: value.id, id: value.id,
sender_id: value.sender_id, sender_id: value.sender_id,
@ -37,6 +45,7 @@ impl From<DbInvoice> for Invoice {
payment_address: value.payment_address, payment_address: value.payment_address,
amount: value.amount, amount: value.amount,
status: status.to_string(), status: status.to_string(),
expires_at,
} }
} }
} }

View file

@ -33,7 +33,7 @@ use super::wallet::{
MoneroError, 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( pub async fn check_monero_subscriptions(
instance: &Instance, instance: &Instance,
@ -57,7 +57,7 @@ pub async fn check_monero_subscriptions(
).await?; ).await?;
for invoice in open_invoices { for invoice in open_invoices {
let invoice_age = Utc::now() - invoice.created_at; 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( set_invoice_status(
db_client, db_client,
&invoice.id, &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_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_ETHEREUM_NAMESPACE: &str = "eip155";
const CAIP2_MONERO_NAMESPACE: &str = "monero"; // unregistered namespace
const ETHEREUM_MAINNET_ID: i32 = 1; const ETHEREUM_MAINNET_ID: i32 = 1;
const ETHEREUM_DEVNET_ID: i32 = 31337; const ETHEREUM_DEVNET_ID: i32 = 31337;
@ -40,6 +41,10 @@ impl ChainId {
pub fn is_ethereum(&self) -> bool { pub fn is_ethereum(&self) -> bool {
self.namespace == CAIP2_ETHEREUM_NAMESPACE self.namespace == CAIP2_ETHEREUM_NAMESPACE
} }
pub fn is_monero(&self) -> bool {
self.namespace == CAIP2_MONERO_NAMESPACE
}
} }
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]