Send notification to sender when subscription expires

Only if sender is local.
This commit is contained in:
silverpill 2022-07-14 17:25:48 +00:00
parent 85899e020f
commit 6f247ad40d
5 changed files with 46 additions and 12 deletions

View file

@ -842,6 +842,7 @@ components:
- mention - mention
- reblog - reblog
- subscription - subscription
- subscription_expiration
example: reply example: reply
created_at: created_at:
description: The timestamp of the notification. description: The timestamp of the notification.

View file

@ -13,9 +13,15 @@ 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::notifications::queries::{
create_subscription_notification,
create_subscription_expiration_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::{
get_profile_by_id,
search_profile_by_wallet_address,
};
use crate::models::relationships::queries::unsubscribe; use crate::models::relationships::queries::unsubscribe;
use crate::models::subscriptions::queries::{ use crate::models::subscriptions::queries::{
create_subscription, create_subscription,
@ -143,6 +149,11 @@ pub async fn check_subscriptions(
&expires_at, &expires_at,
&block_date, &block_date,
).await?; ).await?;
log::info!(
"subscription updated: {0} to {1}",
subscription.sender_id,
subscription.recipient_id,
);
if expires_at > subscription.expires_at { if expires_at > subscription.expires_at {
// Subscription was extended // Subscription was extended
create_subscription_notification( create_subscription_notification(
@ -151,11 +162,6 @@ pub async fn check_subscriptions(
&subscription.recipient_id, &subscription.recipient_id,
).await?; ).await?;
}; };
log::info!(
"subscription updated: {0} to {1}",
subscription.sender_id,
subscription.recipient_id,
);
}; };
}, },
Err(DatabaseError::NotFound(_)) => { Err(DatabaseError::NotFound(_)) => {
@ -168,16 +174,16 @@ 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,
recipient.id, recipient.id,
); );
create_subscription_notification(
db_client,
&sender.id,
&recipient.id,
).await?;
}, },
Err(other_error) => return Err(other_error.into()), Err(other_error) => return Err(other_error.into()),
}; };
@ -191,6 +197,14 @@ pub async fn check_subscriptions(
subscription.sender_id, subscription.sender_id,
subscription.recipient_id, subscription.recipient_id,
); );
let sender = get_profile_by_id(db_client, &subscription.sender_id).await?;
if sender.is_local() {
create_subscription_expiration_notification(
db_client,
&subscription.recipient_id,
&subscription.sender_id,
).await?;
};
}; };
if sync_state.update(&contract.address(), to_block) { if sync_state.update(&contract.address(), to_block) {

View file

@ -47,6 +47,8 @@ impl ApiNotification {
EventType::Mention => "mention", EventType::Mention => "mention",
EventType::Repost => "reblog", EventType::Repost => "reblog",
EventType::Subscription => "subscription", EventType::Subscription => "subscription",
EventType::SubscriptionStart => "", // not supported
EventType::SubscriptionExpiration => "subscription_expiration",
}; };
Self { Self {
id: notification.id.to_string(), id: notification.id.to_string(),

View file

@ -104,6 +104,17 @@ pub async fn create_subscription_notification(
).await ).await
} }
pub async fn create_subscription_expiration_notification(
db_client: &impl GenericClient,
sender_id: &Uuid,
recipient_id: &Uuid,
) -> Result<(), DatabaseError> {
create_notification(
db_client, sender_id, recipient_id, None,
EventType::SubscriptionExpiration,
).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

@ -20,6 +20,8 @@ pub enum EventType {
Mention, Mention,
Repost, Repost,
Subscription, Subscription,
SubscriptionStart,
SubscriptionExpiration,
} }
impl From<&EventType> for i16 { impl From<&EventType> for i16 {
@ -32,6 +34,8 @@ impl From<&EventType> for i16 {
EventType::Mention => 5, EventType::Mention => 5,
EventType::Repost => 6, EventType::Repost => 6,
EventType::Subscription => 7, EventType::Subscription => 7,
EventType::SubscriptionStart => panic!("not supported"),
EventType::SubscriptionExpiration => 9,
} }
} }
} }
@ -48,6 +52,8 @@ impl TryFrom<i16> for EventType {
5 => Self::Mention, 5 => Self::Mention,
6 => Self::Repost, 6 => Self::Repost,
7 => Self::Subscription, 7 => Self::Subscription,
8 => Self::SubscriptionStart,
9 => Self::SubscriptionExpiration,
_ => return Err(ConversionError), _ => return Err(ConversionError),
}; };
Ok(event_type) Ok(event_type)