Skip to content

Commit

Permalink
add : more test
Browse files Browse the repository at this point in the history
  • Loading branch information
PavitraAgarwal21 committed Oct 9, 2024
1 parent 6439d03 commit abbb714
Show file tree
Hide file tree
Showing 4 changed files with 436 additions and 187 deletions.
4 changes: 3 additions & 1 deletion src/base/constants/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ pub mod Errors {
pub const SELF_FOLLOWING: felt252 = 'Karst: self follow is forbidden';
pub const ALREADY_REACTED: felt252 = 'Karst: already react to post!';
pub const TOKEN_DOES_NOT_EXIST: felt252 = 'Karst: token_id does not exist!';
pub const NOT_CHANNEL_OWNER: felt252 = 'Channel: not channel owner ';
pub const NOT_CHANNEL_OWNER: felt252 = 'Channel: not channel owner';
pub const NOT_CHANNEL_MODERATOR: felt252 = 'Channel: not channel moderator';
pub const NOT_CHANNEL_MEMBER: felt252 = 'Channel: not channel member';
pub const BANNED_FROM_CHANNEL: felt252 = 'Channel: banned from channel';
pub const CHANNEL_HAS_NO_MEMBER: felt252 = 'Channel has no members';
pub const UNAUTHORIZED_ACESS: felt252 = 'Karst : Unauthorized access';
}
170 changes: 112 additions & 58 deletions src/channel/channel.cairo
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
//TODO make the channel as the componet
#[starknet::component]
pub mod ChannelComponent {
use core::clone::Clone;
use core::starknet::{
ContractAddress, get_caller_address, get_block_timestamp,
storage::{
StoragePointerWriteAccess, StoragePointerReadAccess, Map, StorageMapReadAccess,
StorageMapWriteAccess
}
ContractAddress, contract_address_const, get_caller_address, get_block_timestamp
};
use starknet::storage::{
StoragePointerReadAccess, StoragePointerWriteAccess, StoragePathEntry, Map,
StorageMapReadAccess, StorageMapWriteAccess, Vec, VecTrait, MutableVecTrait
};
use karst::interfaces::IChannel::IChannel;
use karst::base::{
constants::errors::Errors::{
NOT_CHANNEL_OWNER, NOT_CHANNEL_MODERATOR, NOT_CHANNEL_MEMBER, BANNED_FROM_CHANNEL
NOT_CHANNEL_OWNER, NOT_CHANNEL_MODERATOR, NOT_CHANNEL_MEMBER, BANNED_FROM_CHANNEL,
CHANNEL_HAS_NO_MEMBER, UNAUTHORIZED_ACESS
},
constants::types::{channelParams, channelMember}
};
#[storage]
pub struct Storage {
channels: Map<u256, channelParams>,
channel_counter: u256,
channel_members: Map<ContractAddress, channelMember>,
channel_moderators: Map<(u256, ContractAddress), bool>,
channel_members: Map<(u256, ContractAddress), channelMember>,
channel_moderators: Map<u256, Vec<ContractAddress>>,
}

#[derive(Drop, starknet::Event)]
Expand All @@ -47,20 +47,20 @@ pub mod ChannelComponent {
token_id: u256,
block_timestamp: u64,
}
// this should also contain the IChannel & IChannelNFT implmentation , waht is the self what d.

#[derive(Drop, starknet::Event)]
pub struct ChannelModAdded {
channel_id: u256,
transaction_executor: ContractAddress,
mod_address: ContractAddress,
mod_address: Array<ContractAddress>,
block_timestamp: u64,
}

#[derive(Drop, starknet::Event)]
pub struct ChannelModRemoved {
channel_id: u256,
transaction_executor: ContractAddress,
mod_address: ContractAddress,
mod_address: Array<ContractAddress>,
block_timestamp: u64,
}

Expand All @@ -84,8 +84,6 @@ pub mod ChannelComponent {
}


// mnaking the constructor to store the onwer , who can set the moderators

#[embeddable_as(KarstChannel)]
impl ChannelImpl<
TContractState, +HasComponent<TContractState>, +Drop<TContractState>
Expand Down Expand Up @@ -125,19 +123,17 @@ pub mod ChannelComponent {
/// @param channel_id: The id of the channel
fn join_channel(ref self: ComponentState<TContractState>, channel_id: u256) {
// check that i prioor not baned
let channel_member: channelMember = self.channel_members.read(get_caller_address());
let channel_member: channelMember = self
.channel_members
.read((channel_id, get_caller_address()));
assert(!channel_member.ban_status, BANNED_FROM_CHANNEL);

let mut new_channel: channelParams = self.channels.read(channel_id);
new_channel.channel_total_members += 1;
self.channels.write(channel_id, new_channel);

// calculate the total channel member add +1 is the new member id

let mut channel: channelParams = self.channels.read(channel_id);
channel.channel_total_members += 1;
self.channels.write(channel_id, channel);
self
.channel_members
.write(
get_caller_address(),
(channel_id, get_caller_address()),
channelMember {
profile: get_caller_address(),
channel_id: channel_id,
Expand Down Expand Up @@ -165,31 +161,40 @@ pub mod ChannelComponent {
/// @dev The user must be a member of the channel
fn leave_channel(ref self: ComponentState<TContractState>, channel_id: u256) {
assert(
self.channel_members.read(get_caller_address()).channel_id == channel_id,
self
.channel_members
.read((channel_id, get_caller_address()))
.channel_id == channel_id
&& self
.channel_members
.read((channel_id, get_caller_address()))
.profile == get_caller_address(),
NOT_CHANNEL_MEMBER
);
assert(
!self.channel_members.read(get_caller_address()).ban_status, BANNED_FROM_CHANNEL
!self.channel_members.read((channel_id, get_caller_address())).ban_status,
BANNED_FROM_CHANNEL
);
assert(self.channels.read(channel_id).channel_total_members > 1, 'total_member > 1');
let total_members: u256 = self.channels.read(channel_id).channel_total_members;
assert(total_members > 1, CHANNEL_HAS_NO_MEMBER);

self
.channel_members
.write(
get_caller_address(),
(channel_id, get_caller_address()),
channelMember {
profile: get_caller_address(),
// todo , what default channel id should set max but not optimize to store
profile: contract_address_const::<0>(),
// todo , what default channel id should . as 0 can be channel_id
channel_id: 10000000,
total_publications: 0,
channel_token_id: 0,
ban_status: false,
}
);

let mut new_channel: channelParams = self.channels.read(channel_id);
new_channel.channel_total_members -= 1;
self.channels.write(channel_id, new_channel);
let mut channel: channelParams = self.channels.read(channel_id);
channel.channel_total_members -= 1;
self.channels.write(channel_id, channel);

//TODO Delete the mapping at the caller address
//TODO : burn the NFT
Expand All @@ -215,28 +220,40 @@ pub mod ChannelComponent {
let channel_member: channelParams = self.channels.read(channel_id);
assert(
channel_member.channel_owner == get_caller_address()
|| self.channel_moderators.read((channel_id, get_caller_address())),
NOT_CHANNEL_MODERATOR
|| self.is_channel_mod(get_caller_address(), channel_id),
UNAUTHORIZED_ACESS
);
let mut new_channel: channelParams = self.channels.read(channel_id);
new_channel.channel_metadata_uri = metadata_uri;
self.channels.write(channel_id, new_channel);
let mut channel: channelParams = self.channels.read(channel_id);
channel.channel_metadata_uri = metadata_uri;
self.channels.write(channel_id, channel);
}


/// @notice Add a moderator to the channel
/// @param channel_id: The id of the channel
/// @param moderator: The address of the moderator
/// @param Array<moderator>: The address of the moderator
/// dev only primary moderator/owner can add the moderators
fn add_channel_mods(
ref self: ComponentState<TContractState>, channel_id: u256, moderator: ContractAddress
ref self: ComponentState<TContractState>,
channel_id: u256,
moderator: Array<ContractAddress>
) {
assert(
self.channels.read(channel_id).channel_owner == get_caller_address(),
NOT_CHANNEL_OWNER
);

self.channel_moderators.write((channel_id, moderator), true);
for i in 0
..moderator
.len() {
if (!self.is_channel_mod(*moderator.at(i), channel_id)) {
self
.channel_moderators
.entry(channel_id)
.append()
.write(*moderator.at(i));
}
};
self
.emit(
ChannelModAdded {
Expand All @@ -254,15 +271,35 @@ pub mod ChannelComponent {
/// @param moderator: The address of the moderator
/// dev only primary moderator/owner can remove the moderators
fn remove_channel_mods(
ref self: ComponentState<TContractState>, channel_id: u256, moderator: ContractAddress
ref self: ComponentState<TContractState>,
channel_id: u256,
moderator: Array<ContractAddress>
) {
// let channel_member: channelParams = self.channels.read(channel_id);
assert(
self.channels.read(channel_id).channel_owner == get_caller_address(),
NOT_CHANNEL_OWNER
);

self.channel_moderators.write((channel_id, moderator), false);
for i in 0
..moderator
.len() {
if (self.is_channel_mod(*moderator.at(i), channel_id)) {
let mut channe_moderators = self.channel_moderators.entry(channel_id);
for j in 0
..channe_moderators
.len() {
if (channe_moderators.at(j).read() == *moderator.at(i)) {
// todo zero address set
channe_moderators
.at(j)
.write(contract_address_const::<0>());
}
};
}
};

// remove at the index thus making the best thing which i can made is the person who i
// can make the best place to make the system which is todo
// first know the element and then remove the function and delete the person

self
.emit(
Expand All @@ -275,7 +312,6 @@ pub mod ChannelComponent {
)
}


/// @notice Set the censorship status of the channel
/// @param channel_id: The id of the channel
fn set_channel_censorship_status(
Expand All @@ -286,9 +322,9 @@ pub mod ChannelComponent {
self.channels.read(channel_id).channel_owner == get_caller_address(),
NOT_CHANNEL_OWNER
);
let mut new_channel: channelParams = self.channels.read(channel_id);
new_channel.channel_censorship = censorship_status;
self.channels.write(channel_id, new_channel);
let mut channel: channelParams = self.channels.read(channel_id);
channel.channel_censorship = censorship_status;
self.channels.write(channel_id, channel);
}


Expand All @@ -305,12 +341,21 @@ pub mod ChannelComponent {
// let channel_member: channelParams = self.channels.read(channel_id);
assert(
self.channels.read(channel_id).channel_owner == get_caller_address()
|| self.channel_moderators.read((channel_id, get_caller_address())),
NOT_CHANNEL_MODERATOR
|| self.is_channel_mod(get_caller_address(), channel_id),
UNAUTHORIZED_ACESS
);
// check that channel exits and the profile is a member of the channel
assert(
self.channel_members.read((channel_id, profile)).profile == profile
&& self.channel_members.read((channel_id, profile)).channel_id == channel_id,
NOT_CHANNEL_MEMBER
);
let mut new_channel_member: channelMember = self.channel_members.read(profile);
new_channel_member.ban_status = ban_status;
self.channel_members.write(profile, new_channel_member);

let mut channel_member: channelMember = self
.channel_members
.read((channel_id, profile));
channel_member.ban_status = ban_status;
self.channel_members.write((channel_id, profile), channel_member);

self
.emit(
Expand Down Expand Up @@ -347,7 +392,7 @@ pub mod ChannelComponent {
fn is_channel_member(
self: @ComponentState<TContractState>, profile: ContractAddress, channel_id: u256
) -> bool {
let channel_member: channelMember = self.channel_members.read(profile);
let channel_member: channelMember = self.channel_members.read((channel_id, profile));
if (channel_member.channel_id == channel_id) {
true
} else {
Expand All @@ -368,7 +413,15 @@ pub mod ChannelComponent {
fn is_channel_mod(
self: @ComponentState<TContractState>, profile: ContractAddress, channel_id: u256
) -> bool {
self.channel_moderators.read((channel_id, profile))
let mut dd = self.channel_moderators.entry(channel_id);
let mut flag: bool = false;
for i in 0..dd.len() {
if (dd.at(i).read() == profile) {
flag = true;
break;
}
};
flag
}

fn get_channel_censorship_status(
Expand All @@ -378,9 +431,10 @@ pub mod ChannelComponent {
channel.channel_censorship
}

///TODO introduce new storage for ban status
fn get_ban_status(self: @ComponentState<TContractState>, profile: ContractAddress) -> bool {
let channel_member: channelMember = self.channel_members.read(profile);
fn get_ban_status(
self: @ComponentState<TContractState>, profile: ContractAddress, channel_id: u256
) -> bool {
let channel_member: channelMember = self.channel_members.read((channel_id, profile));
channel_member.ban_status
}
}
Expand Down
22 changes: 6 additions & 16 deletions src/interfaces/IChannel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,21 @@ pub trait IChannel<TState> {
fn join_channel(ref self: TState, channel_id: u256);
fn leave_channel(ref self: TState, channel_id: u256);
fn set_channel_metadata_uri(ref self: TState, channel_id: u256, metadata_uri: ByteArray);
fn add_channel_mods(ref self: TState, channel_id: u256, moderator: ContractAddress);
fn remove_channel_mods(ref self: TState, channel_id: u256, moderator: ContractAddress);
fn add_channel_mods(ref self: TState, channel_id: u256, moderator: Array<ContractAddress>);
fn remove_channel_mods(ref self: TState, channel_id: u256, moderator: Array<ContractAddress>);
fn set_channel_censorship_status(ref self: TState, channel_id: u256, censorship_status: bool);
fn set_ban_status(
ref self: TState, channel_id: u256, profile: ContractAddress, ban_status: bool
);
// // *************************************************************************
// // GETTERS
// // *************************************************************************
// *************************************************************************
// GETTERS
// *************************************************************************
fn get_channel(self: @TState, channel_id: u256) -> channelParams;
fn get_channel_metadata_uri(self: @TState, channel_id: u256) -> ByteArray;
// so is the profile is channel member or not we have to say that which channel id , i think it
// will be good
fn is_channel_member(self: @TState, profile: ContractAddress, channel_id: u256) -> bool;
// what is the means by the paid channel member
// how we can calcualte the get_total_member

fn get_total_members(self: @TState, channel_id: u256) -> u256;

// we have to pass the channel id how is the ch
fn is_channel_mod(self: @TState, profile: ContractAddress, channel_id: u256) -> bool;

// this one is easy
fn get_channel_censorship_status(self: @TState, channel_id: u256) -> bool;

fn get_ban_status(self: @TState, profile: ContractAddress) -> bool;
fn get_ban_status(self: @TState, profile: ContractAddress, channel_id: u256) -> bool;
}

Loading

0 comments on commit abbb714

Please sign in to comment.