Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish Mirror Function #27

Merged
merged 7 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/base/types.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub struct ReferencePubParams {
// * @param pointed_profile_id The profile address to point the mirror to.
// * @param pointed_pub_id The publication ID to point the mirror to.
// */
#[derive(Drop, Serde, starknet::Store)]
#[derive(Drop, Serde, starknet::Store, Clone)]
pub struct MirrorParams {
profile_address: ContractAddress,
metadata_URI: ByteArray,
Expand Down
7 changes: 6 additions & 1 deletion src/interfaces/IPublication.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// INTERFACE of KARST PUBLICATIONS
// *************************************************************************
use starknet::ContractAddress;
use karst::base::types::{PostParams, ReferencePubParams, PublicationType, Publication};
use karst::base::types::{
PostParams, MirrorParams, ReferencePubParams, PublicationType, Publication
};

#[starknet::interface]
pub trait IKarstPublications<T> {
Expand All @@ -28,6 +30,9 @@ pub trait IKarstPublications<T> {
// GETTERS
// *************************************************************************
fn get_publication(self: @T, user: ContractAddress, pubIdAssigned: u256) -> Publication;
fn mirror(
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
ref self: T, mirrorParams: MirrorParams, profile_contract_address: ContractAddress
) -> u256;
fn get_publication_type(
self: @T, profile_address: ContractAddress, pub_id_assigned: u256
) -> PublicationType;
Expand Down
59 changes: 54 additions & 5 deletions src/publication/publication.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use karst::base::types::{
PostParams, PublicationType, CommentParams, ReferencePubParams, Publication, MirrorParams,
QuoteParams
};
use karst::profile;
use karst::interfaces::IProfile::{IKarstProfileDispatcher, IKarstProfileDispatcherTrait};
use core::option::OptionTrait;

mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -31,7 +32,9 @@ pub trait IKarstPublications<T> {
pointed_pub_id: u256,
profile_contract_address: ContractAddress,
) -> u256;
fn mirror(ref self: T, mirrorParams: MirrorParams) -> u256;
fn mirror(
ref self: T, mirrorParams: MirrorParams, profile_contract_address: ContractAddress
) -> u256;
fn quote(ref self: T, quoteParams: QuoteParams) -> u256;
////// Getters//////
fn get_publication(self: @T, user: ContractAddress, pubIdAssigned: u256) -> Publication;
Expand All @@ -48,7 +51,7 @@ pub mod Publications {
// *************************************************************************
// IMPORTS
// *************************************************************************
use starknet::{ContractAddress, get_contract_address, get_caller_address};
use starknet::{ContractAddress, get_contract_address, get_caller_address, get_block_timestamp};
use karst::base::types::{
PostParams, Publication, PublicationType, ReferencePubParams, CommentParams, QuoteParams,
MirrorParams
Expand All @@ -75,6 +78,7 @@ pub mod Publications {
#[derive(Drop, starknet::Event)]
pub enum Event {
Post: Post,
MirrowCreated: MirrowCreated,
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
}

// *************************************************************************
Expand All @@ -89,6 +93,14 @@ pub mod Publications {
pub block_timestamp: u256,
}

#[derive(Drop, starknet::Event)]
pub struct MirrowCreated {
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
pub mirrorParams: MirrorParams,
pub publication_id: u256,
pub transaction_executor: ContractAddress,
pub block_timestamp: u64,
}


// *************************************************************************
// CONSTRUCTOR
Expand All @@ -103,7 +115,6 @@ pub mod Publications {
// *************************************************************************
// PUBLISHING FUNCTIONS
// *************************************************************************

fn post(
ref self: ContractState,
contentURI: ByteArray,
Expand Down Expand Up @@ -159,9 +170,47 @@ pub mod Publications {
// *
// * @return uint256 The created publication's pubId.
// */
fn mirror(ref self: ContractState, mirrorParams: MirrorParams) -> u256 {
fn mirror(
ref self: ContractState,
mirrorParams: MirrorParams,
profile_contract_address: ContractAddress
) -> u256 {
// logic here
0
self._validatePointedPub(mirrorParams.profile_address, mirrorParams.pointed_pub_id);
self
.validateNotBlocked(
mirrorParams.profile_address, mirrorParams.pointed_profile_address, false
);
let ref_mirrorParams = mirrorParams.clone();
// _processMirrorIfNeeded is not needed
let pubIdAssigned = IKarstProfileDispatcher {
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
contract_address: profile_contract_address
};
let pub_id_assigned = pubIdAssigned
.get_user_publication_count(mirrorParams.profile_address);
let publication = self
.get_publication(mirrorParams.pointed_profile_address, mirrorParams.pointed_pub_id);

self
._fillRefeferencePublicationStorage(
mirrorParams.profile_address,
publication.content_URI,
mirrorParams.pointed_profile_address,
mirrorParams.pointed_pub_id,
PublicationType::Mirror,
profile_contract_address,
);
self
.emit(
MirrowCreated {
mirrorParams: ref_mirrorParams,
publication_id: pub_id_assigned,
transaction_executor: mirrorParams.profile_address,
block_timestamp: get_block_timestamp(),
}
);

pub_id_assigned
}

fn quote(ref self: ContractState, quoteParams: QuoteParams) -> u256 {
Expand Down
42 changes: 41 additions & 1 deletion tests/test_publication.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use karst::publication::publication::Publications;
use karst::interfaces::IPublication::{
IKarstPublicationsDispatcher, IKarstPublicationsDispatcherTrait
};
use karst::base::types::{PostParams, ReferencePubParams, PublicationType};
use karst::base::types::{PostParams, MirrorParams, ReferencePubParams, PublicationType};

const HUB_ADDRESS: felt252 = 'HUB';
const USER_ONE: felt252 = 'BOB';
Expand Down Expand Up @@ -232,6 +232,46 @@ fn test_comment() {
);
}

#[test]
fn test_publish_mirrow() {
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
let (
_,
_,
profile_contract_address,
publication_contract_address,
_,
_,
user_one_profile_address,
user_two_profile_address,
user_one_first_post_pointed_pub_id,
) =
__setup__();
let publication_dispatcher = IKarstPublicationsDispatcher {
contract_address: publication_contract_address
};

let metadata_URI = "ipfs://QmSkDCsS32eLpcymxtn1cEn7Rc5hfefLBgfvZysddewga/";

let mirrow_params = MirrorParams {
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
profile_address: user_one_profile_address,
metadata_URI: metadata_URI,
pointed_profile_address: user_two_profile_address,
pointed_pub_id: user_one_first_post_pointed_pub_id,
};

start_prank(CheatTarget::One(publication_contract_address), USER_ONE.try_into().unwrap());
let actual_pub_id_assigned = IKarstProfileDispatcher {
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
contract_address: profile_contract_address
};

let pub_id_assigned = actual_pub_id_assigned
.get_user_publication_count(user_one_profile_address);
let pub_assign_id = publication_dispatcher.mirror(mirrow_params, profile_contract_address);

assert(pub_id_assigned == pub_assign_id, 'Invalid publication id assign');

stop_prank(CheatTarget::One(publication_contract_address),);
}

fn to_address(name: felt252) -> ContractAddress {
name.try_into().unwrap()
Expand Down
Loading