-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Hooks for Memberships Managers! (#31)
* feat(fc-traits-memberships): change autoimplementation of `T` for traits in favour of `NonFungiblesMemberships` structure. * feat(fc-traits-memberships): define `WithHooks` to extend managers and support hooks.
- Loading branch information
Showing
5 changed files
with
280 additions
and
12 deletions.
There are no files selected for viewing
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,66 @@ | ||
use super::*; | ||
use frame_support::dispatch::DispatchResult; | ||
|
||
/// Triggers an action when a membership has been assigned | ||
pub trait OnMembershipAssigned<AccountId: Clone, Group: Clone, Membership: Clone> { | ||
fn on_membership_assigned( | ||
&self, | ||
who: AccountId, | ||
group: Group, | ||
membership: Membership, | ||
) -> DispatchResult; | ||
} | ||
|
||
impl<A: Clone, G: Clone, M: Clone> OnMembershipAssigned<A, G, M> for () { | ||
fn on_membership_assigned(&self, _: A, _: G, _: M) -> DispatchResult { | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<T, A: Clone, G: Clone, M: Clone> OnMembershipAssigned<A, G, M> for T | ||
where | ||
T: Fn(A, G, M) -> DispatchResult, | ||
{ | ||
fn on_membership_assigned(&self, who: A, group: G, membership: M) -> DispatchResult { | ||
self(who, group, membership) | ||
} | ||
} | ||
|
||
/// Triggers an action when a membership has been released | ||
pub trait OnMembershipReleased<Group: Clone, Membership: Clone> { | ||
fn on_membership_released(&self, group: Group, membership: Membership) -> DispatchResult; | ||
} | ||
|
||
impl<T, G: Clone, M: Clone> OnMembershipReleased<G, M> for T | ||
where | ||
T: Fn(G, M) -> DispatchResult, | ||
{ | ||
fn on_membership_released(&self, group: G, membership: M) -> DispatchResult { | ||
self(group, membership) | ||
} | ||
} | ||
|
||
impl<G: Clone, M: Clone> OnMembershipReleased<G, M> for () { | ||
fn on_membership_released(&self, _: G, _: M) -> DispatchResult { | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// Triggers an action when a rank has been set for a membership | ||
pub trait OnRankSet<Group: Clone, Membership: Clone, Rank: Clone = GenericRank> { | ||
fn on_rank_set(&self, group: Group, membership: Membership, rank: Rank) -> DispatchResult; | ||
} | ||
impl<G: Clone, M: Clone, R: Clone> OnRankSet<G, M, R> for () { | ||
fn on_rank_set(&self, _: G, _: M, _: R) -> DispatchResult { | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<T, G: Clone, M: Clone, R: Clone> OnRankSet<G, M, R> for T | ||
where | ||
T: Fn(G, M, R) -> DispatchResult, | ||
{ | ||
fn on_rank_set(&self, group: G, membership: M, rank: R) -> DispatchResult { | ||
self(group, membership, rank) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use super::*; | ||
use core::marker::PhantomData; | ||
use frame_support::traits::Get; | ||
|
||
/// Extends a structure that already implements [`Manager`], and [`Rank`] to support | ||
/// hooks that are triggered after changes in memberships or ranks happen. | ||
pub struct WithHooks<T, OnMembershipAssigned = (), OnMembershipReleased = (), OnRankSet = ()>( | ||
PhantomData<(T, OnMembershipAssigned, OnMembershipReleased, OnRankSet)>, | ||
); | ||
|
||
impl<T, MA, MR, RS, AccountId> Inspect<AccountId> for WithHooks<T, MA, MR, RS> | ||
where | ||
T: Inspect<AccountId>, | ||
{ | ||
type Group = T::Group; | ||
type Membership = T::Membership; | ||
|
||
fn user_memberships( | ||
who: &AccountId, | ||
maybe_group: Option<Self::Group>, | ||
) -> Box<dyn Iterator<Item = (Self::Group, Self::Membership)>> { | ||
T::user_memberships(who, maybe_group) | ||
} | ||
|
||
fn is_member_of(group: &Self::Group, who: &AccountId) -> bool { | ||
T::is_member_of(group, who) | ||
} | ||
|
||
fn check_membership(who: &AccountId, m: &Self::Membership) -> Option<Self::Group> { | ||
T::check_membership(who, m) | ||
} | ||
|
||
fn members_total(group: &Self::Group) -> u32 { | ||
T::members_total(group) | ||
} | ||
} | ||
|
||
impl<T, MA, MR, RS, AccountId, ItemConfig> Manager<AccountId, ItemConfig> | ||
for WithHooks<T, MA, MR, RS> | ||
where | ||
AccountId: Clone, | ||
T: Manager<AccountId, ItemConfig>, | ||
MA: Get<Box<dyn OnMembershipAssigned<AccountId, T::Group, T::Membership>>>, | ||
MR: Get<Box<dyn OnMembershipReleased<T::Group, T::Membership>>>, | ||
{ | ||
fn assign( | ||
group: &Self::Group, | ||
m: &Self::Membership, | ||
who: &AccountId, | ||
) -> Result<(), DispatchError> { | ||
T::assign(group, m, who)?; | ||
MA::get().on_membership_assigned(who.clone(), group.clone(), m.clone())?; | ||
Ok(()) | ||
} | ||
|
||
fn release(group: &Self::Group, m: &Self::Membership) -> Result<(), DispatchError> { | ||
T::release(group, m)?; | ||
MR::get().on_membership_released(group.clone(), m.clone())?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<T, MA, MR, RS, R, AccountId, ItemConfig> Rank<AccountId, ItemConfig, R> | ||
for WithHooks<T, MA, MR, RS> | ||
where | ||
AccountId: Clone, | ||
R: Ord + Clone, | ||
T: Rank<AccountId, ItemConfig, R>, | ||
RS: Get<Box<dyn OnRankSet<T::Group, T::Membership, R>>>, | ||
{ | ||
fn rank_of(group: &Self::Group, m: &Self::Membership) -> Option<R> { | ||
T::rank_of(group, m) | ||
} | ||
|
||
fn set_rank( | ||
group: &Self::Group, | ||
m: &Self::Membership, | ||
rank: impl Into<R>, | ||
) -> Result<(), DispatchError> { | ||
let rank = rank.into(); | ||
T::set_rank(group, m, rank.clone())?; | ||
RS::get().on_rank_set(group.clone(), m.clone(), rank)?; | ||
Ok(()) | ||
} | ||
|
||
fn ranks_total(group: &Self::Group) -> u32 { | ||
T::ranks_total(group) | ||
} | ||
} |
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