Skip to content

Commit

Permalink
Replace social account with space as profile
Browse files Browse the repository at this point in the history
  • Loading branch information
F3Joule committed Jul 13, 2022
1 parent a5655be commit 56e2bd9
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 38 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions integration-tests/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl pallet_posts::Config for TestRuntime {

impl pallet_profiles::Config for TestRuntime {
type Event = Event;
type SpacePermissionsProvider = Spaces;
}

impl pallet_reactions::Config for TestRuntime {
Expand Down
95 changes: 58 additions & 37 deletions pallets/profiles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,87 @@ pub use pallet::*;
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct SocialAccount {
pub followers_count: u32,
pub following_accounts_count: u16,
pub following_spaces_count: u16,
}
use pallet_permissions::SpacePermissions;
use subsocial_support::{traits::SpacePermissionsProvider, SpaceId, SpacePermissionsInfo};

type SpacePermissionsInfoOf<T> =
SpacePermissionsInfo<<T as frame_system::Config>::AccountId, SpacePermissions>;

/// The pallet's configuration trait.
#[pallet::config]
pub trait Config: frame_system::Config {}
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

type SpacePermissionsProvider: SpacePermissionsProvider<SpacePermissionsInfoOf<Self>>;
}

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::generate_store(pub (super) trait Store)]
pub struct Pallet<T>(_);

#[pallet::storage]
#[pallet::getter(fn social_account_by_id)]
pub type SocialAccountById<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, SocialAccount>;
#[pallet::getter(fn profile_space_by_account)]
pub type ProfileSpaceByAccount<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, SpaceId>;

#[pallet::event]
#[pallet::generate_deposit(pub (super) fn deposit_event)]
pub enum Event<T: Config> {
/// Space was successfully assigned as a profile.
SpaceAsProfileAssigned {
account: T::AccountId,
space: SpaceId,
},
}

#[pallet::error]
pub enum Error<T> {
/// Social account was not found by id.
SocialAccountNotFound,
/// Account is not a space owner.
NotSpaceOwner,
}

impl SocialAccount {
pub fn inc_followers(&mut self) {
self.followers_count = self.followers_count.saturating_add(1);
}

pub fn dec_followers(&mut self) {
self.followers_count = self.followers_count.saturating_sub(1);
}
#[pallet::call]
impl<T: Config> Pallet<T> {
// FIXME: cover with tests
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
pub fn set_space_as_profile(origin: OriginFor<T>, space_id: SpaceId) -> DispatchResult {
let sender = ensure_signed(origin)?;

pub fn inc_following_accounts(&mut self) {
self.following_accounts_count = self.following_accounts_count.saturating_add(1);
}
Self::try_set_space_as_profile(&sender, space_id)?;

pub fn dec_following_accounts(&mut self) {
self.following_accounts_count = self.following_accounts_count.saturating_sub(1);
Self::deposit_event(Event::SpaceAsProfileAssigned {
account: sender,
space: space_id,
});
Ok(())
}
}

pub fn inc_following_spaces(&mut self) {
self.following_spaces_count = self.following_spaces_count.saturating_add(1);
}
impl<T: Config> Pallet<T> {
// FIXME: cover with tests
pub fn try_set_space_as_profile(account: &T::AccountId, space_id: SpaceId) -> DispatchResult {
let space_permissions_info =
T::SpacePermissionsProvider::space_permissions_info(space_id)?;
ensure!(
&space_permissions_info.owner == account,
Error::<T>::NotSpaceOwner
);

pub fn dec_following_spaces(&mut self) {
self.following_spaces_count = self.following_spaces_count.saturating_sub(1);
<ProfileSpaceByAccount<T>>::insert(account, space_id);
Ok(())
}
}

impl<T: Config> Pallet<T> {
pub fn get_or_new_social_account(account: T::AccountId) -> SocialAccount {
Self::social_account_by_id(account).unwrap_or(SocialAccount {
followers_count: 0,
following_accounts_count: 0,
following_spaces_count: 0,
})
// FIXME: cover with tests
pub fn unset_space_as_profile(account: &T::AccountId, space_id: SpaceId) {
if let Some(profile_space_id) = Self::profile_space_by_account(account) {
if profile_space_id == space_id {
<ProfileSpaceByAccount<T>>::remove(account);
}
}
}
}
}
2 changes: 2 additions & 0 deletions pallets/space-ownership/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ std = [
'frame-system/std',
'sp-std/std',
'pallet-spaces/std',
'pallet-profiles/std',
'subsocial-support/std',
]

Expand All @@ -28,6 +29,7 @@ scale-info = { version = "2.1.2", default-features = false, features = ["derive"

# Local dependencies
pallet-spaces = { default-features = false, path = '../spaces' }
pallet-profiles = { default-features = false, path = '../profiles' }
subsocial-support = { default-features = false, path = '../support' }

# Substrate dependencies
Expand Down
5 changes: 4 additions & 1 deletion pallets/space-ownership/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub mod pallet {
use frame_system::pallet_prelude::*;

#[pallet::config]
pub trait Config: frame_system::Config + pallet_spaces::Config {
pub trait Config: frame_system::Config + pallet_spaces::Config + pallet_profiles::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
}

Expand Down Expand Up @@ -111,6 +111,9 @@ pub mod pallet {
space.owner = new_owner.clone();
SpaceById::<T>::insert(space_id, space);

// FIXME: cover with tests
pallet_profiles::Pallet::<T>::unset_space_as_profile(&old_owner, space_id);

// Remove space id from the list of spaces by old owner
SpaceIdsByOwner::<T>::mutate(old_owner, |space_ids| {
remove_from_bounded_vec(space_ids, space_id)
Expand Down
1 change: 1 addition & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ impl pallet_reactions::Config for Runtime {

impl pallet_profiles::Config for Runtime {
type Event = Event;
type SpacePermissionsProvider = Spaces;
}

parameter_types! {
Expand Down

0 comments on commit 56e2bd9

Please sign in to comment.