Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channel Ops #8

Merged
merged 1 commit into from
Aug 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ pub trait IPushComm<TContractState> {
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
Expand All @@ -27,6 +39,8 @@ pub trait IPushComm<TContractState> {

#[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;
Expand Down Expand Up @@ -89,6 +103,8 @@ pub mod PushComm {
UnSubscribe: UnSubscribe,
AddDelegate: AddDelegate,
RemoveDelegate: RemoveDelegate,
SendNotification: SendNotification,
UserNotifcationSettingsAdded: UserNotifcationSettingsAdded
}

#[derive(Drop, starknet::Event)]
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
}


Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Loading