Send notification when subscription payment is received

This commit is contained in:
silverpill 2022-05-28 00:44:52 +00:00
parent c2c3ff1544
commit 4ffec71c24
4 changed files with 41 additions and 12 deletions

View file

@ -13,6 +13,7 @@ use web3::{
use crate::config::BlockchainConfig; use crate::config::BlockchainConfig;
use crate::database::{Pool, get_database_client}; use crate::database::{Pool, get_database_client};
use crate::errors::{ConversionError, DatabaseError}; use crate::errors::{ConversionError, DatabaseError};
use crate::models::notifications::queries::create_subscription_notification;
use crate::models::profiles::currencies::Currency; use crate::models::profiles::currencies::Currency;
use crate::models::profiles::queries::search_profile_by_wallet_address; use crate::models::profiles::queries::search_profile_by_wallet_address;
use crate::models::relationships::queries::unsubscribe; use crate::models::relationships::queries::unsubscribe;
@ -94,6 +95,14 @@ pub async fn check_subscriptions(
&expires_at, &expires_at,
&block_date, &block_date,
).await?; ).await?;
if expires_at > subscription.expires_at {
// Subscription was extended
create_subscription_notification(
db_client,
&subscription.sender_id,
&subscription.recipient_id,
).await?;
};
log::info!( log::info!(
"subscription updated: {0} to {1}", "subscription updated: {0} to {1}",
subscription.sender_id, subscription.sender_id,
@ -134,6 +143,11 @@ pub async fn check_subscriptions(
&expires_at, &expires_at,
&block_date, &block_date,
).await?; ).await?;
create_subscription_notification(
db_client,
&sender.id,
&recipient.id,
).await?;
log::info!( log::info!(
"subscription created: {0} to {1}", "subscription created: {0} to {1}",
sender.id, sender.id,

View file

@ -46,6 +46,7 @@ impl ApiNotification {
EventType::Reaction => "favourite", EventType::Reaction => "favourite",
EventType::Mention => "mention", EventType::Mention => "mention",
EventType::Repost => "reblog", EventType::Repost => "reblog",
EventType::Subscription => "subscription",
}; };
Self { Self {
id: notification.id.to_string(), id: notification.id.to_string(),

View file

@ -93,6 +93,17 @@ pub async fn create_repost_notification(
).await ).await
} }
pub async fn create_subscription_notification(
db_client: &impl GenericClient,
sender_id: &Uuid,
recipient_id: &Uuid,
) -> Result<(), DatabaseError> {
create_notification(
db_client, sender_id, recipient_id, None,
EventType::Subscription,
).await
}
pub async fn get_notifications( pub async fn get_notifications(
db_client: &impl GenericClient, db_client: &impl GenericClient,
recipient_id: &Uuid, recipient_id: &Uuid,

View file

@ -11,18 +11,6 @@ use crate::models::attachments::types::DbMediaAttachment;
use crate::models::posts::types::{DbPost, Post}; use crate::models::posts::types::{DbPost, Post};
use crate::models::profiles::types::DbActorProfile; use crate::models::profiles::types::DbActorProfile;
#[allow(dead_code)]
#[derive(FromSql)]
#[postgres(name = "notification")]
struct DbNotification {
id: i32,
sender_id: Uuid,
recipient_id: Uuid,
post_id: Option<Uuid>,
event_type: EventType,
created_at: DateTime<Utc>,
}
#[derive(Debug)] #[derive(Debug)]
pub enum EventType { pub enum EventType {
Follow, Follow,
@ -31,6 +19,7 @@ pub enum EventType {
Reaction, Reaction,
Mention, Mention,
Repost, Repost,
Subscription,
} }
impl From<&EventType> for i16 { impl From<&EventType> for i16 {
@ -42,6 +31,7 @@ impl From<&EventType> for i16 {
EventType::Reaction => 4, EventType::Reaction => 4,
EventType::Mention => 5, EventType::Mention => 5,
EventType::Repost => 6, EventType::Repost => 6,
EventType::Subscription => 7,
} }
} }
} }
@ -57,6 +47,7 @@ impl TryFrom<i16> for EventType {
4 => Self::Reaction, 4 => Self::Reaction,
5 => Self::Mention, 5 => Self::Mention,
6 => Self::Repost, 6 => Self::Repost,
7 => Self::Subscription,
_ => return Err(ConversionError), _ => return Err(ConversionError),
}; };
Ok(event_type) Ok(event_type)
@ -66,6 +57,18 @@ impl TryFrom<i16> for EventType {
int_enum_from_sql!(EventType); int_enum_from_sql!(EventType);
int_enum_to_sql!(EventType); int_enum_to_sql!(EventType);
#[allow(dead_code)]
#[derive(FromSql)]
#[postgres(name = "notification")]
struct DbNotification {
id: i32,
sender_id: Uuid,
recipient_id: Uuid,
post_id: Option<Uuid>,
event_type: EventType,
created_at: DateTime<Utc>,
}
pub struct Notification { pub struct Notification {
pub id: i32, pub id: i32,
pub sender: DbActorProfile, pub sender: DbActorProfile,