Skip to content

Commit

Permalink
Merge pull request #83 from horuslabsio/feat/refactor-pub
Browse files Browse the repository at this point in the history
Feat/refactor pub
  • Loading branch information
Darlington02 authored Jul 10, 2024
2 parents c245f4e + a6e166d commit 24140b6
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 170 deletions.
1 change: 1 addition & 0 deletions src/interfaces.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod IFollowNFT;
pub mod IPublication;
pub mod IHandle;
pub mod IHandleRegistry;

2 changes: 1 addition & 1 deletion src/interfaces/IProfile.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ pub trait IProfile<TState> {
// GETTERS
// *************************************************************************
fn get_profile_metadata(self: @TState, profile_address: ContractAddress) -> ByteArray;
fn get_profile(ref self: TState, profile_address: ContractAddress) -> Profile;
fn get_profile(self: @TState, profile_address: ContractAddress) -> Profile;
fn get_user_publication_count(self: @TState, profile_address: ContractAddress) -> u256;
}
2 changes: 1 addition & 1 deletion src/interfaces/IPublication.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub trait IKarstPublications<TContractState> {
// *************************************************************************
// EXTERNALS
// *************************************************************************
fn initializer(ref self: TContractState, hub_address: ContractAddress);
fn initialize(ref self: TContractState, hub_address: ContractAddress);
fn post(
ref self: TContractState,
contentURI: ByteArray,
Expand Down
1 change: 1 addition & 0 deletions src/mocks.cairo
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod registry;
pub mod interfaces;
1 change: 1 addition & 0 deletions src/mocks/interfaces.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod IComposable;
75 changes: 75 additions & 0 deletions src/mocks/interfaces/IComposable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use starknet::ContractAddress;
use karst::base::types::{Profile, PublicationType, Publication, MirrorParams, QuoteParams};
// *************************************************************************
// INTERFACE of KARST PROFILE
// *************************************************************************
#[starknet::interface]
pub trait IComposable<TState> {
// *************************************************************************
// EXTERNALS
// *************************************************************************
fn initializer(ref self: TState, hub_address: ContractAddress);
fn create_profile(
ref self: TState,
karstnft_contract_address: ContractAddress,
registry_hash: felt252,
implementation_hash: felt252,
salt: felt252,
recipient: ContractAddress
) -> ContractAddress;
fn set_profile_metadata_uri(
ref self: TState, profile_address: ContractAddress, metadata_uri: ByteArray
);
fn increment_publication_count(ref self: TState, profile_address: ContractAddress) -> u256;
// *************************************************************************
// GETTERS
// *************************************************************************
fn get_profile_metadata(self: @TState, profile_address: ContractAddress) -> ByteArray;
fn get_profile(self: @TState, profile_address: ContractAddress) -> Profile;
fn get_user_publication_count(self: @TState, profile_address: ContractAddress) -> u256;

// // *************************************************************************
// PUBLICATION
// *************************************************************************

// *************************************************************************
// EXTERNALS
// *************************************************************************
fn initialize(ref self: TState, hub_address: ContractAddress);
fn post(
ref self: TState,
contentURI: ByteArray,
profile_address: ContractAddress,
profile_contract_address: ContractAddress,
) -> u256;
fn comment(
ref self: TState,
profile_address: ContractAddress,
reference_pub_type: PublicationType,
content_URI: ByteArray,
pointed_profile_address: ContractAddress,
pointed_pub_id: u256,
profile_contract_address: ContractAddress,
) -> u256;
fn quote(
ref self: TState,
reference_pub_type: PublicationType,
quoteParams: QuoteParams,
profile_contract_address: ContractAddress
) -> u256;
fn mirror(
ref self: TState, mirrorParams: MirrorParams, profile_contract_address: ContractAddress
) -> u256;
// *************************************************************************
// GETTERS
// *************************************************************************
fn get_publication(
self: @TState, profile_address: ContractAddress, pub_id_assigned: u256
) -> Publication;
fn get_publication_type(
self: @TState, profile_address: ContractAddress, pub_id_assigned: u256
) -> PublicationType;
fn get_publication_content_uri(
self: @TState, profile_address: ContractAddress, pub_id: u256
) -> ByteArray;
}
15 changes: 11 additions & 4 deletions src/presets/publication.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,34 @@
mod KarstPublication {
use starknet::{ContractAddress};
use karst::publication::publication::PublicationComponent;
use karst::profile::profile::ProfileComponent;

component!(path: PublicationComponent, storage: publication, event: PublicationEvent);

component!(path: ProfileComponent, storage: profile, event: ProfileEvent);
#[abi(embed_v0)]
impl publicationImpl = PublicationComponent::KarstPublication<ContractState>;
#[abi(embed_v0)]
impl profileImpl = ProfileComponent::KarstProfile<ContractState>;

#[storage]
struct Storage {
#[substorage(v0)]
publication: PublicationComponent::Storage
publication: PublicationComponent::Storage,
#[substorage(v0)]
profile: ProfileComponent::Storage
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
PublicationEvent: PublicationComponent::Event
PublicationEvent: PublicationComponent::Event,
#[flat]
ProfileEvent: ProfileComponent::Event
}

#[constructor]
fn constructor(ref self: ContractState, hub_address: ContractAddress) {
self.publication.initializer(hub_address);
self.publication.initialize(hub_address);
}
}
2 changes: 1 addition & 1 deletion src/profile/profile.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ mod ProfileComponent {
// @notice returns the Profile struct of a profile address
// @params profile_address the targeted profile address
fn get_profile(
ref self: ComponentState<TContractState>, profile_address: ContractAddress
self: @ComponentState<TContractState>, profile_address: ContractAddress
) -> Profile {
self.profile.read(profile_address)
}
Expand Down
80 changes: 22 additions & 58 deletions src/publication/publication.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,25 @@ pub mod PublicationComponent {
// *************************************************************************
// IMPORTS
// *************************************************************************
use karst::interfaces::IProfile::IProfile;
use core::option::OptionTrait;
use starknet::{ContractAddress, get_contract_address, get_caller_address, get_block_timestamp};
use karst::interfaces::IPublication::IKarstPublications;
use karst::interfaces::IProfile::{IProfileDispatcher, IProfileDispatcherTrait};
use karst::base::errors::Errors::{NOT_PROFILE_OWNER, BLOCKED_STATUS, UNSUPPORTED_PUB_TYPE};
use karst::base::errors::Errors::{NOT_PROFILE_OWNER, UNSUPPORTED_PUB_TYPE};
use karst::base::{hubrestricted::HubRestricted::hub_only};
use karst::base::types::{
PostParams, Publication, PublicationType, ReferencePubParams, CommentParams, QuoteParams,
MirrorParams
};
use karst::profile::profile::ProfileComponent;


// *************************************************************************
// STORAGE
// *************************************************************************
#[storage]
struct Storage {
publication: LegacyMap<(ContractAddress, u256), Publication>,
blocked_profile_address: LegacyMap<(ContractAddress, ContractAddress), bool>,
karst_hub: ContractAddress
}

Expand Down Expand Up @@ -84,14 +85,17 @@ pub mod PublicationComponent {
// *************************************************************************
#[embeddable_as(KarstPublication)]
impl PublicationsImpl<
TContractState, +HasComponent<TContractState>
TContractState,
+HasComponent<TContractState>,
+Drop<TContractState>,
impl Profile: ProfileComponent::HasComponent<TContractState>
> of IKarstPublications<ComponentState<TContractState>> {
// *************************************************************************
// PUBLISHING FUNCTIONS
// *************************************************************************
/// @notice initialize publication component
/// @param hub_address address of hub contract
fn initializer(ref self: ComponentState<TContractState>, hub_address: ContractAddress) {
fn initialize(ref self: ComponentState<TContractState>, hub_address: ContractAddress) {
self.karst_hub.write(hub_address);
}

Expand All @@ -104,13 +108,12 @@ pub mod PublicationComponent {
profile_address: ContractAddress,
profile_contract_address: ContractAddress
) -> u256 {
let profile_owner = IProfileDispatcher { contract_address: profile_contract_address }
let profile_owner: ContractAddress = get_dep_component!(@self, Profile)
.get_profile(profile_address)
.profile_owner;
let mut profile_instance = get_dep_component_mut!(ref self, Profile);
let pub_id_assigned = profile_instance.increment_publication_count(profile_address);
assert(profile_owner == get_caller_address(), NOT_PROFILE_OWNER);

let pubIdAssigned = IProfileDispatcher { contract_address: profile_contract_address }
.increment_publication_count(profile_address);
let new_post = Publication {
pointed_profile_address: 0.try_into().unwrap(),
pointed_pub_id: 0,
Expand All @@ -120,8 +123,8 @@ pub mod PublicationComponent {
root_pub_id: 0
};

self.publication.write((profile_address, pubIdAssigned), new_post);
pubIdAssigned
self.publication.write((profile_address, pub_id_assigned), new_post);
pub_id_assigned
}

/// @notice performs comment action
Expand Down Expand Up @@ -171,15 +174,8 @@ pub mod PublicationComponent {
profile_contract_address: ContractAddress
) -> u256 {
self._validatePointedPub(mirrorParams.profile_address, mirrorParams.pointed_pub_id);
self
.validateNotBlocked(
mirrorParams.profile_address, mirrorParams.pointed_profile_address, false
);
let ref_mirrorParams = mirrorParams.clone();
let profileDispatcher = IProfileDispatcher {
contract_address: profile_contract_address
};
let pub_id_assigned = profileDispatcher
let pub_id_assigned = get_dep_component!(@self, Profile)
.get_user_publication_count(mirrorParams.profile_address);
let publication = self
.get_publication(mirrorParams.pointed_profile_address, mirrorParams.pointed_pub_id);
Expand Down Expand Up @@ -282,7 +278,10 @@ pub mod PublicationComponent {
// *************************************************************************
#[generate_trait]
impl InternalImpl<
TContractState, +HasComponent<TContractState>
TContractState,
+HasComponent<TContractState>,
+Drop<TContractState>,
impl Profile: ProfileComponent::HasComponent<TContractState>
> of InternalTrait<TContractState> {
/// @notice fill root of publication
/// @param profile_address the profile address creating the publication
Expand Down Expand Up @@ -331,8 +330,8 @@ pub mod PublicationComponent {
referencePubType: PublicationType,
profile_contract_address: ContractAddress
) -> (u256, ContractAddress) {
let pub_id_assigned = IProfileDispatcher { contract_address: profile_contract_address }
.increment_publication_count(profile_address);
let mut profile_instance = get_dep_component_mut!(ref self, Profile);
let pub_id_assigned = profile_instance.increment_publication_count(profile_address);
let root_profile_address = self
._fillRootOfPublicationInStorage(
profile_address,
Expand Down Expand Up @@ -369,7 +368,7 @@ pub mod PublicationComponent {
) -> u256 {
self._validatePointedPub(pointed_profile_address, pointed_pub_id);

let (pub_id_assigned, root_profile_address) = self
let (pub_id_assigned, _root_profile_address) = self
._fillRefeferencePublicationStorage(
profile_address,
content_URI,
Expand All @@ -379,9 +378,6 @@ pub mod PublicationComponent {
profile_contract_address
);

if (root_profile_address != pointed_profile_address) {
self.validateNotBlocked(profile_address, pointed_profile_address, false);
}
pub_id_assigned
}

Expand All @@ -397,38 +393,6 @@ pub mod PublicationComponent {
}
}

/// @notice fill reference publication
/// @param profile_address the blocked profile address
/// @param by_profile_address the profile address that performed the blocking
fn _blockedStatus(
ref self: ComponentState<TContractState>,
profile_address: ContractAddress,
by_profile_address: ContractAddress,
) -> bool {
self.blocked_profile_address.write((profile_address, by_profile_address), false);
let status = self.blocked_profile_address.read((profile_address, by_profile_address));
status
}

/// @notice check a profile is not blocked
/// @param profile_address the blocked profile address
/// @param by_profile_address the profile address that performed the blocking
/// unidirectional_check specifies if check should be one-way or both ways
fn validateNotBlocked(
ref self: ComponentState<TContractState>,
profile_address: ContractAddress,
by_profile_address: ContractAddress,
unidirectional_check: bool
) {
if (profile_address != by_profile_address
&& (self._blockedStatus(profile_address, by_profile_address)
|| (!unidirectional_check
&& self
._blockedStatus(
by_profile_address, profile_address
)))) { // return; ERROR
}
}

/// @notice validates pointed publication
/// @param profile_address the profile address that created the publication
Expand Down
Loading

0 comments on commit 24140b6

Please sign in to comment.