-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Subscriptions pallet #147
Draft
TarekkMA
wants to merge
18
commits into
dappforce:main
Choose a base branch
from
TarekkMA:tarekkma/subscription-pallet
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Subscriptions pallet #147
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
cad2229
SpacesInterface
TarekkMA 01722b5
Add create_space_as_profile
F3Joule 5b0d23e
Fix erros in tests
F3Joule 4982cb1
add subscription pallet
TarekkMA 692bb90
Apply suggestions
TarekkMA c1c054b
Apply suggestions
TarekkMA 0754db0
Add description
TarekkMA 6dedd23
Add a readme file
TarekkMA 9f97631
Add unsubscribe
TarekkMA e7228ed
rename pallet
TarekkMA 3b82451
remove Space from types name
TarekkMA 392df10
fix names
TarekkMA c9ae843
impl traits
TarekkMA 87dcd56
add to runtime
TarekkMA edc1f05
benchmarking
TarekkMA db80514
benchmarking tests
TarekkMA f4fa056
update_subscription_settings test
TarekkMA 446b3ab
mock params
TarekkMA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,9 @@ | ||||||
use super::*; | ||||||
|
||||||
use frame_support::dispatch::DispatchError; | ||||||
|
||||||
use pallet_permissions::SpacePermissionsContext; | ||||||
use subsocial_support::traits::RolesInterface; | ||||||
|
||||||
use super::*; | ||||||
|
||||||
impl<T: Config> Pallet<T> { | ||||||
/// Check that there is a `Role` with such `role_id` in the storage | ||||||
|
@@ -109,6 +111,74 @@ impl<T: Config> Pallet<T> { | |||||
|
||||||
Err(error) | ||||||
} | ||||||
|
||||||
pub fn do_create_role( | ||||||
space_owner: &T::AccountId, | ||||||
space_id: SpaceId, | ||||||
time_to_live: Option<T::BlockNumber>, | ||||||
content: Content, | ||||||
permissions: Vec<SpacePermission>, | ||||||
) -> Result<RoleId, DispatchError> { | ||||||
ensure!(!permissions.is_empty(), Error::<T>::NoPermissionsProvided); | ||||||
|
||||||
ensure_content_is_valid(content.clone())?; | ||||||
ensure!( | ||||||
T::IsContentBlocked::is_allowed_content(content.clone(), space_id), | ||||||
ModerationError::ContentIsBlocked, | ||||||
); | ||||||
|
||||||
Self::ensure_role_manager(space_owner.clone(), space_id)?; | ||||||
|
||||||
let permissions_set = permissions.into_iter().collect(); | ||||||
let new_role = | ||||||
Role::<T>::new(space_owner.clone(), space_id, time_to_live, content, permissions_set)?; | ||||||
|
||||||
// TODO review strange code: | ||||||
let next_role_id = new_role.id.checked_add(1).ok_or(Error::<T>::RoleIdOverflow)?; | ||||||
NextRoleId::<T>::put(next_role_id); | ||||||
|
||||||
RoleById::<T>::insert(new_role.id, new_role.clone()); | ||||||
RoleIdsBySpaceId::<T>::mutate(space_id, |role_ids| role_ids.push(new_role.id)); | ||||||
|
||||||
Ok(new_role.id) | ||||||
} | ||||||
|
||||||
pub fn do_grant_role( | ||||||
manager: Option<T::AccountId>, | ||||||
role_id: RoleId, | ||||||
users: Vec<User<T::AccountId>>, | ||||||
) -> DispatchResult { | ||||||
let users_set: BTreeSet<User<T::AccountId>> = convert_users_vec_to_btree_set(users)?; | ||||||
|
||||||
let role = Self::require_role(role_id)?; | ||||||
|
||||||
if let Some(who) = manager.clone() { | ||||||
Self::ensure_role_manager(who, role.space_id)?; | ||||||
} | ||||||
|
||||||
for user in users_set.iter() { | ||||||
if !Self::users_by_role_id(role_id).contains(user) { | ||||||
<UsersByRoleId<T>>::mutate(role_id, |users| { | ||||||
users.push(user.clone()); | ||||||
}); | ||||||
} | ||||||
if !Self::role_ids_by_user_in_space(user.clone(), role.space_id).contains(&role_id) { | ||||||
<RoleIdsByUserInSpace<T>>::mutate(user.clone(), role.space_id, |roles| { | ||||||
roles.push(role_id); | ||||||
}) | ||||||
} | ||||||
} | ||||||
|
||||||
Self::deposit_event(Event::RoleGranted { | ||||||
account: manager.unwrap_or_else(|| { | ||||||
T::AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes()).unwrap() | ||||||
}), | ||||||
role_id, | ||||||
users: users_set.iter().cloned().collect(), | ||||||
}); | ||||||
|
||||||
Ok(()) | ||||||
} | ||||||
} | ||||||
|
||||||
impl<T: Config> Role<T> { | ||||||
|
@@ -185,3 +255,26 @@ impl<T: Config> PermissionChecker for Pallet<T> { | |||||
Self::ensure_user_has_space_permission(user, ctx, permission, error) | ||||||
} | ||||||
} | ||||||
|
||||||
impl<T: Config> RolesInterface<RoleId, SpaceId, T::AccountId, SpacePermission, T::BlockNumber> | ||||||
for Pallet<T> | ||||||
{ | ||||||
fn get_role_space(role_id: RoleId) -> Result<SpaceId, DispatchError> { | ||||||
let role = Pallet::<T>::require_role(role_id)?; | ||||||
Ok(role.space_id) | ||||||
} | ||||||
|
||||||
fn grant_role(account_id: T::AccountId, role_id: RoleId) -> DispatchResult { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We always use just
Suggested change
|
||||||
Pallet::<T>::do_grant_role(None, role_id, vec![User::Account(account_id)]) | ||||||
} | ||||||
|
||||||
fn create_role( | ||||||
space_owner: &T::AccountId, | ||||||
space_id: SpaceId, | ||||||
time_to_live: Option<T::BlockNumber>, | ||||||
content: Content, | ||||||
permissions: Vec<SpacePermission>, | ||||||
) -> Result<RoleId, DispatchError> { | ||||||
Pallet::<T>::do_create_role(space_owner, space_id, time_to_live, content, permissions) | ||||||
} | ||||||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.