Skip to content

Commit

Permalink
positive externality post pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Jan 8, 2025
1 parent f6003be commit d784207
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ pub trait PositiveExternalityApi<BlockHash, AccountId> {
who: AccountId,
at: Option<BlockHash>,
) -> RpcResult<bool>;
#[method(name = "positiveexternality_postbyaddresslength")]
fn post_by_address_length(
&self,
user: AccountId,
at: Option<BlockHash>,
) -> RpcResult<u64>;

#[method(name = "positiveexternality_paginateposts")]
fn paginate_posts_by_address(
&self,
user: AccountId,
page: u64,
page_size: u64,
at: Option<BlockHash>,
) -> RpcResult<Option<Vec<u64>>>;


}

/// A struct that implements the `SumStorageApi`.
Expand Down Expand Up @@ -206,4 +223,47 @@ where
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?;
Ok(res)
}


fn post_by_address_length(
&self,
user: AccountId,
at: Option<Block::Hash>,
) -> RpcResult<u64> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash);

let runtime_api_result = api.post_by_address_length(at, user);

fn map_err(error: impl ToString, desc: &'static str) -> ErrorObjectOwned {
ErrorObject::owned(Error::RuntimeError.into(), desc, Some(error.to_string()))
}
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?;
Ok(res)
}

fn paginate_posts_by_address(
&self,
user: AccountId,
page: u64,
page_size: u64,
at: Option<Block::Hash>,
) -> RpcResult<Option<Vec<u64>>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash);

let runtime_api_result = api.paginate_posts_by_address(at, user, page, page_size);

fn map_err(error: impl ToString, desc: &'static str) -> ErrorObjectOwned {
ErrorObject::owned(Error::RuntimeError.into(), desc, Some(error.to_string()))
}
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?;
Ok(res)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ sp_api::decl_runtime_apis! {
fn get_commit_period_end_block(user_to_calculate: AccountId) -> Option<u32>;
fn get_vote_period_end_block(user_to_calculate: AccountId) -> Option<u32>;
fn selected_as_juror(user_to_calculate: AccountId, who: AccountId) -> bool;
fn post_by_address_length(user: AccountId) -> u64;
fn paginate_posts_by_address(user: AccountId, page: u64, page_size: u64) -> Option<Vec<u64>>;
}
}
18 changes: 18 additions & 0 deletions custom-pallets/positive-externality/src/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,22 @@ impl<T: Config> Pallet<T> {
}

// Block code end

pub fn post_by_address_length(user: T::AccountId) -> u64 {
PostByAddresss::<T>::get(user).len().try_into().unwrap()
}


pub fn paginate_posts_by_address(user: T::AccountId, page: u64, page_size: u64) -> Option<Vec<u64>> {

let data = PostByAddresss::<T>::get(user);
let start = page * page_size;
let end = start + page_size;

if start as usize >= data.len() {
None
} else {
Some(data[start as usize .. (end as usize).min(data.len() as usize)].to_vec())
}
}
}
6 changes: 3 additions & 3 deletions custom-pallets/positive-externality/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ pub mod pallet {
pub type PostById<T: Config> = StorageMap<_, Twox64Concat, PostId, Post<T>>;

#[pallet::storage]
#[pallet::getter(fn evidence)]
pub type Evidence<T: Config> =
#[pallet::getter(fn post_by_address)]
pub type PostByAddresss<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, Vec<PostId>, ValueQuery>;

#[pallet::type_value]
Expand Down Expand Up @@ -207,7 +207,7 @@ pub mod pallet {

let new_post: Post<T> = Post::new(new_post_id, creator.clone(), content.clone());

Evidence::<T>::mutate(creator, |ids| ids.push(new_post_id));
PostByAddresss::<T>::mutate(creator, |ids| ids.push(new_post_id));

PostById::insert(new_post_id, new_post);
NextPostId::<T>::mutate(|n| {
Expand Down
8 changes: 8 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,14 @@ impl_runtime_apis! {
fn selected_as_juror(user_to_calculate: AccountId, who: AccountId) -> bool {
PositiveExternality::selected_as_juror(user_to_calculate, who)
}
fn post_by_address_length(user: AccountId) -> u64 {
PositiveExternality::post_by_address_length(user)
}

fn paginate_posts_by_address(user: AccountId, page: u64, page_size: u64) -> Option<Vec<u64>>{
PositiveExternality::paginate_posts_by_address(user, page, page_size)
}

}

impl project_tips_runtime_api::ProjectTipsApi<Block, AccountId> for Runtime {
Expand Down

0 comments on commit d784207

Please sign in to comment.