Skip to content

Commit

Permalink
chore: draft for publication
Browse files Browse the repository at this point in the history
  • Loading branch information
Adegbite Ademola Kelvin authored and Adegbite Ademola Kelvin committed Jul 9, 2024
1 parent c245f4e commit 6f6ce6e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 68 deletions.
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
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
85 changes: 24 additions & 61 deletions src/publication/publication.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,26 @@ 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
// *************************************************************************
#[storage]
struct Storage {
publication: LegacyMap<(ContractAddress, u256), Publication>,
blocked_profile_address: LegacyMap<(ContractAddress, ContractAddress), bool>,
karst_hub: ContractAddress
}

Expand Down Expand Up @@ -84,14 +86,16 @@ 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 }
.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,
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,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);

Expand Down Expand Up @@ -282,7 +277,9 @@ 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 +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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
}

Expand All @@ -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<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

0 comments on commit 6f6ce6e

Please sign in to comment.