1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
use std::sync::Arc; use codec::Codec; use sp_blockchain::HeaderBackend; use sp_runtime::{generic::BlockId, traits::Block as BlockT}; use jsonrpc_core::Result; use jsonrpc_derive::rpc; use sp_api::ProvideRuntimeApi; use pallet_utils::rpc::map_rpc_error; pub use profile_follows_runtime_api::ProfileFollowsApi as ProfileFollowsRuntimeApi; #[rpc] pub trait ProfileFollowsApi<BlockHash, AccountId> { #[rpc(name = "profileFollows_filterFollowedAccounts")] fn filter_followed_accounts( &self, at: Option<BlockHash>, account: AccountId, maybe_following: Vec<AccountId>, ) -> Result<Vec<AccountId>>; } pub struct ProfileFollows<C, M> { client: Arc<C>, _marker: std::marker::PhantomData<M>, } impl<C, M> ProfileFollows<C, M> { pub fn new(client: Arc<C>) -> Self { Self { client, _marker: Default::default(), } } } impl<C, Block, AccountId> ProfileFollowsApi<<Block as BlockT>::Hash, AccountId> for ProfileFollows<C, Block> where Block: BlockT, AccountId: Codec, C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>, C::Api: ProfileFollowsRuntimeApi<Block, AccountId>, { fn filter_followed_accounts( &self, at: Option<<Block as BlockT>::Hash>, account: AccountId, maybe_following: Vec<AccountId>, ) -> Result<Vec<AccountId>> { let api = self.client.runtime_api(); let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); let runtime_api_result = api.filter_followed_accounts(&at, account, maybe_following); runtime_api_result.map_err(map_rpc_error) } }