From 6f6ce6e0c3ea742d3bb2df3dc418cc6a22174c29 Mon Sep 17 00:00:00 2001 From: Adegbite Ademola Kelvin Date: Tue, 9 Jul 2024 12:06:00 +0100 Subject: [PATCH] chore: draft for publication --- src/interfaces/IProfile.cairo | 2 +- src/interfaces/IPublication.cairo | 2 +- src/presets/publication.cairo | 15 ++++-- src/profile/profile.cairo | 2 +- src/publication/publication.cairo | 85 +++++++++---------------------- 5 files changed, 38 insertions(+), 68 deletions(-) diff --git a/src/interfaces/IProfile.cairo b/src/interfaces/IProfile.cairo index 88dd1bd..585be56 100644 --- a/src/interfaces/IProfile.cairo +++ b/src/interfaces/IProfile.cairo @@ -25,6 +25,6 @@ pub trait IProfile { // 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; } diff --git a/src/interfaces/IPublication.cairo b/src/interfaces/IPublication.cairo index 8ffeb41..6b2f11b 100644 --- a/src/interfaces/IPublication.cairo +++ b/src/interfaces/IPublication.cairo @@ -11,7 +11,7 @@ pub trait IKarstPublications { // ************************************************************************* // EXTERNALS // ************************************************************************* - fn initializer(ref self: TContractState, hub_address: ContractAddress); + fn initialize(ref self: TContractState, hub_address: ContractAddress); fn post( ref self: TContractState, contentURI: ByteArray, diff --git a/src/presets/publication.cairo b/src/presets/publication.cairo index 6e22116..8637ef3 100644 --- a/src/presets/publication.cairo +++ b/src/presets/publication.cairo @@ -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; + #[abi(embed_v0)] + impl profileImpl = ProfileComponent::KarstProfile; #[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); } } diff --git a/src/profile/profile.cairo b/src/profile/profile.cairo index a8734c4..d7a65c0 100644 --- a/src/profile/profile.cairo +++ b/src/profile/profile.cairo @@ -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, profile_address: ContractAddress + self: @ComponentState, profile_address: ContractAddress ) -> Profile { self.profile.read(profile_address) } diff --git a/src/publication/publication.cairo b/src/publication/publication.cairo index ed3f074..ed5dbf9 100644 --- a/src/publication/publication.cairo +++ b/src/publication/publication.cairo @@ -13,16 +13,19 @@ pub mod PublicationComponent { // ************************************************************************* // IMPORTS // ************************************************************************* - use core::option::OptionTrait; + 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::interfaces::IProfile::{IProfileDispatcher, IProfileDispatcherTrait}; + 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 @@ -30,7 +33,6 @@ pub mod PublicationComponent { #[storage] struct Storage { publication: LegacyMap<(ContractAddress, u256), Publication>, - blocked_profile_address: LegacyMap<(ContractAddress, ContractAddress), bool>, karst_hub: ContractAddress } @@ -84,14 +86,16 @@ pub mod PublicationComponent { // ************************************************************************* #[embeddable_as(KarstPublication)] impl PublicationsImpl< - TContractState, +HasComponent + TContractState, +HasComponent, + +Drop, + impl Profile: ProfileComponent::HasComponent > of IKarstPublications> { // ************************************************************************* // PUBLISHING FUNCTIONS // ************************************************************************* /// @notice initialize publication component /// @param hub_address address of hub contract - fn initializer(ref self: ComponentState, hub_address: ContractAddress) { + fn initialize(ref self: ComponentState, hub_address: ContractAddress) { self.karst_hub.write(hub_address); } @@ -104,13 +108,12 @@ pub mod PublicationComponent { profile_address: ContractAddress, profile_contract_address: ContractAddress ) -> u256 { - let profile_owner = IProfileDispatcher { contract_address: profile_contract_address } - .get_profile(profile_address) - .profile_owner; + let profile_owner = get_dep_component!(@self,Profile).get_profile(profile_address).profile_owner; + let profile_address = get_dep_component!(@self,Profile).get_profile(profile_address).profile_address; + + 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, @@ -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 @@ -171,16 +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 - .get_user_publication_count(mirrorParams.profile_address); + 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); @@ -282,7 +277,9 @@ pub mod PublicationComponent { // ************************************************************************* #[generate_trait] impl InternalImpl< - TContractState, +HasComponent + TContractState, +HasComponent, + +Drop, + impl Profile: ProfileComponent::HasComponent > of InternalTrait { /// @notice fill root of publication /// @param profile_address the profile address creating the publication @@ -331,8 +328,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, @@ -369,7 +366,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, @@ -379,9 +376,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 } @@ -397,38 +391,7 @@ 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, - 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, - 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