Skip to content

Commit

Permalink
chore:fix test base on changes in the interface function
Browse files Browse the repository at this point in the history
  • Loading branch information
mubarak23 committed Oct 11, 2024
1 parent 193edc7 commit 25f49ea
Show file tree
Hide file tree
Showing 3 changed files with 371 additions and 144 deletions.
104 changes: 60 additions & 44 deletions src/community/community.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -207,66 +207,39 @@ pub mod CommunityComponent {
/// @notice adds a new user to a community
/// @param profile user who wants to join community
/// @param community_id id of community to be joined
fn join_community(
ref self: ComponentState<TContractState>, profile: ContractAddress, community_id: u256
) {
fn join_community(ref self: ComponentState<TContractState>, community_id: u256) {
let profile_caller = get_caller_address();
let community = self.communities.read(community_id);

// check user is not already a member and wasn't previously banned
let (is_community_member, _) = self.is_community_member(profile, community_id);
let is_banned = self.get_ban_status(profile, community_id);
let (is_community_member, _) = self.is_community_member(profile_caller, community_id);
let is_banned = self.get_ban_status(profile_caller, community_id);
assert(is_community_member != true, ALREADY_MEMBER);
assert(is_banned != true, BANNED_MEMBER);

self._join_community(profile, community.community_nft_address, community_id);
self._join_community(profile_caller, community.community_nft_address, community_id);
}

/// @notice removes a member from a community
/// @param profile user who wants to leave the community
/// @param community_id id of community to be left
fn leave_community(
ref self: ComponentState<TContractState>, profile: ContractAddress, community_id: u256
) {
fn leave_community(ref self: ComponentState<TContractState>, community_id: u256) {
let profile_caller = get_caller_address();
let community = self.communities.read(community_id);
let community_member_details = self.community_member.read((community_id, profile));
let community_member_details = self
.community_member
.read((community_id, profile_caller));

let (is_community_member, _) = self.is_community_member(profile, community_id);
let (is_community_member, _) = self.is_community_member(profile_caller, community_id);
assert(is_community_member == true, NOT_MEMBER);

// burn user's community token
self
._burn_community_nft(
._leave_community(
profile_caller,
community.community_nft_address,
profile,
community_id,
community_member_details.community_token_id
);

// remove member details and update storage
let updated_member_details = CommunityMember {
profile_address: contract_address_const::<0>(),
community_id: 0,
total_publications: 0,
community_token_id: 0,
ban_status: true
};
self.community_member.write((community_id, profile), updated_member_details);
let community_total_members = community.community_total_members - 1;
let updated_community = CommunityDetails {
community_total_members: community_total_members, ..community
};
self.communities.write(community_id, updated_community);

// emit event
self
.emit(
LeftCommunity {
community_id: community_id,
transaction_executor: get_caller_address(),
token_id: community_member_details.community_token_id,
profile: profile,
block_timestamp: get_block_timestamp(),
}
);
}

/// @notice set community metadata uri
Expand All @@ -285,7 +258,7 @@ pub mod CommunityComponent {
self.communities.write(community_id, updated_community);
}

// TODO: MAKE IT RECEIVE AN ARRAY OF MODERATORS

/// @notice adds a new community mod
/// @param community_id id of community to add moderator
/// @param moderator address to be added as moderator
Expand All @@ -300,7 +273,7 @@ pub mod CommunityComponent {
self._add_community_mods(community_id, community_owner, moderators);
}

// TODO: MAKE IT RECEIVE AN ARRAY OF MODERATORS

/// @notice removes a new community mod
/// @param community_id id of community to remove moderator
/// @param moderator address to be removed as moderator
Expand Down Expand Up @@ -557,14 +530,57 @@ pub mod CommunityComponent {
.emit(
JoinedCommunity {
community_id: community_id,
transaction_executor: get_caller_address(),
transaction_executor: profile,
token_id: minted_token_id,
profile: profile,
block_timestamp: get_block_timestamp(),
}
);
}

/// @notice internal function to leave a community
/// @param profile of the new community member
/// @param community_nft_classhash classhash of community NFT
/// @param community_id of community the new member is trying to join
fn _leave_community(
ref self: ComponentState<TContractState>,
profile_caller: ContractAddress,
community_nft_address: ContractAddress,
community_id: u256,
community_token_id: u256
) {
let community = self.communities.read(community_id);
// burn user's community token
self._burn_community_nft(community_nft_address, profile_caller, community_token_id);

// remove member details and update storage
let updated_member_details = CommunityMember {
profile_address: contract_address_const::<0>(),
community_id: 0,
total_publications: 0,
community_token_id: 0,
ban_status: true
};
self.community_member.write((community_id, profile_caller), updated_member_details);
let community_total_members = community.community_total_members - 1;
let updated_community = CommunityDetails {
community_total_members: community_total_members, ..community
};
self.communities.write(community_id, updated_community);

// emit event
self
.emit(
LeftCommunity {
community_id: community_id,
transaction_executor: profile_caller,
token_id: community_token_id,
profile: profile_caller,
block_timestamp: get_block_timestamp(),
}
);
}


// TODO: JOLT UPGRADE SUBSCRIPTION
/// @notice internal function to upgrade community
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/ICommunity.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub trait ICommunity<TState> {
// EXTERNALS
// *************************************************************************
fn create_comminuty(ref self: TState, community_type: CommunityType) -> u256;
fn join_community(ref self: TState, profile: ContractAddress, community_id: u256);
fn leave_community(ref self: TState, profile: ContractAddress, community_id: u256);
fn join_community(ref self: TState, community_id: u256);
fn leave_community(ref self: TState, community_id: u256);
fn set_community_metadata_uri(ref self: TState, community_id: u256, metadata_uri: ByteArray);
fn add_community_mods(ref self: TState, community_id: u256, moderators: Array<ContractAddress>);
fn remove_community_mods(
Expand Down
Loading

0 comments on commit 25f49ea

Please sign in to comment.