-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge solochain's profile follows pallet to parachain's profiles (#111)
* WIP: move profile follows pallet from solochain * Update `profile-follows` to the new proc macro * Rename profile-follows to account-follows Add account-follows to runtime
- Loading branch information
Showing
10 changed files
with
339 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
[package] | ||
name = 'pallet-account-follows' | ||
version = '0.1.7' | ||
authors = ['DappForce <dappforce@pm.me>'] | ||
edition = '2021' | ||
license = 'GPL-3.0-only' | ||
homepage = 'https://subsocial.network' | ||
repository = 'https://github.com/dappforce/subsocial-parachain' | ||
description = 'Pallet to follow/unfollow accounts' | ||
keywords = ['blockchain', 'cryptocurrency', 'social-network', 'news-feed', 'marketplace'] | ||
categories = ['cryptography::cryptocurrencies'] | ||
|
||
[features] | ||
default = ['std'] | ||
std = [ | ||
'codec/std', | ||
'scale-info/std', | ||
'frame-support/std', | ||
'frame-system/std', | ||
'sp-std/std', | ||
'pallet-profiles/std', | ||
'subsocial-support/std', | ||
] | ||
|
||
[dependencies] | ||
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] } | ||
scale-info = { version = "2.1.2", default-features = false, features = ["derive"] } | ||
|
||
# Local dependencies | ||
pallet-profiles = { default-features = false, path = '../profiles' } | ||
subsocial-support = { default-features = false, path = '../support' } | ||
|
||
# Substrate dependencies | ||
frame-support = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.24', default-features = false } | ||
frame-system = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.24', default-features = false } | ||
sp-std = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.24', default-features = false } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
[package] | ||
name = 'profile-follows-rpc' | ||
version = '0.7.3' | ||
authors = ['DappForce <dappforce@pm.me>'] | ||
edition = '2018' | ||
license = 'GPL-3.0-only' | ||
homepage = 'https://subsocial.network' | ||
repository = 'https://github.com/dappforce/dappforce-subsocial-node' | ||
description = 'RPC methods for the profile-follows pallet' | ||
keywords = ['blockchain', 'cryptocurrency', 'social-network', 'news-feed', 'marketplace'] | ||
categories = ['cryptography::cryptocurrencies'] | ||
|
||
[dependencies.serde] | ||
optional = true | ||
features = ['derive'] | ||
version = '1.0.119' | ||
|
||
[dependencies.codec] | ||
default-features = false | ||
features = ['derive'] | ||
package = 'parity-scale-codec' | ||
version = '2.0.0' | ||
|
||
[dependencies] | ||
jsonrpc-core = '18.0.0' | ||
jsonrpc-core-client = '18.0.0' | ||
jsonrpc-derive = '18.0.0' | ||
|
||
# Local dependencies | ||
pallet-utils = { default-features = false, path = '../../utils' } | ||
|
||
# Custom Runtime API | ||
profile-follows-runtime-api = { default-features = false, path = 'runtime-api' } | ||
|
||
# Substrate dependencies | ||
sp-api = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false } | ||
sp-blockchain = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false } | ||
sp-rpc = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false } | ||
sp-runtime = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false } | ||
|
||
[features] | ||
default = ['std'] | ||
std = [ | ||
'serde', | ||
'sp-runtime/std', | ||
'sp-api/std', | ||
'profile-follows-runtime-api/std', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
[package] | ||
name = 'profile-follows-runtime-api' | ||
version = '0.7.3' | ||
authors = ['DappForce <dappforce@pm.me>'] | ||
edition = '2018' | ||
license = 'GPL-3.0-only' | ||
homepage = 'https://subsocial.network' | ||
repository = 'https://github.com/dappforce/dappforce-subsocial-node' | ||
description = 'Runtime API definition for the profile-follows pallet' | ||
keywords = ['blockchain', 'cryptocurrency', 'social-network', 'news-feed', 'marketplace'] | ||
categories = ['cryptography::cryptocurrencies'] | ||
|
||
[dependencies.serde] | ||
optional = true | ||
features = ["derive"] | ||
version = "1.0.119" | ||
|
||
[dependencies.codec] | ||
default-features = false | ||
features = ['derive'] | ||
package = 'parity-scale-codec' | ||
version = '2.0.0' | ||
|
||
[dependencies] | ||
# Substrate dependencies | ||
sp-api = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false } | ||
sp-runtime = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false } | ||
sp-std = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false } | ||
|
||
[features] | ||
default = ['std'] | ||
std = [ | ||
'serde', | ||
'sp-api/std', | ||
'sp-std/std', | ||
'sp-runtime/std', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
use codec::Codec; | ||
use sp_std::vec::Vec; | ||
|
||
sp_api::decl_runtime_apis! { | ||
pub trait ProfileFollowsApi<AccountId> where | ||
AccountId: Codec | ||
{ | ||
fn filter_followed_accounts(account: AccountId, maybe_following: Vec<AccountId>) -> Vec<AccountId>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
pub use pallet::*; | ||
|
||
// pub mod rpc; | ||
|
||
#[frame_support::pallet] | ||
pub mod pallet { | ||
use super::*; | ||
use frame_support::pallet_prelude::*; | ||
use frame_system::pallet_prelude::*; | ||
|
||
use subsocial_support::remove_from_vec; | ||
|
||
/// The pallet's configuration trait. | ||
#[pallet::config] | ||
pub trait Config: frame_system::Config { | ||
/// The overarching event type. | ||
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>; | ||
} | ||
|
||
#[pallet::pallet] | ||
#[pallet::generate_store(pub (super) trait Store)] | ||
#[pallet::without_storage_info] | ||
pub struct Pallet<T>(_); | ||
|
||
#[pallet::storage] | ||
#[pallet::getter(fn account_followers)] | ||
pub(super) type AccountFollowers<T: Config> = | ||
StorageMap<_, Twox64Concat, T::AccountId, Vec<T::AccountId>, ValueQuery>; | ||
|
||
#[pallet::storage] | ||
#[pallet::getter(fn account_followed_by_account)] | ||
pub(super) type AccountFollowedByAccount<T: Config> = | ||
StorageMap<_, Blake2_128Concat, (T::AccountId, T::AccountId), bool, ValueQuery>; | ||
|
||
#[pallet::storage] | ||
#[pallet::getter(fn accounts_followed_by_account)] | ||
pub(super) type AccountsFollowedByAccount<T: Config> = | ||
StorageMap<_, Twox64Concat, T::AccountId, Vec<T::AccountId>, ValueQuery>; | ||
|
||
#[pallet::event] | ||
#[pallet::generate_deposit(pub (super) fn deposit_event)] | ||
pub enum Event<T: Config> { | ||
AccountFollowed { follower: T::AccountId, account: T::AccountId }, | ||
AccountUnfollowed { follower: T::AccountId, account: T::AccountId }, | ||
} | ||
|
||
#[pallet::error] | ||
pub enum Error<T> { | ||
/// Follower social account was not found by id. | ||
FollowerAccountNotFound, | ||
/// Social account that is being followed was not found by id. | ||
FollowedAccountNotFound, | ||
|
||
/// Account can not follow itself. | ||
AccountCannotFollowItself, | ||
/// Account can not unfollow itself. | ||
AccountCannotUnfollowItself, | ||
|
||
/// Account (Alice) is already a follower of another account (Bob). | ||
AlreadyAccountFollower, | ||
/// Account (Alice) is not a follower of another account (Bob). | ||
NotAccountFollower, | ||
} | ||
|
||
#[pallet::call] | ||
impl<T: Config> Pallet<T> { | ||
#[pallet::weight(10_000 + T::DbWeight::get().reads_writes(2, 3))] | ||
pub fn follow_account(origin: OriginFor<T>, account: T::AccountId) -> DispatchResult { | ||
let follower = ensure_signed(origin)?; | ||
|
||
ensure!(follower != account, Error::<T>::AccountCannotFollowItself); | ||
ensure!( | ||
!<AccountFollowedByAccount<T>>::contains_key((follower.clone(), account.clone())), | ||
Error::<T>::AlreadyAccountFollower | ||
); | ||
|
||
AccountsFollowedByAccount::<T>::mutate(follower.clone(), |ids| { | ||
ids.push(account.clone()) | ||
}); | ||
AccountFollowers::<T>::mutate(account.clone(), |ids| ids.push(follower.clone())); | ||
AccountFollowedByAccount::<T>::insert((follower.clone(), account.clone()), true); | ||
|
||
Self::deposit_event(Event::AccountFollowed { follower, account }); | ||
Ok(()) | ||
} | ||
|
||
#[pallet::weight(10_000 + T::DbWeight::get().reads_writes(2, 3))] | ||
pub fn unfollow_account(origin: OriginFor<T>, account: T::AccountId) -> DispatchResult { | ||
let follower = ensure_signed(origin)?; | ||
|
||
ensure!(follower != account, Error::<T>::AccountCannotUnfollowItself); | ||
ensure!( | ||
<AccountFollowedByAccount<T>>::contains_key((follower.clone(), account.clone())), | ||
Error::<T>::NotAccountFollower | ||
); | ||
|
||
AccountsFollowedByAccount::<T>::mutate(follower.clone(), |account_ids| { | ||
remove_from_vec(account_ids, account.clone()) | ||
}); | ||
AccountFollowers::<T>::mutate(account.clone(), |account_ids| { | ||
remove_from_vec(account_ids, follower.clone()) | ||
}); | ||
AccountFollowedByAccount::<T>::remove((follower.clone(), account.clone())); | ||
|
||
Self::deposit_event(Event::AccountUnfollowed { follower, account }); | ||
Ok(()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use sp_std::prelude::*; | ||
|
||
use crate::{Module, Config}; | ||
|
||
impl<T: Config> Module<T> { | ||
pub fn filter_followed_accounts(account: T::AccountId, maybe_following: Vec<T::AccountId>) -> Vec<T::AccountId> { | ||
maybe_following.iter() | ||
.filter(|maybe_following| Self::account_followed_by_account((&account, maybe_following))) | ||
.cloned().collect() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters