From e11473d82dfa9d7c636a7adc7de5a41799ad7610 Mon Sep 17 00:00:00 2001 From: eddnewgate Date: Sun, 11 Aug 2024 01:47:25 +0545 Subject: [PATCH] feat: channel ops (#8) --- src/lib.cairo | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/src/lib.cairo b/src/lib.cairo index 91ed2de..426059e 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -15,6 +15,18 @@ pub trait IPushComm { fn verify_channel_alias(ref self: TContractState, channel_address: felt252); fn add_delegate(ref self: TContractState, delegate: ContractAddress); fn remove_delegate(ref self: TContractState, delegate: ContractAddress); + fn send_notification( + ref self: TContractState, + channel: ContractAddress, + recipient: ContractAddress, + identity: ByteArray + ) -> bool; + fn change_user_channel_settings( + ref self: TContractState, + channel: ContractAddress, + notif_id: u256, + notif_settings: ByteArray + ); // User fn is_user_subscribed( self: @TContractState, channel: ContractAddress, user: ContractAddress @@ -27,6 +39,8 @@ pub trait IPushComm { #[starknet::contract] pub mod PushComm { + use core::clone::Clone; + use core::num::traits::zero::Zero; use push_comm::IPushComm; use core::starknet::event::EventEmitter; use core::starknet::storage::MutableStorageNode; @@ -89,6 +103,8 @@ pub mod PushComm { UnSubscribe: UnSubscribe, AddDelegate: AddDelegate, RemoveDelegate: RemoveDelegate, + SendNotification: SendNotification, + UserNotifcationSettingsAdded: UserNotifcationSettingsAdded } #[derive(Drop, starknet::Event)] @@ -128,6 +144,23 @@ pub mod PushComm { pub delegate: ContractAddress, } + #[derive(Drop, starknet::Event)] + pub struct SendNotification { + #[key] + pub channel: ContractAddress, + pub recipient: ContractAddress, + pub indentity: ByteArray, + } + + #[derive(Drop, starknet::Event)] + pub struct UserNotifcationSettingsAdded { + #[key] + pub channel: ContractAddress, + pub recipient: ContractAddress, + pub notif_id: u256, + pub notif_settings: ByteArray, + } + #[constructor] fn constructor( @@ -194,6 +227,40 @@ pub mod PushComm { self.users_count.write(user_count + 1); } } + + fn _check_notifi_req( + self: @ContractState, channel: ContractAddress, recipient: ContractAddress + ) -> bool { + let caller_address = get_caller_address(); + let is_admin = self.ownable.owner() == caller_address; + + if (channel.is_zero() && is_admin) + || (channel == caller_address) + || self.delegatedNotificationSenders.entry(channel).entry(caller_address).read() { + return true; + } + + false + } + + fn _send_notification( + ref self: ContractState, + channel: ContractAddress, + recipient: ContractAddress, + indentity: ByteArray + ) -> bool { + let success = self._check_notifi_req(channel, recipient); + if success { + self + .emit( + SendNotification { + channel: channel, recipient: recipient, indentity: indentity + } + ) + } + + false + } } @@ -226,6 +293,32 @@ pub mod PushComm { } } + fn change_user_channel_settings( + ref self: ContractState, + channel: ContractAddress, + notif_id: u256, + notif_settings: ByteArray + ) { + let caller_address = get_caller_address(); + assert!(self._is_user_subscribed(channel, caller_address)); + + let modified_notif_settings = format!("@{}+@{}", notif_id, notif_settings); + self + .user_to_channel_notifs + .entry(caller_address) + .write(channel, modified_notif_settings); + + self + .emit( + UserNotifcationSettingsAdded { + channel: channel, + recipient: caller_address, + notif_id: notif_id, + notif_settings: notif_settings + } + ); + } + // Admin fn complete_migration(ref self: ContractState) { @@ -271,6 +364,15 @@ pub mod PushComm { self.emit(RemoveDelegate { channel: channel, delegate: delegate }); } + fn send_notification( + ref self: ContractState, + channel: ContractAddress, + recipient: ContractAddress, + identity: ByteArray + ) -> bool { + self._send_notification(channel, recipient, identity) + } + // Infos fn set_push_governance_address(ref self: ContractState, governance_address: felt252) {