diff --git a/Cargo.lock b/Cargo.lock index 30f6e13e..ca08f79f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7647,7 +7647,7 @@ checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" [[package]] name = "subvt-app-service" -version = "0.17.30" +version = "0.17.31" dependencies = [ "actix-http", "actix-rt", @@ -7676,7 +7676,7 @@ dependencies = [ [[package]] name = "subvt-block-processor" -version = "0.17.30" +version = "0.17.31" dependencies = [ "anyhow", "async-lock", @@ -7702,7 +7702,7 @@ dependencies = [ [[package]] name = "subvt-config" -version = "0.17.30" +version = "0.17.31" dependencies = [ "config", "serde", @@ -7710,7 +7710,7 @@ dependencies = [ [[package]] name = "subvt-governance" -version = "0.17.30" +version = "0.17.31" dependencies = [ "anyhow", "lazy_static", @@ -7724,7 +7724,7 @@ dependencies = [ [[package]] name = "subvt-logging" -version = "0.17.30" +version = "0.17.31" dependencies = [ "env_logger", "log", @@ -7733,7 +7733,7 @@ dependencies = [ [[package]] name = "subvt-metrics" -version = "0.17.30" +version = "0.17.31" dependencies = [ "env_logger", "log", @@ -7747,7 +7747,7 @@ dependencies = [ [[package]] name = "subvt-network-status-server" -version = "0.17.30" +version = "0.17.31" dependencies = [ "anyhow", "async-trait", @@ -7772,7 +7772,7 @@ dependencies = [ [[package]] name = "subvt-network-status-updater" -version = "0.17.30" +version = "0.17.31" dependencies = [ "anyhow", "async-trait", @@ -7794,7 +7794,7 @@ dependencies = [ [[package]] name = "subvt-nft" -version = "0.17.30" +version = "0.17.31" dependencies = [ "anyhow", "lazy_static", @@ -7806,7 +7806,7 @@ dependencies = [ [[package]] name = "subvt-notification-generator" -version = "0.17.30" +version = "0.17.31" dependencies = [ "anyhow", "async-lock", @@ -7833,7 +7833,7 @@ dependencies = [ [[package]] name = "subvt-notification-processor" -version = "0.17.30" +version = "0.17.31" dependencies = [ "a2", "anyhow", @@ -7868,7 +7868,7 @@ dependencies = [ [[package]] name = "subvt-onekv-updater" -version = "0.17.30" +version = "0.17.31" dependencies = [ "anyhow", "async-trait", @@ -7890,7 +7890,7 @@ dependencies = [ [[package]] name = "subvt-persistence" -version = "0.17.30" +version = "0.17.31" dependencies = [ "anyhow", "chrono", @@ -7909,7 +7909,7 @@ dependencies = [ [[package]] name = "subvt-plotter" -version = "0.17.30" +version = "0.17.31" dependencies = [ "anyhow", "chrono", @@ -7932,7 +7932,7 @@ dependencies = [ [[package]] name = "subvt-proc-macro" -version = "0.17.30" +version = "0.17.31" dependencies = [ "proc-macro2", "quote", @@ -7941,7 +7941,7 @@ dependencies = [ [[package]] name = "subvt-referendum-updater" -version = "0.17.30" +version = "0.17.31" dependencies = [ "anyhow", "async-trait", @@ -7965,7 +7965,7 @@ dependencies = [ [[package]] name = "subvt-report-service" -version = "0.17.30" +version = "0.17.31" dependencies = [ "actix-web", "anyhow", @@ -7991,7 +7991,7 @@ dependencies = [ [[package]] name = "subvt-service-common" -version = "0.17.30" +version = "0.17.31" dependencies = [ "actix-web", "anyhow", @@ -8008,7 +8008,7 @@ dependencies = [ [[package]] name = "subvt-substrate-client" -version = "0.17.30" +version = "0.17.31" dependencies = [ "anyhow", "async-lock", @@ -8037,7 +8037,7 @@ dependencies = [ [[package]] name = "subvt-telegram-bot" -version = "0.17.30" +version = "0.17.31" dependencies = [ "anyhow", "async-recursion", @@ -8075,7 +8075,7 @@ dependencies = [ [[package]] name = "subvt-telemetry-processor" -version = "0.17.30" +version = "0.17.31" dependencies = [ "anyhow", "async-lock", @@ -8099,7 +8099,7 @@ dependencies = [ [[package]] name = "subvt-types" -version = "0.17.30" +version = "0.17.31" dependencies = [ "anyhow", "chrono", @@ -8140,7 +8140,7 @@ dependencies = [ [[package]] name = "subvt-utility" -version = "0.17.30" +version = "0.17.31" dependencies = [ "anyhow", "hex", @@ -8150,7 +8150,7 @@ dependencies = [ [[package]] name = "subvt-validator-details-server" -version = "0.17.30" +version = "0.17.31" dependencies = [ "anyhow", "async-trait", @@ -8174,7 +8174,7 @@ dependencies = [ [[package]] name = "subvt-validator-list-server" -version = "0.17.30" +version = "0.17.31" dependencies = [ "anyhow", "async-trait", @@ -8201,7 +8201,7 @@ dependencies = [ [[package]] name = "subvt-validator-list-updater" -version = "0.17.30" +version = "0.17.31" dependencies = [ "anyhow", "async-lock", diff --git a/subvt-app-service/Cargo.toml b/subvt-app-service/Cargo.toml index b5d7ebd9..8c096d8a 100644 --- a/subvt-app-service/Cargo.toml +++ b/subvt-app-service/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-app-service" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-app-service/src/lib.rs b/subvt-app-service/src/lib.rs index 073396ad..8765079f 100644 --- a/subvt-app-service/src/lib.rs +++ b/subvt-app-service/src/lib.rs @@ -135,7 +135,23 @@ async fn add_user_notification_channel( HttpResponse::NotFound().json(ServiceError::from("Notification channel not found.")) ); } - // delete existing channels with the same code + // validate input + if input.target.is_empty() { + return Ok( + HttpResponse::BadRequest().json(ServiceError::from("Invalid notification target.")) + ); + } + // if channel exists, just return it + let user_notification_channels = state + .postgres + .get_user_notification_channels(auth.id) + .await?; + for channel in user_notification_channels.iter() { + if channel.channel == input.channel && channel.target == input.target { + return Ok(HttpResponse::Ok().json(input)); + } + } + // delete existing channels with the same code, possibly for other users let deleted_channel_count = state .postgres .delete_existing_notification_channels_with_code( @@ -148,21 +164,6 @@ async fn add_user_notification_channel( deleted_channel_count, input.channel.to_string().as_str(), ); - if state - .postgres - .user_notification_channel_target_exists(&input) - .await? - { - return Ok( - HttpResponse::Conflict().json(ServiceError::from("This target exists for the user.")) - ); - } - // validate input - if input.target.is_empty() { - return Ok( - HttpResponse::BadRequest().json(ServiceError::from("Invalid notification target.")) - ); - } input.id = state .postgres .save_user_notification_channel(&input) @@ -273,6 +274,25 @@ async fn create_default_user_notification_rules( ) -> ResultResponse { let mut channel_id_set = HashSet::default(); channel_id_set.insert(input.user_notification_channel_id); + if state.postgres.user_has_created_rules(auth.id).await? { + let rules = state.postgres.get_user_notification_rules(auth.id).await?; + log::info!( + "User {} has already created rules. Add notification channel to {} rules if not added already.", + auth.id, + rules.len() + ); + for rule in rules.iter() { + state + .postgres + .add_user_notification_channel_to_rule( + input.user_notification_channel_id as i32, + rule.id as i32, + ) + .await?; + } + return Ok(HttpResponse::NoContent().finish()); + } + log::info!("Create default notification rules for user {}.", auth.id); for rule in subvt_types::app::notification::rules::DEFAULT_RULES.iter() { state .postgres diff --git a/subvt-block-processor/Cargo.toml b/subvt-block-processor/Cargo.toml index 3e98212f..a3457cec 100644 --- a/subvt-block-processor/Cargo.toml +++ b/subvt-block-processor/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-block-processor" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-config/Cargo.toml b/subvt-config/Cargo.toml index b3f47185..fe965cf7 100644 --- a/subvt-config/Cargo.toml +++ b/subvt-config/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-config" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-governance/Cargo.toml b/subvt-governance/Cargo.toml index 6d29dbe0..ff2dbe35 100644 --- a/subvt-governance/Cargo.toml +++ b/subvt-governance/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-governance" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-logging/Cargo.toml b/subvt-logging/Cargo.toml index c93ec5d5..d788965c 100644 --- a/subvt-logging/Cargo.toml +++ b/subvt-logging/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-logging" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-metrics/Cargo.toml b/subvt-metrics/Cargo.toml index f7bc46d5..0782d9f0 100644 --- a/subvt-metrics/Cargo.toml +++ b/subvt-metrics/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-metrics" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-network-status-server/Cargo.toml b/subvt-network-status-server/Cargo.toml index 826bf0ba..7afcd180 100644 --- a/subvt-network-status-server/Cargo.toml +++ b/subvt-network-status-server/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-network-status-server" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-network-status-updater/Cargo.toml b/subvt-network-status-updater/Cargo.toml index 966f7f3a..59218acb 100644 --- a/subvt-network-status-updater/Cargo.toml +++ b/subvt-network-status-updater/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-network-status-updater" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-nft/Cargo.toml b/subvt-nft/Cargo.toml index 758e99c3..9ce9ddfd 100644 --- a/subvt-nft/Cargo.toml +++ b/subvt-nft/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-nft" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-notification-generator/Cargo.toml b/subvt-notification-generator/Cargo.toml index 8a791a97..3c65b96a 100644 --- a/subvt-notification-generator/Cargo.toml +++ b/subvt-notification-generator/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-notification-generator" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-notification-processor/Cargo.toml b/subvt-notification-processor/Cargo.toml index eb79f124..6fd25155 100644 --- a/subvt-notification-processor/Cargo.toml +++ b/subvt-notification-processor/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-notification-processor" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-onekv-updater/Cargo.toml b/subvt-onekv-updater/Cargo.toml index 207288cf..ae375460 100644 --- a/subvt-onekv-updater/Cargo.toml +++ b/subvt-onekv-updater/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-onekv-updater" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-persistence/Cargo.toml b/subvt-persistence/Cargo.toml index ab457fb7..da3d7f80 100644 --- a/subvt-persistence/Cargo.toml +++ b/subvt-persistence/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-persistence" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-persistence/src/postgres/app/notification_channel.rs b/subvt-persistence/src/postgres/app/notification_channel.rs index 3cab32e7..17f5d207 100644 --- a/subvt-persistence/src/postgres/app/notification_channel.rs +++ b/subvt-persistence/src/postgres/app/notification_channel.rs @@ -31,4 +31,23 @@ impl PostgreSQLAppStorage { .await?; Ok(record_count.0 > 0) } + + pub async fn add_user_notification_channel_to_rule( + &self, + user_notification_channel_id: i32, + user_notification_rule_id: i32, + ) -> anyhow::Result<()> { + sqlx::query( + r#" + INSERT INTO app_user_notification_rule_channel (user_notification_rule_id, user_notification_channel_id) + VALUES ($1, $2) + ON CONFLICT(user_notification_rule_id, user_notification_channel_id) DO NOTHING + "#, + ) + .bind(user_notification_rule_id) + .bind(user_notification_channel_id) + .execute(&self.connection_pool) + .await?; + Ok(()) + } } diff --git a/subvt-persistence/src/postgres/app/user.rs b/subvt-persistence/src/postgres/app/user.rs index b5887ee7..73d6b4b2 100644 --- a/subvt-persistence/src/postgres/app/user.rs +++ b/subvt-persistence/src/postgres/app/user.rs @@ -507,6 +507,20 @@ impl PostgreSQLAppStorage { })) } + pub async fn user_has_created_rules(&self, user_id: u32) -> anyhow::Result { + let count: (i32,) = sqlx::query_as( + r#" + SELECT COUNT(DISTINCT id) + FROM app_user_notification_rule + WHERE user_id = $1 + "#, + ) + .bind(user_id as i32) + .fetch_one(&self.connection_pool) + .await?; + Ok(count.0 > 0) + } + pub async fn get_user_notification_rules( &self, user_id: u32, diff --git a/subvt-plotter/Cargo.toml b/subvt-plotter/Cargo.toml index cffa1d8a..d3bcd244 100644 --- a/subvt-plotter/Cargo.toml +++ b/subvt-plotter/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-plotter" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-proc-macro/Cargo.toml b/subvt-proc-macro/Cargo.toml index 6ad3ed07..347b3abd 100644 --- a/subvt-proc-macro/Cargo.toml +++ b/subvt-proc-macro/Cargo.toml @@ -3,7 +3,7 @@ proc-macro = true [package] name = "subvt-proc-macro" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-referendum-updater/Cargo.toml b/subvt-referendum-updater/Cargo.toml index 8132bbad..3cf7dfc1 100644 --- a/subvt-referendum-updater/Cargo.toml +++ b/subvt-referendum-updater/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-referendum-updater" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-report-service/Cargo.toml b/subvt-report-service/Cargo.toml index c737c0fa..c0f40bfc 100644 --- a/subvt-report-service/Cargo.toml +++ b/subvt-report-service/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-report-service" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-service-common/Cargo.toml b/subvt-service-common/Cargo.toml index c2e0bcaf..2d6217a2 100644 --- a/subvt-service-common/Cargo.toml +++ b/subvt-service-common/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-service-common" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-substrate-client/Cargo.toml b/subvt-substrate-client/Cargo.toml index 8ae2de8f..819368f2 100644 --- a/subvt-substrate-client/Cargo.toml +++ b/subvt-substrate-client/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-substrate-client" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-telegram-bot/Cargo.toml b/subvt-telegram-bot/Cargo.toml index d6751802..1bf34ac1 100644 --- a/subvt-telegram-bot/Cargo.toml +++ b/subvt-telegram-bot/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-telegram-bot" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-telemetry-processor/Cargo.toml b/subvt-telemetry-processor/Cargo.toml index 05a30b09..3bf80e25 100644 --- a/subvt-telemetry-processor/Cargo.toml +++ b/subvt-telemetry-processor/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-telemetry-processor" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-types/Cargo.toml b/subvt-types/Cargo.toml index 4687e78d..91a7eaae 100644 --- a/subvt-types/Cargo.toml +++ b/subvt-types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-types" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-utility/Cargo.toml b/subvt-utility/Cargo.toml index aa0d3361..3391f7c0 100644 --- a/subvt-utility/Cargo.toml +++ b/subvt-utility/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-utility" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-validator-details-server/Cargo.toml b/subvt-validator-details-server/Cargo.toml index f66cbf29..af5acae2 100644 --- a/subvt-validator-details-server/Cargo.toml +++ b/subvt-validator-details-server/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-validator-details-server" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-validator-list-server/Cargo.toml b/subvt-validator-list-server/Cargo.toml index 75d6ec24..b9ae1ea7 100644 --- a/subvt-validator-list-server/Cargo.toml +++ b/subvt-validator-list-server/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-validator-list-server" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0" diff --git a/subvt-validator-list-updater/Cargo.toml b/subvt-validator-list-updater/Cargo.toml index 1c006c8b..942091b9 100644 --- a/subvt-validator-list-updater/Cargo.toml +++ b/subvt-validator-list-updater/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subvt-validator-list-updater" -version = "0.17.30" +version = "0.17.31" edition = "2021" rust-version = "1.67.0"